{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "slider",
  "title": "Slider",
  "description": "Astro range controls with data-slot pointer and keyboard input.",
  "dependencies": [
    "@data-slot/core",
    "@data-slot/slider",
    "cn"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "packages/ui/src/components/ui/slider/index.ts",
      "content": "export { default as Slider } from './slider.astro'\nexport { default as SliderValue } from './slider-value.astro'\nexport { createSliderForm } from './slider-form.ts'\nexport { createSliderValue } from './slider-value.ts'\nexport type { SliderProps, SliderValueProps } from './types.ts'\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/index.ts"
    },
    {
      "path": "packages/ui/src/components/ui/slider/slider.astro",
      "content": "---\nimport type { HTMLAttributes } from \"astro/types\"\n\nimport { cn } from \"cn\"\nimport type { SliderProps } from \"./types.ts\"\n\ntype Props = HTMLAttributes<\"div\"> & SliderProps\n\nconst {\n  class: className,\n  defaultValue,\n  min,\n  max,\n  step,\n  largeStep,\n  orientation = \"horizontal\",\n  thumbAlignment = \"edge\",\n  disabled,\n  format,\n  locale,\n  name,\n  form,\n  ...props\n} = Astro.props\n\nconst serializedDefaultValue = Array.isArray(defaultValue)\n  ? [...defaultValue].sort((a, b) => a - b).join(\",\")\n  : defaultValue\nconst formMin = Math.min(min ?? 0, max ?? 100)\nconst formMax = Math.max(min ?? 0, max ?? 100)\nconst formValues = name\n  ? String(serializedDefaultValue ?? formMin).split(\",\")\n  : []\nconst thumbCount = String(serializedDefaultValue ?? \"\").includes(\",\") ? 2 : 1\n---\n\n<div\n  data-slot=\"slider\"\n  data-slider-format={\n    format || locale ? JSON.stringify({ locale, format }) : undefined\n  }\n  data-default-value={serializedDefaultValue}\n  data-min={min}\n  data-max={max}\n  data-step={step}\n  data-large-step={largeStep}\n  data-orientation={orientation}\n  data-thumb-alignment={thumbAlignment}\n  data-disabled={disabled ? \"true\" : undefined}\n  class={cn(\n    \"data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full\",\n    Astro.slots.has(\"default\") && \"flex flex-col gap-3\",\n    className,\n  )}\n  {...props}\n>\n  {formValues.map((value) => (\n    <input\n      data-slot=\"slider-form-input\"\n      type=\"range\"\n      hidden\n      name={name}\n      form={form}\n      min={formMin}\n      max={formMax}\n      step={step && step > 0 ? step : 1}\n      value={value}\n      disabled={disabled}\n    />\n  ))}\n  <slot />\n  <div\n    data-slot=\"slider-control\"\n    data-orientation={orientation}\n    data-disabled={disabled ? \"true\" : undefined}\n    class=\"flex touch-none select-none data-disabled:pointer-events-none data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-44 data-[orientation=horizontal]:w-full data-[orientation=horizontal]:min-w-44 data-[orientation=vertical]:flex-col data-disabled:opacity-64 relative items-center\"\n  >\n    <div data-slot=\"slider-track\" class=\"relative grow select-none before:absolute before:rounded-full before:bg-input data-[orientation=horizontal]:h-1 data-[orientation=vertical]:h-full data-[orientation=horizontal]:w-full data-[orientation=vertical]:w-1 data-[orientation=horizontal]:before:inset-x-0.5 data-[orientation=vertical]:before:inset-x-0 data-[orientation=horizontal]:before:inset-y-0 data-[orientation=vertical]:before:inset-y-0.5\">\n      <div data-slot=\"slider-range\" class=\"select-none rounded-full bg-primary data-[orientation=horizontal]:ms-0.5 data-[orientation=vertical]:mb-0.5 data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full\"></div>\n    </div>\n    {Array.from({ length: thumbCount }).map(() => (\n      <div\n        data-slot=\"slider-thumb\"\n        class=\"block size-5 shrink-0 select-none rounded-full border border-input bg-white not-dark:bg-clip-padding shadow-xs/5 outline-none transition-[box-shadow,scale] before:absolute before:inset-0 before:rounded-full before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:ring-[3px] focus-visible:ring-ring/24 data-dragging:scale-120 sm:size-4 dark:border-background dark:focus-visible:ring-ring/48 [:focus-visible,[data-dragging]]:shadow-none\"\n        aria-label={thumbCount === 1 ? props[\"aria-label\"] : undefined}\n        aria-labelledby={\n          thumbCount === 1 ? props[\"aria-labelledby\"] : undefined\n        }\n        aria-describedby={props[\"aria-describedby\"]}\n      />\n    ))}\n  </div>\n</div>\n\n<script>\n  import { create } from \"@data-slot/slider\"\n  import type { SliderController } from \"@data-slot/slider\"\n  import { bindSliderForms } from \"./slider-form.ts\"\n  import { bindSliderValues } from \"./slider-value.ts\"\n  // Destroy bindings before a view transition so the next page-load binds once.\n  const controllers: Pick<SliderController, \"destroy\">[] = []\n  const initialize = () => {\n    controllers.push(...create(), ...bindSliderForms(), ...bindSliderValues())\n  }\n  initialize()\n  document.addEventListener(\"astro:page-load\", initialize)\n  document.addEventListener(\"astro:before-swap\", () => {\n    for (const controller of controllers) controller.destroy()\n    controllers.length = 0\n  })\n</script>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/slider.astro"
    },
    {
      "path": "packages/ui/src/components/ui/slider/slider-form.ts",
      "content": "import {\n    clearRootBinding,\n    getParts,\n    getRootBinding,\n    getRoots,\n    hasRootBinding,\n    observeFormReset,\n    setRootBinding,\n} from '@data-slot/core'\nimport type { SliderController } from '@data-slot/slider'\n\nimport type { SliderEventDetail } from './types.ts'\n\nconst binding = '@gnostic/slider-form'\n\n/** Keep native form fields in sync with an initialized slider. */\nexport function createSliderForm(root: HTMLElement) {\n    const existing = getRootBinding<{ destroy(): void }>(root, binding)\n    if (existing) return existing\n    const inputs = getParts<HTMLInputElement>(root, 'slider-form-input')\n    const first = inputs[0]\n    const slider = getRootBinding<SliderController>(root, '@data-slot/slider')\n    if (!first || !slider) return\n\n    const values = (value: SliderController['value']) =>\n        Array.isArray(value) ? value : [value]\n    const fallback = inputs.map((input) => ({\n        step: input.step,\n        value: input.defaultValue,\n    }))\n    const initial = values(slider.value)\n    for (const [index, input] of inputs.entries()) {\n        // The slider applies its step before it emits a value. Keep its exact endpoints.\n        input.step = 'any'\n        input.defaultValue = String(initial[index])\n        input.value = input.defaultValue\n    }\n    const syncInputs = () => {\n        const next = values(slider.value)\n        inputs.forEach((input, index) => {\n            input.value = String(next[index])\n        })\n    }\n    const onSliderChange = (event: CustomEvent<SliderEventDetail>) => {\n        if (event.target === root) syncInputs()\n    }\n    const syncSlider = () => {\n        const second = inputs[1]\n        slider.setValue(\n            second\n                ? [first.valueAsNumber, second.valueAsNumber]\n                : first.valueAsNumber,\n        )\n        syncInputs()\n    }\n    const reset = observeFormReset({\n        root,\n        getForm: () => first.form,\n        onReset: syncSlider,\n    })\n    const events = new AbortController()\n    root.addEventListener('slider:change', onSliderChange, {\n        signal: events.signal,\n    })\n    for (const input of inputs) {\n        input.addEventListener('input', syncSlider, { signal: events.signal })\n        input.addEventListener('change', syncSlider, { signal: events.signal })\n    }\n    const controller = {\n        destroy() {\n            if (events.signal.aborted) return\n            events.abort()\n            reset.destroy()\n            inputs.forEach((input, index) => {\n                input.step = fallback[index]!.step\n                input.defaultValue = fallback[index]!.value\n                input.value = input.defaultValue\n            })\n            clearRootBinding(root, binding, controller)\n        },\n    }\n    return setRootBinding(root, binding, controller)\n}\n\nexport function bindSliderForms(scope: ParentNode = document) {\n    return getRoots<HTMLElement>(scope, 'slider')\n        .filter((root) => !hasRootBinding(root, binding))\n        .flatMap((root) => createSliderForm(root) ?? [])\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/slider-form.ts"
    },
    {
      "path": "packages/ui/src/components/ui/slider/slider-value.astro",
      "content": "---\nimport type { HTMLTag, Polymorphic } from \"astro/types\"\nimport { cn } from \"cn\"\nimport type { SliderValueProps } from \"./types.ts\"\n\ntype Props<Tag extends HTMLTag = \"output\"> = Polymorphic<{ as: Tag }> &\n  SliderValueProps\nconst { as: Tag = \"output\", class: className, sliderId, ...props } = Astro.props\n---\n\n<Tag\n  data-slot=\"slider-value\"\n  data-slider-id={sliderId}\n  aria-live=\"off\"\n  class={cn(\"text-sm tabular-nums text-foreground\", className)}\n  {...props}\n>\n  <slot />\n</Tag>\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/slider-value.astro"
    },
    {
      "path": "packages/ui/src/components/ui/slider/slider-value.ts",
      "content": "import {\n    clearRootBinding,\n    ensureId,\n    getParts,\n    getRootBinding,\n    getRoots,\n    hasRootBinding,\n    setRootBinding,\n} from '@data-slot/core'\nimport type { SliderController } from '@data-slot/slider'\n\nimport type { SliderEventDetail, SliderProps } from './types.ts'\n\nconst binding = '@gnostic/slider-value'\n\n// Create once per binding. Slider changes can fire on every pointer move.\nfunction numberFormatter(root: HTMLElement) {\n    if (!root.dataset.sliderFormat) return\n    const { locale, format } = JSON.parse(root.dataset.sliderFormat) as Pick<\n        SliderProps,\n        'locale' | 'format'\n    >\n    return new Intl.NumberFormat(locale, format)\n}\n\nfunction bindThumbFormat(root: HTMLElement) {\n    const key = '@gnostic/slider-format'\n    if (hasRootBinding(root, key)) return\n    const slider = getRootBinding<SliderController>(root, '@data-slot/slider')\n    const formatter = numberFormatter(root)\n    if (!slider || !formatter) return\n    const thumbs = getParts(root, 'slider-thumb')\n    const previous = thumbs.map((thumb) => thumb.getAttribute('aria-valuetext'))\n    const render = (value: SliderController['value']) => {\n        const values = Array.isArray(value) ? value : [value]\n        thumbs.forEach((thumb, index) =>\n            thumb.setAttribute(\n                'aria-valuetext',\n                formatter.format(values[index]),\n            ),\n        )\n    }\n    const update = (event: CustomEvent<SliderEventDetail>) => {\n        if (event.target === root) render(event.detail.value)\n    }\n    render(slider.value)\n    root.addEventListener('slider:change', update)\n    const controller = {\n        destroy() {\n            if (getRootBinding(root, key) !== controller) return\n            root.removeEventListener('slider:change', update)\n            thumbs.forEach((thumb, index) => {\n                const text = previous[index]\n                if (text == null) thumb.removeAttribute('aria-valuetext')\n                else thumb.setAttribute('aria-valuetext', text)\n            })\n            clearRootBinding(root, key, controller)\n        },\n    }\n    return setRootBinding(root, key, controller)\n}\n\n/** Connect a readout to an initialized data-slot slider. */\nexport function createSliderValue(output: HTMLElement) {\n    const existing = getRootBinding<{ destroy(): void }>(output, binding)\n    if (existing) return existing\n    const id = output.dataset.sliderId\n    const root =\n        id === undefined\n            ? output.closest<HTMLElement>('[data-slot=\"slider\"]')\n            : output.ownerDocument.getElementById(id)\n    if (!root) return\n    const slider = getRootBinding<SliderController>(root, '@data-slot/slider')\n    if (!slider) return\n\n    const linkFields =\n        output instanceof HTMLOutputElement && !output.hasAttribute('for')\n    if (linkFields) {\n        const fields = getParts(root, 'slider-form-input')\n        const sources = fields.length ? fields : getParts(root, 'slider-thumb')\n        output.htmlFor.value = sources\n            .map((source) => ensureId(source, 'slider-input'))\n            .join(' ')\n    }\n    const format = numberFormatter(root)?.format ?? String\n    const fallback = Array.from(output.childNodes)\n    const render = (value: SliderController['value']) => {\n        output.textContent = Array.isArray(value)\n            ? value.map(format).join(' – ')\n            : format(value)\n    }\n    const update = (event: CustomEvent<SliderEventDetail>) => {\n        if (event.target === root) render(event.detail.value)\n    }\n    render(slider.value)\n    root.addEventListener('slider:change', update)\n    const controller = {\n        destroy() {\n            if (getRootBinding(output, binding) !== controller) return\n            root.removeEventListener('slider:change', update)\n            if (linkFields) output.removeAttribute('for')\n            output.replaceChildren(...fallback)\n            clearRootBinding(output, binding, controller)\n        },\n    }\n    return setRootBinding(output, binding, controller)\n}\n\nexport function bindSliderValues(scope: ParentNode = document) {\n    const formatted = getRoots<HTMLElement>(scope, 'slider')\n        .filter((root) => root.hasAttribute('data-slider-format'))\n        .flatMap((root) => bindThumbFormat(root) ?? [])\n    const outputs = getRoots<HTMLElement>(scope, 'slider-value')\n        .filter((output) => !hasRootBinding(output, binding))\n        .flatMap((output) => createSliderValue(output) ?? [])\n    return [...formatted, ...outputs]\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/slider-value.ts"
    },
    {
      "path": "packages/ui/src/components/ui/slider/types.ts",
      "content": "import type { SliderOptions } from '@data-slot/slider'\n\n/**\n * Slider configuration rendered as data attributes.\n */\nexport type SliderProps = Pick<\n    SliderOptions,\n    | 'defaultValue'\n    | 'min'\n    | 'max'\n    | 'step'\n    | 'largeStep'\n    | 'orientation'\n    | 'thumbAlignment'\n    | 'disabled'\n> & {\n    /** Number format for the readout and accessible thumb values. */\n    format?: Intl.NumberFormatOptions\n    /** Language tag or ordered list of tags for number formatting. Defaults to the browser locale. */\n    locale?: string | string[]\n    /** Name submitted with each thumb value. Omit to exclude the slider from form data. */\n    name?: string\n    /** ID of the form that owns the slider. Defaults to the enclosing form. */\n    form?: string\n}\n\n/** Display the current value of a slider. */\nexport interface SliderValueProps {\n    /** ID of the slider when the readout is outside it. Defaults to the nearest parent slider. */\n    sliderId?: string\n}\n\nexport interface SliderEventDetail {\n    value: number | [number, number]\n}\n\ndeclare global {\n    interface HTMLElementEventMap {\n        'slider:change': CustomEvent<SliderEventDetail>\n        'slider:commit': CustomEvent<SliderEventDetail>\n        'slider:set': CustomEvent<SliderEventDetail>\n    }\n}\n",
      "type": "registry:file",
      "target": "~/src/components/ui/slider/types.ts"
    }
  ],
  "meta": {
    "framework": "astro",
    "source": "@gnostic/ui",
    "testedDependencies": {
      "@data-slot/core": "^1.0.2",
      "@data-slot/slider": "^1.1.2",
      "cn": "0.5.2"
    }
  },
  "docs": "https://gnosticai.com/ui/components/slider",
  "categories": [
    "ui"
  ],
  "type": "registry:ui"
}
