"use client";
import React, { useState, Children, useRef, useLayoutEffect } from 'react';
import { motion, AnimatePresence } from 'motion/react';
import './Stepper.css';
export default function Stepper({
children,
initialStep = 1,
onStepChange = () => {},
onFinalStepCompleted = () => {},
stepCircleContainerClassName = '',
stepContainerClassName = '',
contentClassName = '',
footerClassName = '',
backButtonProps = {},
nextButtonProps = {},
backButtonText = 'Back',
nextButtonText = 'Continue',
disableStepIndicators = false,
renderStepIndicator,
...rest
}) {
const [currentStep, setCurrentStep] = useState(initialStep);
const [direction, setDirection] = useState(0);
const stepsArray = Children.toArray(children);
const totalSteps = stepsArray.length;
const isCompleted = currentStep > totalSteps;
const isLastStep = currentStep === totalSteps;
const updateStep = newStep => {
setCurrentStep(newStep);
if (newStep > totalSteps) {
onFinalStepCompleted();
} else {
onStepChange(newStep);
}
};
const handleBack = () => {
if (currentStep > 1) {
setDirection(-1);
updateStep(currentStep - 1);
}
};
const handleNext = () => {
if (!isLastStep) {
setDirection(1);
updateStep(currentStep + 1);
}
};
const handleComplete = () => {
setDirection(1);
updateStep(totalSteps + 1);
};
return (
{stepsArray.map((_, index) => {
const stepNumber = index + 1;
const isNotLastStep = index < totalSteps - 1;
return (
{renderStepIndicator ? (
renderStepIndicator({
step: stepNumber,
currentStep,
onStepClick: clicked => {
setDirection(clicked > currentStep ? 1 : -1);
updateStep(clicked);
}
})
) : (
{
setDirection(clicked > currentStep ? 1 : -1);
updateStep(clicked);
}}
/>
)}
{isNotLastStep && stepNumber} />}
);
})}
{stepsArray[currentStep - 1]}
{!isCompleted && (
{currentStep !== 1 && (
)}
)}
);
}
function StepContentWrapper({ isCompleted, currentStep, direction, children, className }) {
const [parentHeight, setParentHeight] = useState(0);
return (
{!isCompleted && (
setParentHeight(h)}>
{children}
)}
);
}
function SlideTransition({ children, direction, onHeightReady }) {
const containerRef = useRef(null);
useLayoutEffect(() => {
if (containerRef.current) onHeightReady(containerRef.current.offsetHeight);
}, [children, onHeightReady]);
return (
{children}
);
}
const stepVariants = {
enter: dir => ({
x: dir >= 0 ? '-100%' : '100%',
opacity: 0
}),
center: {
x: '0%',
opacity: 1
},
exit: dir => ({
x: dir >= 0 ? '50%' : '-50%',
opacity: 0
})
};
export function Step({ children }) {
return {children}
;
}
function StepIndicator({ step, currentStep, onClickStep, disableStepIndicators }) {
const status = currentStep === step ? 'active' : currentStep < step ? 'inactive' : 'complete';
const handleClick = () => {
if (step !== currentStep && !disableStepIndicators) onClickStep(step);
};
return (
{status === 'complete' ? (
) : status === 'active' ? (
) : (
{step}
)}
);
}
function StepConnector({ isComplete }) {
const lineVariants = {
incomplete: { width: 0, backgroundColor: 'transparent' },
complete: { width: '100%', backgroundColor: '#5227FF' }
};
return (
);
}
function CheckIcon(props) {
return (
);
}