Context Menu
Installation
npx @mielui/svelte add context-menuyarn dlx @mielui/svelte add context-menubunx @mielui/svelte add context-menuUsage
Bind open to observe or close the root menu, and use onOpenChange for interaction callbacks. Pointer and keyboard opening keep the same state.
Open the menu with a right-click, a touch long-press, or the Context Menu key or Shift+F10 on its focused trigger. Arrow keys, Home, End, and typing navigate items; submenus support directional keys. An item onclick handler can call event.preventDefault() to cancel selection and dismissal.
<script lang="ts">
import * as ContextMenu from '@mielui/svelte/components/context-menu';
let copied = $state(false);
</script>
<ContextMenu.Root>
<ContextMenu.Trigger>Right-click here</ContextMenu.Trigger>
<ContextMenu.Content>
<ContextMenu.Item
callback={() => {
copied = true;
}}
>
Copy
</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Root>Examples
Live actions
Each action changes the file in place, and a submenu holds the less common ones.
File actions
Open the file menu to rename it.
Image actions
Right-click or press and hold the illustration.
Task actions
Glass surface
Set surface="glass" on Content and SubContent for a translucent panel with blur, or surface="solid" for an opaque one. Without surface, the panel follows the theme. Glass falls back to an opaque panel when backdrop filtering is unavailable or reduced transparency is on.
Checkbox items
CheckboxItem toggles an option in place. Give each one a value that is unique within the menu, bind checked to read or set the state, and use callback to react when the person toggles it. inset adds the leading space that lines a plain Item up with the checkbox rows.
Items and other parts
Item and CheckboxItem are built on Button, so loading with labels shows progress. variant="destructive" on Item colors the row for an action that removes something and tints the highlight while it is active. Rows always use the menu row style, so Button's other variants, size and unstyled do not apply. element binds the DOM node, and onkeydown runs before the menu handles the key. A row is always a button and does not take href. Navigate from callback.
Root, Trigger, Content, Separator, Sub, SubTrigger and SubContent render their children and accept class and style like any element.
API reference
ContextMenu.Root
Provides context menu state.
childrenSnippet<[]>Content rendered inside.
openBindablebooleanWhether it is open. Bind it to control the state from outside.
Default
falseonOpenChange((open: boolean) => void)Called with the new state whenever it opens or closes.
ContextMenu.CheckboxItem
Renders a checkable menu item.
callback(() => void)Called when the item is chosen.
valueRequiredstring | (string & string[])Value reported for the item.
insetbooleanIndents the item so it lines up with items that have an indicator.
Default
falsecheckedBindablebooleanWhether the item is checked. Bindable.
Default
falsechildrenSnippet<[]>Content rendered inside.
onkeydown((event: KeyboardEvent) => void)Called on key down, before the button handles the key itself.
onclick((event: MouseEvent) => void)Called when the button is activated. Loading and disabled buttons do not call it.
statusButtonStatusControlled visual state. Loading remains focusable and refuses activation.
disabledbooleanPrevents activation. A disabled link loses its destination and is skipped by Tab.
Default
falseelementBindableHTMLButtonElement | HTMLAnchorElementBindable reference to the rendered DOM element. Type is the union of the two possible element types -- narrow at the use site:
loadingbooleanConvenience alias for
status="loading".labelsButtonLabelsOverrides the built-in text and accessible names. Every key is optional and English is the fallback.
ContextMenu.Content
Renders the menu surface.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
surface"solid" | "glass"Surface treatment. Omit it to inherit
--mielui-surfacefrom the theme.
ContextMenu.Item
Renders a menu action.
callback(() => void)Called when the item is chosen.
insetbooleanIndents the item so it lines up with items that have an indicator.
Default
falsevariant"destructive"Marks an action that deletes or cannot be undone. Omit it for a regular item.
childrenSnippet<[]>Content rendered inside.
onkeydown((event: KeyboardEvent) => void)Called on key down, before the button handles the key itself.
onclick((event: MouseEvent) => void)Called when the button is activated. Loading and disabled buttons do not call it.
statusButtonStatusControlled visual state. Loading remains focusable and refuses activation.
disabledbooleanPrevents activation. A disabled link loses its destination and is skipped by Tab.
Default
falseelementBindableHTMLButtonElement | HTMLAnchorElementBindable reference to the rendered DOM element. Type is the union of the two possible element types -- narrow at the use site:
loadingbooleanConvenience alias for
status="loading".labelsButtonLabelsOverrides the built-in text and accessible names. Every key is optional and English is the fallback.
ContextMenu.Separator
Separates menu items.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
ContextMenu.Sub
Provides submenu state.
childrenSnippet<[]>Content rendered inside.
ContextMenu.SubContent
Renders a submenu surface.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
surface"solid" | "glass"Surface treatment. Omit it to inherit
--mielui-surfacefrom the theme.
ContextMenu.SubTrigger
Opens a submenu.
insetbooleanIndents the item so it lines up with items that have an indicator.
Default
falseclassstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
ContextMenu.Trigger
Defines the context menu target.
classstringClasses added to the element.
childrenSnippet<[]>Content rendered inside.
pnpm dlx @mielui/svelte add context-menu