Context Menu
A menu that opens on a right click, with items, keyboard shortcuts, separators and a danger variant.
Use it for the actions that belong to one thing on the page: a message, a channel, a member. It opens at the pointer on a right click, and on a long press on a touch screen. For a menu that opens from a button use a Dropdown Menu.
It is built on Radix Context Menu in React and Reka UI Context Menu in Vue. The primitive owns the behaviour; newt/ui owns the look. The menu moves when it would leave the viewport.
newt-context-menuimport {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuTrigger,
} from "@/registry/bases/newt/ui/context-menu"
export default function ContextMenuDemo() {
return (
<ContextMenu>
<ContextMenuTrigger asChild>
<div
tabIndex={0}
className="flex h-32 w-72 max-w-full items-center justify-center rounded-md border border-dashed border-newt-border text-sm text-newt-text-muted focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link"
>
Right click here
</div>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>Action one</ContextMenuItem>
<ContextMenuItem>
Action two
<ContextMenuShortcut>Ctrl+K</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="danger">Delete</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
)
}<script setup lang="ts">
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuTrigger,
} from "~~/registry/bases/newt/ui/context-menu"
</script>
<template>
<ContextMenu>
<ContextMenuTrigger as-child>
<div
tabindex="0"
class="flex h-32 w-72 max-w-full items-center justify-center rounded-md border border-dashed border-newt-border text-sm text-newt-text-muted focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-text-link"
>
Right click here
</div>
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem>Action one</ContextMenuItem>
<ContextMenuItem>
Action two
<ContextMenuShortcut>Ctrl+K</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSeparator />
<ContextMenuItem variant="danger">Delete</ContextMenuItem>
</ContextMenuContent>
</ContextMenu>
</template>Installation
npx newtui@latest add context-menunpx shadcn@latest add @newtui/context-menunpx shadcn-vue@latest add @newtui/context-menupnpm dlx newtui@latest add context-menupnpm dlx shadcn@latest add @newtui/context-menupnpm dlx shadcn-vue@latest add @newtui/context-menuyarn dlx newtui@latest add context-menuyarn dlx shadcn@latest add @newtui/context-menuyarn dlx shadcn-vue@latest add @newtui/context-menubunx newtui@latest add context-menubunx shadcn@latest add @newtui/context-menubunx shadcn-vue@latest add @newtui/context-menudeno run -A npm:newtui@latest add context-menudeno run -A npm:shadcn@latest add @newtui/context-menudeno run -A npm:shadcn-vue@latest add @newtui/context-menuvlx newtui@latest add context-menuvlx shadcn@latest add @newtui/context-menuvlx shadcn-vue@latest add @newtui/context-menuvp dlx newtui@latest add context-menuvp dlx shadcn@latest add @newtui/context-menuvp dlx shadcn-vue@latest add @newtui/context-menunubx newtui@latest add context-menunubx shadcn@latest add @newtui/context-menunubx shadcn-vue@latest add @newtui/context-menunlx newtui@latest add context-menunlx shadcn@latest add @newtui/context-menunlx shadcn-vue@latest add @newtui/context-menuupx newtui@latest add context-menuupx shadcn@latest add @newtui/context-menuupx shadcn-vue@latest add @newtui/context-menu- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install class-variance-authority radix-uipnpm add class-variance-authority radix-uiyarn add class-variance-authority radix-uibun add class-variance-authority radix-uideno add npm:class-variance-authority npm:radix-uivlt install class-variance-authority radix-uivp add class-variance-authority radix-uinub add class-variance-authority radix-uini class-variance-authority radix-uiupm add class-variance-authority radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install class-variance-authority reka-uipnpm add class-variance-authority reka-uiyarn add class-variance-authority reka-uibun add class-variance-authority reka-uideno add npm:class-variance-authority npm:reka-uivlt install class-variance-authority reka-uivp add class-variance-authority reka-uinub add class-variance-authority reka-uini class-variance-authority reka-uiupm add class-variance-authority reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Copy the source into your project
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import { ContextMenu as ContextMenuPrimitive } from "radix-ui"
import * as React from "react"
import { cn } from "@/lib/utils"
/*
* Radix owns the behaviour: it opens at the pointer on a right click, a long
* press or the menu key, and handles Escape and outside press, arrow keys,
* Home and End, typeahead, and focus return. Only the look is newt/ui's.
*/
const ContextMenu = ContextMenuPrimitive.Root
/*
* The area that answers the right click. It renders a `span` with no look of
* its own; pass `asChild` to put it on your own element.
*/
const ContextMenuTrigger = ContextMenuPrimitive.Trigger
const ContextMenuGroup = ContextMenuPrimitive.Group
const ContextMenuContent = React.forwardRef<
React.ComponentRef<typeof ContextMenuPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Content> & {
/**
* The element the menu is portalled into. Defaults to `document.body`;
* pass the enclosing native `<dialog>` or a themed wrapper to keep it there.
*/
container?: React.ComponentProps<
typeof ContextMenuPrimitive.Portal
>["container"]
}
>(({ className, container, ...props }, ref) => (
<ContextMenuPrimitive.Portal container={container}>
<ContextMenuPrimitive.Content
ref={ref}
className={cn(
"z-[110] w-[188px] rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-floating p-1.5 shadow-elevation-high outline-none",
className
)}
{...props}
/>
</ContextMenuPrimitive.Portal>
))
ContextMenuContent.displayName = "ContextMenuContent"
/*
* `data-highlighted` is the primitive's one state for the pointer and the
* keyboard, so it takes the place of the old `:hover` rule.
*/
const contextMenuItemVariants = cva(
"group flex cursor-pointer select-none items-center justify-between rounded-[var(--newt-radius-sm)] px-2 py-1.5 text-[13px] outline-none transition-colors duration-fast ease-newt data-[highlighted]:text-newt-on-accent data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
{
variants: {
variant: {
default: "text-newt-text-secondary data-[highlighted]:bg-newt-brand",
danger: "text-newt-dnd data-[highlighted]:bg-newt-dnd",
},
},
defaultVariants: {
variant: "default",
},
}
)
export interface ContextMenuItemProps
extends
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Item>,
VariantProps<typeof contextMenuItemVariants> {}
const ContextMenuItem = React.forwardRef<
React.ComponentRef<typeof ContextMenuPrimitive.Item>,
ContextMenuItemProps
>(({ className, variant, ...props }, ref) => (
<ContextMenuPrimitive.Item
ref={ref}
className={cn(contextMenuItemVariants({ variant }), className)}
{...props}
/>
))
ContextMenuItem.displayName = "ContextMenuItem"
const ContextMenuLabel = React.forwardRef<
React.ComponentRef<typeof ContextMenuPrimitive.Label>,
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Label>
>(({ className, ...props }, ref) => (
<ContextMenuPrimitive.Label
ref={ref}
className={cn(
"px-2 py-1.5 text-xs font-bold uppercase tracking-[0.02em] text-newt-text-muted",
className
)}
{...props}
/>
))
ContextMenuLabel.displayName = "ContextMenuLabel"
const ContextMenuShortcut = React.forwardRef<
HTMLSpanElement,
React.ComponentProps<"span">
>(({ className, ...props }, ref) => (
<span
ref={ref}
className={cn(
"text-[11px] text-newt-text-muted group-data-[highlighted]:text-[color-mix(in_srgb,var(--newt-on-accent)_70%,transparent)]",
className
)}
{...props}
/>
))
ContextMenuShortcut.displayName = "ContextMenuShortcut"
const ContextMenuSeparator = React.forwardRef<
React.ComponentRef<typeof ContextMenuPrimitive.Separator>,
React.ComponentPropsWithoutRef<typeof ContextMenuPrimitive.Separator>
>(({ className, ...props }, ref) => (
<ContextMenuPrimitive.Separator
ref={ref}
className={cn("mx-1.5 my-1 h-px bg-newt-border", className)}
{...props}
/>
))
ContextMenuSeparator.displayName = "ContextMenuSeparator"
export {
ContextMenu,
ContextMenuTrigger,
ContextMenuContent,
ContextMenuGroup,
ContextMenuLabel,
ContextMenuItem,
ContextMenuShortcut,
ContextMenuSeparator,
contextMenuItemVariants,
}<script setup lang="ts">
import {
ContextMenuRoot,
useForwardPropsEmits,
type ContextMenuRootEmits,
type ContextMenuRootProps,
} from "reka-ui"
/*
* Reka owns the behaviour: it opens at the pointer on a right click, a long
* press or the menu key, and handles Escape and outside press, arrow keys,
* Home and End, typeahead, and focus return. Only the look is newt/ui's.
*/
const props = defineProps<ContextMenuRootProps>()
const emits = defineEmits<ContextMenuRootEmits>()
const forwarded = useForwardPropsEmits(props, emits)
</script>
<template>
<ContextMenuRoot v-bind="forwarded">
<slot />
</ContextMenuRoot>
</template><script setup lang="ts">
import { ContextMenuTrigger, type ContextMenuTriggerProps } from "reka-ui"
/*
* The area that answers the right click. It renders a `span` with no look of
* its own; pass `as-child` to put it on your own element.
*/
const props = defineProps<ContextMenuTriggerProps>()
</script>
<template>
<ContextMenuTrigger v-bind="props">
<slot />
</ContextMenuTrigger>
</template><script setup lang="ts">
import {
ContextMenuContent,
ContextMenuPortal,
useForwardPropsEmits,
type ContextMenuContentEmits,
type ContextMenuContentProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
defineOptions({ inheritAttrs: false })
const props = defineProps<
ContextMenuContentProps & {
class?: HTMLAttributes["class"]
/**
* The element it is portalled into. Defaults to `body`; pass the
* enclosing native `<dialog>` or a themed wrapper to keep it there.
*/
container?: string | HTMLElement
}
>()
const emits = defineEmits<ContextMenuContentEmits>()
const delegated = computed(() => {
const { class: _class, container: _container, ...rest } = props
return rest
})
const forwarded = useForwardPropsEmits(delegated, emits)
</script>
<template>
<ContextMenuPortal :to="props.container">
<ContextMenuContent
v-bind="{ ...forwarded, ...$attrs }"
:class="
cn(
'z-[110] w-[188px] rounded-[var(--newt-radius-md)] border border-newt-border bg-newt-bg-floating p-1.5 shadow-elevation-high outline-none',
props.class
)
"
>
<slot />
</ContextMenuContent>
</ContextMenuPortal>
</template><script setup lang="ts">
import { ContextMenuGroup, type ContextMenuGroupProps } from "reka-ui"
const props = defineProps<ContextMenuGroupProps>()
</script>
<template>
<ContextMenuGroup v-bind="props">
<slot />
</ContextMenuGroup>
</template><script setup lang="ts">
import {
ContextMenuLabel,
useForwardProps,
type ContextMenuLabelProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<
ContextMenuLabelProps & { class?: HTMLAttributes["class"] }
>()
const delegated = computed(() => {
const { class: _class, ...rest } = props
return rest
})
const forwarded = useForwardProps(delegated)
</script>
<template>
<ContextMenuLabel
v-bind="forwarded"
:class="
cn(
'px-2 py-1.5 text-xs font-bold uppercase tracking-[0.02em] text-newt-text-muted',
props.class
)
"
>
<slot />
</ContextMenuLabel>
</template><script setup lang="ts">
import {
ContextMenuItem,
useForwardPropsEmits,
type ContextMenuItemEmits,
type ContextMenuItemProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import { contextMenuItemVariants, type ContextMenuItemVariants } from "."
const props = defineProps<
ContextMenuItemProps & {
class?: HTMLAttributes["class"]
variant?: ContextMenuItemVariants["variant"]
}
>()
const emits = defineEmits<ContextMenuItemEmits>()
const delegated = computed(() => {
const { class: _class, variant: _variant, ...rest } = props
return rest
})
const forwarded = useForwardPropsEmits(delegated, emits)
</script>
<template>
<ContextMenuItem
v-bind="forwarded"
:class="
cn(contextMenuItemVariants({ variant: props.variant }), props.class)
"
>
<slot />
</ContextMenuItem>
</template><script setup lang="ts">
import type { HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>
<template>
<span
:class="
cn(
'text-[11px] text-newt-text-muted group-data-[highlighted]:text-[color-mix(in_srgb,var(--newt-on-accent)_70%,transparent)]',
props.class
)
"
>
<slot />
</span>
</template><script setup lang="ts">
import {
ContextMenuSeparator,
useForwardProps,
type ContextMenuSeparatorProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<
ContextMenuSeparatorProps & { class?: HTMLAttributes["class"] }
>()
const delegated = computed(() => {
const { class: _class, ...rest } = props
return rest
})
const forwarded = useForwardProps(delegated)
</script>
<template>
<ContextMenuSeparator
v-bind="forwarded"
:class="cn('mx-1.5 my-1 h-px bg-newt-border', props.class)"
/>
</template>import { cva, type VariantProps } from "class-variance-authority"
export { default as ContextMenu } from "./ContextMenu.vue"
export { default as ContextMenuTrigger } from "./ContextMenuTrigger.vue"
export { default as ContextMenuContent } from "./ContextMenuContent.vue"
export { default as ContextMenuGroup } from "./ContextMenuGroup.vue"
export { default as ContextMenuLabel } from "./ContextMenuLabel.vue"
export { default as ContextMenuItem } from "./ContextMenuItem.vue"
export { default as ContextMenuShortcut } from "./ContextMenuShortcut.vue"
export { default as ContextMenuSeparator } from "./ContextMenuSeparator.vue"
/*
* `data-highlighted` is the primitive's one state for the pointer and the
* keyboard, so it takes the place of the old `:hover` rule.
*/
export const contextMenuItemVariants = cva(
"group flex cursor-pointer select-none items-center justify-between rounded-[var(--newt-radius-sm)] px-2 py-1.5 text-[13px] outline-none transition-colors duration-fast ease-newt data-[highlighted]:text-newt-on-accent data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
{
variants: {
variant: {
default: "text-newt-text-secondary data-[highlighted]:bg-newt-brand",
danger: "text-newt-dnd data-[highlighted]:bg-newt-dnd",
},
},
defaultVariants: {
variant: "default",
},
}
)
export type ContextMenuItemVariants = VariantProps<
typeof contextMenuItemVariants
>Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Context Menu to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Context Menu to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { ContextMenu } from "@/components/ui/context-menu"
export function Example() {
return <ContextMenu />
}<script setup lang="ts">
import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuGroup, ContextMenuLabel, ContextMenuItem, ContextMenuShortcut, ContextMenuSeparator } from "@/components/ui/context-menu"
</script>
<template>
<ContextMenu />
</template>ContextMenuTrigger is the area that answers the right click. It renders a span with no look of its own. Use asChild (as-child in Vue) to put it on your own element, as the example does.
Listen to onSelect (@select in Vue) on an item for the choice. Put a ContextMenuShortcut after the item text to show its key. The shortcut is a hint only; bind the key yourself.
API reference
ContextMenu
onOpenChange?(open: boolean) => void
React only. Called when the menu opens or closes; Vue emits `update:open`.
(open: boolean) => voidmodal?boolean
When true, the page outside the menu does not react to the pointer while the menu is open.
booleantrueContextMenuTrigger
disabled?boolean
When true, a right click on the area opens the browser menu.
booleanfalseContextMenuContent
loop?boolean
When true, the arrow keys move from the last item to the first and back.
booleanfalsecontainer?HTMLElement
Element the menu is portalled into. Pass the enclosing native `<dialog>` or a themed wrapper to keep it there.
HTMLElementdocument.bodyContextMenuItem
variant?"default" | "danger"
`danger` marks a destructive action.
"default" | "danger""default"disabled?boolean
Dims the item. The pointer and the arrow keys then skip it.
booleanonSelect / @select?(event: Event) => void
Called when the item is chosen. Call `event.preventDefault()` to keep the menu open.
(event: Event) => voidtextValue?string
The text typeahead matches. Set it when the item content is not plain text.
stringThe registry exports ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuGroup, ContextMenuLabel, ContextMenuItem, ContextMenuShortcut, ContextMenuSeparator and contextMenuItemVariants. Parts without a table accept the props of the primitive part or the native element they render. React uses className; Vue uses class and fallthrough attributes.
Accessibility
The panel is a role="menu" and its items are role="menuitem". ContextMenuGroup is a role="group"; put a ContextMenuLabel inside it as its heading. Pass aria-label to ContextMenuContent when the menu needs a name.
A right click on the trigger area opens the menu at the pointer. A long press does the same on a touch screen. With the keyboard, focus an element inside the area and press Shift+F10 or the menu key. The area itself takes no focus, so put a focusable element inside it, or give it tabIndex={0}.
Focus moves into the menu when it opens. The arrow keys, Home and End move between items. A typed letter moves to the next item that starts with it. Enter or Space chooses the focused item. Escape closes the menu, and so does a press outside.
Do not make a context menu the only way to reach an action. Many people never right click, so offer the same actions in a visible control too.
Tokens
Context Menu reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-context-menu.
--newt-bg-floating--newt-border--newt-radius-md--newt-shadow-elevation-high--newt-radius-sm--newt-text-secondary--newt-duration-fast--newt-ease--newt-brand--newt-on-accent--newt-dnd--newt-text-muted