Documentation / Navigation & actions
Dropdown
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.
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.
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.
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>
);
}Custom trigger and position
asChild passes the behaviour on to the supplied button; side and align place the menu.
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
import { Dropdown } from "@synertech/ui";Props — DropdownProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
closeOnClickOutside |
boolean |
true |
|
closeOnEscape |
boolean |
true |
|
closeOnSelect |
boolean |
true |
|
defaultOpen |
boolean |
false |
|
modal |
boolean |
false |
|
onOpenChange |
(open: boolean) => void |
— | |
open |
boolean |
— |
import { DropdownCheckboxItem } from "@synertech/ui";Props — DropdownCheckboxItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
checked |
boolean |
— | |
className |
string |
— | |
closeOnChange |
boolean |
false |
|
defaultChecked |
boolean |
false |
|
description |
string |
— | |
disabled |
boolean |
false |
|
icon |
ReactNode |
— | |
index deprecated |
number |
— | Deprecated: Ignored. The arrow keys follow the order of the items on screen, read from the document when a key is pressed. |
onCheckedChange |
(checked: boolean) => void |
— |
import { DropdownContent } from "@synertech/ui";Props — DropdownContentProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
align |
Align |
"start" |
|
alignOffset |
number |
0 |
|
animation |
"fade" | "scale" | "slide" | "none" |
"scale" |
|
avoidCollisions |
boolean |
true |
|
className |
string |
— | |
collisionPadding |
number |
8 |
|
maxHeight |
number | string |
300 |
|
maxWidth |
number | string |
— | |
minWidth |
number | string |
— | |
portal |
boolean |
true |
|
side |
Side |
"bottom" |
|
sideOffset |
number |
4 |
import { DropdownItem } from "@synertech/ui";Props — DropdownItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
closeOnSelect |
boolean |
— | |
description |
string |
— | |
disabled |
boolean |
false |
|
icon |
ReactNode |
— | |
iconPosition |
"left" | "right" |
"left" |
|
index deprecated |
number |
— | Deprecated: Ignored. The arrow keys follow the order of the items on screen, read from the document when a key is pressed. |
onSelect |
() => void |
— | |
shortcut |
string |
— | |
variant |
"default" | "danger" | "success" |
"default" |
+ 286 inherited native attributes from ButtonHTMLAttributes, HTMLAttributes, AriaAttributes (className, id, aria-*, event handlers…).
import { DropdownLabel } from "@synertech/ui";Props — DropdownLabelProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
className |
string |
— |
import { DropdownRadioGroup } from "@synertech/ui";Props — DropdownRadioGroupProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
className |
string |
— | |
defaultValue |
string |
"" |
|
onValueChange |
(value: string) => void |
— | |
value |
string |
— |
import { DropdownRadioItem } from "@synertech/ui";Props — DropdownRadioItemProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
value• |
string |
— | |
className |
string |
— | |
closeOnChange |
boolean |
true |
|
description |
string |
— | |
disabled |
boolean |
false |
|
icon |
ReactNode |
— | |
index deprecated |
number |
— | Deprecated: Ignored. The arrow keys follow the order of the items on screen, read from the document when a key is pressed. |
import { DropdownSeparator } from "@synertech/ui";Props — DropdownSeparatorProps
| Prop | Type | Default | Description |
|---|---|---|---|
className |
string |
— |
import { DropdownSubMenu } from "@synertech/ui";Props — DropdownSubMenuProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— |
import { DropdownSubMenuContent } from "@synertech/ui";Props — DropdownSubMenuContentProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
className |
string |
— | |
sideOffset |
number |
0 |
import { DropdownSubMenuTrigger } from "@synertech/ui";Props — DropdownSubMenuTriggerProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
className |
string |
— | |
disabled |
boolean |
false |
|
icon |
ReactNode |
— | |
index deprecated |
number |
— | Deprecated: Ignored. The arrow keys follow the order of the items on screen, read from the document when a key is pressed. |
import { DropdownTrigger } from "@synertech/ui";Props — DropdownTriggerProps
| Prop | Type | Default | Description |
|---|---|---|---|
children• |
ReactNode |
— | |
asChild |
boolean |
false |
Makes the child the trigger, instead of rendering a button around it. The child keeps what it says about itself: its own |
className |
string |
— | |
disabled |
boolean |
false |
Hooks
import { useClickOutside } from "@synertech/ui";useClickOutside(ref: RefObject<HTMLElement | null>, handler: () => void, enabled?: boolean, options?: UseClickOutsideOptions): voidimport { useDropdownContext } from "@synertech/ui";useDropdownContext(): DropdownContextValueimport { useFocusItem } from "@synertech/ui";useFocusItem(itemsRef: RefObject<(HTMLElement | null)[]>, focusedIndex: number, isOpen: boolean): voidimport { useKeyboardNavigation } from "@synertech/ui";useKeyboardNavigation({ isOpen, onClose, itemsCount, focusedIndex, setFocusedIndex, onSelect, loop, }: UseKeyboardNavigationOptions): { focusedIndex: number; }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): PositionTypes
DropdownContextValue
Interfaceinterface DropdownContextValue| Field | Type | Default | Description |
|---|---|---|---|
closeOnClickOutside• |
boolean |
— | |
closeOnEscape• |
boolean |
— | |
closeOnSelect• |
boolean |
— | |
contentId• |
string |
— | |
contentRef• |
{ current: HTMLDivElement | null } |
— | |
focusedIndex• deprecated |
number |
— | Deprecated: Always |
getNextItemIndex• deprecated |
() => number |
— | Deprecated: Kept for custom items written against the counter; unused. |
isOpen• |
boolean |
— | |
itemsRef• deprecated |
RefObject<(HTMLElement | null)[]> |
— | Deprecated: Always empty. The menu reads its items from the document. |
modal• |
boolean |
— | |
registerItem• deprecated |
(index: number, element: HTMLElement | null) => void |
— | Deprecated: Does nothing. An element with a |
setFocusedIndex• deprecated |
Dispatch<SetStateAction<number>> |
— | Deprecated: Does nothing. Move the focus to the item instead. |
setIsOpen• |
(open: boolean) => void |
— | |
triggerId• |
string |
— | |
triggerRef• |
{ current: HTMLButtonElement | null } |
— |
Position
Interfaceinterface Position| Field | Type | Default | Description |
|---|---|---|---|
actualAlign• |
Align |
— | |
actualSide• |
Side |
— | |
isReady• |
boolean |
— | |
left• |
number |
— | |
top• |
number |
— | |
availableHeight |
number |
— | The height the viewport leaves a content that keeps |
UseClickOutsideOptions
Interfaceinterface UseClickOutsideOptions| Field | Type | Default | Description |
|---|---|---|---|
isInside |
(target: Node) => boolean |
— | Says that a press landed inside although it is not in |
UseKeyboardNavigationOptions
Interfaceinterface UseKeyboardNavigationOptions| Field | Type | Default | Description |
|---|---|---|---|
focusedIndex• |
number |
— | |
isOpen• |
boolean |
— | |
itemsCount• |
number |
— | |
onClose• |
() => void |
— | |
setFocusedIndex• |
Dispatch<SetStateAction<number>> |
— | |
loop |
boolean |
— | |
onSelect |
(index: number) => void |
— |
UsePositioningOptions
Interfaceinterface UsePositioningOptionsAlign
Typetype Align = "start" | "center" | "end"Side
Typetype Side = "top" | "right" | "bottom" | "left"