{
  "name": "compare-slider",
  "type": "registry:ui",
  "dependencies": [
    "cn",
    "radix-ui",
    "@base-ui/utils@0.5.0"
  ],
  "files": [
    {
      "path": "ui/compare-slider.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport { clamp } from \"@base-ui/utils/clamp\";\nimport { NOOP } from \"@base-ui/utils/empty\";\nimport { ReactStore, useStore } from \"@base-ui/utils/store\";\nimport { useIsoLayoutEffect } from \"@base-ui/utils/useIsoLayoutEffect\";\nimport { useMergedRefs } from \"@base-ui/utils/useMergedRefs\";\nimport { cn } from \"cn\";\nimport { Slot as SlotPrimitive } from \"radix-ui\";\nimport * as React from \"react\";\n\nimport { IconPlaceholder } from \"@/registry/icons/icon-placeholder\";\n\nconst ROOT_NAME = \"CompareSlider\";\nconst BEFORE_NAME = \"CompareSliderBefore\";\nconst AFTER_NAME = \"CompareSliderAfter\";\nconst LABEL_NAME = \"CompareSliderLabel\";\nconst HANDLE_NAME = \"CompareSliderHandle\";\n\nconst PAGE_KEYS = [\"PageUp\", \"PageDown\"];\nconst ARROW_KEYS = [\"ArrowUp\", \"ArrowDown\", \"ArrowLeft\", \"ArrowRight\"];\n\ntype Interaction = \"hover\" | \"drag\";\ntype Orientation = \"horizontal\" | \"vertical\";\n\ntype RootElement = HTMLDivElement;\n\ninterface StoreState {\n  value: number;\n}\n\ninterface StoreContext {\n  isValueControlled: boolean;\n  onValueChange: (value: number) => void;\n}\n\nconst selectors = {\n  value: (state: StoreState) => state.value,\n};\n\ntype Store = ReturnType<typeof createStore>;\n\nfunction createStore(state: StoreState) {\n  const store = new ReactStore<StoreState, StoreContext, typeof selectors>(\n    state,\n    { isValueControlled: false, onValueChange: NOOP },\n    selectors,\n  );\n\n  function setContext(context: Partial<StoreContext>) {\n    Object.assign(store.context, context);\n  }\n\n  function setValue(value: number) {\n    if (Object.is(store.state.value, value)) return;\n    store.context.onValueChange(value);\n    if (!store.context.isValueControlled) store.set(\"value\", value);\n  }\n\n  return Object.assign(store, { setContext, setValue });\n}\n\nconst StoreContext = React.createContext<Store | null>(null);\n\nfunction useStoreContext(consumerName: string) {\n  const context = React.useContext(StoreContext);\n  if (!context) {\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${ROOT_NAME}\\``);\n  }\n  return context;\n}\n\ninterface CompareSliderContextValue {\n  interaction: Interaction;\n  orientation: Orientation;\n}\n\nconst CompareSliderContext =\n  React.createContext<CompareSliderContextValue | null>(null);\n\nfunction useCompareSliderContext(consumerName: string) {\n  const context = React.useContext(CompareSliderContext);\n  if (!context) {\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${ROOT_NAME}\\``);\n  }\n  return context;\n}\n\ninterface CompareSliderProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n  value?: number;\n  defaultValue?: number;\n  onValueChange?: (value: number) => void;\n  step?: number;\n  interaction?: Interaction;\n  orientation?: Orientation;\n}\n\nfunction CompareSlider(props: CompareSliderProps) {\n  const {\n    value: valueProp,\n    defaultValue = 50,\n    onValueChange,\n    step = 1,\n    interaction = \"drag\",\n    orientation = \"horizontal\",\n    className,\n    children,\n    ref,\n    onPointerMove: onPointerMoveProp,\n    onPointerUp: onPointerUpProp,\n    onPointerDown: onPointerDownProp,\n    onPointerCancel: onPointerCancelProp,\n    onKeyDown: onKeyDownProp,\n    asChild,\n    ...rootProps\n  } = props;\n\n  const [store] = React.useState(() =>\n    createStore({ value: clamp(valueProp ?? defaultValue, 0, 100) }),\n  );\n\n  const isValueControlled = valueProp !== undefined;\n\n  useIsoLayoutEffect(() => {\n    store.setContext({\n      isValueControlled,\n      onValueChange: onValueChange ?? NOOP,\n    });\n  });\n\n  useIsoLayoutEffect(() => {\n    if (valueProp === undefined) return;\n    store.set(\"value\", clamp(valueProp, 0, 100));\n  }, [store, valueProp]);\n\n  const rootRef = React.useRef<RootElement | null>(null);\n  const composedRef = useMergedRefs(ref, rootRef);\n  const isDraggingRef = React.useRef(false);\n\n  const value = useStore(store, selectors.value);\n\n  const onPointerMove = React.useCallback(\n    (event: React.PointerEvent<RootElement>) => {\n      if (!isDraggingRef.current && interaction === \"drag\") {\n        return;\n      }\n      if (!rootRef.current) return;\n\n      onPointerMoveProp?.(event);\n      if (event.defaultPrevented) return;\n\n      const rootRect = rootRef.current.getBoundingClientRect();\n      const isVertical = orientation === \"vertical\";\n      const position = isVertical\n        ? event.clientY - rootRect.top\n        : event.clientX - rootRect.left;\n      const size = isVertical ? rootRect.height : rootRect.width;\n      const percentage = clamp((position / size) * 100, 0, 100);\n\n      store.setValue(percentage);\n    },\n    [interaction, onPointerMoveProp, orientation, store],\n  );\n\n  const onPointerDown = React.useCallback(\n    (event: React.PointerEvent<RootElement>) => {\n      if (interaction !== \"drag\") return;\n\n      onPointerDownProp?.(event);\n      if (event.defaultPrevented) return;\n\n      event.currentTarget.setPointerCapture(event.pointerId);\n      isDraggingRef.current = true;\n    },\n    [interaction, onPointerDownProp],\n  );\n\n  const onPointerUp = React.useCallback(\n    (event: React.PointerEvent<RootElement>) => {\n      if (interaction !== \"drag\") return;\n\n      onPointerUpProp?.(event);\n      if (event.defaultPrevented) return;\n\n      event.currentTarget.releasePointerCapture(event.pointerId);\n      isDraggingRef.current = false;\n    },\n    [interaction, onPointerUpProp],\n  );\n\n  const onPointerCancel = React.useCallback(\n    (event: React.PointerEvent<RootElement>) => {\n      onPointerCancelProp?.(event);\n      onPointerUp(event);\n    },\n    [onPointerCancelProp, onPointerUp],\n  );\n\n  const onKeyDown = React.useCallback(\n    (event: React.KeyboardEvent<RootElement>) => {\n      onKeyDownProp?.(event);\n      if (event.defaultPrevented) return;\n\n      const currentValue = store.state.value;\n      const isVertical = orientation === \"vertical\";\n\n      if (event.key === \"Home\") {\n        event.preventDefault();\n        store.setValue(0);\n      } else if (event.key === \"End\") {\n        event.preventDefault();\n        store.setValue(100);\n      } else if (PAGE_KEYS.concat(ARROW_KEYS).includes(event.key)) {\n        event.preventDefault();\n\n        const isPageKey = PAGE_KEYS.includes(event.key);\n        const isSkipKey =\n          isPageKey || (event.shiftKey && ARROW_KEYS.includes(event.key));\n        const multiplier = isSkipKey ? 10 : 1;\n\n        let direction = 0;\n        if (isVertical) {\n          const isDecreaseKey = [\"ArrowUp\", \"PageUp\"].includes(event.key);\n          direction = isDecreaseKey ? -1 : 1;\n        } else {\n          const isDecreaseKey = [\"ArrowLeft\", \"PageUp\"].includes(event.key);\n          direction = isDecreaseKey ? -1 : 1;\n        }\n\n        const stepInDirection = step * multiplier * direction;\n        const newValue = clamp(currentValue + stepInDirection, 0, 100);\n        store.setValue(newValue);\n      }\n    },\n    [store, onKeyDownProp, orientation, step],\n  );\n\n  const contextValue = React.useMemo<CompareSliderContextValue>(\n    () => ({\n      interaction,\n      orientation,\n    }),\n    [interaction, orientation],\n  );\n\n  const RootPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <StoreContext.Provider value={store}>\n      <CompareSliderContext.Provider value={contextValue}>\n        <RootPrimitive\n          role=\"slider\"\n          aria-orientation={orientation}\n          aria-valuemax={100}\n          aria-valuemin={0}\n          aria-valuenow={value}\n          data-slot=\"compare-slider\"\n          data-orientation={orientation}\n          {...rootProps}\n          ref={composedRef}\n          tabIndex={0}\n          className={cn(\n            \"relative isolate touch-none overflow-hidden transition-all outline-none select-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50\",\n            orientation === \"horizontal\" ? \"w-full\" : \"h-full\",\n            className,\n          )}\n          onPointerDown={onPointerDown}\n          onPointerMove={onPointerMove}\n          onPointerUp={onPointerUp}\n          onPointerCancel={onPointerCancel}\n          onKeyDown={onKeyDown}\n        >\n          {children}\n        </RootPrimitive>\n      </CompareSliderContext.Provider>\n    </StoreContext.Provider>\n  );\n}\n\ninterface CompareSliderBeforeProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n  label?: string;\n}\n\nfunction CompareSliderBefore(props: CompareSliderBeforeProps) {\n  const { className, children, style, label, asChild, ref, ...beforeProps } =\n    props;\n\n  const value = useStore(useStoreContext(BEFORE_NAME), selectors.value);\n  const { orientation } = useCompareSliderContext(BEFORE_NAME);\n\n  const labelId = React.useId();\n\n  const isVertical = orientation === \"vertical\";\n  const clipPath = isVertical\n    ? `inset(${value}% 0 0 0)`\n    : `inset(0 0 0 ${value}%)`;\n\n  const BeforePrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <BeforePrimitive\n      role=\"img\"\n      aria-labelledby={label ? labelId : undefined}\n      aria-hidden={label ? undefined : \"true\"}\n      data-slot=\"compare-slider-before\"\n      data-orientation={orientation}\n      {...beforeProps}\n      ref={ref}\n      className={cn(\"absolute inset-0 h-full w-full object-cover\", className)}\n      style={{\n        clipPath,\n        ...style,\n      }}\n    >\n      {children}\n      {label && (\n        <CompareSliderLabel id={labelId} side=\"before\">\n          {label}\n        </CompareSliderLabel>\n      )}\n    </BeforePrimitive>\n  );\n}\n\ninterface CompareSliderAfterProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n  label?: string;\n}\n\nfunction CompareSliderAfter(props: CompareSliderAfterProps) {\n  const { className, children, style, label, asChild, ref, ...afterProps } =\n    props;\n\n  const value = useStore(useStoreContext(AFTER_NAME), selectors.value);\n  const { orientation } = useCompareSliderContext(AFTER_NAME);\n\n  const labelId = React.useId();\n\n  const isVertical = orientation === \"vertical\";\n  const clipPath = isVertical\n    ? `inset(0 0 ${100 - value}% 0)`\n    : `inset(0 ${100 - value}% 0 0)`;\n\n  const AfterPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <AfterPrimitive\n      role=\"img\"\n      aria-labelledby={label ? labelId : undefined}\n      aria-hidden={label ? undefined : \"true\"}\n      data-slot=\"compare-slider-after\"\n      data-orientation={orientation}\n      {...afterProps}\n      ref={ref}\n      className={cn(\"absolute inset-0 h-full w-full object-cover\", className)}\n      style={{\n        clipPath,\n        ...style,\n      }}\n    >\n      {children}\n      {label && (\n        <CompareSliderLabel id={labelId} side=\"after\">\n          {label}\n        </CompareSliderLabel>\n      )}\n    </AfterPrimitive>\n  );\n}\n\ninterface CompareSliderHandleProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n}\n\nfunction CompareSliderHandle(props: CompareSliderHandleProps) {\n  const { className, children, style, asChild, ref, ...handleProps } = props;\n\n  const value = useStore(useStoreContext(HANDLE_NAME), selectors.value);\n  const { interaction, orientation } = useCompareSliderContext(HANDLE_NAME);\n\n  const isVertical = orientation === \"vertical\";\n\n  const HandlePrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <HandlePrimitive\n      role=\"presentation\"\n      aria-hidden=\"true\"\n      data-slot=\"compare-slider-handle\"\n      data-orientation={orientation}\n      {...handleProps}\n      ref={ref}\n      className={cn(\n        \"absolute z-50 flex items-center justify-center\",\n        isVertical\n          ? \"left-0 h-10 w-full -translate-y-1/2\"\n          : \"top-0 h-full w-10 [translate:-50%_0]\",\n        interaction === \"drag\" && \"cursor-grab active:cursor-grabbing\",\n        className,\n      )}\n      style={{\n        [isVertical ? \"top\" : \"left\"]: `${value}%`,\n        ...style,\n      }}\n    >\n      {children ?? (\n        <>\n          <div\n            className={cn(\n              \"absolute bg-background\",\n              isVertical\n                ? \"top-1/2 h-1 w-full -translate-y-1/2\"\n                : \"left-1/2 h-full w-1 -translate-x-1/2\",\n            )}\n          />\n          {interaction === \"drag\" && (\n            <div className=\"z-50 flex aspect-square size-11 shrink-0 items-center justify-center rounded-full bg-background p-2 [&_svg]:size-4 [&_svg]:stroke-3 [&_svg]:text-muted-foreground [&_svg]:select-none\">\n              {isVertical ? (\n                <div className=\"flex flex-col items-center\">\n                  <IconPlaceholder\n                    lucide=\"ChevronUpIcon\"\n                    tabler=\"IconChevronUp\"\n                    hugeicons=\"ArrowUp01Icon\"\n                    phosphor=\"CaretUpIcon\"\n                    remixicon=\"RiArrowUpSLine\"\n                  />\n                  <IconPlaceholder\n                    lucide=\"ChevronDownIcon\"\n                    tabler=\"IconChevronDown\"\n                    hugeicons=\"ArrowDown01Icon\"\n                    phosphor=\"CaretDownIcon\"\n                    remixicon=\"RiArrowDownSLine\"\n                  />\n                </div>\n              ) : (\n                <div className=\"flex items-center\">\n                  <IconPlaceholder\n                    lucide=\"ChevronLeftIcon\"\n                    tabler=\"IconChevronLeft\"\n                    hugeicons=\"ArrowLeft01Icon\"\n                    phosphor=\"CaretLeftIcon\"\n                    remixicon=\"RiArrowLeftSLine\"\n                  />\n                  <IconPlaceholder\n                    lucide=\"ChevronRightIcon\"\n                    tabler=\"IconChevronRight\"\n                    hugeicons=\"ArrowRight01Icon\"\n                    phosphor=\"CaretRightIcon\"\n                    remixicon=\"RiArrowRightSLine\"\n                  />\n                </div>\n              )}\n            </div>\n          )}\n        </>\n      )}\n    </HandlePrimitive>\n  );\n}\n\ninterface CompareSliderLabelProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n  side?: \"before\" | \"after\";\n}\n\nfunction CompareSliderLabel(props: CompareSliderLabelProps) {\n  const { className, children, side, asChild, ref, ...labelProps } = props;\n\n  const { orientation } = useCompareSliderContext(LABEL_NAME);\n  const isVertical = orientation === \"vertical\";\n\n  const LabelPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <LabelPrimitive\n      ref={ref}\n      data-slot=\"compare-slider-label\"\n      className={cn(\n        \"absolute z-20 rounded-md border border-border bg-background/80 px-3 py-1.5 text-xs font-medium backdrop-blur-sm\",\n        isVertical\n          ? side === \"before\"\n            ? \"top-2 left-2\"\n            : \"bottom-2 left-2\"\n          : side === \"before\"\n            ? \"top-2 left-2\"\n            : \"top-2 right-2\",\n        className,\n      )}\n      {...labelProps}\n    >\n      {children}\n    </LabelPrimitive>\n  );\n}\n\nexport {\n  CompareSlider,\n  CompareSliderAfter,\n  CompareSliderBefore,\n  CompareSliderHandle,\n  CompareSliderLabel,\n  type CompareSliderProps,\n};\n",
      "target": ""
    }
  ]
}