Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

wizard.tsx

146 lines4.8 KB

1import { CheckIcon } from "lucide-react";
2import type { ComponentProps, ReactNode } from "react";
3​
4import { cn } from "../../lib/utils";
5import { AnimatedHeight } from "../AnimatedHeight";
6import { DialogPopup, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "./dialog";
7​
8/** Compose a wizard from its header, panel, and footer; flow logic stays with the caller. */
9export function WizardPopup({
10 children,
11 ...props
12}: Omit<ComponentProps<typeof DialogPopup>, "className" | "style">) {
13 return (
14 <DialogPopup {...props} className="max-w-xl overflow-x-hidden overflow-y-auto">
15 <div className="flex min-h-0 flex-col">{children}</div>
16 </DialogPopup>
17 );
19​
20export function WizardHeader({
21 title,
22 description,
23 identity,
24 children,
25}: {
26 readonly title: ReactNode;
27 readonly description?: ReactNode;
28 /** Optional branding shown in place of the visible title. The title remains accessible. */
29 readonly identity?: ReactNode;
30 readonly children?: ReactNode;
31}) {
32 return (
33 <DialogHeader>
34 <DialogTitle className={identity ? "sr-only" : undefined}>{title}</DialogTitle>
35 {identity}
36 {description ? <DialogDescription>{description}</DialogDescription> : null}
37 {children}
38 </DialogHeader>
39 );
41​
42export function WizardFooter({
43 children,
44 leading,
45}: {
46 readonly children: ReactNode;
47 readonly leading?: ReactNode;
48}) {
49 return (
50 <DialogFooter variant="bare" className={leading ? "sm:justify-between" : undefined}>
51 {leading}
52 {leading ? (
53 <div className="flex flex-col-reverse gap-2 sm:flex-row">{children}</div>
54 ) : (
55 children
56 )}
57 </DialogFooter>
58 );
60​
61export function WizardSteps({
62 steps,
63 currentStep,
64 summaries,
65 showSummaries = false,
66 onStepChange,
67 isStepDisabled,
68}: {
69 readonly steps: readonly string[];
70 readonly currentStep: number;
71 readonly summaries?: readonly (string | null)[];
72 readonly showSummaries?: boolean;
73 readonly isStepDisabled?: (step: number) => boolean;
74 readonly onStepChange?: (step: number) => void;
75}) {
76 const Step = onStepChange ? "button" : "div";
77 return (
78 <ol
79 className="grid auto-cols-fr grid-flow-col gap-1 rounded-xl bg-zinc-25 p-1 ring-1 ring-black/5 dark:bg-white/4 dark:ring-white/5"
80 role="list"
81 aria-label="Setup progress"
82 >
83 {steps.map((step, index) => (
84 <li key={step} className="min-w-0">
85 <Step
86 {...(onStepChange
87 ? { type: "button" as const, disabled: isStepDisabled?.(index) }
88 : {})}
89 className={cn(
90 "flex w-full min-w-0 items-center gap-2 rounded-lg px-2.5 py-2 text-left outline-none focus-visible:ring-2 focus-visible:ring-ring max-sm:justify-center max-sm:px-2",
91 onStepChange &&
92 "cursor-pointer hover:bg-card disabled:cursor-default disabled:hover:bg-transparent",
93 index === currentStep &&
94 "bg-card text-foreground shadow-xs ring-1 ring-black/5 hover:bg-card dark:shadow-none dark:ring-white/5",
95 )}
96 aria-current={index === currentStep ? "step" : undefined}
97 aria-label={`${step}, step ${index + 1}${index < currentStep && summaries?.[index] ? `, ${summaries?.[index]}` : ""}`}
98 onClick={onStepChange ? () => onStepChange(index) : undefined}
99 >
100 <span
101 className={cn(
102 "grid size-5 shrink-0 place-items-center rounded-full text-sm font-medium ring-1",
103 index < currentStep
104 ? "bg-primary text-primary-foreground ring-primary"
105 : index === currentStep
106 ? "bg-primary/10 text-primary ring-primary/30"
107 : "bg-card text-muted-foreground ring-black/10 dark:bg-white/5 dark:ring-white/10",
108 )}
109 aria-hidden
110 >
111 {index < currentStep ? <CheckIcon className="size-4 shrink-0" /> : index + 1}
112 </span>
113 <span
114 className={cn(
115 "min-w-0 truncate text-sm font-medium max-sm:hidden",
116 index === currentStep ? "text-foreground" : "text-muted-foreground",
117 )}
118 >
119 {step}
120 {showSummaries && index < currentStep && summaries?.[index]
121 ? `: ${summaries[index]}`
122 : null}
123 </span>
124 </Step>
125 </li>
126 ))}
127 </ol>
128 );
130​
131export function WizardPanel({
132 children,
133 holdHeight = false,
134}: {
135 readonly children: ReactNode;
136 readonly holdHeight?: boolean;
137}) {
138 return (
139 <div
140 data-slot="dialog-panel"
141 className="min-w-0 space-y-4 bg-zinc-25/80 px-6 py-5 ring-1 ring-black/5 dark:bg-white/2 dark:ring-white/5"
142 >
143 <AnimatedHeight holdHeight={holdHeight}>{children}</AnimatedHeight>
144 </div>
145 );