Hoppa till huvudinnehåll

Menu

Meny, menylist, kontextmeny

En meny kan användas för att samla ihop funktioner som sällan används eller som kompletterar nuvarande funktion.

import { Button, Menu, MenuItem, MenuPopover, MenuTrigger } from '@midas-ds/components'
import { MenuIcon } from 'lucide-react'
<MenuTrigger>
<Button
aria-label='Menu'
variant='icon'
>
<MenuIcon size={20} />
</Button>
<MenuPopover>
<Menu>
<MenuItem>Open</MenuItem>
<MenuItem>Rename...</MenuItem>
<MenuItem>Duplicate</MenuItem>
<MenuItem>Share...</MenuItem>
<MenuItem>Delete...</MenuItem>
</Menu>
</MenuPopover>
</MenuTrigger>

Beskrivning​

Komponenten bygger på React Aria Menu, kodexempel i React Arias dokumentation är applicerbara genom att importera komponeterna från @midas-ds/components.

Midas exporterar följande komponenter och typer:

  • Menu & MenuProps
  • MenuItem & MenuItemProps
  • MenuPopover & MenuPopoverProps
  • MenuSection & MenuSectionProps
  • Separator & SeparatorProps
  • MenuTrigger & MenuTriggerProps

Hantera handlingar​

onAction
  • –

Använd onAction på Menu för att lyssna på vilket menyval användaren väljer. Callbacken tar emot id-värdet från det valda MenuItem-elementet — se till att sätta ett id på varje MenuItem.

<Menu onAction={key => console.log(key)}>
<MenuItem id='open'>Open</MenuItem>
<MenuItem id='rename'>Rename...</MenuItem>
</Menu>

onAction kan också sättas direkt på ett enskilt MenuItem om du vill koppla specifik logik till ett alternativ:

<Menu>
<MenuItem id='open' onAction={() => openFile()}>Open</MenuItem>
<MenuItem id='delete' onAction={() => deleteFile()}>Delete...</MenuItem>
</Menu>

Länkar​

React Arias dokumentation visar exempel på hur menyn kan innehålla länkar. Länkar i en meny är tillåtna så länge menyns syfte är tydligt för användaren, exempelvis en meny som blandar handlingar med kontextuella länkar som "Öppna i nytt fönster" eller "Gå till inställningar".

Riktlinjer​

  • Använd endast till sekundära handlingar som inte är högt prioriterade.
  • Knappen ska helst ha en titel, annars bör en aria-label användas.

API​

NameTypeDefaultDescription
sizelarge

Component size (large: height 40px, medium: height 32px)

className'react-aria-Menu'

The CSS className for the element. A function may be provided to compute the class based on component state.

renderEmptyState-

Provides content to display when there are no items in the menu.

shouldCloseOnSelect-

Whether the menu should close when the menu item is selected.

disabledKeysIterable<Key>-

The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.

itemsIterable<T>-

Item objects in the collection.

autoFocus-

Where the focus should be set.

selectionMode-

The type of selection that is allowed in the collection.

shouldFocusWrap-

Whether keyboard navigation is circular.

escapeKeyBehavior'clearSelection'

Whether pressing the escape key should clear selection in the menu or not.

Most experiences should not modify this option as it eliminates a keyboard user's ability to easily clear selection. Only use if the escape key is being handled externally or should not trigger selection clearing contextually.

disallowEmptySelection-

Whether the collection allows empty selection.

selectedKeys-

The currently selected keys in the collection (controlled).

defaultSelectedKeys-

The initial selected keys in the collection (uncontrolled).

children-

The contents of the collection.

dependenciesreadonly any[]-

Values that should invalidate the item cache when using dynamic collections.

style-

The inline style for the element. A function may be provided to compute the style based on component state.

renderDOMRenderFunction<"div", MenuRenderProps>-

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.

Requirements:

  • You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>).
  • Only a single root DOM element can be rendered (no fragments).
  • You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
slot-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Separator​