1// AlignUI HorizontalStepper v0.0.0
2
3import * as React from 'react';
4import { tv, type VariantProps } from '@/utils/tv';
5import type { PolymorphicComponentProps } from '@/utils/polymorphic';
6import { recursiveCloneChildren } from '@/utils/recursive-clone-children';
7import { cn } from '@/utils/cn';
8import { RiArrowRightSLine } from '@remixicon/react';
9import { Slot } from '@radix-ui/react-slot';
10
11const HORIZONTAL_STEPPER_ROOT_NAME = 'HorizontalStepperRoot';
12const HORIZONTAL_STEPPER_SEPARATOR_NAME = 'HorizontalStepperSeparator';
13const HORIZONTAL_STEPPER_ITEM_NAME = 'HorizontalStepperItem';
14const HORIZONTAL_STEPPER_ITEM_INDICATOR_NAME = 'HorizontalStepperItemIndicator';
15
16function HorizontalStepperRoot({
17 asChild,
18 children,
19 className,
20 ...rest
21}: React.HTMLAttributes<HTMLDivElement> & {
22 asChild?: boolean;
23}) {
24 const Component = asChild ? Slot : 'div';
25
26 return (
27 <Component
28 className={cn('flex flex-wrap justify-center gap-4', className)}
29 {...rest}
30 >
31 {children}
32 </Component>
33 );
34}
35HorizontalStepperRoot.displayName = HORIZONTAL_STEPPER_ROOT_NAME;
36
37function HorizontalStepperSeparatorIcon<T extends React.ElementType>({
38 className,
39 as,
40 ...rest
41}: PolymorphicComponentProps<T>) {
42 const Component = as || RiArrowRightSLine;
43
44 return (
45 <Component
46 className={cn('size-5 shrink-0 text-text-soft-400', className)}
47 {...rest}
48 />
49 );
50}
51HorizontalStepperSeparatorIcon.displayName = HORIZONTAL_STEPPER_SEPARATOR_NAME;
52
53const horizontalStepperItemVariants = tv({
54 slots: {
55 root: [
56 // base
57 'flex items-center gap-2 text-paragraph-sm',
58 ],
59 indicator: [
60 // base
61 'flex size-5 shrink-0 items-center justify-center rounded-full text-label-xs',
62 ],
63 },
64 variants: {
65 state: {
66 completed: {
67 root: 'text-text-strong-950',
68 indicator: 'bg-success-base text-static-white',
69 },
70 active: {
71 root: 'text-text-strong-950',
72 indicator: 'bg-primary-base text-static-white',
73 },
74 default: {
75 root: 'text-text-sub-600',
76 indicator:
77 'bg-bg-white-0 text-text-sub-600 ring-1 ring-inset ring-stroke-soft-200',
78 },
79 },
80 },
81 defaultVariants: {
82 state: 'default',
83 },
84});
85
86type HorizontalStepperItemSharedProps = VariantProps<
87 typeof horizontalStepperItemVariants
88>;
89
90type HorizontalStepperItemProps =
91 React.ButtonHTMLAttributes<HTMLButtonElement> &
92 VariantProps<typeof horizontalStepperItemVariants> & {
93 asChild?: boolean;
94 };
95
96const HorizontalStepperItem = React.forwardRef<
97 HTMLButtonElement,
98 HorizontalStepperItemProps
99>(({ asChild, children, state, className, ...rest }, forwardedRef) => {
100 const uniqueId = React.useId();
101 const Component = asChild ? Slot : 'button';
102 const { root } = horizontalStepperItemVariants({ state });
103
104 const sharedProps: HorizontalStepperItemSharedProps = {
105 state,
106 };
107
108 const extendedChildren = recursiveCloneChildren(
109 children as React.ReactElement[],
110 sharedProps,
111 [HORIZONTAL_STEPPER_ITEM_INDICATOR_NAME],
112 uniqueId,
113 asChild,
114 );
115
116 return (
117 <Component
118 ref={forwardedRef}
119 className={root({ class: className })}
120 {...rest}
121 >
122 {extendedChildren}
123 </Component>
124 );
125});
126HorizontalStepperItem.displayName = HORIZONTAL_STEPPER_ITEM_NAME;
127
128function HorizontalStepperItemIndicator({
129 state,
130 className,
131 children,
132 ...rest
133}: React.HTMLAttributes<HTMLDivElement> & HorizontalStepperItemSharedProps) {
134 const { indicator } = horizontalStepperItemVariants({ state });
135
136 if (state === 'completed') {
137 return (
138 <div className={indicator({ class: className })} {...rest}>
139 <svg viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg' fill='none'>
140 <path
141 fill='currentColor'
142 d='M15.1 7.453 8.726 13.82 4.9 10l1.275-1.274 2.55 2.548 5.1-5.094L15.1 7.453Z'
143 />
144 </svg>
145 </div>
146 );
147 }
148
149 return (
150 <div className={indicator({ class: className })} {...rest}>
151 {children}
152 </div>
153 );
154}
155HorizontalStepperItemIndicator.displayName =
156 HORIZONTAL_STEPPER_ITEM_INDICATOR_NAME;
157
158export {
159 HorizontalStepperRoot as Root,
160 HorizontalStepperSeparatorIcon as SeparatorIcon,
161 HorizontalStepperItem as Item,
162 HorizontalStepperItemIndicator as ItemIndicator,
163};
164