Components
Menubar
Menu triggers in a row: a Black strip in a hairline with the brand corner, triggers in DM Sans Medium, each opening a panel of rows in the card's #262626 fill. Arrow keys move along the bar and through the rows.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add menubar<script type="module" src="/spyglass-ds/elements/menubar.js"></script>Usage
A tool's commands grouped under a few words, as in an editor: a board's actions, its view, its sharing. Three to five menus.
Never
- Site or app navigation: use the Sidebar or Navigation Menu
- One menu alone: use Dropdown Menu
- A spectrum color for the open trigger: it takes the #262626 fill
Adds with it: popover packages lucide-react
Props
The web component takes the same as attributes or properties (<spyglass-menubar>); title is heading there.
<Menubar>
| Prop | Type | Default |
|---|---|---|
label | string The bar's name for assistive tech. | |
children | ReactNode MenubarMenu elements. |
<MenubarMenu>
| Prop | Type | Default |
|---|---|---|
label | string The trigger's label. | |
width | string The panel's width (a CSS length). | '224px' |
children | ReactNode |
<MenubarItem>
| Prop | Type | Default |
|---|---|---|
shortcut | string A key or keys, shown muted at the end. | |
danger | boolean A destructive row (red). | |
disabled | boolean | |
keepOpen | boolean Keeps the menu open after it is chosen. | |
checked | boolean Checked, for a radio or checkbox row. | |
role | 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' | 'menuitem' |
onSelect | () => void | |
className | string | |
children | ReactNode |
<MenubarLabel>
| Prop | Type | Default |
|---|---|---|
children | ReactNode |
<MenubarSeparator>
| Prop | Type | Default |
|---|
Examples
The same example in React and as web components; both render identically.
'use client'
import { useState } from 'react'
import { Copy, Download, FolderPlus, Link, Trash2, UserPlus } from 'lucide-react'
import { Menubar } from '@/react/components/ui/Menubar'
import { MenubarItem } from '@/react/components/ui/MenubarItem'
import { MenubarLabel } from '@/react/components/ui/MenubarLabel'
import { MenubarMenu } from '@/react/components/ui/MenubarMenu'
import { MenubarSeparator } from '@/react/components/ui/MenubarSeparator'
// A board's tools as a menubar: the board itself, how its creatives are sorted, and who sees it. Open one, then point
// at the next or press Right.
const sorts = [{ id: 'newest', label: 'Newest first' }, { id: 'longest', label: 'Longest running' }, { id: 'saved', label: 'Most saved' }] as const
export default function Board() {
const [sort, setSort] = useState<'newest' | 'longest' | 'saved'>('newest')
return (
<div className="min-h-72">
<Menubar label="Board">
<MenubarMenu label="Board">
<MenubarItem shortcut="⌘N"><FolderPlus size={16} strokeWidth={1.5} className="shrink-0 text-fg-muted" aria-hidden="true" />New board</MenubarItem>
<MenubarItem><Copy size={16} strokeWidth={1.5} className="shrink-0 text-fg-muted" aria-hidden="true" />Duplicate</MenubarItem>
<MenubarItem><Download size={16} strokeWidth={1.5} className="shrink-0 text-fg-muted" aria-hidden="true" />Export as PDF</MenubarItem>
<MenubarSeparator />
<MenubarItem danger><Trash2 size={16} strokeWidth={1.5} className="shrink-0" aria-hidden="true" />Delete board</MenubarItem>
</MenubarMenu>
<MenubarMenu label="View">
<MenubarLabel>Sort creatives</MenubarLabel>
{sorts.map((s) => (
<MenubarItem key={s.id} role="menuitemradio" checked={sort === s.id} onSelect={() => setSort(s.id)}>{s.label}</MenubarItem>
))}
</MenubarMenu>
<MenubarMenu label="Share">
<MenubarItem><Link size={16} strokeWidth={1.5} className="shrink-0 text-fg-muted" aria-hidden="true" />Copy link</MenubarItem>
<MenubarItem><UserPlus size={16} strokeWidth={1.5} className="shrink-0 text-fg-muted" aria-hidden="true" />Invite a teammate</MenubarItem>
</MenubarMenu>
</Menubar>
</div>
)
}<!-- A board's tools as a menubar: the board itself, how its creatives are sorted, and who sees it. Open one, then point
at the next or press Right. -->
<div class="min-h-72">
<spyglass-menubar label="Board">
<spyglass-menubar-menu label="Board">
<spyglass-menubar-item shortcut="⌘N"><i data-lucide="folder-plus" data-size="16" data-stroke-width="1.5" class="shrink-0 text-fg-muted" aria-hidden="true"></i>New board</spyglass-menubar-item>
<spyglass-menubar-item><i data-lucide="copy" data-size="16" data-stroke-width="1.5" class="shrink-0 text-fg-muted" aria-hidden="true"></i>Duplicate</spyglass-menubar-item>
<spyglass-menubar-item><i data-lucide="download" data-size="16" data-stroke-width="1.5" class="shrink-0 text-fg-muted" aria-hidden="true"></i>Export as PDF</spyglass-menubar-item>
<spyglass-menubar-separator></spyglass-menubar-separator>
<spyglass-menubar-item danger><i data-lucide="trash-2" data-size="16" data-stroke-width="1.5" class="shrink-0" aria-hidden="true"></i>Delete board</spyglass-menubar-item>
</spyglass-menubar-menu>
<spyglass-menubar-menu label="View">
<spyglass-menubar-label>Sort creatives</spyglass-menubar-label>
<spyglass-menubar-item item-role="menuitemradio" data-sort="newest" checked>Newest first</spyglass-menubar-item>
<spyglass-menubar-item item-role="menuitemradio" data-sort="longest">Longest running</spyglass-menubar-item>
<spyglass-menubar-item item-role="menuitemradio" data-sort="saved">Most saved</spyglass-menubar-item>
</spyglass-menubar-menu>
<spyglass-menubar-menu label="Share">
<spyglass-menubar-item><i data-lucide="link" data-size="16" data-stroke-width="1.5" class="shrink-0 text-fg-muted" aria-hidden="true"></i>Copy link</spyglass-menubar-item>
<spyglass-menubar-item><i data-lucide="user-plus" data-size="16" data-stroke-width="1.5" class="shrink-0 text-fg-muted" aria-hidden="true"></i>Invite a teammate</spyglass-menubar-item>
</spyglass-menubar-menu>
</spyglass-menubar>
</div>
<script type="module">
const rows = document.querySelectorAll('spyglass-menubar-item[data-sort]')
for (const row of rows) row.addEventListener('select', () => { for (const r of rows) r.checked = r === row })
</script>Source
'use client'
import { createContext, useRef, useState, type Dispatch, type ReactNode, type SetStateAction } from 'react'
// Menu triggers in a row (shadcn's menubar): a Black strip in a hairline with the brand corner, 32px triggers in DM Sans
// Medium, each opening a panel of rows in the card's #262626 fill (MenubarMenu with MenubarItem, MenubarLabel and
// MenubarSeparator). One trigger is in the tab order; Left and Right move along the row, Down, Enter or Space open, and
// with a menu open Left and Right open the next one. While one is open, pointing at another trigger switches to it at
// once. Menus opened from the keyboard show without motion.
export interface MenubarEntry { key: string, el: () => HTMLElement | null, open: () => Promise<void> }
export interface MenubarContextValue {
active: string | null
setActive: Dispatch<SetStateAction<string | null>>
focused: string | null
setFocused: Dispatch<SetStateAction<string | null>>
entries: MenubarEntry[]
move: (from: string, step: number, open: boolean) => void
}
export const MenubarContext = createContext<MenubarContextValue | null>(null)
/** Closes the menu a row is in (refocusing its trigger). */
export const MenubarClose = createContext<() => void>(() => {})
export interface MenubarProps {
/** The bar's name for assistive tech. */
label: string
/** MenubarMenu elements. */
children?: ReactNode
}
export function Menubar({ label, children }: MenubarProps) {
const [active, setActive] = useState<string | null>(null)
const [focused, setFocused] = useState<string | null>(null)
const entries = useRef<MenubarEntry[]>([]).current
function move(from: string, step: number, open: boolean) {
const at = entries.findIndex((e) => e.key === from)
const next = entries[(at + step + entries.length) % entries.length]
if (!next) return
setFocused(next.key)
next.el()?.focus()
if (open) void next.open()
}
return (
<MenubarContext.Provider value={{ active, setActive, focused, setFocused, entries, move }}>
<div data-ds="menubar" role="menubar" aria-label={label} className="inline-flex items-center gap-1 rounded-control border border-edge bg-surface p-1">
{children}
</div>
</MenubarContext.Provider>
)
}
export default Menubar'use client'
import { useContext, useEffect, useId, useRef, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react'
import { MenubarClose, MenubarContext } from '@/react/components/ui/Menubar'
import { PopoverPanel } from '@/react/components/ui/PopoverPanel'
import { onMenuRows, usePopover } from '@/react/hooks/usePopover'
// One menu of a Menubar: its trigger and a panel of rows under it (MenubarItem, MenubarLabel, MenubarSeparator). The
// open trigger takes the #262626 fill; the others fill on hover.
export interface MenubarMenuProps {
/** The trigger's label. */
label: string
/** The panel's width (a CSS length). */
width?: string
children?: ReactNode
}
export function MenubarMenu({ label, width = '224px', children }: MenubarMenuProps) {
const bar = useContext(MenubarContext)!
const key = useId()
const pop = usePopover({ side: 'bottom', align: 'start', gap: 8 })
async function open(focus: 'first' | 'last' | 'none' = 'first', keyboard = true) {
await pop.show({ keyboard })
if (focus === 'none') return pop.panel.current?.focus({ preventScroll: true })
const rows = [...(pop.panel.current?.querySelectorAll<HTMLElement>('[role^="menuitem"]:not([disabled])') ?? [])]
;(focus === 'last' ? rows.at(-1) : rows[0])?.focus()
}
const openRef = useRef(open)
openRef.current = open
useEffect(() => {
const entry = { key, el: () => pop.anchor.current, open: () => openRef.current('first') }
bar.entries.push(entry)
bar.setFocused((f) => f ?? key)
return () => { bar.entries.splice(bar.entries.indexOf(entry), 1) }
}, []) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (pop.open) bar.setActive(key)
else bar.setActive((a) => (a === key ? null : a))
}, [pop.open]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (bar.active !== key && pop.open) pop.hide(false, true)
}, [bar.active]) // eslint-disable-line react-hooks/exhaustive-deps
function onClick(event: MouseEvent) {
if (pop.open) return pop.hide(event.detail === 0, event.detail === 0)
void open(event.detail === 0 ? 'first' : 'none', event.detail === 0)
}
function onEnter() {
if (bar.active && bar.active !== key) void open('none', true)
}
function onTriggerKey(event: KeyboardEvent) {
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault()
bar.move(key, event.key === 'ArrowRight' ? 1 : -1, false)
}
else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault()
void open(event.key === 'ArrowDown' ? 'first' : 'last')
}
}
function onPanelKey(event: KeyboardEvent) {
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault()
pop.hide(false, true)
bar.move(key, event.key === 'ArrowRight' ? 1 : -1, true)
return
}
onMenuRows(event, pop.panel.current, pop.hide)
}
return (
<MenubarClose.Provider value={() => pop.hide(true)}>
<button
data-ds="menubar-menu"
ref={(el) => { pop.anchor.current = el }}
type="button"
role="menuitem"
aria-haspopup="menu"
aria-expanded={pop.open}
tabIndex={bar.focused === key ? 0 : -1}
className="inline-flex h-control-sm cursor-pointer items-center rounded-ds-sm px-3 type-ui-strong text-fg-soft -outline-offset-2 transition-[background-color,color] duration-160 ease-standard hover:not-aria-expanded:bg-action-ghost-hover hover:text-fg aria-expanded:bg-action-secondary aria-expanded:text-fg max-md:h-control-touch"
onClick={onClick}
onPointerEnter={onEnter}
onFocus={() => bar.setFocused(key)}
onKeyDown={onTriggerKey}
>
{label}
</button>
<PopoverPanel data-ds="menubar-menu" pop={pop} role="menu" aria-label={label} tabIndex={-1} className="flex flex-col p-1" style={{ width }} onKeyDown={onPanelKey}>
{children}
</PopoverPanel>
</MenubarClose.Provider>
)
}
export default MenubarMenu'use client'
import { useContext, type PointerEvent, type ReactNode } from 'react'
import { Check } from 'lucide-react'
import { MenubarClose } from '@/react/components/ui/Menubar'
import { cn } from '@/react/lib/cn'
// One row of a MenubarMenu: 32px (44px on phones), DM Sans soft gray, the brand's small corner; the row under the
// pointer or focus takes the #262626 fill and White type. Put a 16px Lucide icon first, and a `shortcut` shows muted at
// the end. `checked` makes it a radio or checkbox row with a check (`role`). `danger` is a destructive row's red.
// Choosing it calls `onSelect` and closes the menu, unless `keepOpen`.
export interface MenubarItemProps {
/** A key or keys, shown muted at the end. */
shortcut?: string
/** A destructive row (red). */
danger?: boolean
disabled?: boolean
/** Keeps the menu open after it is chosen. */
keepOpen?: boolean
/** Checked, for a radio or checkbox row. */
checked?: boolean
role?: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox'
onSelect?: () => void
className?: string
children?: ReactNode
}
export function MenubarItem({ shortcut, danger, disabled, keepOpen, checked, role = 'menuitem', onSelect, className, children }: MenubarItemProps) {
const close = useContext(MenubarClose)
function choose() {
onSelect?.()
if (!keepOpen) close()
}
// The row under the pointer takes focus, so the pointer and the keys share one highlight.
function onPointerMove(event: PointerEvent<HTMLButtonElement>) {
if (!disabled && document.activeElement !== event.currentTarget) event.currentTarget.focus({ preventScroll: true })
}
return (
<button
data-ds="menubar-item"
type="button"
role={role}
aria-checked={role === 'menuitem' ? undefined : !!checked}
disabled={disabled}
data-danger={danger || undefined}
className={cn('flex h-control-sm w-full shrink-0 cursor-pointer items-center gap-3 rounded-ds-sm px-2 text-left type-ui text-fg-soft outline-none transition-[background-color,color] duration-160 ease-standard focus:bg-action-secondary focus:text-fg disabled:cursor-not-allowed disabled:text-fg-faint data-danger:text-fg-danger max-md:h-control-touch', className)}
onPointerMove={onPointerMove}
onClick={choose}
>
<span className="flex min-w-0 flex-1 items-center gap-3">{children}</span>
{shortcut && <span className="type-caption text-fg-muted">{shortcut}</span>}
{role !== 'menuitem' && checked && <Check size={16} strokeWidth={1.5} aria-hidden="true" className="shrink-0" />}
</button>
)
}
export default MenubarItemimport type { ReactNode } from 'react'
// A group's name in a MenubarMenu: DM Sans Medium in capitals at 6%, muted.
export interface MenubarLabelProps {
children?: ReactNode
}
export function MenubarLabel({ children }: MenubarLabelProps) {
return <p data-ds="menubar-label" className="px-2 pt-2 pb-1 type-label text-fg-muted">{children}</p>
}
export default MenubarLabel// A hairline between groups of rows in a MenubarMenu.
export function MenubarSeparator() {
return <div data-ds="menubar-separator" role="separator" className="-mx-1 my-1 h-px shrink-0 bg-edge" />
}
export default MenubarSeparator// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html, type PropertyValues } from 'lit'
import { DsElement, define, tagName } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Menubar.tsx as <spyglass-menubar>: menu triggers in a row: a Black strip in a hairline with the brand corner, 32px
// triggers in DM Sans Medium, each opening a panel of rows in the card's #262626 fill (<spyglass-menubar-menu> with
// <spyglass-menubar-item>, -label and -separator). One trigger is in the tab order; Left and Right move along the row,
// Down, Enter or Space open, and with a menu open Left and Right open the next one. While one is open, pointing at
// another trigger switches to it at once.
/** What a <spyglass-menubar-menu> offers its bar. */
export interface MenubarEntry extends HTMLElement { focusTrigger(): void, openMenu(focus?: 'first' | 'last' | 'none'): Promise<void> }
/**
* @summary Menu triggers in a row.
* @slot - The menus: <spyglass-menubar-menu>.
*/
export class Menubar extends DsElement {
static styles = [DsElement.styles, styles]
static properties = { label: {} }
/** The bar's name for assistive tech. */
label = ''
/** The open menu, if any. */
active: Element | null = null
/** The trigger in the tab order (the first until another takes focus). */
focused: Element | null = null
/** The menus, in the order they sit. */
entries() {
return [...this.querySelectorAll<MenubarEntry>(tagName('menubar-menu'))]
}
/** The trigger in the tab order. */
current() {
return this.focused ?? this.entries()[0] ?? null
}
private refresh() {
for (const m of this.entries()) (m as unknown as { requestUpdate(): void }).requestUpdate?.()
}
setActive(menu: Element | null) {
if (this.active === menu) return
this.active = menu
this.refresh()
}
setFocused(menu: Element) {
if (this.focused === menu) return
this.focused = menu
this.refresh()
}
move(from: Element, step: number, open: boolean) {
const list = this.entries()
const at = list.indexOf(from as MenubarEntry)
const next = list[(at + step + list.length) % list.length]
if (!next) return
this.setFocused(next)
next.focusTrigger()
if (open) void next.openMenu('first')
}
protected firstUpdated(changed: PropertyValues<this>) {
super.firstUpdated(changed)
// Menus that drew before the bar was defined learn which trigger is in the tab order.
this.refresh()
}
render() {
return html`<div data-ds="menubar" role="menubar" aria-label=${this.label} class="inline-flex items-center gap-1 rounded-control border border-edge bg-surface p-1"><slot></slot></div>`
}
}
define('menubar', Menubar)import { html, nothing, type PropertyValues } from 'lit'
import { DsElement, define, tagName } from '@/elements/lib/element'
import { onMenuRows } from '@/elements/lib/menu'
import { PopoverController, popoverStyles } from '@/elements/lib/popover'
import type { Menubar } from '@/elements/components/ui/menubar'
import styles from 'virtual:tw'
// MenubarMenu.tsx as <spyglass-menubar-menu>: one menu of a <spyglass-menubar>: its trigger and a panel of rows
// (<spyglass-menubar-item>, -label, -separator) under it, in the top layer. The open trigger takes the #262626 fill.
type Row = HTMLElement & { disabled?: boolean, keepOpen?: boolean }
/**
* @summary One menu of a menubar.
* @slot - The rows.
*/
export class MenubarMenu extends DsElement {
static styles = [DsElement.styles, popoverStyles, styles]
static properties = { label: {}, width: {} }
/** The trigger's text and the menu's name. */
label = ''
/** The panel's width (a CSS length). */
width = '224px'
private wasOpen = false
private pop = new PopoverController(this, {
side: 'bottom',
align: 'start',
gap: 8,
anchor: () => this.renderRoot.querySelector<HTMLElement>('button[role="menuitem"]'),
panel: () => this.renderRoot.querySelector<HTMLElement>('.ds-pop'),
})
private get bar() {
const b = this.closest(tagName('menubar')) as Menubar | null
return b && typeof b.setActive === 'function' ? b : null
}
// A chosen row closes the menu, focus back on the trigger, unless the row keeps it open.
private onSelect = (event: Event) => {
const row = event.composedPath()[0] as Row
if (row?.localName !== tagName('menubar-item')) return
if (!row.keepOpen) this.pop.hide(true)
}
connectedCallback() {
super.connectedCallback()
this.addEventListener('select', this.onSelect)
}
disconnectedCallback() {
super.disconnectedCallback()
this.removeEventListener('select', this.onSelect)
}
private rows() {
return [...this.querySelectorAll<Row>(tagName('menubar-item'))].filter((r) => !r.disabled)
}
/** Focuses the trigger (the bar's arrows). */
focusTrigger() {
this.renderRoot.querySelector<HTMLElement>('button[role="menuitem"]')?.focus()
}
/** Opens the menu, focusing its first or last row, or the panel. */
async openMenu(focus: 'first' | 'last' | 'none' = 'first', keyboard = true) {
await this.pop.show({ keyboard })
await this.updateComplete
if (focus === 'none') return this.renderRoot.querySelector<HTMLElement>('.ds-pop')?.focus({ preventScroll: true })
const rows = this.rows()
;(focus === 'last' ? rows.at(-1) : rows[0])?.focus()
}
private onClick = (event: MouseEvent) => {
if (this.pop.open) return this.pop.hide(event.detail === 0, event.detail === 0)
void this.openMenu(event.detail === 0 ? 'first' : 'none', event.detail === 0)
}
private onEnter = () => {
const bar = this.bar
if (bar?.active && bar.active !== this) void this.openMenu('none', true)
}
private onTriggerKey = (event: KeyboardEvent) => {
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault()
this.bar?.move(this, event.key === 'ArrowRight' ? 1 : -1, false)
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
event.preventDefault()
void this.openMenu(event.key === 'ArrowDown' ? 'first' : 'last')
}
}
// The rows' keys, after Left and Right, which open the next menu.
private onPanelKey = (event: KeyboardEvent) => {
if (event.key === 'ArrowRight' || event.key === 'ArrowLeft') {
event.preventDefault()
this.pop.hide(false, true)
this.bar?.move(this, event.key === 'ArrowRight' ? 1 : -1, true)
return
}
onMenuRows(event, this.rows(), (refocus) => this.pop.hide(refocus))
}
protected updated(changed: PropertyValues<this>) {
super.updated(changed)
const bar = this.bar
if (!bar) return
if (this.pop.open !== this.wasOpen) {
this.wasOpen = this.pop.open
if (this.pop.open) bar.setActive(this)
else if (bar.active === this) bar.setActive(null)
}
if (bar.active !== this && this.pop.open) this.pop.hide(false, true)
}
render() {
const bar = this.bar
const tabbable = !bar || bar.current() === this
const panel = this.pop.rendered
? html`<div data-ds="menubar-menu" popover="manual" class="ds-pop flex flex-col p-1 fade-y" role="menu" aria-label=${this.label} tabindex="-1" data-side=${this.pop.pos.side} ?data-instant=${this.pop.instant} ?data-placed=${this.pop.pos.placed} style=${this.pop.style(`width: ${this.width}`)} @keydown=${this.onPanelKey} @focusout=${this.pop.onFocusOut}><slot></slot></div>`
: nothing
return html`<button data-ds="menubar-menu" type="button" role="menuitem" aria-haspopup="menu" aria-expanded=${String(this.pop.open)} tabindex=${tabbable ? 0 : -1} class="inline-flex h-control-sm cursor-pointer items-center rounded-ds-sm px-3 type-ui-strong text-fg-soft -outline-offset-2 transition-[background-color,color] duration-160 ease-standard hover:not-aria-expanded:bg-action-ghost-hover hover:text-fg aria-expanded:bg-action-secondary aria-expanded:text-fg max-md:h-control-touch" @click=${this.onClick} @pointerenter=${this.onEnter} @focus=${() => this.bar?.setFocused(this)} @keydown=${this.onTriggerKey}>${this.label}</button>${panel}`
}
}
define('menubar-menu', MenubarMenu)import { html, LitElement, nothing } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { Check } from 'lucide'
import { DsElement, define } from '@/elements/lib/element'
import { icon } from '@/elements/lib/icon'
import styles from 'virtual:tw'
// MenubarItem.tsx as <spyglass-menubar-item>: one row of a menubar menu: 32px (44px on phones), DM Sans soft gray, the
// small corner; the row under the pointer or focus takes the #262626 fill and White type. Put a 16px Lucide icon first;
// `shortcut` shows muted at the end. `item-role` menuitemradio or menuitemcheckbox with `checked` makes a checked row.
// `danger` is a destructive row's red. Choosing it fires `select` and closes the menu, unless `keep-open`.
/**
* @summary One row of a menubar menu.
* @slot - The row: an icon and the label.
* @fires select - The row was chosen.
*/
export class MenubarItem extends DsElement {
static styles = [DsElement.styles, styles]
static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true }
static properties = { shortcut: {}, danger: { type: Boolean }, disabled: { type: Boolean, reflect: true }, keepOpen: { type: Boolean, attribute: 'keep-open' }, checked: { type: Boolean }, itemRole: { attribute: 'item-role' } }
/** A key or keys, shown muted at the end. */
shortcut?: string
/** A destructive row (red). */
danger = false
disabled = false
/** Choosing it leaves the menu open. */
keepOpen = false
/** Checked, for a radio or checkbox row. */
checked = false
/** menuitem, menuitemradio or menuitemcheckbox. */
itemRole: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem'
// The row under the pointer takes focus, so the pointer and the keys share one highlight.
private onPointerMove = (event: PointerEvent) => {
const el = event.currentTarget as HTMLElement
if (!this.disabled && (this.renderRoot as ShadowRoot).activeElement !== el) el.focus({ preventScroll: true })
}
render() {
const role = this.itemRole
return html`<button data-ds="menubar-item" type="button" role=${role} aria-checked=${ifDefined(role === 'menuitem' ? undefined : String(this.checked))} ?disabled=${this.disabled} ?data-danger=${this.danger} class="flex h-control-sm w-full shrink-0 cursor-pointer items-center gap-3 rounded-ds-sm px-2 text-left type-ui text-fg-soft outline-none transition-[background-color,color] duration-160 ease-standard focus:bg-action-secondary focus:text-fg disabled:cursor-not-allowed disabled:text-fg-faint data-danger:text-fg-danger max-md:h-control-touch" @pointermove=${this.onPointerMove} @click=${() => this.emit('select')}><span class="flex min-w-0 flex-1 items-center gap-3"><slot></slot></span>${this.shortcut ? html`<span class="type-caption text-fg-muted">${this.shortcut}</span>` : nothing}${role !== 'menuitem' && this.checked ? icon(Check, 'check', { size: 16, strokeWidth: 1.5, class: 'shrink-0', attrs: { 'aria-hidden': 'true' } }) : nothing}</button>`
}
}
define('menubar-item', MenubarItem)import { html } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// MenubarLabel.tsx as <spyglass-menubar-label>: a group's name in a menubar menu, DM Sans Medium in capitals at 6%,
// muted.
/**
* @summary A group's name in a menubar menu.
* @slot - The name.
*/
export class MenubarLabel extends DsElement {
static styles = [DsElement.styles, styles]
render() {
return html`<p data-ds="menubar-label" class="px-2 pt-2 pb-1 type-label text-fg-muted"><slot></slot></p>`
}
}
define('menubar-label', MenubarLabel)import { html } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// MenubarSeparator.tsx as <spyglass-menubar-separator>: a hairline between groups of rows in a menubar menu.
/**
* @summary A hairline between groups of rows.
*/
export class MenubarSeparator extends DsElement {
static styles = [DsElement.styles, styles]
render() {
return html`<div data-ds="menubar-separator" role="separator" class="-mx-1 my-1 h-px shrink-0 bg-edge"></div>`
}
}
define('menubar-separator', MenubarSeparator)Reference: Derived from the brand guidelines (no menubar in the brand assets): Black ground, the neutral steps for fills and hairlines, DM Sans Medium for triggers, the plan card's #262626 fill for panels.