# Kbd

> Keycaps that display a keyboard shortcut next to an action.

```tsx title="hero.tsx"
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
import * as React from 'react'

const shortcuts = [
  { action: 'Go to anything', keys: [{ symbol: '⌘', name: 'Command' }, { symbol: 'K' }] },
  { action: 'Save draft', keys: [{ symbol: '⌘', name: 'Command' }, { symbol: 'S' }] },
  {
    action: 'Publish app',
    keys: [{ symbol: '⌘', name: 'Command' }, { symbol: '⇧', name: 'Shift' }, { symbol: 'P' }],
  },
  { action: 'Run workflow', keys: [{ symbol: '⌥', name: 'Option' }, { symbol: 'R' }] },
]

export function KbdHeroDemo() {
  return (
    <dl className="grid w-full max-w-72 grid-cols-[1fr_auto] items-center gap-x-6 gap-y-3">
      {shortcuts.map(({ action, keys }) => (
        <div key={action} className="contents">
          <dt className="system-sm-regular text-text-secondary">{action}</dt>
          <dd className="m-0">
            <KbdGroup>
              {keys.map(({ symbol, name }) => (
                <Kbd key={symbol}>
                  {name ? (
                    <React.Fragment>
                      <span aria-hidden="true">{symbol}</span>
                      <span className="sr-only">{name}</span>
                    </React.Fragment>
                  ) : (
                    symbol
                  )}
                </Kbd>
              ))}
            </KbdGroup>
          </dd>
        </div>
      ))}
    </dl>
  )
}
```

## Usage guidelines

- **Show keys, not behavior**: `Kbd` only displays a key. The feature registers the shortcut and formats it for the platform, then renders one `Kbd` per key part.
- **Group a combination**: Wrap the keys of one shortcut in `KbdGroup`. Keys sit side by side without a `+` separator, and each keeps its own `<kbd>` element.
- **Keep the action's name clean**: Inside a button, menu item or other control, hide the keycaps with `aria-hidden`, so the accessible name stays the action, such as "Publish", instead of "Publish ⌘ ⇧ P". When the feature handles the shortcut, also set `aria-keyshortcuts` on the control, such as `Meta+Shift+P`. Do not declare a shortcut that nothing handles.
- **Name symbol keys in standalone text**: Screen readers announce symbols like `⌘`, `⇧` and `⌥` inconsistently or not at all. When keycaps are the content itself, such as a shortcut list, hide the symbol with `aria-hidden` and put the key name in `sr-only` text inside the same `Kbd`, so the key reads as "Command" once instead of the symbol plus the name.
- **Do not label `Kbd` or `KbdGroup`**: `<kbd>` and the group's `<span>` have no role that permits naming, so `aria-label` and `aria-labelledby` on them are not reliably announced. Put the key name in the content instead.
- **Match the surface**: `gray` is the default. Use `white` on a primary button or another dark, saturated surface. Set `disabled` when the owning control is disabled; it only dims the keycap and adds no ARIA state.
- **Hint, not entry point**: A shortcut in a tooltip is a visual hint for pointer users. The control still needs its own accessible name and a visible way to run the action.

## Anatomy

```tsx
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
```

```tsx
<KbdGroup>
  <Kbd />
  <Kbd />
</KbdGroup>
```

## Examples

### In a tooltip

The tooltip repeats the trigger's label and adds the keycaps. Assistive technology does not read the tooltip, so a trigger whose shortcut the feature handles sets `aria-keyshortcuts`. This example only displays the keys and binds no shortcut, so it leaves the attribute off.

```tsx title="tooltip.tsx"
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
import { Tooltip, TooltipContent, TooltipTrigger } from '@langgenius/dify-ui/tooltip'

export function KbdTooltipDemo() {
  return (
    <Tooltip>
      <TooltipTrigger
        render={
          <IconButton aria-label="Go to anything" size="lg" variant="secondary">
            <span aria-hidden="true" className="i-ri-search-line size-4" />
          </IconButton>
        }
      />
      <TooltipContent side="bottom" className="flex items-center gap-1">
        <span className="px-0.5">Go to anything</span>
        <KbdGroup>
          <Kbd>⌘</Kbd>
          <Kbd>K</Kbd>
        </KbdGroup>
      </TooltipContent>
    </Tooltip>
  )
}
```

### In a menu

Each item shows its shortcut at the end of the row. The keycaps are `aria-hidden`, so the item is announced by its label alone. An item whose shortcut the feature handles also sets `aria-keyshortcuts`; these items only display keys.

```tsx title="menu.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'
import { Menu, MenuContent, MenuItem, MenuSeparator, MenuTrigger } from '@langgenius/dify-ui/menu'

const nodeActions = [
  { label: 'Copy', icon: 'i-ri-file-copy-line', keys: ['⌘', 'C'] },
  { label: 'Duplicate', icon: 'i-ri-stack-line', keys: ['⌘', 'D'] },
  { label: 'Run this step', icon: 'i-ri-play-line', keys: ['⇧', 'R'] },
]

export function KbdMenuDemo() {
  return (
    <Menu>
      <MenuTrigger
        render={
          <Button className="data-popup-open:bg-components-button-secondary-bg-hover data-popup-open:inset-ring-components-button-secondary-border-hover" />
        }
      >
        LLM node
        <span aria-hidden="true" className="i-ri-arrow-down-s-line size-4 text-text-tertiary" />
      </MenuTrigger>
      <MenuContent align="start" className="w-56">
        {nodeActions.map(({ label, icon, keys }) => (
          <MenuItem key={label}>
            <span aria-hidden="true" className={`${icon} size-4 shrink-0 text-text-tertiary`} />
            <span className="min-w-0 flex-1 truncate">{label}</span>
            <KbdGroup aria-hidden="true">
              {keys.map((key) => (
                <Kbd key={key}>{key}</Kbd>
              ))}
            </KbdGroup>
          </MenuItem>
        ))}
        <MenuSeparator />
        <MenuItem variant="destructive">
          <span aria-hidden="true" className="i-ri-delete-bin-line size-4 shrink-0" />
          <span className="min-w-0 flex-1 truncate">Delete</span>
          <Kbd aria-hidden="true">Del</Kbd>
        </MenuItem>
      </MenuContent>
    </Menu>
  )
}
```

### In a button

Use `color="white"` on a primary button, and dim the keycaps with `disabled` together with the button.

```tsx title="button.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Kbd, KbdGroup } from '@langgenius/dify-ui/kbd'

export function KbdButtonDemo() {
  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button variant="primary">
        Publish
        <KbdGroup aria-hidden="true">
          <Kbd color="white">⌘</Kbd>
          <Kbd color="white">⇧</Kbd>
          <Kbd color="white">P</Kbd>
        </KbdGroup>
      </Button>
      <Button variant="primary" disabled>
        Publish
        <KbdGroup aria-hidden="true">
          <Kbd color="white" disabled>
            ⌘
          </Kbd>
          <Kbd color="white" disabled>
            ⇧
          </Kbd>
          <Kbd color="white" disabled>
            P
          </Kbd>
        </KbdGroup>
      </Button>
      <Button>
        Save draft
        <KbdGroup aria-hidden="true">
          <Kbd>⌘</Kbd>
          <Kbd>S</Kbd>
        </KbdGroup>
      </Button>
    </div>
  )
}
```

## API reference

### Kbd

Displays a single keyboard key or key-like token. Renders a `<kbd>` element.

**Props**

| Prop        | Type                | Default  | Description                                                                      |
| ----------- | ------------------- | -------- | -------------------------------------------------------------------------------- |
| `color?`    | `'gray' \| 'white'` | `'gray'` | The background and text color of the key.                                        |
| `disabled?` | `boolean`           | `false`  | Whether the key is dimmed, such as for a shortcut that is currently unavailable. |

**Data attributes**

| Attribute       | Description                                              |
| --------------- | -------------------------------------------------------- |
| `data-disabled` | Present when the key hint belongs to a disabled control. |

### KbdGroup

Groups multiple `Kbd` elements inline without replacing their individual `<kbd>` semantics. Renders a `<span>` element.

Accepts the props of the element it renders.
