Skip to content

Commit 651539f

Browse files
authored
fix prevent AutoScroll hook name collision with unique ID (#5650)
* fix prevent hook name collision with unique ID * fix scope wasNearBottom and hadScrollbar ref
1 parent 687d20f commit 651539f

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

reflex/components/core/auto_scroll.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,27 @@ def add_hooks(self) -> list[str | Var]:
5252
The hooks required for the component.
5353
"""
5454
ref_name = self.get_ref()
55+
unique_id = ref_name
5556
return [
56-
"const wasNearBottom = useRef(false);",
57-
"const hadScrollbar = useRef(false);",
57+
f"const wasNearBottom_{unique_id} = useRef(false);",
58+
f"const hadScrollbar_{unique_id} = useRef(false);",
5859
f"""
59-
const checkIfNearBottom = () => {{
60+
const checkIfNearBottom_{unique_id} = () => {{
6061
if (!{ref_name}.current) return;
6162
6263
const container = {ref_name}.current;
6364
const nearBottomThreshold = 50; // pixels from bottom to trigger auto-scroll
6465
6566
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight;
6667
67-
wasNearBottom.current = distanceFromBottom <= nearBottomThreshold;
68+
wasNearBottom_{unique_id}.current = distanceFromBottom <= nearBottomThreshold;
6869
6970
// Track if container had a scrollbar
70-
hadScrollbar.current = container.scrollHeight > container.clientHeight;
71+
hadScrollbar_{unique_id}.current = container.scrollHeight > container.clientHeight;
7172
}};
7273
""",
7374
f"""
74-
const scrollToBottomIfNeeded = () => {{
75+
const scrollToBottomIfNeeded_{unique_id} = () => {{
7576
if (!{ref_name}.current) return;
7677
7778
const container = {ref_name}.current;
@@ -80,37 +81,37 @@ def add_hooks(self) -> list[str | Var]:
8081
// Scroll if:
8182
// 1. User was near bottom, OR
8283
// 2. Container didn't have scrollbar before but does now
83-
if (wasNearBottom.current || (!hadScrollbar.current && hasScrollbarNow)) {{
84+
if (wasNearBottom_{unique_id}.current || (!hadScrollbar_{unique_id}.current && hasScrollbarNow)) {{
8485
container.scrollTop = container.scrollHeight;
8586
}}
8687
8788
// Update scrollbar state for next check
88-
hadScrollbar.current = hasScrollbarNow;
89+
hadScrollbar_{unique_id}.current = hasScrollbarNow;
8990
}};
9091
""",
9192
f"""
9293
useEffect(() => {{
9394
const container = {ref_name}.current;
9495
if (!container) return;
9596
96-
scrollToBottomIfNeeded();
97+
scrollToBottomIfNeeded_{unique_id}();
9798
9899
// Create ResizeObserver to detect height changes
99100
const resizeObserver = new ResizeObserver(() => {{
100-
scrollToBottomIfNeeded();
101+
scrollToBottomIfNeeded_{unique_id}();
101102
}});
102103
103104
// Track scroll position before height changes
104-
container.addEventListener('scroll', checkIfNearBottom);
105+
container.addEventListener('scroll', checkIfNearBottom_{unique_id});
105106
106107
// Initial check
107-
checkIfNearBottom();
108+
checkIfNearBottom_{unique_id}();
108109
109110
// Observe container for size changes
110111
resizeObserver.observe(container);
111112
112113
return () => {{
113-
container.removeEventListener('scroll', checkIfNearBottom);
114+
container.removeEventListener('scroll', checkIfNearBottom_{unique_id});
114115
resizeObserver.disconnect();
115116
}};
116117
}});

0 commit comments

Comments
 (0)