Skip to content

Commit de183bf

Browse files
committed
use random id for maximum safety
1 parent 7841e27 commit de183bf

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

reflex/components/core/auto_scroll.py

+28-27
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from reflex.components.el.elements.typography import Div
66
from reflex.constants.compiler import MemoizationDisposition, MemoizationMode
77
from reflex.utils.imports import ImportDict
8-
from reflex.vars.base import Var
8+
from reflex.vars.base import Var, get_unique_variable_name
99

1010

1111
class AutoScroll(Div):
@@ -25,9 +25,8 @@ def create(cls, *children, **props):
2525
An AutoScroll component.
2626
"""
2727
props.setdefault("overflow", "auto")
28-
custom_attrs = props.pop("custom_attrs", {})
29-
custom_attrs["ref"] = Var("containerRef")
30-
return super().create(*children, **props, custom_attrs=custom_attrs)
28+
props.setdefault("id", get_unique_variable_name())
29+
return super().create(*children, **props)
3130

3231
def add_imports(self) -> ImportDict | list[ImportDict]:
3332
"""Add imports required for the component.
@@ -43,15 +42,16 @@ def add_hooks(self) -> list[str | Var]:
4342
Returns:
4443
The hooks required for the component.
4544
"""
45+
ref_name = self.get_ref()
4646
return [
4747
"const containerRef = useRef(null);",
4848
"const wasNearBottom = useRef(false);",
4949
"const hadScrollbar = useRef(false);",
50-
"""
51-
const checkIfNearBottom = () => {
52-
if (!containerRef.current) return;
50+
f"""
51+
const checkIfNearBottom = () => {{
52+
if (!{ref_name}.current) return;
5353
54-
const container = containerRef.current;
54+
const container = {ref_name}.current;
5555
const nearBottomThreshold = 50; // pixels from bottom to trigger auto-scroll
5656
5757
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight;
@@ -60,34 +60,35 @@ def add_hooks(self) -> list[str | Var]:
6060
6161
// Track if container had a scrollbar
6262
hadScrollbar.current = container.scrollHeight > container.clientHeight;
63-
};
63+
}};
6464
""",
65-
"""
66-
const scrollToBottomIfNeeded = () => {
67-
if (!containerRef.current) return;
65+
f"""
66+
const scrollToBottomIfNeeded = () => {{
67+
if (!{ref_name}.current) return;
6868
69-
const container = containerRef.current;
69+
const container = {ref_name}.current;
7070
const hasScrollbarNow = container.scrollHeight > container.clientHeight;
7171
7272
// Scroll if:
7373
// 1. User was near bottom, OR
7474
// 2. Container didn't have scrollbar before but does now
75-
if (wasNearBottom.current || (!hadScrollbar.current && hasScrollbarNow)) {
75+
if (wasNearBottom.current || (!hadScrollbar.current && hasScrollbarNow)) {{
7676
container.scrollTop = container.scrollHeight;
77-
}
77+
}}
7878
7979
// Update scrollbar state for next check
80-
hadScrollbar.current = hasScrollbarNow;};
80+
hadScrollbar.current = hasScrollbarNow;
81+
}};
8182
""",
82-
"""
83-
useEffect(() => {
84-
const container = containerRef.current;
83+
f"""
84+
useEffect(() => {{
85+
const container = {ref_name}.current;
8586
if (!container) return;
8687
8788
// Create ResizeObserver to detect height changes
88-
const resizeObserver = new ResizeObserver(() => {
89-
scrollToBottomIfNeeded();
90-
});
89+
const resizeObserver = new ResizeObserver(() => {{
90+
scrollToBottomIfNeeded();
91+
}});
9192
9293
// Track scroll position before height changes
9394
container.addEventListener('scroll', checkIfNearBottom);
@@ -98,11 +99,11 @@ def add_hooks(self) -> list[str | Var]:
9899
// Observe container for size changes
99100
resizeObserver.observe(container);
100101
101-
return () => {
102-
container.removeEventListener('scroll', checkIfNearBottom);
103-
resizeObserver.disconnect();
104-
};
105-
});
102+
return () => {{
103+
container.removeEventListener('scroll', checkIfNearBottom);
104+
resizeObserver.disconnect();
105+
}};
106+
}});
106107
""",
107108
]
108109

0 commit comments

Comments
 (0)