Menu

A menu provides relevant actions based on the user's current selection, enhancing usability by offering quick access to necessary options and reducing cognitive load.

UsageCodeAccessibility
ActionsProfileSettingsRemoveUpdateRemove
<sl-menu-button>
  <span slot="button">Actions</span>
  <sl-menu-item>
    <sl-icon name="smile"></sl-icon>
    Profile
  </sl-menu-item>
  <sl-menu-item>
    <sl-icon name="far-gear"></sl-icon>
    Settings
  </sl-menu-item>
  <sl-menu-item>
    <sl-icon name="far-trash"></sl-icon>
    Remove
  </sl-menu-item>
</sl-menu-button>

<sl-menu-button aria-label="Show more options">
  <sl-icon name="ellipsis" slot="button"></sl-icon>
  <sl-menu-item>Update</sl-menu-item>
  <sl-menu-item>Remove</sl-menu-item>
</sl-menu-button>

When to Use

The following guidance describes when to use the Menu component.

Contextual Actions

Some interface elements can have additional actions, and providing a menu can enhance efficiency and streamline workflow. For instance, options to view details, open related documents, or access settings, offering extra details and actions specific to the element.

Reducing Visual Clutter

Keeping a clean interface with repetitive elements (like cards or table rows), but additional actions are still necessary, a menu fixes this scenario while providing access to secondary actions. For example, sorting a list of elements or changing the view of the page.

When not to Use

The following guidance describes when not to use the Menu component.

Primary Navigation

Menus should not be used for primary navigation in applications because they are hidden until triggered by a specific action, making them less discoverable for users. Basic actions should always be visible and accessible to ensure users can efficiently navigate to essential features without additional steps. Relying on menus for primary navigation can also create inconsistency in accessing core functions by requiring users to learn multiple ways to perform basic tasks.

Anatomy

ItemNameDescriptionOptional
1Button MenuThis is the trigger button to open the menu.no
2Menu PopoverThis is the container for all the options of the menu, and appears close to the button menu.no
3Option ItemsThis are the menu options that can be separete on "Option Button" that behave like a regular button, "Selection Option" that can behave like a checkbox or radio button, and "Sub-Menu Button" to open a nested menu.no
4Group HeadingTitle for the option groups to make eaiser to understand the function of the options.yes
5Option IconsYou can display an icon in each option to enhance the readability.yes
6Option ShortcutsAdding shortcuts to the options to improve usability.yes
7Group HeadingTitle for the option groups to make eaiser to understand the function of the options.yes
8Danger OptionSpecific danger option of the menu.yes

Options

The Figma component and the naming of its properties is still a work in progress. This information will be added as soon as possible.

Behaviours

Grouped Options

Splitting the related actions with a divider creates multiple categories, with the possibility of adding a heading to these ones. This helps users to quickly locate the available actions based on the relation of each action in the same group.

Overflow Content

Using sub-menus or nested options to organize related actions. This approach helps maintain clarity and reduces visual clutter by nesting similar or related actions under sub-menus. For example, within a "File" menu option, nested menus might include "Save As," "Print," and "Export," enhancing usability. However, this solution isn’t the magic one to create an infinite sub-menus to nested everything. Avoid adding more than 2 nested menus, so don’t exceed 3 levels.

Shortcut Options

Keyboard shortcuts next to menu options, providing alternative access methods for frequently used actions. These are typically indicated by symbols, numbers, or combinations, allowing proficient users to perform actions quickly without relying solely on mouse interactions. For instance, the "Paste" option in a text editor or the shortcut "Ctrl + V" enables users to execute the action promptly using the keyboard.

More settingsListCards
Sort byNewestOldestName
EditRemove
  <sl-menu-button position="bottom-end">
    <span slot="button">More settings</span>
    <sl-menu-item-group selects="single">
      <sl-menu-item selectable selected shortcut="$mod+Digit1">List</sl-menu-item>
      <sl-menu-item selectable shortcut="$mod+Digit2">Cards</sl-menu-item>
    </sl-menu-item-group>
    <hr />
    <sl-menu-item>
      Sort by
      <sl-menu selects="single" slot="submenu">
        <sl-menu-item selectable selected>Newest</sl-menu-item>
        <sl-menu-item selectable>Oldest</sl-menu-item>
        <sl-menu-item selectable>Name</sl-menu-item>
      </sl-menu>
    </sl-menu-item>
    <hr />
    <sl-menu-item disabled>Edit</sl-menu-item>
    <sl-menu-item>Remove</sl-menu-item>
  </sl-menu-button>

Menu button API

Component has a range of properties to define the experience in different use cases.

Properties

NameAttributeTypeDefaultDescription
disableddisabledboolean | undefinedWhether the button is disabled; when set no interaction is possible.
fillfill'solid' | 'outline' | 'link' | 'ghost''outline'The fill of the button.
positionposition'top' | 'right' | 'bottom' | 'left' | AlignedPlacement | undefinedThe position of the menu relative to the button.
sizesize'sm' | 'md' | 'lg''md'The size of the button.
variantvariant'default' | 'primary' | 'success' | 'info' | 'warning' | 'danger''default'The variant of the button.

Methods

NameParametersReturnDescription
setAttributesTargettarget: HTMLElementvoid

Slots

NameDescription
defaultThe menu items should be slotted in the default slot.
buttonAny content for the button should be slotted here.

CSS Parts

NameDescription
buttonThe button element.

Menu item group API

Component has a range of properties to define the experience in different use cases.

Properties

NameAttributeTypeDefaultDescription
headingheadingstring | undefinedThe optional heading for the group.
selectsselects'single' | 'multiple' | undefinedDetermines whether if and how many menu items can be selected within this group.

Slots

NameDescription
defaultThe menu items within a group.
headerThe header of the group.

Menu item API

Component has a range of properties to define the experience in different use cases.

Properties

NameAttributeTypeDefaultDescription
disableddisabledboolean | undefinedWhether this menu item is disabled.
selectableselectableboolean | undefinedWhether this menu item can be selected.
selectedselectedboolean | undefinedWhether this menu item has been selected.
shortcutshortcutstring | undefinedKeyboard shortcut for activating this menu item.
variantvariant'default' | 'danger' | undefinedThe variant of the menu item.

Events

NameEvent typeDescription
sl-selectSlSelectEventEmits the current selected state as a boolean when the user toggles the menu item.

Slots

NameDescription
defaultContent to display inside the menu item.
submenuThe menu items that will be displayed when the menu item is shown.

CSS Parts

NameDescription
wrapperThe wrapper around the menu item content.

Menu API

Component has a range of properties to define the experience in different use cases.

Properties

NameAttributeTypeDefaultDescription
offsetoffsetnumber | undefinedThe offset of the menu to its anchor. This is a property on this instance so that it can be overridden
by the menu item in case of a nested menu.
You should not need to set this property yourself.
positionposition'top' | 'right' | 'bottom' | 'left' | AlignedPlacement | undefined'right-start'The position of the menu relative to its anchor.
selectsselects'single' | 'multiple' | undefinedDetermines whether if and how many menu items can be selected.

Events

NameEvent typeDescription
sl-selectSlSelectEvent<void>Emits when the menu item selection changes.

Slots

NameDescription
defaultThe menu's content: menu items or menu item groups.

CSS Properties

NameDescription
--sl-menu-max-inline-sizeThe maximum inline size of the menu.
--sl-menu-min-inline-sizeThe minimum inline size of the menu.

Keyboard interactions

CommandDescription
Space/EnterOpen the menu and the sub-menus. When you open the menu, the focus move to the first option.
Up & DownOnce you have open the menu. You can move between options with the up and down keys. The focus indicator loops, so when you are at the last option and press down it will focus on the first option. And if you are at the first option and press up it'll focus the last option.
RightWhen you are in a sub-menu option right key open it and the focus move to the first option.
Right & EscThis keys close the menu. But, when you are in a sub-menu right & esc keys close it and the focus move parent option. This works from any option, doesn't matter where you are.

WAI-ARIA

AttributeValueDescriptionUser suppliedSpecifies whether the attribute is always set in the component (no) or it needs to be provided by the developer (yes)
role'button'sl-button as part of sl-menu-button has a role of button.no
aria-expandedbooleanSet to true if the sl-menu is visible (opened), false if it is hidden.no
aria-detailsstringUsed to link the menu element with the button (that triggers the menu component). It contains id of the menu element and is added to the button element.no
aria-labelstringPlease add when only icon button is used (so no text for the menu button is added).yes
aria-disabledbooleanAnnounces the menu button component as disabled with a screen reader. See [Note 1] below.yes
AttributeValueDescriptionUser supplied
role'menu'Declare component as a menu.no
AttributeValueDescriptionUser supplied
role'menuitem'Declare an element as an menu item.no
aria-expandedbooleanAdded when menu item is expandable and connected with another menu element (submenu). Set to true if the sl-menu (submenu) is visible (opened), false if it is hidden.no
aria-detailsstringUsed to link the menu (submenu) element with the expandable menu item (that triggers the [sub]menu). It contains id of the menu (submenu) and is added to the menu item element.no
aria-disabledbooleanAnnounces the menu item component as disabled with a screen reader. See [Note 1] below.yes
aria-labelstringCan be added when there is no label/text that could be described by. eg. when there is only an icon added.yes

Notes:

  1. The aria-disabled should not be used as a one-for-one replacement for the disabled attribute because they have different functionalities:
  • disabled dims the menu button/menu item visually, takes it out of the tab-focus sequence, prevents actions (click, enter) on it and announces it as 'dimmed' or 'disabled' in a screen reader.

  • aria-disabled only does the latter. You will need to disable the functionality yourself. This might be useful for scenarios where you don't want to take the menu button / menu item out of the navigation flow.

When disabled is added there is no need to also add aria-disabled. Everything aria-disabled does, disabled does as well. You can read more on the difference and in which scenarios which option might be preferable on the MDN page about aria-disabled.

Interactive example