Skip to content

Commit c8cb047

Browse files
gunnartorfisclaude
andcommitted
fix: incremental Map index + jsx dep in useLayoutEffect
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9f3fd96 commit c8cb047

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/toast-store.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,8 @@ class ToastStore {
7171
this.subscribers.forEach((callback) => callback());
7272
};
7373

74-
private rebuildIndex = (toasts: ToastProps[]): Map<string | number, ToastProps> => {
75-
const map = new Map<string | number, ToastProps>();
76-
for (const toast of toasts) {
77-
map.set(toast.id, toast);
78-
}
79-
return map;
74+
private cloneIndex = (): Map<string | number, ToastProps> => {
75+
return new Map(this.state.toastsById);
8076
};
8177

8278
private startTimer = ({
@@ -272,10 +268,14 @@ class ToastStore {
272268
});
273269
}
274270

271+
const updatedIndex = this.cloneIndex();
272+
const updatedEntry = updatedToasts.find((t) => t.id === options.id);
273+
if (updatedEntry) updatedIndex.set(options.id!, updatedEntry);
274+
275275
this.state = {
276276
...this.state,
277277
toasts: updatedToasts,
278-
toastsById: this.rebuildIndex(updatedToasts),
278+
toastsById: updatedIndex,
279279
shouldShowOverlay: true,
280280
};
281281
} else {
@@ -288,12 +288,15 @@ class ToastStore {
288288

289289
const visibleToasts =
290290
this.config.visibleToasts ?? toastDefaultValues.visibleToasts;
291+
const newIndex = this.cloneIndex();
292+
newIndex.set(newToast.id, newToast);
291293
const updatedHeights = { ...this.state.toastHeights };
292294
let heightsChanged = false;
293295
if (newToasts.length > visibleToasts) {
294296
const removedToast = newToasts.shift();
295297
if (removedToast) {
296298
this.clearTimer(removedToast.id);
299+
newIndex.delete(removedToast.id);
297300
if (removedToast.id in updatedHeights) {
298301
delete updatedHeights[removedToast.id];
299302
heightsChanged = true;
@@ -304,7 +307,7 @@ class ToastStore {
304307
this.state = {
305308
...this.state,
306309
toasts: newToasts,
307-
toastsById: this.rebuildIndex(newToasts),
310+
toastsById: newIndex,
308311
toastRefs: newToastRefs,
309312
toastHeights: heightsChanged ? updatedHeights : this.state.toastHeights,
310313
toastHeightsVersion: heightsChanged
@@ -382,10 +385,13 @@ class ToastStore {
382385
const shouldAutoCollapse =
383386
filteredToasts.length <= 1 && this.state.isExpanded;
384387

388+
const updatedIndex = this.cloneIndex();
389+
updatedIndex.delete(id);
390+
385391
this.state = {
386392
...this.state,
387393
toasts: filteredToasts,
388-
toastsById: this.rebuildIndex(filteredToasts),
394+
toastsById: updatedIndex,
389395
toastHeights: updatedHeights,
390396
toastHeightsVersion: this.state.toastHeightsVersion + 1,
391397
isExpanded: shouldAutoCollapse ? false : this.state.isExpanded,

src/toast.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export const Toast = React.forwardRef<ToastRef, ToastInternalProps>(
302302
}
303303
const { height } = toastRef.current.getBoundingClientRect();
304304
toastStore.setToastHeight(id, height);
305-
}, [id, variant, title, description]);
305+
}, [id, variant, title, description, jsx]);
306306

307307
const defaultStyles = useDefaultStyles({
308308
invert,

0 commit comments

Comments
 (0)