Repository navigation
Expand file tree
/
Copy pathComposerBannerStack.tsx
More file actions
377 lines (359 loc) · 13.1 KB
/
Copy pathComposerBannerStack.tsx
File metadata and controls
377 lines (359 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { InfoIcon } from "lucide-react";
import { useEffect, useId, useLayoutEffect, useRef, useState, type ReactNode } from "react";
import { cn } from "~/lib/utils";
import { Button } from "../ui/button";
import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
import { ComposerBanner, type ComposerBannerVariant } from "./ComposerBanner";
import { observeResize } from "~/lib/observeResize";
// Match the duration-220 exit transition before removing a dismissed notice.
const DISMISS_TRANSITION_MS = 220;
export interface ComposerBannerStackItem {
readonly id: string;
readonly variant: ComposerBannerVariant;
readonly priority?: "urgent" | "activity" | "notice";
readonly icon: ReactNode;
readonly title: ReactNode;
readonly description?: ReactNode;
readonly children?: ReactNode;
readonly actions?: ReactNode;
readonly dismissLabel?: string;
readonly onDismiss?: () => void;
}
export type ComposerBannerStackContent = Pick<
ComposerBannerStackItem,
"id" | "variant" | "priority"
> & { readonly content: ReactNode };
type ComposerBannerStackEntry = ComposerBannerStackItem | ComposerBannerStackContent;
function bannerPriority(item: ComposerBannerStackEntry) {
if (item.priority === "activity") {
return 0;
}
if (item.priority === "urgent" || item.variant === "error" || item.variant === "warning") {
return 1;
}
return 2;
}
interface ComposerBannerStackProps {
readonly className?: string;
readonly items: ReadonlyArray<ComposerBannerStackEntry>;
/** Attachments that sit between the stacked notices and the front item, such as the queue.
The stacked notices peek above the whole column, never from between two attachments. */
readonly attachedAbove?: ReactNode;
}
export function ComposerBannerStack({ className, items, attachedAbove }: ComposerBannerStackProps) {
const [stackExpanded, setStackExpanded] = useState(false);
const noticesRef = useRef<HTMLDivElement>(null);
const peekRef = useRef<HTMLButtonElement>(null);
const expandedItemsRef = useRef<HTMLDivElement>(null);
const pendingFocusRef = useRef<"peek" | "notice" | null>(null);
const expandedItemsId = useId();
const [requestedExitingItemId, setExitingItemId] = useState<string | null>(null);
const dismissTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const exitingItemId =
requestedExitingItemId !== null && items.some((item) => item.id === requestedExitingItemId)
? requestedExitingItemId
: null;
useEffect(() => {
return () => {
if (dismissTimeoutRef.current) {
clearTimeout(dismissTimeoutRef.current);
}
};
}, []);
useEffect(() => {
if (items.length < 2) setStackExpanded(false);
}, [items.length]);
useLayoutEffect(() => {
if (stackExpanded && pendingFocusRef.current === "notice") {
pendingFocusRef.current = null;
const firstControl = expandedItemsRef.current?.querySelector<HTMLElement>(
'button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]',
);
(firstControl ?? expandedItemsRef.current)?.focus({ preventScroll: true });
} else if (!stackExpanded && pendingFocusRef.current === "peek") {
pendingFocusRef.current = null;
peekRef.current?.focus({ preventScroll: true });
}
}, [stackExpanded]);
// Activity stays attached. Urgency and severity only order the notices behind it.
const orderedItems = items.toSorted((a, b) => bannerPriority(a) - bannerPriority(b));
const frontItem = orderedItems[0];
if (!frontItem) {
return (
<>
{null}
{attachedAbove}
{null}
</>
);
}
const stackedItems = orderedItems.slice(1);
const hasStack = stackedItems.length > 0;
const showCollapsedStackCap = hasStack && exitingItemId !== frontItem.id;
const firstStackedItem = stackedItems[0];
const requestDismiss = (item: ComposerBannerStackEntry) => {
if (!("onDismiss" in item) || !item.onDismiss || exitingItemId) {
return;
}
setExitingItemId(item.id);
if (dismissTimeoutRef.current) {
clearTimeout(dismissTimeoutRef.current);
}
dismissTimeoutRef.current = setTimeout(() => {
dismissTimeoutRef.current = null;
item.onDismiss?.();
}, DISMISS_TRANSITION_MS);
};
return (
<>
{hasStack ? (
<div
ref={noticesRef}
data-chat-composer-collapsed-controls="true"
className={cn("relative z-20 min-h-3", stackExpanded && "z-50")}
onPointerEnter={(event) => {
if (event.pointerType === "touch") return;
if (document.activeElement === peekRef.current) {
pendingFocusRef.current = "notice";
}
setStackExpanded(true);
}}
onPointerLeave={(event) => {
if (!event.currentTarget.contains(document.activeElement)) setStackExpanded(false);
}}
onBlurCapture={(event) => {
if (
!event.currentTarget.contains(event.relatedTarget) &&
!event.currentTarget.matches(":hover")
) {
setStackExpanded(false);
}
}}
onKeyDown={(event) => {
if (event.key !== "Escape" || !stackExpanded) return;
event.preventDefault();
event.stopPropagation();
pendingFocusRef.current = "peek";
setStackExpanded(false);
}}
>
{showCollapsedStackCap && firstStackedItem ? (
<ComposerBanner.Peek
ref={peekRef}
variant={firstStackedItem.variant}
aria-label="Show other notices"
aria-expanded={stackExpanded}
aria-controls={expandedItemsId}
aria-hidden={stackExpanded || undefined}
tabIndex={stackExpanded ? -1 : 0}
onClick={(event) => {
event.currentTarget.focus({ preventScroll: true });
pendingFocusRef.current = "notice";
setStackExpanded(true);
}}
className={cn(stackExpanded && "pointer-events-none invisible opacity-0")}
/>
) : null}
<div
id={expandedItemsId}
ref={expandedItemsRef}
role="group"
aria-label="Other notices"
tabIndex={-1}
data-composer-banner-stack-expanded-items="true"
className={cn(
"grid transition-[grid-template-rows] duration-150 ease-out focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
stackExpanded ? "grid-rows-[1fr]" : "grid-rows-[0fr]",
)}
>
<div className="min-h-0 overflow-hidden">
<div
className={cn(
"transform-gpu space-y-2 pb-2 transition-[opacity,transform] duration-150 ease-out will-change-[opacity,transform]",
stackExpanded
? "pointer-events-auto visible translate-y-0 opacity-100"
: "pointer-events-none invisible translate-y-1 opacity-0",
)}
>
{stackedItems.map((item) => (
<div
key={item.id}
className={cn(
"transition-[opacity,translate] duration-220 ease-in",
exitingItemId === item.id
? "pointer-events-none translate-y-28 opacity-0"
: "opacity-100",
)}
>
<ComposerBannerStackAlert
item={item}
attached={false}
exiting={exitingItemId === item.id}
onDismissRequest={() => requestDismiss(item)}
/>
</div>
))}
</div>
</div>
</div>
</div>
) : null}
{attachedAbove}
<ComposerBanner.Attachment
className={className}
data-composer-banner-drawer="true"
data-chat-composer-collapsed-controls="true"
>
<div
key={frontItem.id}
className={cn(
"relative z-10 transition-[opacity,translate] duration-220 ease-in",
exitingItemId === frontItem.id
? "pointer-events-none translate-y-16 opacity-0"
: "opacity-100",
)}
onPointerDownCapture={() => {
setStackExpanded(false);
const activeElement = document.activeElement;
if (
activeElement instanceof HTMLElement &&
noticesRef.current?.contains(activeElement)
) {
activeElement.blur();
}
}}
>
<ComposerBannerStackAlert
item={frontItem}
attached
exiting={exitingItemId === frontItem.id}
onDismissRequest={() => requestDismiss(frontItem)}
/>
</div>
</ComposerBanner.Attachment>
</>
);
}
/** Keep full descriptions reachable only when their inline copy is clipped. */
function NoticeDescription({ children }: { children: ReactNode }) {
const descriptionRef = useRef<HTMLSpanElement>(null);
const detailsRef = useRef<HTMLButtonElement>(null);
const [showDetails, setShowDetails] = useState(false);
useLayoutEffect(() => {
const description = descriptionRef.current;
if (!description) return;
const measure = () => {
// Ignore the space taken by the details button itself so it cannot
// sustain its own overflow after the description would otherwise fit.
const recoveredWidth = detailsRef.current ? detailsRef.current.offsetWidth + 4 : 0;
const hidden = getComputedStyle(description).position === "absolute";
setShowDetails(
hidden ||
[description, ...description.querySelectorAll("*")].some(
(element) => element.scrollWidth > element.clientWidth + recoveredWidth,
),
);
};
measure();
const stopObserving = observeResize(description, measure);
// A child can reveal new text without resizing its clipped box.
const mutations = new MutationObserver(measure);
mutations.observe(description, { childList: true, subtree: true, characterData: true });
return () => {
stopObserving();
mutations.disconnect();
};
}, []);
return (
<span className="contents">
<span
ref={descriptionRef}
className="w-0 min-w-0 grow truncate text-muted-foreground @max-[400px]:sr-only"
>
{children}
</span>
{showDetails ? (
<Popover>
<PopoverTrigger
openOnHover
render={
<Button
ref={detailsRef}
size="icon-xs"
variant="ghost-muted"
aria-label="Show notice details"
className="flex-none"
/>
}
>
<InfoIcon />
</PopoverTrigger>
<PopoverPopup
aria-label="Notice details"
tooltipStyle
side="top"
className="max-w-[min(30rem,calc(100vw-2rem))] whitespace-normal wrap-anywhere [--inline-button-text-align:start] [--inline-button-white-space:normal] [&_[data-slot=inline-button]]:max-w-full"
>
<ComposerBanner.Scroll className="max-h-[min(var(--available-height),24rem,40dvh)]">
{children}
</ComposerBanner.Scroll>
</PopoverPopup>
</Popover>
) : null}
</span>
);
}
function ComposerBannerStackAlert({
item,
attached,
exiting,
onDismissRequest,
}: {
readonly item: ComposerBannerStackEntry;
readonly attached: boolean;
readonly exiting: boolean;
readonly onDismissRequest: () => void;
}) {
if ("content" in item) {
return (
<ComposerBanner.Root
density="comfortable"
placement={attached ? "attached" : "floating"}
variant={item.variant}
>
{item.content}
</ComposerBanner.Root>
);
}
return (
<ComposerBanner.Root
role="alert"
placement={attached ? "attached" : "floating"}
variant={item.variant}
density="comfortable"
>
<ComposerBanner.Row layout="wrap-actions">
<ComposerBanner.Icon className="h-(--composer-banner-icon-column) self-start">
{item.icon}
</ComposerBanner.Icon>
<ComposerBanner.Content className="whitespace-nowrap">
<span className="min-w-0 truncate font-medium leading-7 sm:leading-6">{item.title}</span>
{item.description ? <NoticeDescription>{item.description}</NoticeDescription> : null}
</ComposerBanner.Content>
{item.actions || item.onDismiss ? (
<ComposerBanner.Actions>
{item.actions}
{item.onDismiss ? (
<ComposerBanner.Dismiss
aria-label={item.dismissLabel ?? "Dismiss warning"}
disabled={exiting}
onClick={onDismissRequest}
/>
) : null}
</ComposerBanner.Actions>
) : null}
</ComposerBanner.Row>
{item.children ? <ComposerBanner.Children>{item.children}</ComposerBanner.Children> : null}
</ComposerBanner.Root>
);
}