Skip to content

Commit 92b1cd5

Browse files
committed
update link component with render prop
1 parent 678d23f commit 92b1cd5

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

reflex_ui/components/base/link.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from reflex.components.react_router.dom import ReactRouterLink, To
66
from reflex.vars.base import Var
77

8-
from reflex_ui.components.component import CoreComponent
98
from reflex_ui.components.icons.hugeicon import hi
9+
from reflex_ui.utils.twmerge import cn
1010

1111
LiteralLinkVariant = Literal["primary", "secondary"]
1212
LiteralLinkSize = Literal["xs", "sm", "md", "lg", "xl"]
@@ -16,6 +16,7 @@ class ClassNames:
1616
"""Class names for the link component."""
1717

1818
ROOT = "font-medium underline-offset-2 hover:underline w-fit group/link relative"
19+
ICON = "absolute top-1/2 -translate-y-1/2 right-[-1.25rem] group-hover/link:opacity-100 text-secondary-9 opacity-0"
1920

2021

2122
LINK_VARIANTS: dict[str, dict[str, str]] = {
@@ -33,7 +34,7 @@ class ClassNames:
3334
}
3435

3536

36-
class Link(ReactRouterLink, CoreComponent):
37+
class Link(ReactRouterLink):
3738
"""Link component."""
3839

3940
# The size of the link. Defaults to "sm".
@@ -55,23 +56,28 @@ def create(cls, *children, **props) -> ReactRouterLink:
5556
cls.validate_size(size)
5657
variant = props.pop("variant", "secondary")
5758
cls.validate_variant(variant)
59+
render_component = props.pop("render_", None)
5860
show_icon = props.pop("show_icon", False)
61+
link_class_name = props.get("class_name", "")
5962

60-
# Apply default styling
61-
cls.set_class_name(
62-
f"{ClassNames.ROOT} {LINK_VARIANTS['size'][size]} {LINK_VARIANTS['variant'][variant]}",
63-
props,
64-
)
63+
# Get the class name and children from the render component
64+
if render_component:
65+
children = list(getattr(render_component, "children", []))
66+
class_name = cn(
67+
getattr(render_component, "class_name", ""), link_class_name
68+
)
69+
else:
70+
children = list(children)
71+
size_class = LINK_VARIANTS["size"][size]
72+
variant_class = LINK_VARIANTS["variant"][variant]
73+
class_name = cn(
74+
f"{ClassNames.ROOT} {size_class} {variant_class}", link_class_name
75+
)
6576

66-
children = list(children)
6777
if show_icon:
68-
children.append(
69-
hi(
70-
"LinkSquare02Icon",
71-
class_name="absolute top-1/2 -translate-y-1/2 right-[-1.25rem] group-hover/link:opacity-100 text-secondary-9 opacity-0",
72-
),
73-
)
78+
children.append(hi("LinkSquare02Icon", class_name=ClassNames.ICON))
7479

80+
props["class_name"] = class_name
7581
return super().create(*children, **props)
7682

7783
@staticmethod
@@ -93,12 +99,7 @@ def validate_size(size: LiteralLinkSize):
9399
raise ValueError(message)
94100

95101
def _exclude_props(self) -> list[str]:
96-
return [
97-
*super()._exclude_props(),
98-
"size",
99-
"variant",
100-
"show_icon",
101-
]
102+
return [*super()._exclude_props(), "size", "variant", "show_icon", "render_"]
102103

103104

104105
link = Link.create

reflex_ui/components/base/link.pyi

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ from reflex.components.react_router.dom import ReactRouterLink, To
1111
from reflex.event import EventType, PointerEventInfo
1212
from reflex.vars.base import Var
1313

14-
from reflex_ui.components.component import CoreComponent
15-
1614
LiteralLinkVariant = Literal["primary", "secondary"]
1715
LiteralLinkSize = Literal["xs", "sm", "md", "lg", "xl"]
1816

1917
class ClassNames:
2018
ROOT = "font-medium underline-offset-2 hover:underline w-fit group/link relative"
19+
ICON = "absolute top-1/2 -translate-y-1/2 right-[-1.25rem] group-hover/link:opacity-100 text-secondary-9 opacity-0"
2120

2221
LINK_VARIANTS: dict[str, dict[str, str]]
2322

24-
class Link(ReactRouterLink, CoreComponent):
23+
class Link(ReactRouterLink):
2524
@classmethod
2625
def create(
2726
cls,
@@ -254,7 +253,6 @@ class Link(ReactRouterLink, CoreComponent):
254253
spell_check: Var[bool] | bool | None = None,
255254
tab_index: Var[int] | int | None = None,
256255
title: Var[str] | str | None = None,
257-
unstyled: Var[bool] | bool | None = None,
258256
style: Sequence[Mapping[str, Any]]
259257
| Mapping[str, Any]
260258
| Var[Mapping[str, Any]]

0 commit comments

Comments
 (0)