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 { EllipsisVertical } from 'lucide-react'

<MenuTrigger>
<Button
aria-label='Meny'
variant='icon'
>
<EllipsisVertical />
</Button>
<MenuPopover>
<Menu>
<MenuItem id='open'>Öppna</MenuItem>
<MenuItem id='rename'>Byt namn...</MenuItem>
<MenuItem id='duplicate'>Duplicera</MenuItem>
<MenuItem id='delete'>Ta bort...</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
  • MenuHeader & MenuHeaderProps
  • 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>

Dynamiska sektioner​

Använd MenuSection och MenuHeader för att skapa sektioner i din meny, se exempel nedan där Collection används i en dynamisk kollektion.

import {
Collection,
Menu,
MenuHeader,
MenuItem,
MenuSection,
} from '@midas-ds/components'

export const SectionedExample = () => {
const items = [
{
name: 'Left Panel',
id: 'left',
children: [{ id: 1, name: 'Final Copy (1)' }],
},
{
name: 'Right Panel',
id: 'right',
children: [
{ id: 2, name: 'index.ts' },
{ id: 3, name: 'package.json' },
{ id: 4, name: 'license.txt' },
],
},
]

return (
<Menu
aria-label='Filer'
items={items}
>
{section => (
<MenuSection>
<MenuHeader>{section.name}</MenuHeader>
<Collection items={section.children}>
{item => <MenuItem>{item.name}</MenuItem>}
</Collection>
</MenuSection>
)}
</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.

autoFocus-

Where the focus should be set.

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.

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.

dependencies-

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​