{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "button",
  "type": "registry:ui",
  "title": "Button",
  "description": "The button and the link-as-button: primary, secondary, ghost, accent and soft variants, three sizes, a 44 px hit area.",
  "dependencies": [
    "radix-ui@^1.6.7"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/editing.json",
    "https://ui.klarhimmel.com/r/theme.json",
    "https://ui.klarhimmel.com/r/utils.json"
  ],
  "files": [
    {
      "path": "src/ui/button.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/ui/button.tsx",
      "content": "import { Children, forwardRef, isValidElement, useContext, type AnchorHTMLAttributes, type ButtonHTMLAttributes, type ReactNode } from 'react';\nimport { EditingContext, Media, Text } from '../editing';\nimport { Slot } from 'radix-ui';\nimport { cn } from '../lib/utils';\n\nexport type ButtonVariant = 'primary' | 'secondary' | 'ghost' | 'accent' | 'soft' | 'danger';\nexport type ButtonSize = 'sm' | 'md' | 'lg';\n\nconst variants: Record<ButtonVariant, string> = {\n  primary: 'bg-kh-primary text-kh-on-primary shadow-kh-1 hover:bg-[color-mix(in_oklab,var(--kh-primary)_86%,var(--kh-canvas))] active:bg-[color-mix(in_oklab,var(--kh-primary)_76%,var(--kh-canvas))]',\n  secondary: 'bg-kh-surface text-kh-ink border [border-color:var(--kh-edge)] shadow-kh-1 hover:bg-kh-surface-2 active:bg-kh-surface-3',\n  ghost: 'border [border-color:var(--kh-ghost-edge)] text-kh-ink-2 hover:bg-kh-surface-2 hover:text-kh-ink active:bg-kh-surface-3',\n  soft: 'border [border-color:var(--kh-ghost-edge)] bg-kh-surface-2 text-kh-ink hover:bg-kh-surface-3',\n  accent: 'bg-kh-accent-fill text-kh-accent-fg shadow-kh-1 hover:bg-kh-accent-hover',\n  // an action that ends or removes something (Cancel subscription): quiet until hovered, never the page's main button\n  danger: 'border border-[color-mix(in_oklab,var(--kh-danger)_45%,var(--kh-line))] text-kh-danger hover:bg-[color-mix(in_oklab,var(--kh-danger)_8%,var(--kh-surface))] active:bg-[color-mix(in_oklab,var(--kh-danger)_14%,var(--kh-surface))]',\n};\n/** Every look a button comes in, for a picker that cycles them. */\nexport const BUTTON_VARIANTS = Object.keys(variants) as ButtonVariant[];\n\nconst sizes: Record<ButtonSize, string> = {\n  sm: 'min-h-9 px-3 gap-1.5 text-kh-small after:absolute after:-inset-y-1 after:inset-x-0 after:content-[\"\"]',\n  md: 'min-h-11 px-4 gap-2 text-kh-small',\n  lg: 'min-h-12 px-6 gap-2 text-kh-body',\n};\n\n/** Class string for anything that should look like a button (an <a>, a router Link). */\nexport const buttonClass = (variant: ButtonVariant = 'primary', size: ButtonSize = 'md', full = false) =>\n  cn(\n    'relative inline-flex max-w-full shrink-0 select-none items-center justify-center whitespace-normal rounded-kh text-center font-medium no-underline [overflow-wrap:anywhere]', // a label longer than its row wraps instead of widening the page\n    'transition-[background-color,border-color,color] duration-(--kh-dur) ease-out',\n    'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)',\n    'disabled:pointer-events-none disabled:opacity-50',\n    variants[variant],\n    sizes[size],\n    full && 'w-full',\n  );\n\nexport interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {\n  variant?: ButtonVariant;\n  size?: ButtonSize;\n  icon?: ReactNode;\n  iconEnd?: ReactNode;\n  full?: boolean;\n  /** Render the single child (an <a>, a router Link) with button styling instead of a <button>. */\n  asChild?: boolean;\n}\n\n/**\n * True when what a control says comes from editable text slots and every one of them is empty (an emptied label), with no aria-label to\n * name it. Such a control is not drawn outside the editor: a nameless button or link is worse than none. Controls with static text, or\n * an icon and a fixed aria-label, are never touched.\n */\nexport function nameEmptied(children: ReactNode, ariaLabel?: string): boolean {\n  if (ariaLabel?.trim()) return false;\n  let slots = 0;\n  let named = false;\n  const walk = (n: ReactNode) => {\n    Children.forEach(n, (c) => {\n      if (typeof c === 'string' || typeof c === 'number') { if (String(c).trim()) named = true; return; }\n      if (!isValidElement(c)) return;\n      const p = c.props as { value?: unknown; children?: ReactNode; 'aria-label'?: string; 'aria-hidden'?: unknown };\n      if (c.type === Text) { slots++; if (typeof p.value === 'string' && p.value.trim()) named = true; return; }\n      if (c.type === Media) { const alt = (p.value as { alt?: string } | undefined)?.alt; if (alt?.trim()) named = true; return; } // an image with alt text names its link\n      if (p['aria-label']?.trim()) { named = true; return; }\n      if (p['aria-hidden']) return;\n      walk(p.children);\n    });\n  };\n  walk(children);\n  return slots > 0 && !named;\n}\n\nexport const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(\n  { variant = 'primary', size = 'md', icon, iconEnd, full, asChild, className, children, type = 'button', ...rest },\n  ref,\n) {\n  const cls = cn(buttonClass(variant, size, full), className);\n  const editing = !!useContext(EditingContext);\n  if (!editing && !asChild && nameEmptied(children, rest['aria-label'])) return null;\n  if (asChild) {\n    // Slot merges the classes onto the child; icons around a child's own content are not supported, put them inside the child.\n    return <Slot.Root ref={ref} className={cls} {...rest}>{children}</Slot.Root>;\n  }\n  return (\n    <button ref={ref} type={type} className={cls} {...rest}>\n      {icon}\n      {children}\n      {iconEnd}\n    </button>\n  );\n});\n\n/** Only http(s), mailto, tel, site-relative and fragment links survive; anything else (javascript:, data:) becomes '#'. */\nexport function safeHref(href: string | undefined): string {\n  const h = (href ?? '').trim();\n  if (/^(https?:\\/\\/|mailto:|tel:|\\/(?!\\/)|#|\\?)/i.test(h)) return h;\n  return h === '' ? '#' : '#';\n}\n\nexport interface ButtonLinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {\n  variant?: ButtonVariant;\n  size?: ButtonSize;\n  icon?: ReactNode;\n  iconEnd?: ReactNode;\n  full?: boolean;\n}\n\n/** A link that looks like a button. Navigates; use Button for actions. */\nexport function ButtonLink({ variant = 'primary', size = 'md', icon, iconEnd, full, className, children, href, ...rest }: ButtonLinkProps) {\n  const editing = !!useContext(EditingContext);\n  if (!editing && nameEmptied(children, rest['aria-label'])) return null;\n  return (\n    <a href={safeHref(href)} className={cn(buttonClass(variant, size, full), className)} {...rest}>\n      {icon}\n      {children}\n      {iconEnd}\n    </a>\n  );\n}\n"
    }
  ]
}
