Skip to content

Commit ff2194a

Browse files
committed
fix(map): attach desktop drawer scroll listener when ref is already set
Previously, the desktop branch of useMapDrawerState only attached the scroll listener inside the MutationObserver callback. If drawerContentRef.current was already populated when the effect ran (common on subsequent listing changes), no DOM mutation fired, the listener never attached, and the sticky drawer header stayed hidden. Now we attach immediately when the ref is set and only fall back to the observer when it isn't. Made-with: Cursor
1 parent 308436c commit ff2194a

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

src/features/map/hooks/useMapDrawerState.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,45 @@ export function useMapDrawerState({
112112
};
113113
}, [isDesktop, isFullSnap]);
114114

115-
// Desktop: the drawer is portalled, so it isn't in the tree on first
116-
// render. Watch the document for it to mount, then attach once.
115+
// Desktop: the drawer is portalled, so it may not be in the tree on the
116+
// first render. If the ref is already set by the time this effect runs
117+
// (common on subsequent listing changes), attach directly; otherwise fall
118+
// back to a MutationObserver that attaches once the portal mounts.
117119
useEffect(() => {
118120
if (!isDesktop || !isListingSelected) return;
119121

122+
let attachedTo: HTMLElement | null = null;
123+
120124
const handleScroll = () => {
121-
if (drawerContentRef.current) {
122-
setIsDrawerHeaderShown(
123-
drawerContentRef.current.scrollTop > SCROLL_THRESHOLD
124-
);
125+
if (attachedTo) {
126+
setIsDrawerHeaderShown(attachedTo.scrollTop > SCROLL_THRESHOLD);
125127
}
126128
};
127129

128-
const observer = new MutationObserver(() => {
129-
const drawerContent = drawerContentRef.current;
130-
if (drawerContent) {
131-
drawerContent.addEventListener("scroll", handleScroll);
132-
observer.disconnect();
133-
}
134-
});
130+
const attach = (element: HTMLElement) => {
131+
attachedTo = element;
132+
element.addEventListener("scroll", handleScroll);
133+
};
135134

136-
observer.observe(document.body, { childList: true, subtree: true });
135+
let observer: MutationObserver | null = null;
136+
137+
if (drawerContentRef.current) {
138+
attach(drawerContentRef.current);
139+
} else {
140+
observer = new MutationObserver(() => {
141+
const drawerContent = drawerContentRef.current;
142+
if (drawerContent) {
143+
attach(drawerContent);
144+
observer?.disconnect();
145+
observer = null;
146+
}
147+
});
148+
observer.observe(document.body, { childList: true, subtree: true });
149+
}
137150

138151
return () => {
139-
observer.disconnect();
140-
drawerContentRef.current?.removeEventListener("scroll", handleScroll);
152+
observer?.disconnect();
153+
attachedTo?.removeEventListener("scroll", handleScroll);
141154
};
142155
}, [isDesktop, isListingSelected]);
143156

0 commit comments

Comments
 (0)