Stepper
formcn/stepperFreeComponent
Stepper component
Install it
npx shadcn@latest add https://formcn.dev/r/stepper.jsonSource
1import { Slot } from '@radix-ui/react-slot'2import { CheckIcon, LoaderCircleIcon } from 'lucide-react'3import * as React from 'react'4import { createContext, useContext } from 'react'56import { cn } from '@/lib/utils'78// Types9type StepperContextValue = {10 activeStep: number11 setActiveStep: (step: number) => void12 orientation: 'horizontal' | 'vertical'13}1415type StepItemContextValue = {16 step: number17 state: StepState18 isDisabled: boolean19 isLoading: boolean20}2122type StepState = 'active' | 'completed' | 'inactive' | 'loading'2324// Contexts25const StepperContext = createContext<StepperContextValue | undefined>(undefined)26const StepItemContext = createContext<StepItemContextValue | undefined>(27 undefined,28)2930const useStepper = () => {31 const context = useContext(StepperContext)32 if (!context) {33 throw new Error('useStepper must be used within a Stepper')34 }35 return context36}3738const useStepItem = () => {39 const context = useContext(StepItemContext)40 if (!context) {41 throw new Error('useStepItem must be used within a StepperItem')42 }43 return context44}4546// Components47interface StepperProps extends React.HTMLAttributes<HTMLDivElement> {48 defaultValue?: number49 value?: number50 onValueChange?: (value: number) => void51 orientation?: 'horizontal' | 'vertical'52}5354function Stepper({55 defaultValue = 0,56 value,57 onValueChange,58 orientation = 'horizontal',59 className,60 ...props61}: StepperProps) {62 const [activeStep, setInternalStep] = React.useState(defaultValue)6364 const setActiveStep = React.useCallback(65 (step: number) => {66 if (value === undefined) {67 setInternalStep(step)68 }69 onValueChange?.(step)70 },71 [value, onValueChange],72 )7374 const currentStep = value ?? activeStep7576 return (77 <StepperContext.Provider78 value={{79 activeStep: currentStep,80 orientation,81 setActiveStep,82 }}83 >84 <div85 className={cn(86 'group/stepper inline-flex data-[orientation=horizontal]:w-full data-[orientation=horizontal]:flex-row data-[orientation=vertical]:flex-col',87 className,88 )}89 data-orientation={orientation}90 data-slot="stepper"91 {...props}92 />93 </StepperContext.Provider>94 )95}9697// StepperItem98interface StepperItemProps extends React.HTMLAttributes<HTMLDivElement> {99 step: number100 completed?: boolean101 disabled?: boolean102 loading?: boolean103}104105function StepperItem({106 step,107 completed = false,108 disabled = false,109 loading = false,110 className,111 children,112 ...props113}: StepperItemProps) {114 const { activeStep } = useStepper()115116// … truncated
Files it writes
More from formcn
All 9 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| File Inputfile-upload | formcn | Components | Free | no deps | |
| Multi-selectmulti-select | formcn | Components | Free | no deps | |
| Multi-step formmulti-step-viewer | formcn | Components | Free | 1 dep | |
| Passwordpassword | formcn | Components | Free | no deps | |
| Ratingrating | formcn | Components | Free | no deps | |
| Tag Inputtag-input | formcn | Components | Free | 1 dep |
