{
  "name": "swap",
  "type": "registry:ui",
  "dependencies": [
    "cn",
    "radix-ui",
    "@base-ui/utils@0.5.0"
  ],
  "files": [
    {
      "path": "ui/swap.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport { useControlled } from \"@base-ui/utils/useControlled\";\nimport { cn } from \"cn\";\nimport { Slot as SlotPrimitive } from \"radix-ui\";\nimport * as React from \"react\";\n\ntype RootElement = HTMLDivElement;\n\nconst ON_NAME = \"SwapOn\";\nconst OFF_NAME = \"SwapOff\";\n\nfunction getDataState(swapped: boolean) {\n  return swapped ? \"on\" : \"off\";\n}\n\nconst SwapContext = React.createContext<boolean | null>(null);\n\nfunction useSwapContext(consumerName: string) {\n  const context = React.useContext(SwapContext);\n  if (context === null) {\n    throw new Error(`\\`${consumerName}\\` must be used within \\`Swap\\``);\n  }\n  return context;\n}\n\nfunction useSwap<T>(selector: (state: { swapped: boolean }) => T): T {\n  const swapped = useSwapContext(\"useSwap\");\n  return selector({ swapped });\n}\n\ninterface SwapProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n  swapped?: boolean;\n  defaultSwapped?: boolean;\n  onSwappedChange?: (swapped: boolean) => void;\n  activationMode?: \"click\" | \"hover\";\n  animation?: \"fade\" | \"rotate\" | \"flip\" | \"scale\";\n  disabled?: boolean;\n}\n\nfunction Swap(props: SwapProps) {\n  const {\n    swapped: swappedProp,\n    defaultSwapped,\n    onSwappedChange,\n    activationMode = \"click\",\n    animation = \"fade\",\n    disabled,\n    asChild,\n    className,\n    onClick: onClickProp,\n    onMouseEnter: onMouseEnterProp,\n    onMouseLeave: onMouseLeaveProp,\n    onKeyDown: onKeyDownProp,\n    ...rootProps\n  } = props;\n\n  const [swapped, setSwappedState] = useControlled({\n    controlled: swappedProp,\n    default: defaultSwapped ?? false,\n    name: \"Swap\",\n    state: \"swapped\",\n  });\n\n  const isClickMode = activationMode === \"click\";\n\n  function setSwapped(nextSwapped: boolean) {\n    if (nextSwapped === swapped) return;\n    onSwappedChange?.(nextSwapped);\n    setSwappedState(nextSwapped);\n  }\n\n  function onClick(event: React.MouseEvent<RootElement>) {\n    onClickProp?.(event);\n    if (event.defaultPrevented || !isClickMode || disabled) return;\n\n    setSwapped(!swapped);\n  }\n\n  function onMouseEnter(event: React.MouseEvent<RootElement>) {\n    onMouseEnterProp?.(event);\n    if (event.defaultPrevented || activationMode !== \"hover\" || disabled)\n      return;\n\n    setSwapped(true);\n  }\n\n  function onMouseLeave(event: React.MouseEvent<RootElement>) {\n    onMouseLeaveProp?.(event);\n    if (event.defaultPrevented || activationMode !== \"hover\" || disabled)\n      return;\n\n    setSwapped(false);\n  }\n\n  function onKeyDown(event: React.KeyboardEvent<RootElement>) {\n    onKeyDownProp?.(event);\n    if (event.defaultPrevented || !isClickMode || disabled) return;\n\n    if (event.key === \"Enter\" || event.key === \" \") {\n      event.preventDefault();\n      setSwapped(!swapped);\n    }\n  }\n\n  const RootPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <SwapContext.Provider value={swapped}>\n      <RootPrimitive\n        role={isClickMode ? \"button\" : undefined}\n        aria-pressed={isClickMode ? swapped : undefined}\n        aria-disabled={disabled}\n        data-slot=\"swap\"\n        data-animation={animation}\n        data-state={getDataState(swapped)}\n        data-disabled={disabled ? \"\" : undefined}\n        tabIndex={isClickMode && !disabled ? 0 : undefined}\n        {...rootProps}\n        className={cn(\"relative inline-flex cursor-pointer items-center justify-center select-none data-disabled:cursor-not-allowed data-disabled:opacity-50\", className)}\n        onClick={onClick}\n        onMouseEnter={onMouseEnter}\n        onMouseLeave={onMouseLeave}\n        onKeyDown={onKeyDown}\n      />\n    </SwapContext.Provider>\n  );\n}\n\ninterface SwapOnProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n}\n\nfunction SwapOn({ asChild, className, ...props }: SwapOnProps) {\n  const swapped = useSwapContext(ON_NAME);\n\n  const OnPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <OnPrimitive\n      data-slot=\"swap-on\"\n      data-state={getDataState(swapped)}\n      {...props}\n      className={cn(\"transition-all duration-300 data-[state=off]:absolute data-[state=off]:opacity-0 data-[state=on]:opacity-100 motion-reduce:transition-none [*[data-animation=flip]_&]:data-[state=off]:transform-[rotateY(180deg)] [*[data-animation=flip]_&]:data-[state=on]:transform-[rotateY(0deg)] motion-reduce:[*[data-animation=flip]_&]:data-[state=off]:transform-[rotateY(0deg)] [*[data-animation=rotate]_&]:data-[state=off]:rotate-180 [*[data-animation=rotate]_&]:data-[state=on]:rotate-0 motion-reduce:[*[data-animation=rotate]_&]:data-[state=off]:rotate-0 [*[data-animation=scale]_&]:data-[state=off]:scale-0 [*[data-animation=scale]_&]:data-[state=on]:scale-100 motion-reduce:[*[data-animation=scale]_&]:data-[state=off]:scale-100\", className)}\n    />\n  );\n}\n\ninterface SwapOffProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n}\n\nfunction SwapOff({ asChild, className, ...props }: SwapOffProps) {\n  const swapped = useSwapContext(OFF_NAME);\n\n  const OffPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <OffPrimitive\n      data-slot=\"swap-off\"\n      data-state={getDataState(swapped)}\n      {...props}\n      className={cn(\"transition-all duration-300 data-[state=off]:opacity-100 data-[state=on]:absolute data-[state=on]:opacity-0 motion-reduce:transition-none [*[data-animation=flip]_&]:data-[state=off]:transform-[rotateY(0deg)] [*[data-animation=flip]_&]:data-[state=on]:transform-[rotateY(180deg)] motion-reduce:[*[data-animation=flip]_&]:data-[state=on]:transform-[rotateY(0deg)] [*[data-animation=rotate]_&]:data-[state=off]:rotate-0 [*[data-animation=rotate]_&]:data-[state=on]:rotate-180 motion-reduce:[*[data-animation=rotate]_&]:data-[state=on]:rotate-0 [*[data-animation=scale]_&]:data-[state=off]:scale-100 [*[data-animation=scale]_&]:data-[state=on]:scale-0 motion-reduce:[*[data-animation=scale]_&]:data-[state=on]:scale-100\", className)}\n    />\n  );\n}\n\nexport { Swap, SwapOff, SwapOn, type SwapProps, useSwap };\n",
      "target": ""
    }
  ]
}