Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(layer): remove stale toggle listeners in useLayer (infra-10) (#3420)

Cindy Zhangc807ee4f3e01
diff --git a/.changeset/layer-toggle-listener-leak.md b/.changeset/layer-toggle-listener-leak.md
new file mode 100644
index 0000000000..e4454e9aaf
--- /dev/null
+++ b/.changeset/layer-toggle-listener-leak.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] useLayer: the popover `toggle` event listener is now removed when the layer element detaches or when the handler identity changes (a new `onHide`), instead of accumulating stale-closure listeners on the same element. This prevents duplicate/stale `onHide` firing over a layer's lifetime (#3343).
+@cixzhang
diff --git a/packages/core/src/Layer/useLayer.tsx b/packages/core/src/Layer/useLayer.tsx
index 57c07867d6..901cbd0a62 100644
--- a/packages/core/src/Layer/useLayer.tsx
+++ b/packages/core/src/Layer/useLayer.tsx
@@ -15,6 +15,7 @@
import React, {
useCallback,
+ useEffect,
useId,
useRef,
useState,
@@ -377,17 +378,63 @@ export function useLayer(
[onHide],
);
- // Ref callback for popover element — sets up toggle listener
+ // Ref callback for popover element — sets up the `toggle` listener.
+ // Tracks the element + handler currently bound so the listener is removed
+ // when the element detaches or when `handleToggle`'s identity changes (a new
+ // `onHide` prop), preventing stale-closure listeners from accumulating on the
+ // same element (infra-10).
+ const listenedElRef = useRef<HTMLElement | null>(null);
+ const listenedHandlerRef = useRef<((e: Event) => void) | null>(null);
+
+ const bindToggleListener = useCallback(
+ (el: HTMLElement | null, handler: (e: Event) => void) => {
+ if (
+ listenedElRef.current &&
+ listenedHandlerRef.current &&
+ (listenedElRef.current !== el || listenedHandlerRef.current !== handler)
+ ) {
+ listenedElRef.current.removeEventListener(
+ 'toggle',
+ listenedHandlerRef.current,
+ );
+ listenedElRef.current = null;
+ listenedHandlerRef.current = null;
+ }
+ if (el && listenedElRef.current !== el) {
+ el.addEventListener('toggle', handler);
+ listenedElRef.current = el;
+ listenedHandlerRef.current = handler;
+ }
+ },
+ [],
+ );
+
const popoverRefCallback = useCallback(
(el: HTMLElement | null) => {
popoverRef.current = el;
- if (el) {
- el.addEventListener('toggle', handleToggle);
- }
+ bindToggleListener(el, handleToggle);
},
- [handleToggle],
+ [handleToggle, bindToggleListener],
);
+ // Re-bind when the handler identity changes while the element stays mounted,
+ // and detach on unmount.
+ useEffect(() => {
+ if (popoverRef.current) {
+ bindToggleListener(popoverRef.current, handleToggle);
+ }
+ return () => {
+ if (listenedElRef.current && listenedHandlerRef.current) {
+ listenedElRef.current.removeEventListener(
+ 'toggle',
+ listenedHandlerRef.current,
+ );
+ listenedElRef.current = null;
+ listenedHandlerRef.current = null;
+ }
+ };
+ }, [handleToggle, bindToggleListener]);
+
// Render function for context mode
const renderContext = useCallback(
(children: ReactNode, props?: ContextRenderProps) => {