Forráskód Böngészése

bring back some notion of link

Khaleel Al-Adhami 2 hete
szülő
commit
343971ff40
2 módosított fájl, 60 hozzáadás és 1 törlés
  1. 1 1
      pyi_hashes.json
  2. 59 0
      reflex/components/radix/themes/typography/link.py

+ 1 - 1
pyi_hashes.json

@@ -105,7 +105,7 @@
   "reflex/components/radix/themes/typography/blockquote.pyi": "04de9fdb22583d87faaba5619bdc6e3e",
   "reflex/components/radix/themes/typography/code.pyi": "bd58d40878c3488f1ba58a122e78f4e7",
   "reflex/components/radix/themes/typography/heading.pyi": "91bfc9176f7e9ef33d1f69711ceddbe1",
-  "reflex/components/radix/themes/typography/link.pyi": "64bb0621ecc514b7b7b4086958edf30e",
+  "reflex/components/radix/themes/typography/link.pyi": "8b887a78a5cfea103ffd05433b03d8ed",
   "reflex/components/radix/themes/typography/text.pyi": "d2ba2f718acd0eaf7b5923fe6a27d59c",
   "reflex/components/react_player/audio.pyi": "bd7e024d39ac641f8279ee0f6afd7985",
   "reflex/components/react_player/react_player.pyi": "40db798bcb7fa40207d24f49722135ae",

+ 59 - 0
reflex/components/radix/themes/typography/link.py

@@ -13,6 +13,7 @@ from reflex.components.core.colors import color
 from reflex.components.core.cond import cond
 from reflex.components.el.elements.inline import A
 from reflex.components.markdown.markdown import MarkdownComponentMap
+from reflex.utils.imports import ImportDict
 from reflex.vars.base import Var
 
 from ..base import LiteralAccentColor, RadixThemesComponent
@@ -20,6 +21,39 @@ from .base import LiteralTextSize, LiteralTextTrim, LiteralTextWeight
 
 LiteralLinkUnderline = Literal["auto", "hover", "always", "none"]
 
+LiteralLinkDiscover = Literal["none", "render"]
+
+
+class ReactRouterLink(A):
+    """Links are accessible elements used primarily for navigation. This component is styled to resemble a hyperlink and semantically renders an <a>."""
+
+    library = "react-router"
+
+    tag = "Link"
+
+    alias = "ReactRouterLink"
+
+    # The page to link to.
+    to: Var[str]
+
+    # Replaces the current entry in the history stack instead of pushing a new one onto it.
+    replace: Var[bool]
+
+    # Will use document navigation instead of client side routing when the link is clicked: the browser will handle the transition normally (as if it were an <a href>).
+    reload_document: Var[bool]
+
+    # Prevents the scroll position from being reset to the top of the window when the link is clicked and the app is using ScrollRestoration. This only prevents new locations resetting scroll to the top, scroll position will be restored for back/forward button navigation.
+    prevent_scroll_reset: Var[bool]
+
+    # Defines the link discovery behavior
+    discover: Var[LiteralLinkDiscover]
+
+    # Enables a View Transition for this navigation.
+    view_transition: Var[bool]
+
+
+react_router_link = ReactRouterLink.create()
+
 
 class Link(RadixThemesComponent, A, MemoizationLeaf, MarkdownComponentMap):
     """A semantic element for navigation between pages."""
@@ -50,6 +84,14 @@ class Link(RadixThemesComponent, A, MemoizationLeaf, MarkdownComponentMap):
     # If True, the link will open in a new tab
     is_external: Var[bool]
 
+    def add_imports(self) -> ImportDict:
+        """Add imports for the Link component.
+
+        Returns:
+            The import dict.
+        """
+        return react_router_link._get_imports()  # pyright: ignore [reportReturnType]
+
     @classmethod
     def create(cls, *children, **props) -> Component:
         """Create a Link component.
@@ -75,6 +117,23 @@ class Link(RadixThemesComponent, A, MemoizationLeaf, MarkdownComponentMap):
         if href is not None:
             if not len(children):
                 raise ValueError("Link without a child will not display")
+
+            if "as_child" not in props:
+                # Extract props for the ReactRouterLink, the rest go to the Link/A element.
+                known_react_router_link_props = ReactRouterLink.get_props()
+                next_link_props = {}
+                for prop in props.copy():
+                    if prop in known_react_router_link_props:
+                        next_link_props[prop] = props.pop(prop)
+
+                next_link_props["to"] = next_link_props.pop("href", href)
+
+                # If user does not use `as_child`, by default we render using react_router_link to avoid page refresh during internal navigation
+                return super().create(
+                    ReactRouterLink.create(*children, **next_link_props),
+                    as_child=True,
+                    **props,
+                )
         else:
             props["href"] = "#"