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

wizard.tsx

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