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

wizard.tsx

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