Context Menu
Menu styled for right-click use, built on the @foldkit/ui Menu submodel. Opens at the pointer when the caller records the pointer position as maybePoint; keyboard open anchors to the trigger region.
Differences vs shadcn/ui
- Opens at the pointer on right-click or touch long-press; keyboard open anchors to the region, since foldkit gives no point for it. Pointer position is the caller’s model state, passed as `maybePoint`. iOS Safari fires no `contextmenu` on long-press, so it does not open there.Submenus, keep-open checkbox/radio rows, destructive, and inset rows follow the menu caveats.The trigger is the anchor, so content passed as `buttonContent` travels with it while the menu is open; region content that must stay in place belongs beside the submodel.
Installation
Add this component to your project:
pnpm dlx shadcn@latest add @foldcn/context-menuSource
The component ships as plain source — no build step, no wrapper. Copy it and make it yours.
registry/default/ui/context-menu.ts297 lines/** Stateful submodel — import the whole module as a namespace and wire its
* Model/Message/init/update into your app:
* `import * as ContextMenu from '@/components/ui/context-menu'`
*/
import { Menu as FoldkitMenu } from '@foldkit/ui'
import type { AnchorConfig } from '@foldkit/ui/menu'
import { Option, Schema as S } from 'effect'
import {
childAttributes,
inertHtml,
type ChildAttribute,
type Html,
type HtmlBuilder,
} from 'foldkit/html'
import { Check, ChevronRight } from 'lucide'
import { icon } from '@/lib/icons'
import { cn } from '@/lib/utils'
// Re-export the @foldkit/ui Menu surface. A context menu is a Menu variant
// whose trigger is moved to the pointer: the caller records the right-click
// (or touch long-press) position as a `Point`, passes it to `viewInputs` as
// `maybePoint`, and the trigger becomes a zero-size fixed element at that
// point. foldkit's anchor positioning (flip + shift with the anchor padding)
// then places the panel at the pointer and keeps it inside the viewport.
// Without a point (keyboard open) the trigger keeps its normal place. The
// trigger is the anchor, so content passed as `buttonContent` travels with it
// while the menu is open; region content that must stay in place belongs
// beside the submodel.
export const create = FoldkitMenu.create
export const init = (config: InitConfig): Model => FoldkitMenu.init({ isAnimated: true, ...config })
export const buttonId = FoldkitMenu.buttonId
export const Model = FoldkitMenu.Model
export type Model = typeof Model.Type
export const Message = FoldkitMenu.Message
export type Message = typeof Message.Type
export const OutMessage = FoldkitMenu.OutMessage
export type OutMessage = typeof OutMessage.Type
// Remaining gaps vs upstream: items highlight via data-active (upstream
// focus:) per the derivation mapping. The panel's data-side is emitted from
// the anchor placement (foldkit anchors expose placement, not side). Per-item
// data-slot (context-menu-item etc) cannot be stamped — primitive has no
// per-item attribute hook. The primitive's item config is `{ className, content }`
// over one flat level, so item kinds are foldcn-owned: destructive/inset via
// `contextMenuItemDestructiveClass` / `contextMenuItemInsetClass`; checkbox/radio
// via `contextMenuItemIndicator` (keep the panel open by handling `SelectedItem`
// yourself instead of forwarding it to `update`, which would close the menu);
// submenus via a nested bundle built with `subViewInputs` that the consumer
// slice opens/closes and key-coordinates (see the web menu demo).
/** Viewport (client) coordinates of the pointer that requested the menu. */
export const Point = S.Struct({ x: S.Number, y: S.Number })
export type Point = typeof Point.Type
/** Pointer-down handler result: the point for a right-click or a touch/pen
* press (which becomes a long-press `contextmenu` on touch browsers that
* fire it), none for other buttons. Wire it to the region's `h.OnPointerDown`
* alongside `h.OnContextMenu(Opened)`. */
export const pointFromPointerDown = (
pointerType: string,
button: number,
clientX: number,
clientY: number,
): Option.Option<Point> =>
button === 2 || pointerType === 'touch' || pointerType === 'pen'
? Option.some({ x: clientX, y: clientY })
: Option.none()
export type Bundle<Item extends string = string> = FoldkitMenu.Bundle<Item>
export type InitConfig = FoldkitMenu.InitConfig
export type ViewInputs<Item extends string = string> = FoldkitMenu.ViewInputs<Item>
export type ItemConfig = FoldkitMenu.ItemConfig
export type GroupHeading = FoldkitMenu.GroupHeading
export const contextMenuTriggerClass = 'select-none'
export const contextMenuItemsClass =
'data-enter:animate-in data-leave:animate-out data-leave:fade-out-0 data-enter:fade-in-0 data-leave:zoom-out-95 data-enter:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 ring-foreground/10 text-popover-foreground min-w-36 rounded-lg p-1 shadow-md ring-1 duration-100 not-data-[side=bottom]:data-[placement=bottom]:slide-in-from-top-2 not-data-[side=left]:data-[placement=left]:slide-in-from-right-2 not-data-[side=right]:data-[placement=right]:slide-in-from-left-2 not-data-[side=top]:data-[placement=top]:slide-in-from-bottom-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=inline-end]:slide-in-from-left-2 animate-none! relative before:pointer-events-none before:absolute before:inset-0 before:-z-1 before:rounded-[inherit] before:backdrop-blur-2xl before:backdrop-saturate-150 **:data-[slot$=-item]:focus:bg-foreground/10 **:data-[slot$=-item]:data-highlighted:bg-foreground/10 **:data-[slot$=-separator]:bg-foreground/5 **:data-[slot$=-trigger]:focus:bg-foreground/10 **:data-[slot$=-trigger]:aria-expanded:bg-foreground/10! **:data-[variant=destructive]:focus:bg-foreground/10! **:data-[variant=destructive]:text-accent-foreground! **:data-[variant=destructive]:**:text-accent-foreground! bg-popover z-50 max-h-(--available-height) origin-(--transform-origin) overflow-x-hidden overflow-y-auto outline-none'
export const contextMenuItemsAnimatedClass = contextMenuItemsClass
export const contextMenuItemClass =
'focus:bg-accent focus:text-accent-foreground data-[variant=destructive]:text-destructive data-[variant=destructive]:focus:bg-destructive/10 dark:data-[variant=destructive]:focus:bg-destructive/20 data-[variant=destructive]:focus:text-destructive data-[variant=destructive]:*:[svg]:text-destructive focus:*:[svg]:text-accent-foreground gap-1.5 rounded-md px-1.5 py-1 text-sm data-inset:pl-7 [&_svg:not([class*=\'size-\'])]:size-4 group/context-menu-item relative flex cursor-default items-center outline-hidden select-none data-active:bg-accent data-active:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0'
export const contextMenuSeparatorClass = 'bg-border -mx-1 my-1 h-px'
export const contextMenuHeadingClass = 'text-muted-foreground px-1.5 py-1 text-xs font-medium data-inset:pl-7'
export const contextMenuShortcutClass = 'text-muted-foreground group-focus/context-menu-item:text-accent-foreground ml-auto text-xs tracking-widest group-data-active/context-menu-item:text-accent-foreground'
export const contextMenuSubTriggerClass =
'focus:bg-accent focus:text-accent-foreground data-open:bg-accent data-open:text-accent-foreground gap-1.5 rounded-md px-1.5 py-1 text-sm data-inset:pl-7 [&_svg:not([class*=\'size-\'])]:size-4 group/context-menu-item relative flex cursor-default items-center outline-hidden select-none data-active:bg-accent data-active:text-accent-foreground data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0'
/** Highlight for the parent row while its submenu is open. */
export const contextMenuSubTriggerOpenClass = 'bg-accent text-accent-foreground'
/** Upstream `ContextMenuSubContent` string (the nested panel). */
export const contextMenuSubContentClass =
'data-enter:animate-in data-leave:animate-out data-leave:fade-out-0 data-enter:fade-in-0 data-leave:zoom-out-95 data-enter:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 text-popover-foreground min-w-32 rounded-lg border p-1 shadow-lg duration-100 animate-none! relative before:pointer-events-none before:absolute before:inset-0 before:-z-1 before:rounded-[inherit] before:backdrop-blur-2xl before:backdrop-saturate-150 **:data-[slot$=-item]:focus:bg-foreground/10 **:data-[slot$=-item]:data-highlighted:bg-foreground/10 **:data-[slot$=-separator]:bg-foreground/5 **:data-[slot$=-trigger]:focus:bg-foreground/10 **:data-[slot$=-trigger]:aria-expanded:bg-foreground/10! **:data-[variant=destructive]:focus:bg-foreground/10! **:data-[variant=destructive]:text-accent-foreground! **:data-[variant=destructive]:**:text-accent-foreground! bg-popover z-50 max-h-(--available-height) w-auto origin-(--transform-origin) overflow-x-hidden overflow-y-auto outline-none'
/** Row className for upstream `variant="destructive"`. */
export const contextMenuItemDestructiveClass = 'text-destructive data-active:bg-destructive/10 data-active:text-destructive dark:data-active:bg-destructive/20 [&_svg]:text-destructive!'
/** Row className for upstream `inset`. */
export const contextMenuItemInsetClass = 'pl-7'
/** Upstream `ContextMenuCheckboxItem` / `ContextMenuRadioItem` strings, with
* the foldkit highlight twin. */
export const contextMenuCheckboxItemClass =
'focus:bg-accent focus:text-accent-foreground gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm data-inset:pl-7 [&_svg:not([class*=\'size-\'])]:size-4 data-active:bg-accent data-active:text-accent-foreground relative flex cursor-default items-center outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0'
export const contextMenuRadioItemClass =
'focus:bg-accent focus:text-accent-foreground gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm data-inset:pl-7 [&_svg:not([class*=\'size-\'])]:size-4 data-active:bg-accent data-active:text-accent-foreground relative flex cursor-default items-center outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0'
export const contextMenuItemIndicatorClass = 'absolute right-2 pointer-events-none'
/** Check indicator for a checkbox or radio row: render it inside the row's
* `content` (empty when unchecked) and give the row `contextMenuCheckboxItemClass`
* / `contextMenuRadioItemClass`. */
export const contextMenuItemIndicator = <M>(h: HtmlBuilder<M>, isChecked: boolean): Html =>
h.span(
[
h.Class(contextMenuItemIndicatorClass),
h.DataAttribute('slot', 'context-menu-item-indicator'),
],
isChecked ? [icon(h, Check)] : [],
)
/** Trailing chevron for a submenu trigger row. */
export const contextMenuSubTriggerChevron = <M>(h: HtmlBuilder<M>): Html =>
icon(h, ChevronRight, 'rtl:rotate-180 ml-auto size-4')
export const contextMenuBackdropClass = 'fixed inset-0 z-0'
export const contextMenuWrapperClass = 'relative inline-block'
// Applied only while a pointer point is set: the trigger collapses to an
// invisible zero-size anchor (positioned by inline style) at the pointer.
export const contextMenuTriggerAnchorClass = 'pointer-events-none size-0 opacity-0'
export const CONTEXT_MENU_ANCHOR: AnchorConfig = {
placement: 'bottom-start',
gap: 4,
padding: 8,
}
export type ContextMenuViewInputsConfig<Item extends string> = Readonly<{
items: ReadonlyArray<Item>
itemToConfig: ViewInputs<Item>['itemToConfig']
buttonContent: Html
/** Where the pointer requested the menu; the panel anchors here. */
maybePoint?: Option.Option<Point>
anchor?: AnchorConfig
isItemDisabled?: (item: Item, index: number) => boolean
itemToSearchText?: (item: Item, index: number) => string
isButtonDisabled?: boolean
isAnimated?: boolean
itemGroupKey?: (item: Item, index: number) => string
groupToHeading?: (groupKey: string) => GroupHeading | undefined
triggerClass?: string
itemsClass?: string
itemClass?: string
backdropClass?: string
wrapperClass?: string
separatorClass?: string
groupClass?: string
ariaLabel?: string
ariaLabelledBy?: string
/** Extra trigger attributes. Build them with `childAttributes` so handlers
* dispatch through your own update. */
triggerAttributes?: ReadonlyArray<ChildAttribute>
/** Extra panel attributes (used for submenu ArrowRight/ArrowLeft). */
contentAttributes?: ReadonlyArray<ChildAttribute>
}>
/** Build styled `Menu.ViewInputs` for a context menu with foldcn's classes. */
export const viewInputs = <Item extends string>(
config: ContextMenuViewInputsConfig<Item>,
): ViewInputs<Item> => {
// Upstream slide-in variants key on data-side; foldkit anchors expose
// placement ("bottom-start"), so derive the physical side here.
const anchor = config.anchor ?? CONTEXT_MENU_ANCHOR
const side = (anchor.placement ?? 'bottom').split('-')[0] || 'bottom'
return {
items: config.items,
anchor,
isItemDisabled: config.isItemDisabled,
itemToSearchText: config.itemToSearchText,
isButtonDisabled: config.isButtonDisabled,
buttonContent: config.buttonContent,
itemGroupKey: config.itemGroupKey,
groupToHeading: config.groupToHeading
? (groupKey) => {
const heading = config.groupToHeading!(groupKey)
if (!heading) return undefined
return {
content: heading.content,
className: cn(contextMenuHeadingClass, heading.className),
}
}
: undefined,
ariaLabel: config.ariaLabel,
ariaLabelledBy: config.ariaLabelledBy,
buttonClassName: cn(
contextMenuTriggerClass,
Option.isSome(config.maybePoint ?? Option.none()) ? contextMenuTriggerAnchorClass : undefined,
config.triggerClass,
),
buttonAttributes: [
...childAttributes([
inertHtml.DataAttribute('slot', 'context-menu-trigger'),
// Replaces the primitive's open-state Style (position: relative) so the
// zero-size trigger sits at the pointer while the menu is open.
...Option.match(config.maybePoint ?? Option.none(), {
onNone: () => [],
onSome: ({ x, y }) => [
inertHtml.Style({ position: 'fixed', left: `${x}px`, top: `${y}px`, zIndex: '1' }),
],
}),
]),
...(config.triggerAttributes ?? []),
],
itemsClassName: cn(
config.isAnimated !== false ? contextMenuItemsAnimatedClass : contextMenuItemsClass,
config.itemsClass,
),
itemsAttributes: [
...childAttributes([
inertHtml.DataAttribute('slot', 'context-menu-content'),
inertHtml.DataAttribute('side', side),
]),
...(config.contentAttributes ?? []),
],
itemToConfig: (item, context) => {
const { className, content } = config.itemToConfig(item, context)
return { className: cn(contextMenuItemClass, config.itemClass, className), content }
},
separatorClassName: cn(contextMenuSeparatorClass, config.separatorClass),
separatorAttributes: childAttributes([
inertHtml.DataAttribute('slot', 'context-menu-separator'),
]),
groupClassName: config.groupClass,
groupAttributes: childAttributes([inertHtml.DataAttribute('slot', 'context-menu-group')]),
backdropClassName: cn(contextMenuBackdropClass, config.backdropClass),
className: cn(contextMenuWrapperClass, config.wrapperClass),
attributes: childAttributes([inertHtml.DataAttribute('slot', 'context-menu')]),
}
}
// The trigger button sits inside the parent row (px-1.5) inside the parent
// panel (p-1), so the gap clears both and the offset lines the first row up with
// the trigger row (the sub panel's own p-1).
export const CONTEXT_MENU_SUB_ANCHOR: AnchorConfig = {
placement: 'right-start',
gap: 14,
offset: -8,
padding: 8,
}
/** `viewInputs` for a nested menu, rendered as the `content` of a parent row.
* The trigger is a full-width, pointer-transparent button: the parent row owns
* the click (its `SelectedItem` message opens the submenu — handle it yourself, do not forward it) and the panel
* anchors to the trigger. Pair it with `contextMenuSubTriggerClass` on the
* parent row's className and `contextMenuSubTriggerChevron` in the trigger's
* content. */
export const subViewInputs = <Item extends string>(
config: ContextMenuViewInputsConfig<Item>,
): ViewInputs<Item> => {
const anchor = config.anchor ?? CONTEXT_MENU_SUB_ANCHOR
const inputs = viewInputs({ wrapperClass: 'block w-full', ...config, anchor })
return {
...inputs,
buttonClassName: cn(
'pointer-events-none flex w-full items-center gap-1.5',
config.triggerClass,
),
buttonAttributes: [
...childAttributes([
inertHtml.DataAttribute('slot', 'context-menu-sub-trigger'),
inertHtml.Tabindex(-1),
]),
...(config.triggerAttributes ?? []),
],
itemsClassName: cn(contextMenuSubContentClass, config.itemsClass),
itemsAttributes: [
...childAttributes([
inertHtml.DataAttribute('slot', 'context-menu-sub-content'),
inertHtml.DataAttribute('side', anchor.placement?.split('-')[0] ?? 'right'),
]),
...(config.contentAttributes ?? []),
],
}
}