{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle",
  "title": "Toggle",
  "description": "A button that stays pressed until it is pressed again, such as a star or a bold button, alone or joined in a Toggle group. Pressed, it fills and its icon can roll to another; it never changes size.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/button-group.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/toggle.tsx",
      "content": "'use client'\n\nimport { buttonVariants } from '@/registry/components/kino/button'\nimport { ButtonGroup } from '@/registry/components/kino/button-group'\nimport { SwapIcon } from '@/registry/lib/swap'\nimport {\n  Toggle as TogglePrimitive,\n  ToggleGroup as ToggleGroupPrimitive,\n  ToggleIcon as ToggleIconPrimitive,\n  type ToggleState,\n} from '@ui-sdk/react/toggle'\nimport type { VariantProps } from 'class-variance-authority'\nimport { type ComponentProps, createContext, type ReactNode, use } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ntype ToggleSize = NonNullable<VariantProps<typeof buttonVariants>['size']>\ntype ToggleVariant = 'default' | 'outline'\n\n// Off and on are both a Button's variants, of one size, so a toggle that turns\n// on never changes size. The sizes are the Button's: an icon size is square and\n// presses as deep as an icon Button, and a short label is never narrower than that.\nconst toggleMinWidth: Record<ToggleSize, string> = {\n  xs: 'min-w-(--control-height-xs)',\n  sm: 'min-w-(--control-height-sm)',\n  default: 'min-w-(--control-height)',\n  lg: 'min-w-(--control-height-lg)',\n  'icon-xs': '',\n  'icon-sm': '',\n  icon: '',\n  'icon-lg': '',\n}\n\n/** A toggle's classes, pressed or not, for an element that isn't a `Toggle`. */\nfunction toggleVariants({\n  pressed = false,\n  variant = 'default',\n  size = 'default',\n}: {\n  pressed?: boolean\n  variant?: ToggleVariant | null\n  size?: ToggleSize | null\n} = {}) {\n  return cn(\n    buttonVariants({\n      variant: pressed ? 'default' : variant === 'outline' ? 'outline' : 'ghost',\n      size: size ?? 'default',\n    }),\n    toggleMinWidth[size ?? 'default'],\n    // Forced colours drop the fill, so a pressed toggle takes the system's own, hovered or not.\n    pressed\n      ? 'forced-colors:bg-[Highlight]! forced-colors:text-[HighlightText]! forced-colors:forced-color-adjust-none forced-colors:focus-visible:outline-[CanvasText]'\n      : 'text-muted-foreground hover:text-foreground',\n  )\n}\n\n/** Whether the toggle around it is pressed, for its icon. */\nconst PressedContext = createContext(false)\n\n/** The size a group gives the toggles in it, or null outside one. */\nconst ToggleGroupSizeContext = createContext<ToggleSize | null>(null)\n\n/** The variant a toggle takes unless given one: outline in a group or a Toolbar, beside their outline buttons. */\nconst ToggleVariantContext = createContext<ToggleVariant>('default')\n\ntype TogglePrimitiveProps = ComponentProps<typeof TogglePrimitive>\n\nfunction Toggle({\n  className,\n  variant,\n  size,\n  children,\n  ...props\n}: Omit<TogglePrimitiveProps, 'children'> & {\n  /** Outline in a group or a Toolbar, beside the controls around it. */\n  variant?: ToggleVariant\n  /** The group's size in a group. */\n  size?: ToggleSize\n  children?: ReactNode\n}) {\n  const groupSize = use(ToggleGroupSizeContext)\n  const inGroup = groupSize !== null\n  const ownSize = size ?? groupSize ?? 'default'\n  const ownVariant = variant ?? use(ToggleVariantContext)\n  return (\n    <TogglePrimitive\n      // ui-sdk takes `className` as a function of state too, so this one keeps that type.\n      className={(state: ToggleState) =>\n        cn(\n          toggleVariants({ pressed: state.pressed, variant: ownVariant, size: ownSize }),\n          // A column of labels reads down its start edge; an icon stays centred.\n          inGroup &&\n            !ownSize.startsWith('icon') &&\n            'in-data-[orientation=vertical]:justify-start',\n          typeof className === 'function' ? className(state) : className,\n        )\n      }\n      {...props}\n    >\n      {(state: ToggleState) => (\n        <PressedContext value={state.pressed}>{children}</PressedContext>\n      )}\n    </TogglePrimitive>\n  )\n}\n\n/**\n * The toggle's icon: `children` at rest and `pressedIcon` while pressed. The\n * one leaving rolls up and the one arriving rises from below, as a Button's\n * icon does, so the toggle shows what it turned into.\n */\nfunction ToggleIcon({\n  pressedIcon,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof ToggleIconPrimitive>, 'children' | 'className'> & {\n  pressedIcon: ReactNode\n  children: ReactNode\n}) {\n  return (\n    // ui-sdk's part hides the icon from screen readers; the roll draws it.\n    <ToggleIconPrimitive className='contents' {...props}>\n      <SwapIcon on={use(PressedContext)} onIcon={pressedIcon}>\n        {children}\n      </SwapIcon>\n    </ToggleIconPrimitive>\n  )\n}\n\n/**\n * Toggles joined as a Button group, pressed one at a time or several at once:\n * one highlight glides between them under the pointer, and the pressed ones\n * are filled. `size` sets every toggle's.\n */\nfunction ToggleGroup<Value extends string>({\n  size = 'default',\n  orientation = 'horizontal',\n  ...props\n}: ComponentProps<typeof ToggleGroupPrimitive<Value>> & { size?: ToggleSize }) {\n  return (\n    <ToggleGroupSizeContext value={size}>\n      <ToggleVariantContext value='outline'>\n        <ToggleGroupPrimitive<Value>\n          orientation={orientation}\n          render={(groupProps) => (\n            <ButtonGroup {...groupProps} orientation={orientation} />\n          )}\n          {...props}\n        />\n      </ToggleVariantContext>\n    </ToggleGroupSizeContext>\n  )\n}\n\nexport { Toggle, ToggleGroup, ToggleIcon, ToggleVariantContext, toggleVariants }\nexport type { ToggleSize, ToggleVariant }\n",
      "type": "registry:component",
      "target": "@components/kino/toggle.tsx"
    }
  ],
  "type": "registry:component"
}