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-[var(--chat-composer-attached-outline)]",
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
102 className={cn("group/banner-stack chat-composer-drawer-slot", className)}
103 data-composer-banner-drawer="true"
104 >
105 <div
106 className={cn(
107 "relative flex flex-col-reverse",
108 hasStack ? "group-hover/banner-stack:z-50 group-focus-within/banner-stack:z-50" : null,
109 )}
110 >
111 {showCollapsedStackCap && firstStackedItem ? (
112 <div
113 className={cn(
114 "pointer-events-none absolute inset-x-0 -top-3 z-0 mx-auto h-3 rounded-t-2xl",
115 "chat-composer-banner-stack-cap border border-b-0 shadow-[0_6px_18px_rgba(0,0,0,0.06)]",
116 stackCapBorderClass[firstStackedItem.variant],
117 "transition-opacity duration-150 ease-out",
118 "group-hover/banner-stack:opacity-0 group-focus-within/banner-stack:opacity-0",
119 )}
120 style={{ width: "96%" }}
121 aria-hidden="true"
122 />
123 ) : null}
124 <div
125 className={cn(
126 "relative z-10",
127 exitingItemId === frontItem.id ? "pointer-events-none" : null,
128 )}
129 style={{
130 ...exitTransitionStyle,
131 ...(exitingItemId === frontItem.id ? frontExitStyle : restingStyle),
132 }}
133 >
134 <ComposerBannerStackAlert
135 item={frontItem}
136 attached
137 exiting={exitingItemId === frontItem.id}
138 onDismissRequest={() => requestDismiss(frontItem)}
139 />
140 </div>
141 {hasStack ? (
142 <div
143 data-composer-banner-stack-expanded-items="true"
144 className={cn(
145 "relative z-20 grid grid-rows-[0fr] transition-[grid-template-rows] duration-150 ease-out",
146 "group-hover/banner-stack:grid-rows-[1fr] group-focus-within/banner-stack:grid-rows-[1fr]",
147 )}
148 >
149 <div className="min-h-0 overflow-hidden">
150 <div
151 className={cn(
152 "invisible pointer-events-none space-y-2 pb-2 opacity-0",
153 "translate-y-1 transform-gpu transition-[opacity,transform] duration-150 ease-out will-change-[opacity,transform]",
154 "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",
155 "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",
156 )}
157 >
158 {stackedItems.map((item) => (
159 <div
160 key={item.id}
161 className={cn(exitingItemId === item.id ? "pointer-events-none" : null)}
162 style={{
163 ...exitTransitionStyle,
164 ...(exitingItemId === item.id ? stackedExitStyle : restingStyle),
165 }}
166 >
167 <ComposerBannerStackAlert
168 item={item}
169 attached={false}
170 exiting={exitingItemId === item.id}
171 onDismissRequest={() => requestDismiss(item)}
172 />
173 </div>
174 ))}
175 </div>
176 </div>
177 </div>
178 ) : null}
179 </div>
180 </div>
181 );
182}
183
184function ComposerBannerStackAlert({
185 item,
186 attached,
187 exiting,
188 onDismissRequest,
189}: {
190 readonly item: ComposerBannerStackItem;
191 readonly attached: boolean;
192 readonly exiting: boolean;
193 readonly onDismissRequest: () => void;
194}) {
195 const dismissOnly = item.onDismiss && !item.actions;
196
197 return (
198 <Alert
199 variant={item.variant}
200 className={cn(
201 attached
202 ? "chat-composer-drawer-surface chat-composer-drawer-attached px-3 pt-2 pb-[calc(var(--chat-composer-attachment-overlap)_+_0.375rem)] text-xs sm:px-4"
203 : "alert-glass rounded-[22px]",
204 item.className,
205 )}
206 data-variant={item.variant}
207 >
208 {item.icon}
209 <AlertTitle>{item.title}</AlertTitle>
210 {item.description ? <AlertDescription>{item.description}</AlertDescription> : null}
211 {item.actions || item.onDismiss ? (
212 <AlertAction
213 className={cn(
214 item.actionClassName,
215 dismissOnly
216 ? "max-sm:col-start-3 max-sm:row-start-1 max-sm:mt-0 max-sm:self-start"
217 : undefined,
218 )}
219 >
220 {item.actions}
221 {item.onDismiss ? (
222 <Button
223 size="icon-xs"
224 variant="ghost"
225 aria-label={item.dismissLabel ?? "Dismiss warning"}
226 disabled={exiting}
227 onClick={onDismissRequest}
228 >
229 <XIcon className="size-3.5" />
230 </Button>
231 ) : null}
232 </AlertAction>
233 ) : null}
234 </Alert>
235 );
236}