Skip to content
2k
On This Page

Kbd

Used to display textual user input from keyboard.

⌘⇧⌥⌃Ctrl+B
import { Kbd, KbdGroup } from "~/components/ui/kbd"

export default function KbdDemo() {
  return (
    <div class="flex flex-col items-center gap-4">
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>⇧</Kbd>
        <Kbd>⌥</Kbd>
        <Kbd>⌃</Kbd>
      </KbdGroup>
      <KbdGroup>
        <Kbd>Ctrl</Kbd>
        <span>+</span>
        <Kbd>B</Kbd>
      </KbdGroup>
    </div>
  )
}

Installation

pnpm dlx solidui-cli@latest add kbd

Copy and paste the following code into your project.

components/ui/kbd.tsx
import { type ComponentProps } from "@solidjs/web"

import { cn } from "~/lib/utils"

type KbdProps = ComponentProps<"kbd">

const Kbd = (props: KbdProps) => (
  <kbd
    data-slot="kbd"
    {...props}
    class={cn(
      "h-5 w-fit min-w-5 gap-1 rounded-sm bg-muted px-1 font-sans text-xs font-medium text-muted-foreground in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background in-data-[slot=tooltip-content]:dark:bg-background/10 [&_svg:not([class*='size-'])]:size-3 pointer-events-none inline-flex items-center justify-center select-none",
      props.class
    )}
  />
)

type KbdGroupProps = ComponentProps<"span">

const KbdGroup = (props: KbdGroupProps) => (
  <span
    data-slot="kbd-group"
    {...props}
    class={cn("gap-1 inline-flex items-center", props.class)}
  />
)

export { Kbd, KbdGroup, type KbdGroupProps, type KbdProps }

Update the import paths to match your project setup.

Usage

import { Kbd } from "~/components/ui/kbd"
<Kbd>Ctrl</Kbd>

Composition

Use the following composition to build Kbd and KbdGroup:

Kbd
KbdGroup
├── Kbd
└── Kbd

Group

Use the KbdGroup component to group keyboard keys together.

Use Ctrl + BCtrl + K to open the command palette

import { Kbd, KbdGroup } from "~/components/ui/kbd"

export default function KbdGroupExample() {
  return (
    <div class="flex flex-col items-center gap-4">
      <p class="text-sm text-muted-foreground">
        Use{" "}
        <KbdGroup>
          <Kbd>Ctrl + B</Kbd>
          <Kbd>Ctrl + K</Kbd>
        </KbdGroup>{" "}
        to open the command palette
      </p>
    </div>
  )
}

Button

Use the Kbd component inside a Button component to display a keyboard key inside a button.

import { Button } from "~/components/ui/button"
import { Kbd } from "~/components/ui/kbd"

export default function KbdButton() {
  return (
    <Button variant="outline">
      Accept{" "}
      <Kbd data-icon="inline-end" class="translate-x-0.5">
        ⏎
      </Kbd>
    </Button>
  )
}

Tooltip

You can use the Kbd component inside a Tooltip component to display a tooltip with a keyboard key.

import { Button } from "~/components/ui/button"
import { ButtonGroup } from "~/components/ui/button-group"
import { Kbd, KbdGroup } from "~/components/ui/kbd"
import { Tooltip, TooltipContent, TooltipTrigger } from "~/components/ui/tooltip"

export default function KbdTooltip() {
  return (
    <div class="flex flex-wrap gap-4">
      <ButtonGroup>
        <Tooltip>
          <TooltipTrigger
            render={(partProps) => <Button {...partProps} variant="outline" />}
          >
            Save
          </TooltipTrigger>
          <TooltipContent>
            Save Changes <Kbd>S</Kbd>
          </TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger
            render={(partProps) => <Button {...partProps} variant="outline" />}
          >
            Print
          </TooltipTrigger>
          <TooltipContent>
            Print Document{" "}
            <KbdGroup>
              <Kbd>Ctrl</Kbd>
              <Kbd>P</Kbd>
            </KbdGroup>
          </TooltipContent>
        </Tooltip>
      </ButtonGroup>
    </div>
  )
}

Input Group

You can use the Kbd component inside a InputGroupAddon component to display a keyboard key inside an input group.

⌘K
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import {
  InputGroup,
  InputGroupAddon,
  InputGroupInput
} from "~/components/ui/input-group"
import { Kbd } from "~/components/ui/kbd"

export default function KbdInputGroup() {
  return (
    <div class="flex w-full max-w-xs flex-col gap-6">
      <InputGroup>
        <InputGroupInput placeholder="Search..." />
        <InputGroupAddon>
          <svg
            width="24"
            height="24"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
            class="size-4 text-muted-foreground"
          >
            <path d="m21 21-4.34-4.34" />
            <circle cx="11" cy="11" r="8" />
          </svg>
        </InputGroupAddon>
        <InputGroupAddon align="inline-end">
          <Kbd>⌘</Kbd>
          <Kbd>K</Kbd>
        </InputGroupAddon>
      </InputGroup>
    </div>
  )
}

API Reference

Kbd

Use Kbd to display a keyboard key or shortcut.

PropTypeDefault
classstring-
<Kbd>Ctrl</Kbd>

KbdGroup

Use KbdGroup to group multiple Kbd elements together.

PropTypeDefault
classstring-
<KbdGroup>
  <Kbd>Ctrl</Kbd>
  <Kbd>B</Kbd>
</KbdGroup>