Synertech Web/ui v0.57.5 Site Storybook

Documentation / Navigation & actions

Dropdown

Client@synertech/uiOpen in Storybook

A composable dropdown menu, opened by a trigger and rendered in a portal positioned according to the available space. Dropdown holds the state, DropdownTrigger opens it, DropdownContent holds the entries: DropdownItem (an action), DropdownCheckboxItem (a checkbox), DropdownRadioGroup / DropdownRadioItem (an exclusive choice), DropdownLabel, DropdownSeparator and DropdownSubMenu.

Uncontrolled by default (defaultOpen), controlled with open + onOpenChange. The menu closes when an action is chosen (closeOnSelect), on an outside click, on another click on the trigger and on Escape, which returns focus to the trigger. Checkboxes keep the menu open, radio buttons close it. IDs come from useId(), with no hydration mismatch on the server. Client component ("use client").

From the keyboard, the focus enters the menu when it opens and the arrow keys, Home and End move through the entries in the order they are on screen, skipping the disabled ones; Enter or Space activates the focused one. Right arrow opens a submenu, Left arrow or Escape goes back one level. A custom item has nothing to register: a menuitem role is enough for the arrows to reach it.

Examples

Basic usage

A trigger and a list of actions; onSelect is called on choice, then the menu closes.

Open in a tab
import { Dropdown, DropdownContent, DropdownItem, DropdownTrigger } from "@synertech/ui";

export function Basic() {
  return (
    <Dropdown>
      <DropdownTrigger>Options</DropdownTrigger>
      <DropdownContent>
        <DropdownItem onSelect={() => console.log("Profil")}>Mon profil</DropdownItem>
        <DropdownItem onSelect={() => console.log("Paramètres")}>Paramètres</DropdownItem>
        <DropdownItem onSelect={() => console.log("Déconnexion")}>Se déconnecter</DropdownItem>
      </DropdownContent>
    </Dropdown>
  );
}

Icons, shortcuts and destructive action

An entry can carry an icon, a description, a shortcut and the danger variant.

Open in a tab
import { Dropdown, DropdownContent, DropdownItem, DropdownLabel, DropdownSeparator, DropdownTrigger } from "@synertech/ui";

const EditIcon = () => (
  <svg
    className="h-4 w-4"
    fill="none"
    viewBox="0 0 24 24"
    stroke="currentColor"
    strokeWidth={2}
    aria-hidden="true"
  >
    <path
      strokeLinecap="round"
      strokeLinejoin="round"
      d="M15.2 5.2l3.6 3.6M4 20h4L19 9a2.5 2.5 0 00-3.6-3.6L4 16.4V20z"
    />
  </svg>
);

const TrashIcon = () => (
  <svg
    className="h-4 w-4"
    fill="none"
    viewBox="0 0 24 24"
    stroke="currentColor"
    strokeWidth={2}
    aria-hidden="true"
  >
    <path strokeLinecap="round" strokeLinejoin="round" d="M6 7h12M9 7V4h6v3m-8 0l1 13h8l1-13" />
  </svg>
);

export function RichItems() {
  return (
    <Dropdown>
      <DropdownTrigger>Document</DropdownTrigger>
      <DropdownContent minWidth={220}>
        <DropdownLabel>Édition</DropdownLabel>
        <DropdownItem icon={<EditIcon />} shortcut="⌘R">
          Renommer
        </DropdownItem>
        <DropdownItem description="Crée une copie dans le même dossier">Dupliquer</DropdownItem>
        <DropdownItem disabled>Déplacer (lecture seule)</DropdownItem>
        <DropdownSeparator />
        <DropdownItem icon={<TrashIcon />} variant="danger">
          Supprimer
        </DropdownItem>
      </DropdownContent>
    </Dropdown>
  );
}

Checkboxes and exclusive choice

Checkboxes keep the menu open; the radio group, controlled here, closes it on choice.

Open in a tab
import { useState } from "react";
import { Dropdown, DropdownCheckboxItem, DropdownContent, DropdownLabel, DropdownRadioGroup, DropdownRadioItem, DropdownSeparator, DropdownTrigger } from "@synertech/ui";

export function Selection() {
  const [columns, setColumns] = useState({ statut: true, montant: true, date: false });
  const [sort, setSort] = useState("recent");

  return (
    <Dropdown>
      <DropdownTrigger>Affichage</DropdownTrigger>
      <DropdownContent>
        <DropdownLabel>Colonnes</DropdownLabel>
        <DropdownCheckboxItem
          checked={columns.statut}
          onCheckedChange={(statut) => setColumns({ ...columns, statut })}
        >
          Statut
        </DropdownCheckboxItem>
        <DropdownCheckboxItem
          checked={columns.montant}
          onCheckedChange={(montant) => setColumns({ ...columns, montant })}
        >
          Montant
        </DropdownCheckboxItem>
        <DropdownCheckboxItem
          checked={columns.date}
          onCheckedChange={(date) => setColumns({ ...columns, date })}
        >
          Date d’échéance
        </DropdownCheckboxItem>
        <DropdownSeparator />
        <DropdownLabel>Tri</DropdownLabel>
        <DropdownRadioGroup value={sort} onValueChange={setSort}>
          <DropdownRadioItem value="recent">Plus récents</DropdownRadioItem>
          <DropdownRadioItem value="ancien">Plus anciens</DropdownRadioItem>
          <DropdownRadioItem value="montant">Montant décroissant</DropdownRadioItem>
        </DropdownRadioGroup>
      </DropdownContent>
    </Dropdown>
  );
}

Submenu

DropdownSubMenu opens a second level next to its entry.

Open in a tab
import { Dropdown, DropdownContent, DropdownItem, DropdownSubMenu, DropdownSubMenuContent, DropdownSubMenuTrigger, DropdownTrigger } from "@synertech/ui";

export function SubMenu() {
  return (
    <Dropdown>
      <DropdownTrigger>Partager</DropdownTrigger>
      <DropdownContent>
        <DropdownItem>Copier le lien</DropdownItem>
        <DropdownSubMenu>
          <DropdownSubMenuTrigger>Envoyer vers</DropdownSubMenuTrigger>
          <DropdownSubMenuContent>
            <DropdownItem>E-mail</DropdownItem>
            <DropdownItem>Slack</DropdownItem>
            <DropdownItem>Teams</DropdownItem>
          </DropdownSubMenuContent>
        </DropdownSubMenu>
      </DropdownContent>
    </Dropdown>
  );
}

Custom trigger and position

asChild passes the behaviour on to the supplied button; side and align place the menu.

Open in a tab
import { useState } from "react";
import { Button, Dropdown, DropdownContent, DropdownItem, DropdownTrigger } from "@synertech/ui";

export function CustomTrigger() {
  const [open, setOpen] = useState(false);

  return (
    <Dropdown open={open} onOpenChange={setOpen}>
      <DropdownTrigger asChild>
        <Button size="sm">Actions {open ? "▴" : "▾"}</Button>
      </DropdownTrigger>
      <DropdownContent side="top" align="end" animation="slide">
        <DropdownItem variant="success">Valider la commande</DropdownItem>
        <DropdownItem>Mettre en attente</DropdownItem>
      </DropdownContent>
    </Dropdown>
  );
}

API reference

Components

ComponentClient
import { Dropdown } from "@synertech/ui";

Props — DropdownProps

PropTypeDefaultDescription
ComponentClient
import { DropdownCheckboxItem } from "@synertech/ui";

Props — DropdownCheckboxItemProps

PropTypeDefaultDescription
ComponentClient
import { DropdownContent } from "@synertech/ui";

Props — DropdownContentProps

PropTypeDefaultDescription
ComponentClient
import { DropdownItem } from "@synertech/ui";

Props — DropdownItemProps

PropTypeDefaultDescription

+ 286 inherited native attributes from ButtonHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).

ComponentClient
import { DropdownLabel } from "@synertech/ui";

Props — DropdownLabelProps

PropTypeDefaultDescription
ComponentClient
import { DropdownRadioGroup } from "@synertech/ui";

Props — DropdownRadioGroupProps

PropTypeDefaultDescription
ComponentClient
import { DropdownRadioItem } from "@synertech/ui";

Props — DropdownRadioItemProps

PropTypeDefaultDescription
ComponentClient
import { DropdownSeparator } from "@synertech/ui";

Props — DropdownSeparatorProps

PropTypeDefaultDescription
ComponentClient
import { DropdownSubMenu } from "@synertech/ui";

Props — DropdownSubMenuProps

PropTypeDefaultDescription
ComponentClient
import { DropdownSubMenuContent } from "@synertech/ui";

Props — DropdownSubMenuContentProps

PropTypeDefaultDescription
ComponentClient
import { DropdownSubMenuTrigger } from "@synertech/ui";

Props — DropdownSubMenuTriggerProps

PropTypeDefaultDescription
ComponentClient
import { DropdownTrigger } from "@synertech/ui";

Props — DropdownTriggerProps

PropTypeDefaultDescription

Hooks

useClickOutside

HookClient
import { useClickOutside } from "@synertech/ui";
useClickOutside(ref: RefObject<HTMLElement | null>, handler: () => void, enabled?: boolean, options?: UseClickOutsideOptions): void
import { useDropdownContext } from "@synertech/ui";
useDropdownContext(): DropdownContextValue

useFocusItem

HookClient
import { useFocusItem } from "@synertech/ui";
useFocusItem(itemsRef: RefObject<(HTMLElement | null)[]>, focusedIndex: number, isOpen: boolean): void
import { useKeyboardNavigation } from "@synertech/ui";
useKeyboardNavigation({ isOpen, onClose, itemsCount, focusedIndex, setFocusedIndex, onSelect, loop, }: UseKeyboardNavigationOptions): { focusedIndex: number; }

usePositioning

HookClient

Places a floating content against its trigger, in viewport coordinates (position: fixed).

With avoidCollisions, two rules, in this order. The content goes to the other side of the trigger only when it does not fit on the side asked for AND the other side has more room — flipping to a side that has less sent a menu above the top of a short screen, where what was cut off could not be reached at all. Then it is kept inside the viewport on both axes, at the price of covering its trigger when neither side holds it.

Nothing is measured, and nothing listens, while isOpen is false.

import { usePositioning } from "@synertech/ui";
usePositioning({ triggerRef, contentRef, isOpen, side, align, sideOffset, alignOffset, avoidCollisions, collisionPadding, }: UsePositioningOptions): Position

Types

FieldTypeDefaultDescription

Position

Interface
interface Position
FieldTypeDefaultDescription
actualAlign• Align —
actualSide• Side —
isReady• boolean —
left• number —
top• number —
availableHeight number —

The height the viewport leaves a content that keeps collisionPadding from both of its ends. A content taller than that cannot be on screen in full wherever it is placed: give it this as its max-height, and let it scroll. Undefined until the first measure, and without avoidCollisions.

FieldTypeDefaultDescription
isInside (target: Node) => boolean —

Says that a press landed inside although it is not in ref: on the trigger, which closes its menu itself, or in a layer portalled next to it.

FieldTypeDefaultDescription
focusedIndex• number —
isOpen• boolean —
itemsCount• number —
onClose• () => void —
setFocusedIndex• Dispatch<SetStateAction<number>> —
loop boolean —
onSelect (index: number) => void —
FieldTypeDefaultDescription
contentRef• RefObject<HTMLElement | null> —
isOpen• boolean —
triggerRef• RefObject<HTMLElement | null> —
align Align —
alignOffset number —
avoidCollisions boolean —
collisionPadding number —
side Side —
sideOffset number —

Align

Type
type Align = "start" | "center" | "end"

Side

Type
type Side = "top" | "right" | "bottom" | "left"