Skip to content

Commit 84b0864

Browse files
pickleloadhami3310
andauthored
Add option to scroll to bottom (#4276)
* Add option to scroll to bottom * use var shenangins --------- Co-authored-by: Khaleel Al-Adhami <[email protected]>
1 parent e5e4941 commit 84b0864

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

reflex/event.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -767,18 +767,25 @@ def set_focus(ref: str) -> EventSpec:
767767
)
768768

769769

770-
def scroll_to(elem_id: str) -> EventSpec:
770+
def scroll_to(elem_id: str, align_to_top: bool | Var[bool] = True) -> EventSpec:
771771
"""Select the id of a html element for scrolling into view.
772772
773773
Args:
774-
elem_id: the id of the element
774+
elem_id: The id of the element to scroll to.
775+
align_to_top: Whether to scroll to the top (True) or bottom (False) of the element.
775776
776777
Returns:
777778
An EventSpec to scroll the page to the selected element.
778779
"""
779-
js_code = f"document.getElementById('{elem_id}').scrollIntoView();"
780-
781-
return call_script(js_code)
780+
get_element_by_id = FunctionStringVar.create("document.getElementById")
781+
782+
return call_script(
783+
get_element_by_id(elem_id)
784+
.call(elem_id)
785+
.to(ObjectVar)
786+
.scrollIntoView.to(FunctionVar)
787+
.call(align_to_top)
788+
)
782789

783790

784791
def set_value(ref: str, value: Any) -> EventSpec:

0 commit comments

Comments
 (0)