{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "shortcut-button",
  "title": "Shortcut Button",
  "description": "A button whose tooltip shows its label and its keyboard shortcut. With `listen`, the key presses it from anywhere on the page, except while someone types or a menu or dialog is open.",
  "registryDependencies": [
    "button",
    "kbd",
    "tooltip"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/shortcut-button.tsx",
      "content": "'use client'\n\nimport { type ComponentProps, type ReactNode, useEffect, useRef } from 'react'\n\nimport { Button } from '@/components/ui/button'\nimport { Kbd } from '@/components/ui/kbd'\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipProvider,\n  TooltipTrigger,\n} from '@/components/ui/tooltip'\n\nimport { cn } from '@/lib/utils'\n\nconst KEY_NAMES: Record<string, string> = { Escape: 'Esc' }\n\nconst OWNS_ITS_KEYS =\n  'input, textarea, select, [contenteditable], [role=dialog], [role=alertdialog], [role=menu], [role=listbox], [data-key-scope]'\n\n/** Whether the key press goes to a field, menu or dialog that uses keys itself, so a page-wide shortcut leaves it. A dialog that holds the button keeps its shortcut. */\nfunction ownsItsKeys(event: KeyboardEvent, button?: HTMLElement | null) {\n  if (!(event.target instanceof Element)) return false\n  const owner = event.target.closest(OWNS_ITS_KEYS)\n  if (owner === null) return false\n  return !(owner.matches('[role=dialog]') && button && owner.contains(button))\n}\n\n/** A shortcut as its key cap shows it: `Shift+P` as ⇧P, `Escape` as Esc. */\nfunction keyName(shortcut: string) {\n  return shortcut\n    .split('+')\n    .map((part) => (part === 'Shift' ? '⇧' : (KEY_NAMES[part] ?? part)))\n    .join('')\n}\n\n/** A tooltip's text with the shortcut that presses its button, such as Project ⇧P. */\nfunction ShortcutHint({ label, shortcut }: { label: ReactNode; shortcut?: string }) {\n  return (\n    <>\n      {label}\n      {shortcut && <Kbd>{keyName(shortcut)}</Kbd>}\n    </>\n  )\n}\n\n/**\n * Clicks the button when `key` is pressed alone, unless someone is typing, a menu or dialog has\n * focus, or the page already used the key.\n */\nfunction usePressOnKey(\n  key: string,\n  button: { current: HTMLButtonElement | null },\n  on: boolean,\n) {\n  useEffect(() => {\n    if (!on) return\n    const match = key.toLowerCase()\n    const onKeyDown = (event: KeyboardEvent) => {\n      // Chrome's autofill sends keydown events without a key.\n      if (event.defaultPrevented || event.repeat || event.key?.toLowerCase() !== match)\n        return\n      if (event.metaKey || event.ctrlKey || event.altKey) return\n      if (ownsItsKeys(event, button.current)) return\n      event.preventDefault()\n      button.current?.click()\n    }\n    window.addEventListener('keydown', onKeyDown, { capture: true })\n    return () => window.removeEventListener('keydown', onKeyDown, { capture: true })\n  }, [button, key, on])\n}\n\nconst collapsed = {\n  lg: 'max-lg:aspect-square max-lg:px-0',\n  sm: 'max-sm:aspect-square max-sm:px-0',\n}\n\ninterface ShortcutButtonProps extends Omit<ComponentProps<'button'>, 'ref'> {\n  /** Below this width the button squares to its icon. Hide its text there too, with `max-lg:sr-only` or `max-sm:sr-only`. */\n  collapseBelow?: 'lg' | 'sm'\n  /** The tooltip's text, such as `New issue`. The button's name comes from `aria-label` or its text. */\n  label: string\n  /**\n   * Presses the button when its key is pressed anywhere on the page. Leave it off when a list or\n   * panel already listens for the key, as a row's actions do.\n   */\n  listen?: boolean\n  /** The key, shown in the tooltip, such as `C` or `Escape` (shown as Esc). Leave out for a button with a tooltip and no key, such as a row's Delete. */\n  shortcut?: string\n  /** @default 'icon-sm' */\n  size?: 'icon-xs' | 'icon-sm' | 'sm'\n  /** `default` for the one filled action of a view, such as Accept, `secondary` for a toggle that's on. @default 'ghost' */\n  variant?: 'default' | 'ghost' | 'outline' | 'secondary'\n}\n\n/** A button whose tooltip shows its label and, when it has one, its keyboard shortcut. */\nfunction ShortcutButton({\n  children,\n  className,\n  collapseBelow,\n  disabled,\n  label,\n  listen = false,\n  shortcut,\n  size = 'icon-sm',\n  variant = 'ghost',\n  ...props\n}: ShortcutButtonProps) {\n  const button = useRef<HTMLButtonElement>(null)\n  usePressOnKey(shortcut ?? '', button, listen && shortcut !== undefined)\n\n  return (\n    <TooltipProvider>\n      <Tooltip>\n        <TooltipTrigger aria-keyshortcuts={shortcut} ref={button} {...props} asChild>\n          <Button\n            variant={variant}\n            size={size}\n            disabled={disabled}\n            className={cn(collapseBelow && collapsed[collapseBelow], className)}\n          >\n            {children}\n          </Button>\n        </TooltipTrigger>\n        <TooltipContent>\n          <ShortcutHint label={label} shortcut={shortcut} />\n        </TooltipContent>\n      </Tooltip>\n    </TooltipProvider>\n  )\n}\n\nexport { ownsItsKeys, ShortcutButton, ShortcutHint }\n\nexport type { ShortcutButtonProps }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/shortcut-button.tsx"
    }
  ],
  "type": "registry:component"
}
