{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "slider-20",
  "title": "Slider with steppers",
  "description": "Monthly credits set by dragging or by minus and plus buttons.",
  "dependencies": [
    "@base-ui/react",
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "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-20.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 { MinusIcon, PlusIcon } from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\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 MIN_CREDITS = 0;\nconst MAX_CREDITS = 200;\nconst CREDIT_STEP = 5;\nconst INITIAL_READOUT: Readout = { value: 100, slideKey: 0, direction: 1 };\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 firstValueOf(value: SliderValue) {\n  if (typeof value === 'number') {\n    return value;\n  }\n  return value[0];\n}\n\nfunction clampCredits(credits: number) {\n  const atLeastMin = Math.max(MIN_CREDITS, credits);\n  return Math.min(MAX_CREDITS, atLeastMin);\n}\n\nfunction directionOf(previous: number, next: number) {\n  if (next < previous) {\n    return -1;\n  }\n  return 1;\n}\n\nfunction readoutAfter(current: Readout, value: number, shouldSlide: boolean) {\n  if (!shouldSlide) {\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 steppedReadout(current: Readout, step: number) {\n  const nextValue = current.value + step;\n  const credits = clampCredits(nextValue);\n  return readoutAfter(current, credits, true);\n}\n\nexport default function Slider20() {\n  const [readout, setReadout] = useState(INITIAL_READOUT);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const digitVariants = digitVariantsOf(shouldReduceMotion);\n  const isAtMin = readout.value === MIN_CREDITS;\n  const isAtMax = readout.value === MAX_CREDITS;\n\n  function changeCredits(\n    value: SliderValue,\n    details: SliderRootChangeEventDetails\n  ) {\n    const nextValue = firstValueOf(value);\n    const shouldSlide = details.reason !== 'drag';\n    setReadout((current) => readoutAfter(current, nextValue, shouldSlide));\n  }\n\n  function decreaseCredits() {\n    setReadout((current) => steppedReadout(current, -CREDIT_STEP));\n  }\n\n  function increaseCredits() {\n    setReadout((current) => steppedReadout(current, CREDIT_STEP));\n  }\n\n  return (\n    <div className='flex w-full max-w-xs flex-col gap-3'>\n      <Label className='gap-1 tabular-nums'>\n        <span className='relative flex' aria-hidden='true'>\n          <AnimatePresence\n            mode='popLayout'\n            initial={false}\n            custom={readout.direction}\n          >\n            <motion.span\n              key={readout.slideKey}\n              className='flex'\n              custom={readout.direction}\n              variants={digitVariants}\n              initial='enter'\n              animate='visible'\n              exit='exit'\n              transition={DIGIT_TRANSITION}\n            >\n              {readout.value}\n            </motion.span>\n          </AnimatePresence>\n        </span>\n        <span>credits a month</span>\n      </Label>\n      <div className='flex items-center gap-4'>\n        <Button\n          variant='outline'\n          size='icon'\n          aria-label='Remove 5 credits'\n          onClick={decreaseCredits}\n          disabled={isAtMin}\n          focusableWhenDisabled\n        >\n          <MinusIcon />\n        </Button>\n        <Slider\n          className='grow'\n          value={readout.value}\n          onValueChange={changeCredits}\n          min={MIN_CREDITS}\n          max={MAX_CREDITS}\n          step={CREDIT_STEP}\n          thumbLabels={['Credits a month']}\n        />\n        <Button\n          variant='outline'\n          size='icon'\n          aria-label='Add 5 credits'\n          onClick={increaseCredits}\n          disabled={isAtMax}\n          focusableWhenDisabled\n        >\n          <PlusIcon />\n        </Button>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "slider"
  ],
  "type": "registry:component"
}