zakodium-oss / react-science

React components and tools to build scientific applications.
https://react-science.pages.dev
MIT License
3 stars 6 forks source link

Create a dropdown / contextual menu component #260

Closed lpatiny closed 2 years ago

lpatiny commented 2 years ago

2022-10-20 12 00 44

stropitek commented 2 years ago

Inspirations

Requirements

API

I weighed between adopting an API similar to what Ant does and our API in zakodium/components and I favor the latter.

I would change the options to be a simple array, not array of array, as this can be confusing. A divider within the options can be defined with a special option type: 'divider' instead, like in Ant.

The new trigger prop allows to use it either on the click of a button or when right-clicking a zone.

when trigger='click', the user should set children to be the element which both acts as the element to be clicked and the reference for react-popper.

when trigger='contextMenu', the children act as the zone which respond to the context menu event. It will be probably required to setup a virtual element at the position where the mouse is (see docs and to use a boundary modifier to constrain the positioning of the contextual menu to be within the children.

API

export interface MenuOption<T> {
  type: 'option';
  label: ReactNode;
  icon?: ReactNode;
  disabled?: boolean;
  data?: T;
}

export interface MenuDivider {
  type: 'divider';
}

type MenuOptions<T> = Array<MenuOption<T> | MenuDivider>;

export interface DropdownMenuProps<T> {
  children: ReactNode; // The button contents
  options: MenuOptions<T>;
  onSelect: (selected: DropdownOption<T>) => void;
  placement?: Placement;
  trigger: 'click' | 'contextMenu'
}

Concerns / open questions

targos commented 2 years ago

emotion also creates classes, I don't see any issues with using it with headless ui

stropitek commented 2 years ago

@Sebastien-Ahkrin first step is:

The menu could be a grid with 2 columns:

The idea is that later (not in this PR) we will want to add 2 other column:

targos commented 2 years ago

As an example of the full grid, here's the Edge menu: image

Sebastien-Ahkrin commented 2 years ago

Capture d’écran 2022-10-24 à 15 00 37

@stropitek @targos Do you want to keep the old styles from nmrium ?

targos commented 2 years ago

Not at all. You can take inspiration from my screenshot if you want.

stropitek commented 2 years ago

No you shouldn't, you can get inspired by the example in Ant (see link in my proposal)