-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOverlayContainer.tsx
More file actions
564 lines (483 loc) · 16.7 KB
/
OverlayContainer.tsx
File metadata and controls
564 lines (483 loc) · 16.7 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import {
FocusEvent,
Key,
RefObject,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useEvent } from '../../../_internal';
import { tasty } from '../../../tasty';
import { useLayoutEffect } from '../../../utils/react/useLayoutEffect';
import { DisplayTransition } from '../../helpers/DisplayTransition/DisplayTransition';
import { Portal } from '../../portal';
import { ToastItem } from '../Toast/ToastItem';
import { NotificationItem } from './NotificationItem';
import type { InternalToast } from '../Toast/types';
import type { DismissReason, InternalNotification } from './types';
// ─── Constants ───────────────────────────────────────────────────────
const COLLAPSE_VISIBLE_HEIGHT = 10;
const CONTAINER_OFFSET = 16;
const ITEM_GAP = 8;
const DEFAULT_ITEM_HEIGHT = 56;
// ─── Styled Elements ─────────────────────────────────────────────────
const OverlayContainerElement = tasty({
styles: {
position: 'fixed',
top: '2x',
left: '50%',
transform: 'translateX(-50%)',
zIndex: 100,
padding: '1x',
height: '0',
pointerEvents: 'none',
},
});
const OverlayItemWrapper = tasty({
styles: {
position: 'absolute',
top: '0',
left: '50%',
transform: {
'': 'translateX(-50%) translateY(-50%)',
'isMeasured & isShown': 'translateX(-50%) translateY(0)',
},
width: 'max-content 50x',
pointerEvents: 'auto',
transition: {
'': 'opacity $transition ease-in, transform $transition ease-in',
isMeasured:
'top $transition ease-in, opacity $transition ease-in, transform $transition ease-in',
},
opacity: {
'': 0,
isShown: 1,
},
},
});
// ─── Unified Item Type ───────────────────────────────────────────────
type OverlayItem =
| { kind: 'toast'; data: InternalToast }
| { kind: 'notification'; data: InternalNotification };
function getItemId(item: OverlayItem): string {
return item.data.internalId;
}
function isItemExiting(item: OverlayItem): boolean {
return item.data.isExiting === true;
}
function getItemCreatedAt(item: OverlayItem): number {
return item.data.createdAt;
}
// ─── useItemPositions Hook ───────────────────────────────────────────
interface ItemPositionsResult {
heights: Record<string, number>;
/** Set of item IDs that have been measured AND painted at their correct position. */
settledIds: Set<string>;
positions: Map<string, number>;
lastPositionsRef: ReturnType<typeof useRef<Map<string, number>>>;
itemRefs: ReturnType<typeof useRef<Map<string, HTMLDivElement>>>;
createRefCallback: (
itemId: string,
displayRef: (el: HTMLElement | null) => void,
) => (el: HTMLDivElement | null) => void;
}
/**
* Manages height measurement, position calculation, and ref tracking
* for overlay items. Extracted from OverlayContainer for readability.
*/
function useItemPositions(visibleItems: OverlayItem[]): ItemPositionsResult {
const [heights, setHeights] = useState<Record<string, number>>({});
const itemRefs = useRef<Map<string, HTMLDivElement>>(new Map());
const lastPositionsRef = useRef<Map<string, number>>(new Map());
// Items that have been measured AND painted at their correct position.
// Lags one frame behind `heights` so the browser paints the item at
// its final `top` before the CSS `top` transition is enabled.
const [settledIds, setSettledIds] = useState<Set<string>>(new Set());
const rafRef = useRef<number | null>(null);
// Measure heights using layout effect to avoid visible layout shifts.
// Runs every render but bails early if nothing changed.
useLayoutEffect(() => {
const newHeights: Record<string, number> = {};
let hasChanges = false;
itemRefs.current.forEach((el, id) => {
const height = el.offsetHeight || DEFAULT_ITEM_HEIGHT;
newHeights[id] = height;
if (heights[id] !== height) {
hasChanges = true;
}
});
for (const id of Object.keys(heights)) {
if (!itemRefs.current.has(id)) {
hasChanges = true;
}
}
if (hasChanges) {
setHeights(newHeights);
}
});
// After heights change, schedule settledIds update for the next frame.
// This ensures the item is painted at its correct position (with no top
// transition) before we enable the transition.
useEffect(() => {
const heightKeys = Object.keys(heights);
const newIds = heightKeys.filter((id) => !settledIds.has(id));
const hasStaleIds =
settledIds.size > 0 && [...settledIds].some((id) => !(id in heights));
if (newIds.length === 0 && !hasStaleIds) return;
if (rafRef.current != null) {
cancelAnimationFrame(rafRef.current);
}
// For pruning-only updates (no new items), apply synchronously since
// there's no need to wait for a paint frame.
if (newIds.length === 0 && hasStaleIds) {
setSettledIds((prev) => new Set(heightKeys.filter((id) => prev.has(id))));
return;
}
rafRef.current = requestAnimationFrame(() => {
rafRef.current = null;
setSettledIds((prev) => {
const next = new Set<string>();
// Only keep IDs that still exist in heights (prune removed items)
for (const id of heightKeys) {
if (prev.has(id)) {
next.add(id);
}
}
for (const id of newIds) {
next.add(id);
}
return next;
});
});
return () => {
if (rafRef.current != null) {
cancelAnimationFrame(rafRef.current);
rafRef.current = null;
}
};
}, [heights]);
const createRefCallback = useCallback(
(itemId: string, displayRef: (el: HTMLElement | null) => void) =>
(el: HTMLDivElement | null) => {
displayRef(el);
if (el) {
itemRefs.current.set(itemId, el);
} else {
itemRefs.current.delete(itemId);
}
},
[],
);
const positions = useMemo(() => {
const posMap = new Map<string, number>();
let currentTop = 0;
for (const item of visibleItems) {
const id = getItemId(item);
posMap.set(id, currentTop);
const height = heights[id] ?? DEFAULT_ITEM_HEIGHT;
currentTop += height + ITEM_GAP;
}
return posMap;
}, [visibleItems, heights]);
useEffect(() => {
positions.forEach((pos, id) => {
lastPositionsRef.current.set(id, pos);
});
}, [positions]);
return {
heights,
settledIds,
positions,
lastPositionsRef,
itemRefs,
createRefCallback,
};
}
// ─── useCollapseState Hook ───────────────────────────────────────────
interface CollapseStateResult {
isCollapsed: boolean;
handleMouseEnter: () => void;
handleMouseLeave: () => void;
handleFocus: () => void;
handleBlur: (e: FocusEvent) => void;
containerRef: RefObject<HTMLDivElement | null>;
}
/**
* Manages collapse/expand behavior and pause state for the overlay container.
* Extracted from OverlayContainer for readability.
*/
function useCollapseState(
canCollapse: boolean,
allItems: OverlayItem[],
itemRefs: ReturnType<typeof useRef<Map<string, HTMLDivElement>>>,
onPauseChange: (paused: boolean) => void,
): CollapseStateResult {
const [isCollapsed, setIsCollapsed] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const boundsRef = useRef<DOMRect | null>(null);
const updateBounds = useCallback(() => {
const refs = itemRefs.current;
if (!refs || refs.size === 0) {
boundsRef.current = null;
return;
}
let minX = Infinity,
minY = Infinity,
maxX = -Infinity,
maxY = -Infinity;
refs.forEach((el) => {
const rect = el.getBoundingClientRect();
minX = Math.min(minX, rect.left);
minY = Math.min(minY, rect.top);
maxX = Math.max(maxX, rect.right);
maxY = Math.max(maxY, rect.bottom);
});
if (typeof DOMRect !== 'undefined') {
boundsRef.current = new DOMRect(minX, minY, maxX - minX, maxY - minY);
} else {
boundsRef.current = {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
top: minY,
right: maxX,
bottom: maxY,
left: minX,
toJSON: () => ({}),
} as DOMRect;
}
}, [itemRefs]);
// Track mouse for collapse — expand when mouse leaves bounds
useEffect(() => {
if (!isCollapsed) return;
const handleMouseMove = (e: MouseEvent) => {
const bounds = boundsRef.current;
if (!bounds) {
setIsCollapsed(false);
onPauseChange(false);
return;
}
const padding = 20;
const isInBounds =
e.clientX >= bounds.left - padding &&
e.clientX <= bounds.right + padding &&
e.clientY >= bounds.top - padding &&
e.clientY <= bounds.bottom + padding;
if (!isInBounds) {
setIsCollapsed(false);
onPauseChange(false);
}
};
window.addEventListener('mousemove', handleMouseMove);
return () => window.removeEventListener('mousemove', handleMouseMove);
}, [isCollapsed, onPauseChange]);
// Update bounds when items change
useEffect(() => {
updateBounds();
}, [allItems, updateBounds]);
const handleMouseEnter = useCallback(() => {
updateBounds();
onPauseChange(true);
if (canCollapse) {
setIsCollapsed(true);
}
}, [updateBounds, canCollapse, onPauseChange]);
const handleMouseLeave = useCallback(() => {
if (!isCollapsed) {
onPauseChange(false);
}
}, [isCollapsed, onPauseChange]);
const handleFocus = useCallback(() => {
onPauseChange(true);
}, [onPauseChange]);
const handleBlur = useCallback(
(e: FocusEvent) => {
if (
containerRef.current &&
!containerRef.current.contains(e.relatedTarget as Node)
) {
onPauseChange(false);
}
},
[onPauseChange],
);
return {
isCollapsed,
handleMouseEnter,
handleMouseLeave,
handleFocus,
handleBlur,
containerRef,
};
}
// ─── Overlay Container ───────────────────────────────────────────────
//
// INTENTIONAL: Position calculations are done manually (absolute positioning + JS
// height tracking) rather than relying on CSS flexbox/grid layout. This is
// deliberate — the container mixes enter/exit animations, collapse behavior, and
// heterogeneous item sizes (toasts vs notifications) which require per-item
// position control. CSS-only solutions (e.g., flex column with gap) break when
// items exit asynchronously or when collapse transforms need to be computed per-item.
export interface OverlayContainerProps {
toasts: InternalToast[];
notifications: InternalNotification[];
onToastExitComplete: (internalId: string) => void;
onNotificationExitComplete: (internalId: string) => void;
onNotificationDismiss: (id: Key, reason: DismissReason) => void;
onNotificationRestore: (id: Key) => void;
onPauseChange: (paused: boolean) => void;
}
export function OverlayContainer({
toasts,
notifications,
onToastExitComplete,
onNotificationExitComplete,
onNotificationDismiss,
onNotificationRestore,
onPauseChange,
}: OverlayContainerProps) {
// Merge toasts and notifications into a single ordered list
const allItems: OverlayItem[] = useMemo(() => {
const items: OverlayItem[] = [
...toasts.map((t): OverlayItem => ({ kind: 'toast', data: t })),
...notifications.map(
(n): OverlayItem => ({ kind: 'notification', data: n }),
),
];
// Sort by createdAt ascending (oldest first = bottom of stack, newest last = top)
items.sort((a, b) => getItemCreatedAt(a) - getItemCreatedAt(b));
return items;
}, [toasts, notifications]);
const visibleItems = useMemo(
() => allItems.filter((item) => !isItemExiting(item)),
[allItems],
);
const hasNotifications = notifications.some((n) => !n.isExiting);
const hasActionableToasts = toasts.some((t) => !t.isExiting && t.actions);
const canCollapse = !hasNotifications && !hasActionableToasts;
const {
heights,
settledIds,
positions,
lastPositionsRef,
itemRefs,
createRefCallback,
} = useItemPositions(visibleItems);
const {
isCollapsed,
handleMouseEnter,
handleMouseLeave,
handleFocus,
handleBlur,
containerRef,
} = useCollapseState(canCollapse, allItems, itemRefs, onPauseChange);
// ─── Callbacks ─────────────────────────────────────────────────────
// When the user dismisses a notification they were hovering, the element
// is removed from the DOM so mouseLeave never fires. Explicitly unpause
// so remaining notifications' timers resume.
const handleNotificationDismiss = useEvent(
(id: Key, reason: DismissReason) => {
onNotificationDismiss(id, reason);
onPauseChange(false);
},
);
const handleExitComplete = useEvent((item: OverlayItem) => {
const id = getItemId(item);
lastPositionsRef.current?.delete(id);
if (item.kind === 'toast') {
onToastExitComplete(item.data.internalId);
} else {
onNotificationExitComplete(item.data.internalId);
}
});
// useCallback (not useEvent) because this is called during render.
// useEvent defers the ref update to useLayoutEffect, so during render
// it would still read the previous closure's positions/heights.
const getItemStyle = useCallback(
(item: OverlayItem, index: number, total: number) => {
const id = getItemId(item);
const baseTop =
positions.get(id) ?? lastPositionsRef.current?.get(id) ?? 0;
const height = heights[id] ?? DEFAULT_ITEM_HEIGHT;
if (!isCollapsed || !canCollapse) {
return { top: `${baseTop}px` };
}
const isNewest = index === total - 1;
const collapsedTop = COLLAPSE_VISIBLE_HEIGHT - CONTAINER_OFFSET - height;
return {
top: `${collapsedTop}px`,
zIndex: index,
opacity: isNewest ? 1 : 0,
pointerEvents: 'none' as const,
};
},
[isCollapsed, canCollapse, positions, heights, lastPositionsRef],
);
// Build a visibleIndex lookup map to avoid O(n²) findIndex inside render loop
const visibleIndexMap = useMemo(() => {
const map = new Map<string, number>();
visibleItems.forEach((item, index) => {
map.set(getItemId(item), index);
});
return map;
}, [visibleItems]);
if (allItems.length === 0) return null;
return (
<Portal>
<OverlayContainerElement
ref={containerRef}
onFocus={handleFocus}
onBlur={handleBlur}
>
{allItems.map((item) => {
const itemId = getItemId(item);
const visibleIndex = visibleIndexMap.get(itemId) ?? 0;
const isExiting = isItemExiting(item);
return (
<DisplayTransition
key={itemId}
animateOnMount
isShown={!isExiting}
onRest={(transition) => {
if (transition === 'exit') {
handleExitComplete(item);
}
}}
>
{({ isShown, ref }) => (
<OverlayItemWrapper
ref={createRefCallback(itemId, ref)}
mods={{ isShown, isMeasured: settledIds.has(itemId) }}
style={getItemStyle(item, visibleIndex, visibleItems.length)}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
{item.kind === 'toast' ? (
<ToastItem
{...item.data.itemProps}
title={item.data.title}
description={item.data.description}
theme={item.data.theme}
icon={item.data.icon}
isLoading={item.data.isLoading}
actions={item.data.actions}
/>
) : (
<NotificationItem
notification={item.data}
onDismiss={handleNotificationDismiss}
onRestore={onNotificationRestore}
/>
)}
</OverlayItemWrapper>
)}
</DisplayTransition>
);
})}
</OverlayContainerElement>
</Portal>
);
}