Context Menu

Displays a menu at the pointer's position when the trigger is right-clicked or long-pressed.

Features

  • Can be controlled or uncontrolled.
  • Supports submenus with configurable reading direction.
  • Customize menu positioning.
  • Optionally render a pointing arrow.
  • Fully managed focus.
  • Full keyboard navigation.
  • Typeahead support

Anatomy

  • Trigger: The element which when right clicked, opens the context menu.
  • Menu: The root container for the popover menu
  • Item: A menuitem which can be a link or a function.
  • Checkbox Item: A menu item which can be checked or unchecked.
  • Radio Group: A group of radio items.
  • Radio Item: A menu item which can be selected from a group of items.
  • Sub Trigger: A button which toggles the submenu's open state.
  • Sub Menu: A menu which is nested inside another menu.
  • Separator: A visual divider between menu items.

Usage

The first thing you need to do is create a dropdown menu using the createDropdownMenu function.

    <script lang="ts">
  import { createContextMenu } from '@melt-ui/svelte'
  const { menu, item, trigger, arrow } = createContextMenu()
</script>

Then you can use the menu, item, and trigger to construct a dropdown menu. A high level example of how to structure the menu is shown below.

    <script lang="ts">
  import { createContextMenu } from '@melt-ui/svelte'
  const { menu, item, trigger, arrow } = createContextMenu()
</script>
 
<span {...$trigger} use:trigger>Right click here.</span>
<div {...$menu} use:menu>
  <div {...$item} use:item>...</div>
  <div {...$item} use:item>...</div>
  <div {...$item} use:item>...</div>
  <div {...$arrow} />
</div>

The trigger sits outside of the menu and is used to toggle the menu's open state. The item elements go inside the menu element. The arrow element is optional and can be used to render an arrow which points to the trigger.

At this point, our menu doesn't really do much except open and close. To add functionality, we could turn the item elements into links, or we could pass a custom onSelect function to the item action, which will be called when that item is selected.

    <a href="/1" {...$item} use:item>Item 1</a>
<div {...$item} use:item={{ onSelect: (e) => console.log('Item 2!') }}>Item 2</div>
<div {...$item} use:item={{ onSelect: (e) => console.log('Item 3!') }}>Item 3</div>

If you wanted to prevent the default behavior that occurs when you select an item, you can call e.preventDefault() in your onSelect function, which will prevent the default behavior from occurring.

    <div
  {...$item}
  use:item={{
    onSelect: (e) => {
      e.preventDefault()
    }
  }}>
  Item 2
</div>

API Reference

createContextMenu

Props

Prop Default Type / Description
positioning placement: 'right'
FloatingConfig

A configuration object which determines how the floating element is positioned relative to the trigger.

arrowSize 8
number

The size of the arrow which points to the trigger in pixels.

preventScroll true
boolean

Whether or not to prevent scrolling of the document when the context menu is open.

Returned Props

Returned Prop Type Description
open
Writable<boolean>

A writable store that controls the open state of the context menu.

options
Writable<CreateContextMenuProps>

A writable store that controls the options of the context menu.

menu
-

The builder store used to create the context menu.

trigger
-

The builder store used to create the context menu trigger.

checkboxItem
-

The builder store used to create a checkbox menu item.

separator
-

The builder store used to create a separator menu item.

arrow
-

The builder store used to create the context menu arrow.

trigger

Data Attributes

Data Attribute Value
[data-state]

'open' | 'closed'

[data-melt-context-menu-trigger]

Present on all trigger elements.

Data Attributes

Data Attribute Value
[data-state]

'open' | 'closed'

[data-melt-context-menu]

Present on all context-menu elements.

item

Props

Prop Default Type / Description
onSelect -
(e: Event) => void

A callback which is called when the item is selected. To prevent the default behavior, call e.preventDefault() in the callback.

Data Attributes

Data Attribute Value
[data-orientation]

'vertical' | 'horizontal'

[data-context-menu-item]

Present on all item elements.

[data-highlighted]

Present when the item is highlighted.

checkboxItem

Props

Prop Default Type / Description
checked -
Writable<boolean>

A writable store which controls the checked state of the checkbox item.

onSelect -
(e: Event) => void

A callback which is called when the item is selected. To prevent the default behavior, call e.preventDefault() in the callback.

Data Attributes

Data Attribute Value
[data-orientation]

'vertical' | 'horizontal'

[data-melt-context-menu-checkbox-item]

Present on all checkbox item elements.

[data-highlighted]

Present when the checkbox item is highlighted.

createMenuRadioGroup

Props

Prop Default Type / Description
value -
string

The value of the selected radio item.

Returned Props

Returned Prop Type Description
value
Writable<string | null>

A writable store which controls the value of the selected radio item.

isChecked
Readable<(itemValue: string) => boolean>

A derived store which returns a function that checks if a radio item is selected.

radioGroup
-

The builder store used to create the radio group.

radioItem
-

The builder store used to create a radio menu item.

radioGroup

Data Attributes

Data Attribute Value
[data-melt-context-menu-radio-group]

Present on all radio group elements.

radioItem

Props

Prop Default Type / Description
value * -
string

The value of the radio item.

disabled false
boolean

Whether the radio item is disabled.

onSelect -
(e: Event) => void

A callback which is called when the item is selected. To prevent the default behavior, call e.preventDefault() in the callback.

Data Attributes

Data Attribute Value
[data-checked]

'checked' | 'unchecked'

[data-disabled]

Present when the radio item is disabled.

[data-value]

The value of the radio item.

[data-orientation]

'vertical' | 'horizontal'

[data-melt-context-menu-radio-item]

Present on all radio item elements.

[data-highlighted]

Present when the radio item is highlighted.

createSubMenu

Props

Prop Default Type / Description
positioning placement: 'right'
FloatingConfig

A configuration object which determines how the floating element is positioned relative to the trigger.

disabled false
boolean

Whether the submenu is disabled.

Returned Props

Returned Prop Type Description
subOpen
Writable<boolean>

A writable store that controls the open state of the submenu.

options
Writable<CreateContextSubmenuProps>

A writable store that controls the options of the submenu.

subMenu
-

The store used to create the submenu.

subTrigger
-

The builder store used to create the submenu trigger.

arrow
-

The builder store used to create the submenumenu arrow.

subTrigger

Data Attributes

Data Attribute Value
[data-state]

'open' | 'closed'

[data-disabled]

Present when the subtrigger is disabled.

[data-melt-context-menu-subtrigger]

Present on all subtrigger elements.

[data-highlighted]

Present when the subtrigger is highlighted.

Data Attributes

Data Attribute Value
[data-state]

'open' | 'closed'

[data-melt-context-menu-submenu]

Present on all submenu elements.

separator

Data Attributes

Data Attribute Value
[data-melt-context-menu-separator]

Present on all separator elements.

arrow

Data Attributes

Data Attribute Value
[data-arrow]

'true'

[data-melt-context-menu-arrow]

Present on all arrow elements.

Accessibility

Adheres to the Menu WAI-ARIA design pattern

Key Behavior
Space

When focused on the trigger, opens the dropdown and focuses the first item. When focused on an item, selects the item.

Enter

When focused on the trigger, opens the dropdown and focuses the first item. When focused on an item, selects the item.

ArrowDown

When focused on the trigger, opens the dropdown and focuses the first item. When focused on an item, shifts focus to the next item.

ArrowUp

When focused on an item, shifts focus to the next item..

ArrowRight

When focused on a subTrigger, opens the subMenu and focuses the first item.

ArrowLeft

When focused on within a subMenu, closes the submenu and shifts focus to that submenu's subTrigger.

Esc

Closes the dropdown menu and focuses the trigger.