Theming

You can style the menu by loading the default CSS and overriding variables, or by passing a theme config (class and/or tokens). Item variants add optional CSS classes for danger, info, success, warning, and muted.

Default styles

Load the default stylesheet so the menu has background, padding, and hover states. Without it, the menu is unstyled but still functional.

@import "@enegalan/contextmenu.js/dist/style.css";

Or in JS:

import "@enegalan/contextmenu.js/dist/style.css";

Override with CSS variables

Target the menu root (e.g. .cm-menu or a class you set via theme.class) and set variables. This applies to all menus using that class.

@import "@enegalan/contextmenu.js/dist/style.css";
 
.cm-menu {
  --cm-bg: #1e1e1e;
  --cm-fg: #eee;
  --cm-radius: 8px;
}

Theme config (class + tokens)

Pass theme when creating the menu. class adds a CSS class to the menu root so you can scope overrides. tokens set CSS variables (e.g. bg--cm-bg, fg--cm-fg).

createContextMenu({
  menu: [...],
  theme: {
    class: "my-menu",
    tokens: { bg: "#1e1e1e", fg: "#eee" },
  },
});

CSS variables reference

Set these on the menu root (e.g. .cm-menu or your theme class). All variables are listed with their default values from the default stylesheet.

VariableDescriptionDefault
--cm-bgMenu background color#fff
--cm-fgForeground (text) color#1a1a1a
--cm-radiusMenu border radius12px
--cm-shadowBox shadow of the menu0 4px 12px rgba(0, 0, 0, 0.15)
--cm-borderMenu border1px solid rgba(0, 0, 0, 0.08)
--cm-z-indexZ-index of the menu and overlay9999
--cm-menu-paddingInner padding of the menu (vertical horizontal)4px 0
--cm-menu-min-widthMinimum menu width8rem
--cm-menu-max-heightMaximum height before the menu scrolls. Use none for no limit.none
VariableDescriptionDefault
--cm-item-padding-xHorizontal padding of each item12px
--cm-item-padding-yVertical padding of each item8px
--cm-item-radiusBorder radius of each item0
--cm-item-hover-bgBackground of menu items on hover and focusrgba(0, 0, 0, 0.06)
--cm-font-sizeBase font size of the menu14px
--cm-disabled-opacityOpacity applied to disabled items0.5
--cm-leading-widthWidth of the leading slot (icon/check area)20px

Shortcuts

VariableDescriptionDefault
--cm-shortcut-font-sizeFont size of keyboard shortcut text0.9em
--cm-shortcut-opacityOpacity of shortcut text0.7
--cm-shortcut-icon-sizeSize of shortcut icons1em

Separators

VariableDescriptionDefault
--cm-separator-bgBackground (line) color of separatorsrgba(0, 0, 0, 0.08)
--cm-separator-marginMargin around separator lines (vertical horizontal)4px 8px
--cm-separator-heightHeight of separator lines1px

Labels

VariableDescriptionDefault
--cm-label-font-sizeFont size of section labels0.75em
--cm-label-opacityOpacity of section labels0.7

Badges

VariableDescriptionDefault
--cm-badge-font-sizeFont size of item badges0.75em
--cm-badge-opacityOpacity of badges0.8
--cm-badge-paddingPadding inside badges1.5px 8px
--cm-badge-border-radiusBorder radius of badges10px
--cm-badge-box-shadowBox shadow of badgesnone
--cm-badge-backgroundBackground color of badgesrgba(0, 0, 0, 0.1)
--cm-badge-border-widthBadge border width0
--cm-badge-border-styleBadge border stylesolid
--cm-badge-border-colorBadge border colortransparent

Check and radio

VariableDescriptionDefault
--cm-check-sizeSize of checkbox indicators14px
--cm-radio-sizeSize of radio indicators14px
VariableDescriptionDefault
--cm-submenu-arrow-sizeSize of the arrow indicating a submenu (triangle)5px

Spinner

VariableDescriptionDefault
--cm-spinner-sizeSize of the loading spinner14px
--cm-spinner-durationDuration of one full rotation of the loading spinner0.6s

Animations

VariableDescriptionDefault
--cm-enter-durationDuration of the enter animation120ms
--cm-enter-easingEasing of the enter animationease-out
--cm-leave-durationDuration of the leave animation80ms
--cm-leave-easingEasing of the leave animationease-in

For more details and examples, see Animations.

Item variants

These variables control the look of items that use variant (e.g. variant: "danger"). Each variant has a text color and a hover/focus background.

VariableDescriptionDefault
--cm-danger-colorText color for variant danger#c53030
--cm-danger-hover-bgHover/focus background for variant dangerrgba(197, 48, 48, 0.1)
--cm-info-colorText color for variant info#2b6cb0
--cm-info-hover-bgHover/focus background for variant inforgba(43, 108, 176, 0.1)
--cm-success-colorText color for variant success#276749
--cm-success-hover-bgHover/focus background for variant successrgba(39, 103, 73, 0.1)
--cm-warning-colorText color for variant warning#c05621
--cm-warning-hover-bgHover/focus background for variant warningrgba(192, 86, 33, 0.1)
--cm-muted-colorText color for variant muted#718096
--cm-muted-hover-bgHover/focus background for variant mutedrgba(113, 128, 150, 0.1)

Item variants

Action items can set variant to get an extra CSS class and often a different color. Classes added: .cm-item--danger, .cm-item--info, .cm-item--success, .cm-item--warning, .cm-item--muted. You can override these in your CSS.

{ label: "Delete", variant: "danger", onClick: () => remove() }

Runtime theme change

Use menu.setOptions({ theme }) to switch theme after creation. Pass theme: undefined to clear.

menu.setOptions({ theme: { class: "my-dark", tokens: { bg: "#1a1a1a", fg: "#eee" } } });
menu.setOptions({ theme: undefined }); // clear theme

Dark theme example

Copy-paste to get a dark menu: load the default CSS, then add a class and set tokens: theme: { class: "cm-dark", tokens: { bg: "#1a1a1a", fg: "#e0e0e0" } }. In your CSS, target .cm-dark and override --cm-item-hover-bg / --cm-separator-bg if you want different hover and separator colors.