{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"velocity-tabs","title":"Velocity Tabs","author":"CoDesign-Spa27","description":"An accessible tab selector with a spring indicator that stretches in response to movement velocity.","dependencies":["motion"],"files":[{"path":"components/ui-components/velocity-tabs.tsx","content":"\"use client\";\n\nimport {\n    motion,\n    useMotionValue,\n    useReducedMotion,\n    useSpring,\n    useTransform,\n    useVelocity,\n} from \"motion/react\";\nimport { type ReactNode, useEffect, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport type VelocityTab<T extends string> = {\n    disabled?: boolean;\n    label: ReactNode;\n    value: T;\n};\n\nexport type VelocityTabsProps<T extends string> = {\n    \"aria-label\"?: string;\n    className?: string;\n    defaultValue?: T;\n    indicatorClassName?: string;\n    onValueChange?: (value: T) => void;\n    tabClassName?: string;\n    tabs: readonly VelocityTab<T>[];\n    value?: T;\n};\n\nconst SPRING = {\n    damping: 34,\n    mass: 0.8,\n    stiffness: 420,\n} as const;\n\nfunction getInitialValue<T extends string>(tabs: readonly VelocityTab<T>[], defaultValue?: T) {\n    if (\n        defaultValue !== undefined &&\n        tabs.some((tab) => tab.value === defaultValue && !tab.disabled)\n    ) {\n        return defaultValue;\n    }\n\n    return tabs.find((tab) => !tab.disabled)?.value;\n}\n\nexport function VelocityTabs<T extends string>({\n    \"aria-label\": ariaLabel = \"Choose a view\",\n    className,\n    defaultValue,\n    indicatorClassName,\n    onValueChange,\n    tabClassName,\n    tabs,\n    value,\n}: VelocityTabsProps<T>) {\n    const [uncontrolledValue, setUncontrolledValue] = useState<T | undefined>(() =>\n        getInitialValue(tabs, defaultValue),\n    );\n    const shouldReduceMotion = useReducedMotion();\n    const selectedValue = value ?? uncontrolledValue;\n    const requestedIndex = tabs.findIndex((tab) => tab.value === selectedValue && !tab.disabled);\n    const selectedIndex =\n        requestedIndex >= 0 ? requestedIndex : tabs.findIndex((tab) => !tab.disabled);\n    const motionIndex = Math.max(0, selectedIndex);\n\n    const targetIndex = useMotionValue(motionIndex);\n    const springIndex = useSpring(targetIndex, SPRING);\n    const velocity = useVelocity(springIndex);\n    const x = useTransform(springIndex, (index) => `${index * 100}%`);\n    const scaleX = useTransform(\n        velocity,\n        [-12, -3, 0, 3, 12],\n        [1.25, 1.08, 1, 1.08, 1.25],\n    );\n\n    const blur = useTransform(\n        velocity,\n        [-12, -3, 0, 3, 12],\n        [1.5, 0.4, 0, 0.4, 1.5],\n    );\n    const filter = useTransform(blur, (amount) => `blur(${amount}px)`);\n\n    useEffect(() => {\n        targetIndex.set(motionIndex);\n    }, [motionIndex, targetIndex]);\n\n    if (tabs.length === 0) {\n        return null;\n    }\n\n    function selectTab(tab: VelocityTab<T>, index: number) {\n        if (tab.disabled || tab.value === selectedValue) {\n            return;\n        }\n\n        if (value === undefined) {\n            setUncontrolledValue(tab.value);\n        }\n\n        targetIndex.set(index);\n        onValueChange?.(tab.value);\n    }\n\n    return (\n        <fieldset\n            aria-label={ariaLabel}\n            className={cn(\"relative grid min-w-0 w-full rounded-lg border bg-accent p-1\", className)}\n            style={{ gridTemplateColumns: `repeat(${tabs.length}, minmax(0, 1fr))` }}\n        >\n            {selectedIndex >= 0 ? (\n                <motion.span\n                    aria-hidden=\"true\"\n                    className={cn(\n                        \"pointer-events-none absolute inset-y-1 left-1 rounded-md bg-primary shadow-sm\",\n                        indicatorClassName,\n                    )}\n                    style={{\n                        filter: shouldReduceMotion ? \"none\" : filter,\n                        scaleX: shouldReduceMotion ? 1 : scaleX,\n                        width: `calc((100% - 8px) / ${tabs.length})`,\n                        x,\n                    }}\n                />\n            ) : null}\n\n            {tabs.map((tab, tabIndex) => {\n                const isActive = tabIndex === selectedIndex && !tab.disabled;\n\n                return (\n                    <button\n                        aria-pressed={isActive}\n                        className={cn(\n                            \"relative z-10 min-w-0 rounded-md px-3 py-1.5 text-sm font-medium outline-none\",\n                            \"transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n                            \"disabled:cursor-not-allowed disabled:opacity-50\",\n                            isActive ? \"text-primary-foreground\" : \"text-muted-foreground hover:text-foreground\",\n                            tabClassName,\n                        )}\n                        disabled={tab.disabled}\n                        key={tab.value}\n                        onClick={() => selectTab(tab, tabIndex)}\n                        type=\"button\"\n                    >\n                        <span className=\"block truncate\">{tab.label}</span>\n                    </button>\n                );\n            })}\n        </fieldset>\n    );\n}\n","type":"registry:ui"}],"categories":["navigation","tabs","animation"],"type":"registry:ui"}