Open History to see the last commit
1import { useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react";
2import { XIcon } from "lucide-react";
3
4import { cn } from "~/lib/utils";
5import { Alert, AlertAction, AlertDescription, AlertTitle } from "../ui/alert";
6import { Button } from "../ui/button";
7
8const DISMISS_TRANSITION_MS = 220;
9const frontExitStyle = {
10 opacity: 0,
11 transform: "translate3d(0, 4rem, 0)",
12} satisfies CSSProperties;
13const stackedExitStyle = {
14 opacity: 0,
15 transform: "translate3d(0, 7rem, 0)",
16} satisfies CSSProperties;
17const restingStyle = {
18 opacity: 1,
19 transform: "none",
20} satisfies CSSProperties;
21const exitTransitionStyle = {
22 transition: `transform ${DISMISS_TRANSITION_MS}ms ease-in, opacity ${DISMISS_TRANSITION_MS}ms ease-in`,
23} satisfies CSSProperties;
24
25// The collapsed cap peeking above the front banner is the only hint that more
26// banners are stacked behind it, so its border must match the severity of the
27// first hidden banner — a neutral banner must not masquerade as a warning.
28const stackCapBorderClass: Record<ComposerBannerStackItem["variant"], string> = {
29 default: "border-border",
30 error: "border-destructive/24",
31 info: "border-info/24",
32 success: "border-success/24",
33 warning: "border-warning/24",
34};
35
36export interface ComposerBannerStackItem {
37 readonly id: string;
38 readonly variant: "default" | "error" | "info" | "success" | "warning";
39 // Ordering hint for stack assemblers: front this banner even though its
40 // variant is calm (e.g. live update progress). The stack itself ignores it.
41 readonly urgent?: boolean;
42 readonly icon: ReactNode;
43 readonly title: ReactNode;
44 readonly description?: ReactNode;
45 readonly actions?: ReactNode;
46 readonly className?: string;
47 readonly actionClassName?: string;
48 readonly dismissLabel?: string;
49 readonly onDismiss?: () => void;
50}
51
52interface ComposerBannerStackProps {
53 readonly className?: string;
54 readonly items: ReadonlyArray<ComposerBannerStackItem>;
55}
56
57export function ComposerBannerStack({ className, items }: ComposerBannerStackProps) {
58 const [requestedExitingItemId, setExitingItemId] = useState<string | null>(null);
59 const dismissTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
60 const exitingItemId =
61 requestedExitingItemId !== null && items.some((item) => item.id === requestedExitingItemId)
62 ? requestedExitingItemId
63 : null;
64
65 useEffect(() => {
66 return () => {
67 if (dismissTimeoutRef.current) {
68 clearTimeout(dismissTimeoutRef.current);
69 }
70 };
71 }, []);
72
73 if (items.length === 0) {
74 return null;
75 }
76
77 const frontItem = items[0];
78 if (!frontItem) {
79 return null;
80 }
81 const stackedItems = items.slice(1);
82 const hasStack = stackedItems.length > 0;
83 const showCollapsedStackCap = hasStack && exitingItemId !== frontItem.id;
84 const firstStackedItem = stackedItems[0];
85
86 const requestDismiss = (item: ComposerBannerStackItem) => {
87 if (!item.onDismiss || exitingItemId) {
88 return;
89 }
90 setExitingItemId(item.id);
91 if (dismissTimeoutRef.current) {
92 clearTimeout(dismissTimeoutRef.current);
93 }
94 dismissTimeoutRef.current = setTimeout(() => {
95 dismissTimeoutRef.current = null;
96 item.onDismiss?.();
97 }, DISMISS_TRANSITION_MS);
98 };
99
100 return (
101 <div className={cn("group/banner-stack mx-auto mb-2 max-w-3xl", className)}>
102 <div
103 className={cn(
104 "relative flex flex-col-reverse",
105 hasStack ? "group-hover/banner-stack:z-50 group-focus-within/banner-stack:z-50" : null,
106 )}
107 >
108 {showCollapsedStackCap && firstStackedItem ? (
109 <div
110 className={cn(
111 "pointer-events-none absolute inset-x-0 -top-3 z-0 mx-auto h-3 rounded-t-[22px]",
112 "border border-b-0 bg-background/96 shadow-[0_6px_18px_rgba(0,0,0,0.06)]",
113 stackCapBorderClass[firstStackedItem.variant],
114 "transition-opacity duration-150 ease-out",
115 "group-hover/banner-stack:opacity-0 group-focus-within/banner-stack:opacity-0",
116 )}
117 style={{ width: "96%" }}
118 aria-hidden="true"
119 />
120 ) : null}
121 <div
122 className={cn(
123 "relative z-10",
124 exitingItemId === frontItem.id ? "pointer-events-none" : null,
125 )}
126 style={{
127 ...exitTransitionStyle,
128 ...(exitingItemId === frontItem.id ? frontExitStyle : restingStyle),
129 }}
130 >
131 <ComposerBannerStackAlert
132 item={frontItem}
133 exiting={exitingItemId === frontItem.id}
134 onDismissRequest={() => requestDismiss(frontItem)}
135 />
136 </div>
137 {hasStack ? (
138 <div
139 data-composer-banner-stack-expanded-items="true"
140 className={cn(
141 "relative z-20 grid grid-rows-[0fr] transition-[grid-template-rows] duration-150 ease-out",
142 "group-hover/banner-stack:grid-rows-[1fr] group-focus-within/banner-stack:grid-rows-[1fr]",
143 )}
144 >
145 <div className="min-h-0 overflow-hidden">
146 <div
147 className={cn(
148 "invisible pointer-events-none space-y-2 pb-2 opacity-0",
149 "translate-y-1 transform-gpu transition-[opacity,transform] duration-150 ease-out will-change-[opacity,transform]",
150 "group-hover/banner-stack:visible group-hover/banner-stack:pointer-events-auto group-hover/banner-stack:translate-y-0 group-hover/banner-stack:opacity-100",
151 "group-focus-within/banner-stack:visible group-focus-within/banner-stack:pointer-events-auto group-focus-within/banner-stack:translate-y-0 group-focus-within/banner-stack:opacity-100",
152 )}
153 >
154 {stackedItems.map((item) => (
155 <div
156 key={item.id}
157 className={cn(exitingItemId === item.id ? "pointer-events-none" : null)}
158 style={{
159 ...exitTransitionStyle,
160 ...(exitingItemId === item.id ? stackedExitStyle : restingStyle),
161 }}
162 >
163 <ComposerBannerStackAlert
164 item={item}
165 exiting={exitingItemId === item.id}
166 onDismissRequest={() => requestDismiss(item)}
167 />
168 </div>
169 ))}
170 </div>
171 </div>
172 </div>
173 ) : null}
174 </div>
175 </div>
176 );
177}
178
179function ComposerBannerStackAlert({
180 item,
181 exiting,
182 onDismissRequest,
183}: {
184 readonly item: ComposerBannerStackItem;
185 readonly exiting: boolean;
186 readonly onDismissRequest: () => void;
187}) {
188 const dismissOnly = item.onDismiss && !item.actions;
189
190 return (
191 <Alert
192 variant={item.variant}
193 className={cn("alert-glass rounded-[22px]", item.className)}
194 data-variant={item.variant}
195 >
196 {item.icon}
197 <AlertTitle>{item.title}</AlertTitle>
198 {item.description ? <AlertDescription>{item.description}</AlertDescription> : null}
199 {item.actions || item.onDismiss ? (
200 <AlertAction
201 className={cn(
202 item.actionClassName,
203 dismissOnly
204 ? "max-sm:col-start-3 max-sm:row-start-1 max-sm:mt-0 max-sm:self-start"
205 : undefined,
206 )}
207 >
208 {item.actions}
209 {item.onDismiss ? (
210 <Button
211 size="icon-xs"
212 variant="ghost"
213 aria-label={item.dismissLabel ?? "Dismiss warning"}
214 disabled={exiting}
215 onClick={onDismissRequest}
216 >
217 <XIcon className="size-3.5" />
218 </Button>
219 ) : null}
220 </AlertAction>
221 ) : null}
222 </Alert>
223 );
224}