Files

145 lines
4.9 KiB
TypeScript
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
import React, { use } from 'react';
import { Menu } from '@base-ui/react/menu';
import { ContextMenu } from '@base-ui/react/context-menu';
import type { LucideIcon } from 'lucide-react';
import Icon from '@/components/elements/Icon';
2026-10-12 17:39:58 -04:00
import { PanelLink, type PanelDestination } from '@/sdk/navigation';
2026-10-12 09:04:03 -04:00
import { useSkin } from '@/themes/skins';
2026-10-05 18:10:18 -04:00
type MenuItemComponent = React.ComponentType<React.ComponentProps<typeof Menu.Item>>;
const MenuItemContext = React.createContext<MenuItemComponent>(Menu.Item as MenuItemComponent);
const ContextMenuItem = (props: React.ComponentProps<typeof Menu.Item>) => <ContextMenu.Item {...props} />;
2026-10-12 09:04:03 -04:00
export interface DropdownMenuProps {
2026-10-05 18:10:18 -04:00
/** Rendered inside the trigger `<button>`. */
triggerContent: React.ReactNode;
triggerClassName?: string;
openOnHover?: boolean;
2026-10-05 18:10:18 -04:00
children: React.ReactNode;
className?: string;
}
function DropdownMenu({ triggerContent, triggerClassName, openOnHover, children, className }: DropdownMenuProps) {
2026-10-12 09:04:03 -04:00
const ui = useSkin('DropdownMenu', { context: false });
2026-10-05 18:10:18 -04:00
return (
<Menu.Root>
2026-10-12 09:04:03 -04:00
<Menu.Trigger
{...ui('trigger', triggerClassName)}
openOnHover={openOnHover}
closeDelay={openOnHover ? 150 : 0}
>
{triggerContent}
</Menu.Trigger>
2026-10-12 17:58:43 -04:00
<Menu.Portal>
2026-10-12 09:04:03 -04:00
<Menu.Positioner {...ui('positioner')} sideOffset={4} align='end'>
<Menu.Popup {...ui('root', className)}>
2026-10-05 18:10:18 -04:00
<MenuItemContext.Provider value={Menu.Item as MenuItemComponent}>
{children}
</MenuItemContext.Provider>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
);
}
2026-10-12 09:04:03 -04:00
export interface ContextDropdownMenuProps {
2026-10-05 18:10:18 -04:00
className?: string;
menuClassName?: string;
children: React.ReactNode;
items: React.ReactNode;
}
/** Opens `items` at the cursor on right-click or long-press over `children`. */
export function ContextDropdownMenu({ className, menuClassName, children, items }: ContextDropdownMenuProps) {
2026-10-12 09:04:03 -04:00
const ui = useSkin('DropdownMenu', { context: true });
2026-10-05 18:10:18 -04:00
return (
<ContextMenu.Root>
2026-10-12 09:04:03 -04:00
<ContextMenu.Trigger {...ui('trigger', className)}>{children}</ContextMenu.Trigger>
2026-10-12 17:58:43 -04:00
<ContextMenu.Portal>
2026-10-12 09:04:03 -04:00
<ContextMenu.Positioner {...ui('positioner')}>
<ContextMenu.Popup {...ui('root', menuClassName)}>
2026-10-05 18:10:18 -04:00
<MenuItemContext.Provider value={ContextMenuItem}>{items}</MenuItemContext.Provider>
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
);
}
2026-10-12 09:04:03 -04:00
export interface DropdownMenuItemProps extends Omit<React.ComponentProps<typeof Menu.Item>, 'className'> {
className?: string;
2026-10-05 18:10:18 -04:00
icon?: LucideIcon;
danger?: boolean;
description?: React.ReactNode;
2026-10-12 17:39:58 -04:00
/** Makes the item a link to a panel page; following it closes the menu. */
destination?: PanelDestination;
2026-10-12 20:14:18 -04:00
/** Makes the item a choice that is on or off, such as the scheme in use: a `menuitemcheckbox`. */
checked?: boolean;
2026-10-05 18:10:18 -04:00
}
/** An item for either DropdownMenu or ContextDropdownMenu. */
2026-10-12 17:39:58 -04:00
export function DropdownMenuItem({
icon,
danger,
description,
destination,
2026-10-12 20:14:18 -04:00
checked,
2026-10-12 17:39:58 -04:00
className,
children,
...props
}: DropdownMenuItemProps) {
2026-10-05 18:10:18 -04:00
const Item = use(MenuItemContext);
2026-10-12 20:14:18 -04:00
const ui = useSkin('DropdownMenuItem', { danger: !!danger, described: !!description, checked: !!checked });
2026-10-12 17:39:58 -04:00
const content = (
<>
2026-10-05 18:10:18 -04:00
{icon && (
2026-10-12 09:04:03 -04:00
<span {...ui('icon')}>
<Icon icon={icon} {...ui('glyph')} />
2026-10-05 18:10:18 -04:00
</span>
)}
2026-10-12 09:04:03 -04:00
<span {...ui('text')}>
<span {...ui('label')}>{children}</span>
{description && <span {...ui('description')}>{description}</span>}
2026-10-05 18:10:18 -04:00
</span>
2026-10-12 17:39:58 -04:00
</>
);
if (destination) {
return (
<Menu.LinkItem
{...ui('root', className)}
id={props.id}
label={props.label}
closeOnClick
render={<PanelLink destination={destination} />}
>
{content}
</Menu.LinkItem>
);
}
2026-10-12 20:14:18 -04:00
if (checked !== undefined) {
return (
<Menu.CheckboxItem closeOnClick {...ui('root', className)} {...props} checked={checked}>
{content}
</Menu.CheckboxItem>
);
}
2026-10-12 17:39:58 -04:00
return (
<Item {...ui('root', className)} {...props}>
{content}
2026-10-05 18:10:18 -04:00
</Item>
);
}
// Assigned as a static property; Object.assign would hide the component from Fast Refresh.
const DropdownMenuWithItems = DropdownMenu as typeof DropdownMenu & { Item: typeof DropdownMenuItem };
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
DropdownMenuWithItems.Item = DropdownMenuItem;
export default DropdownMenuWithItems;