{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "slider-12",
  "title": "Range slider with values",
  "description": "A range whose two ends are written beside the label.",
  "dependencies": [
    "@base-ui/react",
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/label.json",
    "https://ui.ndekocode.dev/r/slider.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/slider/slider-12.tsx",
      "content": "'use client';\n\nimport type { SliderRootChangeEventDetails } from '@base-ui/react/slider';\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useState } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { Label } from '@/registry/ndeko/ui/label';\nimport { Slider } from '@/registry/ndeko/ui/slider';\n\ntype SliderValue = number | readonly number[];\n\ntype Readout = {\n  value: number;\n  slideKey: number;\n  direction: number;\n};\n\nconst INITIAL_DISCOUNTS = [25, 75];\nconst PERCENT = { style: 'unit', unit: 'percent' } as const;\nconst DIGIT_SHIFT_PX = 8;\n\nconst DIGIT_TRANSITION: Transition = { duration: 0.18, ease: [0.2, 0, 0, 1] };\n\nconst DIGIT_VARIANTS: Variants = {\n  enter: (direction: number) => {\n    const offset = direction * DIGIT_SHIFT_PX;\n    return { opacity: 0, y: offset };\n  },\n  visible: { opacity: 1, y: 0 },\n  exit: (direction: number) => {\n    const offset = direction * DIGIT_SHIFT_PX;\n    return { opacity: 0, y: -offset };\n  },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction digitVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return DIGIT_VARIANTS;\n}\n\nfunction valuesOf(value: SliderValue) {\n  if (typeof value === 'number') {\n    return [value];\n  }\n  return [...value];\n}\n\nfunction directionOf(previous: number, next: number) {\n  if (next < previous) {\n    return -1;\n  }\n  return 1;\n}\n\nfunction readoutOf(value: number): Readout {\n  return { value, slideKey: 0, direction: 1 };\n}\n\nfunction readoutAfter(current: Readout, value: number, shouldSlide: boolean) {\n  const hasChanged = value !== current.value;\n  const isSliding = shouldSlide && hasChanged;\n  if (!isSliding) {\n    return { ...current, value };\n  }\n  const direction = directionOf(current.value, value);\n  const slideKey = current.slideKey + 1;\n  return { value, slideKey, direction };\n}\n\nfunction readoutsAfter(\n  current: readonly Readout[],\n  values: readonly number[],\n  shouldSlide: boolean\n) {\n  return values.map((value, index) =>\n    readoutAfter(current[index], value, shouldSlide)\n  );\n}\n\nfunction valueOfReadout(readout: Readout) {\n  return readout.value;\n}\n\ntype SlidingFigureProps = {\n  readout: Readout;\n  variants: Variants;\n  children: React.ReactNode;\n};\n\nfunction SlidingFigure({ readout, variants, children }: SlidingFigureProps) {\n  return (\n    <span className='relative inline-flex'>\n      <AnimatePresence\n        mode='popLayout'\n        initial={false}\n        custom={readout.direction}\n      >\n        <motion.span\n          key={readout.slideKey}\n          className='inline-flex'\n          custom={readout.direction}\n          variants={variants}\n          initial='enter'\n          animate='visible'\n          exit='exit'\n          transition={DIGIT_TRANSITION}\n        >\n          {children}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\nexport default function Slider12() {\n  const [readouts, setReadouts] = useState(() =>\n    INITIAL_DISCOUNTS.map(readoutOf)\n  );\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const digitVariants = digitVariantsOf(shouldReduceMotion);\n  const discounts = readouts.map(valueOfReadout);\n  const [lowest, highest] = readouts;\n\n  function changeDiscounts(\n    value: SliderValue,\n    details: SliderRootChangeEventDetails\n  ) {\n    const nextDiscounts = valuesOf(value);\n    const shouldSlide = details.reason !== 'drag';\n    setReadouts((current) =>\n      readoutsAfter(current, nextDiscounts, shouldSlide)\n    );\n  }\n\n  return (\n    <div className='flex w-full max-w-xs flex-col gap-3'>\n      <div className='flex items-center justify-between gap-2'>\n        <Label>Discount range</Label>\n        <span\n          className='text-[13px] font-medium tabular-nums'\n          aria-hidden='true'\n        >\n          <SlidingFigure readout={lowest} variants={digitVariants}>\n            {lowest.value}%\n          </SlidingFigure>{' '}\n          to{' '}\n          <SlidingFigure readout={highest} variants={digitVariants}>\n            {highest.value}%\n          </SlidingFigure>\n        </span>\n      </div>\n      <Slider\n        value={discounts}\n        onValueChange={changeDiscounts}\n        format={PERCENT}\n        thumbLabels={['Lowest discount', 'Highest discount']}\n      />\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "slider"
  ],
  "type": "registry:component"
}