Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

feat(menu): add destructive variant to dropdown/context menu items (#4753)

freddymetae0fef445dd1c
diff --git a/.changeset/menu-item-destructive-variant.md b/.changeset/menu-item-destructive-variant.md
new file mode 100644
index 0000000000..b120d0f54d
--- /dev/null
+++ b/.changeset/menu-item-destructive-variant.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] DropdownMenuItem now accepts a `variant` prop (`'default' | 'destructive'`); `'destructive'` renders the label, description, and icon in the error color for dangerous actions like Delete. The data-driven `items` API accepts the same `variant` field, and because ContextMenu shares the menu-item data shape, context-menu items get it too. Defaults to `'default'`, so existing menus are unchanged.
+@freddymeta
diff --git a/apps/storybook/stories/ContextMenu.stories.tsx b/apps/storybook/stories/ContextMenu.stories.tsx
index 2a3b31edb3..902dda18b2 100644
--- a/apps/storybook/stories/ContextMenu.stories.tsx
+++ b/apps/storybook/stories/ContextMenu.stories.tsx
@@ -112,6 +112,30 @@ export const WithIcons: Story = {
),
};
+export const DestructiveItem: Story = {
+ name: 'Destructive item',
+ render: () => (
+ <ContextMenu
+ items={[
+ {label: 'Rename', onClick: () => console.log('Rename')},
+ {
+ label: 'Duplicate',
+ icon: ClipboardDocumentIcon,
+ onClick: () => console.log('Duplicate'),
+ },
+ {type: 'divider'},
+ {
+ label: 'Delete',
+ icon: TrashIcon,
+ variant: 'destructive',
+ onClick: () => console.log('Delete'),
+ },
+ ]}>
+ <div {...stylex.props(triggerStyles.area)}>Right-click for actions</div>
+ </ContextMenu>
+ ),
+};
+
export const WithSections: Story = {
render: () => (
<ContextMenu
diff --git a/apps/storybook/stories/DropdownMenu.stories.tsx b/apps/storybook/stories/DropdownMenu.stories.tsx
index 0c4a3e2181..7fb423abe8 100644
--- a/apps/storybook/stories/DropdownMenu.stories.tsx
+++ b/apps/storybook/stories/DropdownMenu.stories.tsx
@@ -180,6 +180,30 @@ export const WithDisabledItems: Story = {
),
};
+export const DestructiveItem: Story = {
+ name: 'Destructive item',
+ render: () => (
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={[
+ {label: 'Edit', onClick: () => console.log('Edit')},
+ {
+ label: 'Duplicate',
+ icon: 'copy',
+ onClick: () => console.log('Duplicate'),
+ },
+ {type: 'divider'},
+ {
+ label: 'Delete',
+ icon: 'close',
+ variant: 'destructive',
+ onClick: () => console.log('Delete'),
+ },
+ ]}
+ />
+ ),
+};
+
// Controlled mode
export const Controlled: Story = {
render: () => {
diff --git a/packages/core/src/ContextMenu/ContextMenu.doc.mjs b/packages/core/src/ContextMenu/ContextMenu.doc.mjs
index 7dde47e18a..26a5c87aa5 100644
--- a/packages/core/src/ContextMenu/ContextMenu.doc.mjs
+++ b/packages/core/src/ContextMenu/ContextMenu.doc.mjs
@@ -32,7 +32,7 @@ export const docs = {
{
name: 'items',
type: 'ContextMenuOption[]',
- description: 'Array of menu entries. Each entry is one of: an action item `{label, onClick?, icon?, isDisabled?}`, a divider `{type: "divider"}`, or a section `{type: "section", title?, items: [...action items]}`.',
+ description: 'Array of menu entries. Each entry is one of: an action item `{label, onClick?, icon?, isDisabled?, variant?}` (variant `"destructive"` renders it in the error color), a divider `{type: "divider"}`, or a section `{type: "section", title?, items: [...action items]}`.',
required: true,
},
{
diff --git a/packages/core/src/ContextMenu/ContextMenu.test.tsx b/packages/core/src/ContextMenu/ContextMenu.test.tsx
index 96d67575ee..1dd6ff259d 100644
--- a/packages/core/src/ContextMenu/ContextMenu.test.tsx
+++ b/packages/core/src/ContextMenu/ContextMenu.test.tsx
@@ -348,6 +348,27 @@ describe('ContextMenu items', () => {
});
});
+describe('ContextMenu destructive variant', () => {
+ it('forwards a destructive item variant to the shared menu item', () => {
+ render(
+ <ContextMenu
+ items={[
+ {label: 'Delete', variant: 'destructive', onClick: () => {}},
+ {label: 'Rename', onClick: () => {}},
+ ]}>
+ <div>Right-click me</div>
+ </ContextMenu>,
+ );
+
+ expect(
+ screen.getByRole('menuitem', {name: 'Delete', hidden: true}),
+ ).toHaveAttribute('data-variant', 'destructive');
+ expect(
+ screen.getByRole('menuitem', {name: 'Rename', hidden: true}),
+ ).not.toHaveAttribute('data-variant');
+ });
+});
+
describe('ContextMenu sections', () => {
it('renders section with title', () => {
render(
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs
index cd24567260..8f57632c38 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs
+++ b/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs
@@ -24,7 +24,7 @@ export const docs = {
theming: {
targets: [
{className: 'astryx-dropdown-menu'},
- {className: 'astryx-dropdown-menu-item', visualProps: ['size']},
+ {className: 'astryx-dropdown-menu-item', visualProps: ['size', 'variant']},
{
className: 'astryx-dropdown-menu-radio',
visualProps: ['size'],
@@ -59,7 +59,7 @@ export const docs = {
{
name: 'items',
type: 'DropdownMenuOption[]',
- description: 'Array of menu entries. Each entry is one of: an action item `{label, onClick?, icon?, isDisabled?}`, a divider `{type: "divider"}`, or a section `{type: "section", title?, items: [...action items]}`.',
+ description: 'Array of menu entries. Each entry is one of: an action item `{label, onClick?, icon?, isDisabled?, variant?}` (variant `"destructive"` renders it in the error color), a divider `{type: "divider"}`, or a section `{type: "section", title?, items: [...action items]}`.',
required: true,
},
{
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 1b7471d7c5..b2b9091f1f 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -537,6 +537,78 @@ describe('DropdownMenu theming slots', () => {
});
});
+describe('DropdownMenuItem destructive variant', () => {
+ it('marks a compound-mode item destructive via data-variant', () => {
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem
+ label="Delete"
+ variant="destructive"
+ onClick={() => {}}
+ />
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ </DropdownMenu>,
+ );
+
+ const del = screen.getByRole('menuitem', {name: 'Delete', hidden: true});
+ const edit = screen.getByRole('menuitem', {name: 'Edit', hidden: true});
+ expect(del).toHaveAttribute('data-variant', 'destructive');
+ // Default items carry no variant attribute, so existing usage is unchanged.
+ expect(edit).not.toHaveAttribute('data-variant');
+ });
+
+ it('forwards variant from the data-driven items API', () => {
+ render(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={[
+ {label: 'Delete', variant: 'destructive', onClick: () => {}},
+ {label: 'Edit', onClick: () => {}},
+ ]}
+ />,
+ );
+
+ expect(
+ screen.getByRole('menuitem', {name: 'Delete', hidden: true}),
+ ).toHaveAttribute('data-variant', 'destructive');
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveAttribute('data-variant');
+ });
+
+ it('forwards variant to items nested inside a section', () => {
+ render(
+ <DropdownMenu
+ button={{label: 'Actions'}}
+ items={[
+ {
+ type: 'section',
+ title: 'Danger zone',
+ items: [
+ {label: 'Delete', variant: 'destructive', onClick: () => {}},
+ ],
+ },
+ ]}
+ />,
+ );
+
+ expect(
+ screen.getByRole('menuitem', {name: 'Delete', hidden: true}),
+ ).toHaveAttribute('data-variant', 'destructive');
+ });
+
+ it('defaults to no variant attribute', () => {
+ render(
+ <DropdownMenu button={{label: 'Actions'}}>
+ <DropdownMenuItem label="Edit" onClick={() => {}} />
+ </DropdownMenu>,
+ );
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).not.toHaveAttribute('data-variant');
+ });
+});
+
describe('DropdownMenu button customization', () => {
it('renders with different button variants', () => {
const {rerender} = render(
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index 6aeab84e6c..ced3259787 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -104,6 +104,11 @@ export interface DropdownMenuItemData {
onClick?: () => void;
isDisabled?: boolean;
icon?: ReactNode | IconType;
+ /**
+ * Visual variant for a leaf item. `'destructive'` renders it in the error
+ * color for dangerous actions (e.g. Delete). @default 'default'
+ */
+ variant?: 'default' | 'destructive';
/**
* Nested submenu entries. When present, this row becomes a submenu (a
* flyout revealing `items`) instead of a leaf action — no separate item
diff --git a/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs
index 9050d46df6..acaa5a9379 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs
+++ b/packages/core/src/DropdownMenu/DropdownMenuItem.doc.mjs
@@ -7,7 +7,8 @@ export const docs = {
subComponentOf: 'DropdownMenu',
displayName: 'Dropdown Menu Item',
isHiddenFromOverview: true,
- description: 'Helper component for custom item rendering with consistent styling.',
+ description:
+ 'Helper component for custom item rendering with consistent styling.',
playground: {
// Standalone DropdownMenuItem has no required props, so the properties-tab
// preview renders an empty row without seeded content. Seed a label and
@@ -18,7 +19,8 @@ export const docs = {
{
name: 'icon',
type: 'IconType',
- description: 'Icon to display before the label. See `astryx docs icons` for valid semantic names.',
+ description:
+ 'Icon to display before the label. See `astryx docs icons` for valid semantic names.',
},
{
name: 'label',
@@ -33,12 +35,21 @@ export const docs = {
{
name: 'endContent',
type: 'ReactNode',
- description: 'Additional content rendered after the label and description.',
+ description:
+ 'Additional content rendered after the label and description.',
+ },
+ {
+ name: 'variant',
+ type: "'default' | 'destructive'",
+ description:
+ "Visual variant. 'destructive' renders the label, description, and icon in the error color for dangerous actions (e.g. Delete).",
+ default: "'default'",
},
{
name: 'xstyle',
type: 'StyleXStyles',
- description: 'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value: not an inline style object like style={{}}.',
+ description:
+ 'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value: not an inline style object like style={{}}.',
},
],
};
@@ -69,6 +80,13 @@ export const docsZh = {
type: 'ReactNode',
description: '在标签和描述之后渲染的附加内容。',
},
+ {
+ name: 'variant',
+ type: "'default' | 'destructive'",
+ description:
+ "视觉变体。'destructive' 会以错误色渲染标签、描述和图标,用于危险操作(如删除)。",
+ default: "'default'",
+ },
{
name: 'xstyle',
type: 'StyleXStyles',
@@ -87,6 +105,8 @@ export const docsDense = {
label: 'primary label text',
description: 'secondary text below label',
endContent: 'additional content after label+description',
+ variant:
+ "'destructive' renders the item in the error color for dangerous actions",
xstyle: 'StyleX styles for root container',
},
};
diff --git a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx b/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
index 1864a67ec7..27eb4fc961 100644
--- a/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenuItem.tsx
@@ -62,6 +62,15 @@ const menuItemStyles = stylex.create({
opacity: 0.5,
cursor: 'not-allowed',
},
+ destructive: {
+ // Only recolor the text/icon; the hover / focus background stays the shared
+ // neutral overlay from `root` so the hover state matches every other menu
+ // item. The root color covers the label/description via the Item custom
+ // properties (and any bare text). Semantic error tokens keep it theme-aware.
+ color: colorVars['--color-error'],
+ '--_item-label-color': colorVars['--color-error'],
+ '--_item-description-color': colorVars['--color-error'],
+ },
});
const itemSizeStyles = stylex.create({
@@ -91,6 +100,11 @@ export interface DropdownMenuItemProps extends Pick<
isDisabled?: boolean;
/** Additional content to render after the label/description. */
endContent?: ReactNode;
+ /**
+ * Visual variant. `'destructive'` renders the label, description, and icon in
+ * the error color for dangerous actions (e.g. Delete). @default 'default'
+ */
+ variant?: 'default' | 'destructive';
}
/**
@@ -103,7 +117,7 @@ export interface DropdownMenuItemProps extends Pick<
* ```
* <DropdownMenu button={{ label: 'Actions' }}>
* <DropdownMenuItem icon={PencilIcon} label="Edit" onClick={handleEdit} />
- * <DropdownMenuItem label="Delete" endContent={<Badge label="⌘D" />} onClick={handleDelete} />
+ * <DropdownMenuItem label="Delete" variant="destructive" onClick={handleDelete} />
* </DropdownMenu>
* ```
*/
@@ -114,6 +128,7 @@ export function DropdownMenuItem({
onClick,
isDisabled = false,
endContent,
+ variant = 'default',
xstyle,
className,
style,
@@ -134,6 +149,8 @@ export function DropdownMenuItem({
[isDisabled],
);
+ const isDestructive = variant === 'destructive';
+
return (
<Item
role="menuitem"
@@ -141,7 +158,10 @@ export function DropdownMenuItem({
onPointerMove={handlePointerMove}
startContent={
icon
- ? renderIconSlot(icon, {size: 'sm', color: 'secondary'})
+ ? renderIconSlot(icon, {
+ size: 'sm',
+ color: isDestructive ? 'error' : 'secondary',
+ })
: undefined
}
label={label}
@@ -152,13 +172,20 @@ export function DropdownMenuItem({
xstyle={[
menuItemStyles.root,
itemSizeStyles[menuSize],
+ isDestructive && menuItemStyles.destructive,
isDisabled && menuItemStyles.disabled,
xstyle,
]}
- {...mergeProps(themeProps('dropdown-menu-item', {size: menuSize}), {
- className,
- style,
- })}
+ {...mergeProps(
+ themeProps('dropdown-menu-item', {
+ size: menuSize,
+ variant: isDestructive ? 'destructive' : null,
+ }),
+ {
+ className,
+ style,
+ },
+ )}
/>
);
}
diff --git a/packages/core/src/DropdownMenu/renderDropdownItems.tsx b/packages/core/src/DropdownMenu/renderDropdownItems.tsx
index 254387660c..0edbe1a4bd 100644
--- a/packages/core/src/DropdownMenu/renderDropdownItems.tsx
+++ b/packages/core/src/DropdownMenu/renderDropdownItems.tsx
@@ -88,6 +88,7 @@ export function renderDropdownItems(items: DropdownMenuOption[]): ReactNode {
label={item.label}
onClick={item.onClick}
isDisabled={item.isDisabled}
+ variant={item.variant}
/>
))}
</div>,
@@ -116,6 +117,7 @@ export function renderDropdownItems(items: DropdownMenuOption[]): ReactNode {
label={option.label}
onClick={option.onClick}
isDisabled={option.isDisabled}
+ variant={option.variant}
/>,
);
}
diff --git a/packages/core/src/Item/Item.tsx b/packages/core/src/Item/Item.tsx
index f49d717c4d..0eefb7de96 100644
--- a/packages/core/src/Item/Item.tsx
+++ b/packages/core/src/Item/Item.tsx
@@ -274,7 +274,9 @@ const styles = stylex.create({
textAlign: 'start',
},
label: {
- color: colorVars['--color-text-primary'],
+ // Falls back to the primary text token; a parent (e.g. a destructive menu
+ // item) can recolor the label by setting --_item-label-color.
+ color: `var(--_item-label-color, ${colorVars['--color-text-primary']})`,
fontSize: typeScaleVars['--text-body-size'],
lineHeight: typeScaleVars['--text-body-leading'],
},
@@ -289,7 +291,8 @@ const styles = stylex.create({
WebkitBoxOrient: 'vertical' as const,
},
description: {
- color: colorVars['--color-text-secondary'],
+ // Companion to --_item-label-color for the secondary line.
+ color: `var(--_item-description-color, ${colorVars['--color-text-secondary']})`,
fontSize: typeScaleVars['--text-supporting-size'],
lineHeight: typeScaleVars['--text-supporting-leading'],
},