{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "onboarding",
  "title": "Onboarding",
  "description": "A step-by-step flow with a segmented progress bar and CSS-only step transitions",
  "dependencies": [
    "@base-ui/react"
  ],
  "registryDependencies": [
    "button",
    "progress"
  ],
  "files": [
    {
      "path": "registry/onboarding.tsx",
      "content": "\"use client\";\n\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport {\n  createContext,\n  use,\n  useCallback,\n  useEffect,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\nimport type { Dispatch, ReactNode, Ref, SetStateAction } from \"react\";\nimport { flushSync } from \"react-dom\";\n\nimport { Button } from \"@/components/ui/button\";\nimport { Progress } from \"@/components/ui/progress\";\nimport { cn } from \"@/lib/utils\";\n\ninterface UseOnboardingReturn {\n  canGoToNextStep: boolean;\n  canGoToPrevStep: boolean;\n  currentStep: number;\n  goToNextStep: () => void;\n  goToPrevStep: () => void;\n  reset: () => void;\n  setStep: Dispatch<SetStateAction<number>>;\n  totalSteps: number;\n}\n\ntype OnboardingDirection = \"next\" | \"previous\";\n\ninterface OnboardingContextValue {\n  api: UseOnboardingReturn;\n  direction?: OnboardingDirection;\n  onComplete?: () => void;\n}\n\nconst OnboardingContext = createContext<OnboardingContextValue | null>(null);\n\nfunction useOnboardingContext() {\n  const context = use(OnboardingContext);\n  if (!context) {\n    throw new Error(\n      \"useOnboarding must be used within an Onboarding component\"\n    );\n  }\n  return context;\n}\n\nfunction useOnboarding(): UseOnboardingReturn {\n  return useOnboardingContext().api;\n}\n\ninterface OnboardingProps extends React.ComponentProps<\"div\"> {\n  defaultValue?: number;\n  onComplete?: () => void;\n  totalSteps: number;\n}\n\nfunction Onboarding({\n  children,\n  className,\n  defaultValue = 1,\n  onComplete,\n  totalSteps,\n  ...props\n}: OnboardingProps) {\n  const [currentStep, setCurrentStep] = useState(defaultValue);\n  const [direction, setDirection] = useState<OnboardingDirection>();\n  const canGoToNextStep = currentStep < totalSteps;\n  const canGoToPrevStep = currentStep > 1;\n\n  const goToStep = useCallback(\n    (nextStep: number) => {\n      if (nextStep === currentStep) {\n        return;\n      }\n      setDirection(nextStep > currentStep ? \"next\" : \"previous\");\n      setCurrentStep(nextStep);\n    },\n    [currentStep]\n  );\n\n  const setStep = useCallback<Dispatch<SetStateAction<number>>>(\n    (step) => {\n      const nextStep = typeof step === \"function\" ? step(currentStep) : step;\n      if (nextStep < 1 || nextStep > totalSteps) {\n        throw new Error(`Step ${nextStep} is out of range 1–${totalSteps}`);\n      }\n      goToStep(nextStep);\n    },\n    [currentStep, goToStep, totalSteps]\n  );\n\n  const api = useMemo<UseOnboardingReturn>(\n    () => ({\n      canGoToNextStep,\n      canGoToPrevStep,\n      currentStep,\n      goToNextStep: () => {\n        if (canGoToNextStep) {\n          goToStep(currentStep + 1);\n        }\n      },\n      goToPrevStep: () => {\n        if (canGoToPrevStep) {\n          goToStep(currentStep - 1);\n        }\n      },\n      reset: () => goToStep(1),\n      setStep,\n      totalSteps,\n    }),\n    [\n      canGoToNextStep,\n      canGoToPrevStep,\n      currentStep,\n      goToStep,\n      setStep,\n      totalSteps,\n    ]\n  );\n\n  const context = useMemo<OnboardingContextValue>(\n    () => ({ api, direction, onComplete }),\n    [api, direction, onComplete]\n  );\n\n  return (\n    <OnboardingContext value={context}>\n      <div\n        className={cn(\"flex flex-col gap-6\", className)}\n        data-direction={direction}\n        data-slot=\"onboarding\"\n        {...props}\n      >\n        {children}\n      </div>\n    </OnboardingContext>\n  );\n}\n\ninterface OnboardingStepProps extends React.ComponentProps<\"div\"> {\n  value: number;\n}\n\nfunction OnboardingContent(props: React.ComponentProps<\"div\">) {\n  return <div data-slot=\"onboarding-content\" {...props} />;\n}\n\ntype TransitionStatus = \"starting\" | \"ending\" | undefined;\n\nconst runningAnimations = (element: HTMLElement) =>\n  element\n    .getAnimations?.({ subtree: true })\n    .filter(\n      (animation) =>\n        animation.playState === \"running\" &&\n        animation.effect?.getComputedTiming().endTime !==\n          Number.POSITIVE_INFINITY\n    ) ?? [];\n\nfunction assignRef<T>(ref: Ref<T> | undefined, node: T | null) {\n  if (typeof ref === \"function\") {\n    return ref(node);\n  }\n  if (ref) {\n    ref.current = node;\n  }\n}\n\nfunction useStepTransition(isCurrent: boolean) {\n  const ref = useRef<HTMLDivElement>(null);\n  const [mounted, setMounted] = useState(isCurrent);\n  const [status, setStatus] = useState<TransitionStatus>();\n  const [wasCurrent, setWasCurrent] = useState(isCurrent);\n\n  if (isCurrent !== wasCurrent) {\n    setWasCurrent(isCurrent);\n    setMounted(true);\n    if (!isCurrent) {\n      setStatus(\"ending\");\n    } else if (mounted) {\n      setStatus(undefined);\n    } else {\n      setStatus(\"starting\");\n    }\n  }\n\n  useEffect(() => {\n    if (status !== \"starting\") {\n      return;\n    }\n    const frame = requestAnimationFrame(() => setStatus(undefined));\n    return () => cancelAnimationFrame(frame);\n  }, [status]);\n\n  useLayoutEffect(() => {\n    const element = ref.current;\n    if (status !== \"ending\" || !element) {\n      return;\n    }\n    let cancelled = false;\n    const unmountWhenDone = async () => {\n      const unmount = () => {\n        setMounted(false);\n        setStatus(undefined);\n      };\n      let animations = runningAnimations(element);\n      if (animations.length === 0) {\n        unmount();\n        return;\n      }\n      while (animations.length > 0) {\n        await Promise.allSettled(\n          animations.map((animation) => animation.finished)\n        );\n        if (cancelled) {\n          return;\n        }\n        animations = runningAnimations(element);\n      }\n      flushSync(unmount);\n    };\n    void unmountWhenDone();\n    return () => {\n      cancelled = true;\n    };\n  }, [status]);\n\n  return { mounted, ref, status };\n}\n\nfunction OnboardingStep({\n  className,\n  ref: forwardedRef,\n  value,\n  ...props\n}: OnboardingStepProps) {\n  const { currentStep } = useOnboarding();\n  const { direction } = useOnboardingContext();\n  const { mounted, ref, status } = useStepTransition(value === currentStep);\n\n  const setRefs = useCallback(\n    (node: HTMLDivElement | null) => {\n      ref.current = node;\n      return assignRef(forwardedRef, node);\n    },\n    [forwardedRef, ref]\n  );\n\n  if (!mounted) {\n    return null;\n  }\n\n  return (\n    <div\n      className={cn(\"flex flex-col gap-4\", className)}\n      data-direction={direction}\n      data-ending-style={status === \"ending\" ? \"\" : undefined}\n      data-slot=\"onboarding-step\"\n      data-starting-style={status === \"starting\" ? \"\" : undefined}\n      data-step={value}\n      inert={status === \"ending\"}\n      ref={setRefs}\n      {...props}\n    />\n  );\n}\n\nfunction OnboardingMedia({ className, ...props }: React.ComponentProps<\"div\">) {\n  return (\n    <div\n      className={cn(\n        \"flex items-center justify-center overflow-hidden\",\n        className\n      )}\n      data-slot=\"onboarding-media\"\n      {...props}\n    />\n  );\n}\n\nfunction OnboardingHeader({\n  className,\n  ...props\n}: React.ComponentProps<\"div\">) {\n  return (\n    <div\n      className={cn(\"flex flex-col gap-1\", className)}\n      data-slot=\"onboarding-header\"\n      {...props}\n    />\n  );\n}\n\nfunction OnboardingTitle({\n  children,\n  className,\n  ...props\n}: React.ComponentProps<\"h2\">) {\n  return (\n    <h2\n      className={cn(\"text-xl font-bold\", className)}\n      data-slot=\"onboarding-title\"\n      {...props}\n    >\n      {children}\n    </h2>\n  );\n}\n\nfunction OnboardingDescription({\n  className,\n  ...props\n}: React.ComponentProps<\"p\">) {\n  return (\n    <p\n      className={cn(\"text-muted-foreground\", className)}\n      data-slot=\"onboarding-description\"\n      {...props}\n    />\n  );\n}\n\nfunction OnboardingFooter({\n  className,\n  ...props\n}: React.ComponentProps<\"div\">) {\n  return (\n    <div\n      className={cn(\"flex flex-col gap-3\", className)}\n      data-slot=\"onboarding-footer\"\n      {...props}\n    />\n  );\n}\n\nfunction OnboardingIndicator({\n  className,\n  ...props\n}: React.ComponentProps<\"div\">) {\n  const { currentStep, totalSteps } = useOnboarding();\n  const steps = Array.from({ length: totalSteps }, (_, index) => index + 1);\n\n  return (\n    <div\n      aria-label=\"Onboarding progress\"\n      aria-valuemax={totalSteps}\n      aria-valuemin={1}\n      aria-valuenow={currentStep}\n      aria-valuetext={`Step ${currentStep} of ${totalSteps}`}\n      className={cn(\"flex gap-1\", className)}\n      data-slot=\"onboarding-indicator\"\n      role=\"progressbar\"\n      {...props}\n    >\n      {steps.map((step) => (\n        <Progress\n          aria-hidden\n          className=\"flex-1\"\n          key={step}\n          value={step <= currentStep ? 100 : 0}\n        />\n      ))}\n    </div>\n  );\n}\n\nfunction OnboardingNav({ className, ...props }: React.ComponentProps<\"div\">) {\n  return (\n    <div\n      className={cn(\"flex items-center justify-between\", className)}\n      data-slot=\"onboarding-nav\"\n      {...props}\n    />\n  );\n}\n\ninterface OnboardingPrevButtonProps extends React.ComponentProps<\n  typeof Button\n> {\n  children?: ReactNode;\n}\n\nfunction OnboardingPrevButton({\n  children = \"Previous\",\n  ...props\n}: OnboardingPrevButtonProps) {\n  const { goToPrevStep, canGoToPrevStep } = useOnboarding();\n\n  return (\n    <Button\n      data-slot=\"onboarding-prev-button\"\n      {...mergeProps<typeof Button>(\n        {\n          disabled: !canGoToPrevStep,\n          focusableWhenDisabled: true,\n          onClick: goToPrevStep,\n          variant: \"ghost\",\n        },\n        props\n      )}\n    >\n      {children}\n    </Button>\n  );\n}\n\ninterface OnboardingNextButtonProps extends React.ComponentProps<\n  typeof Button\n> {\n  children?: ReactNode;\n  lastLabel?: ReactNode;\n}\n\nfunction OnboardingNextButton({\n  children = \"Continue\",\n  lastLabel = \"Got it\",\n  ...props\n}: OnboardingNextButtonProps) {\n  const { goToNextStep, canGoToNextStep } = useOnboarding();\n  const { onComplete } = useOnboardingContext();\n  const isLastStep = !canGoToNextStep;\n\n  const handleClick = () => {\n    if (isLastStep) {\n      onComplete?.();\n    } else {\n      goToNextStep();\n    }\n  };\n\n  return (\n    <Button\n      data-slot=\"onboarding-next-button\"\n      {...mergeProps<typeof Button>(\n        { onClick: handleClick, variant: \"default\" },\n        props\n      )}\n    >\n      {isLastStep ? lastLabel : children}\n    </Button>\n  );\n}\n\nexport {\n  Onboarding,\n  OnboardingContent,\n  OnboardingDescription,\n  OnboardingFooter,\n  OnboardingHeader,\n  OnboardingIndicator,\n  OnboardingMedia,\n  OnboardingNav,\n  OnboardingNextButton,\n  OnboardingPrevButton,\n  OnboardingStep,\n  OnboardingTitle,\n  useOnboarding,\n};\nexport type { UseOnboardingReturn };\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}