Prechádzať zdrojové kódy

Scroll to feature (#2948)

* updated watchdog dep

* added scroll_to feature

* Fix pre-commit and pyi issues

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
Yummy-Yums 1 rok pred
rodič
commit
e2eced79b1
3 zmenil súbory, kde vykonal 16 pridanie a 0 odobranie
  1. 1 0
      reflex/__init__.py
  2. 1 0
      reflex/__init__.pyi
  3. 14 0
      reflex/event.py

+ 1 - 0
reflex/__init__.py

@@ -142,6 +142,7 @@ _MAPPING = {
         "remove_local_storage",
         "set_clipboard",
         "set_focus",
+        "scroll_to",
         "set_value",
         "stop_propagation",
         "upload_files",

+ 1 - 0
reflex/__init__.pyi

@@ -124,6 +124,7 @@ from reflex.event import remove_cookie as remove_cookie
 from reflex.event import remove_local_storage as remove_local_storage
 from reflex.event import set_clipboard as set_clipboard
 from reflex.event import set_focus as set_focus
+from reflex.event import scroll_to as scroll_to
 from reflex.event import set_value as set_value
 from reflex.event import stop_propagation as stop_propagation
 from reflex.event import upload_files as upload_files

+ 14 - 0
reflex/event.py

@@ -443,6 +443,20 @@ def set_focus(ref: str) -> EventSpec:
     )
 
 
+def scroll_to(elem_id: str) -> EventSpec:
+    """Select the id of a html element for scrolling into view.
+
+    Args:
+        elem_id: the id of the element
+
+    Returns:
+        An EventSpec to scroll the page to the selected element.
+    """
+    js_code = f"document.getElementById('{elem_id}').scrollIntoView();"
+
+    return call_script(js_code)
+
+
 def set_value(ref: str, value: Any) -> EventSpec:
     """Set the value of a ref.