Components

Kbd

A key: DM Sans 12px in the soft neutral, in a 20px #1A1A1A box with a hairline and the small corner. KbdGroup holds the keys of one shortcut.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add kbd
<script type="module" src="/spyglass-ds/elements/kbd.js"></script>
Shortcuts
Search creatives
⌘K
Save to a board
⌘S
Next result
J
Previous result
K

Press Esc to close any panel.

Usage

Keyboard shortcuts beside the action they run (in a menu, a tooltip, a help list) and a key named in a sentence.

Never

  • The only way to learn an action: the action must be visible on its own
  • Key names in capitals or in mono
  • Shortcuts shown on touch screens with no keyboard

Props

The web component takes the same as attributes or properties (<spyglass-kbd>); title is heading there.

<Kbd>

PropTypeDefault
childrenReactNode

<KbdGroup>

PropTypeDefault
childrenReactNode

Examples

The same example in React and as web components; both render identically.

react/examples/kbd/Shortcuts.tsx
import { Kbd } from '@/react/components/ui/Kbd'
import { KbdGroup } from '@/react/components/ui/KbdGroup'
// The shortcuts beside what they do, and a key inside a sentence.

const ROWS = [
  { label: 'Search creatives', keys: ['⌘', 'K'] },
  { label: 'Save to a board', keys: ['⌘', 'S'] },
  { label: 'Next result', keys: ['J'] },
  { label: 'Previous result', keys: ['K'] },
]

export default function Shortcuts() {
  return (
    <div className="flex w-72 max-w-full flex-col gap-6">
      <dl className="flex flex-col gap-3">
        {ROWS.map((row) => (
          <div key={row.label} className="flex items-center justify-between gap-4">
            <dt className="type-ui text-fg-soft">{row.label}</dt>
            <dd>
              <KbdGroup>{row.keys.map((k) => <Kbd key={k}>{k}</Kbd>)}</KbdGroup>
            </dd>
          </div>
        ))}
      </dl>
      <p className="type-caption text-fg-muted">Press <Kbd>Esc</Kbd> to close any panel.</p>
    </div>
  )
}
elements/examples/kbd/Shortcuts.html
<!-- The shortcuts beside what they do, and a key inside a sentence. -->
<div class="flex w-72 max-w-full flex-col gap-6">
  <dl class="flex flex-col gap-3">
    <div class="flex items-center justify-between gap-4">
      <dt class="type-ui text-fg-soft">Search creatives</dt>
      <dd><spyglass-kbd-group><spyglass-kbd>⌘</spyglass-kbd><spyglass-kbd>K</spyglass-kbd></spyglass-kbd-group></dd>
    </div>
    <div class="flex items-center justify-between gap-4">
      <dt class="type-ui text-fg-soft">Save to a board</dt>
      <dd><spyglass-kbd-group><spyglass-kbd>⌘</spyglass-kbd><spyglass-kbd>S</spyglass-kbd></spyglass-kbd-group></dd>
    </div>
    <div class="flex items-center justify-between gap-4">
      <dt class="type-ui text-fg-soft">Next result</dt>
      <dd><spyglass-kbd-group><spyglass-kbd>J</spyglass-kbd></spyglass-kbd-group></dd>
    </div>
    <div class="flex items-center justify-between gap-4">
      <dt class="type-ui text-fg-soft">Previous result</dt>
      <dd><spyglass-kbd-group><spyglass-kbd>K</spyglass-kbd></spyglass-kbd-group></dd>
    </div>
  </dl>
  <p class="type-caption text-fg-muted">Press <spyglass-kbd>Esc</spyglass-kbd> to close any panel.</p>
</div>

Source

react/components/ui/Kbd.tsx
import type { HTMLAttributes, ReactNode } from 'react'
import { cn } from '@/react/lib/cn'

// A key: DM Sans 12px in the soft neutral, in a 20px #1A1A1A box with a #666666 hairline and the small corner. Key
// names as words or symbols (⌘, Shift, K), never in capitals or mono. Put a shortcut's keys in KbdGroup.
export interface KbdProps extends HTMLAttributes<HTMLElement> {
  children?: ReactNode
}

export const kbdClasses = 'inline-flex h-5 min-w-5 shrink-0 items-center justify-center gap-1 rounded-ds-sm border border-edge-strong bg-surface-subtle px-1 type-caption whitespace-nowrap text-fg-soft select-none'

export function Kbd({ className, children, ...rest }: KbdProps) {
  return <kbd data-ds="kbd" className={cn(kbdClasses, className)} {...rest}>{children}</kbd>
}

export default Kbd
react/components/ui/KbdGroup.tsx
import type { HTMLAttributes, ReactNode } from 'react'
import { cn } from '@/react/lib/cn'

// The keys of one shortcut, 4px apart: <KbdGroup><Kbd>⌘</Kbd><Kbd>K</Kbd></KbdGroup>.
export interface KbdGroupProps extends HTMLAttributes<HTMLElement> {
  children?: ReactNode
}

export function KbdGroup({ className, children, ...rest }: KbdGroupProps) {
  return <kbd data-ds="kbd-group" className={cn('inline-flex items-center gap-1', className)} {...rest}>{children}</kbd>
}

export default KbdGroup
react/lib/cn.ts
// 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(' ')
}
elements/components/ui/kbd.ts
import { html } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Kbd.tsx as <spyglass-kbd>: a key, DM Sans 12px in the soft neutral, in a 20px #1A1A1A box with a hairline and the
// small corner. Put a shortcut's keys in <spyglass-kbd-group>.

/**
 * @summary A key.
 * @slot - The key's name or symbol.
 */
export class Kbd extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('kbd'))

	render() {
		return html`<kbd data-ds="kbd" class="inline-flex h-5 min-w-5 shrink-0 items-center justify-center gap-1 rounded-ds-sm border border-edge-strong bg-surface-subtle px-1 type-caption whitespace-nowrap text-fg-soft select-none"><slot></slot></kbd>`
	}
}

define('kbd', Kbd)
elements/components/ui/kbd-group.ts
import { html } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'

// KbdGroup.tsx as <spyglass-kbd-group>: the keys of one shortcut, 4px apart.

/**
 * @summary The keys of one shortcut.
 * @slot - <spyglass-kbd> elements.
 */
export class KbdGroup extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('kbd'))

	render() {
		return html`<kbd data-ds="kbd-group" class="inline-flex items-center gap-1"><slot></slot></kbd>`
	}
}

define('kbd-group', KbdGroup)

Reference: Derived from the brand guidelines (no key in the brand assets): DM Sans, the neutral steps for the box and its hairline, the small corner.