Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dynamic icons name #4636

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion reflex/components/lucide/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from reflex.components.component import Component
from reflex.utils import format
from reflex.utils.imports import ImportVar
from reflex.vars.base import Var


class LucideIconComponent(Component):
"""Lucide Icon Component."""

library = "lucide-react@0.469.0"
library = "lucide-react@0.471.1"


class Icon(LucideIconComponent):
Expand Down Expand Up @@ -47,6 +48,10 @@ def create(cls, *children, **props) -> Component:
if "tag" not in props:
raise AttributeError("Missing 'tag' keyword-argument for Icon")

if isinstance(props["tag"], Var):
icon_name = props.pop("tag")
return DynamicIcon.create(name=icon_name, **props)

adhami3310 marked this conversation as resolved.
Show resolved Hide resolved
if (
not isinstance(props["tag"], str)
or format.to_snake_case(props["tag"]) not in LUCIDE_ICON_LIST
Expand All @@ -67,6 +72,21 @@ def create(cls, *children, **props) -> Component:
return super().create(*children, **props)


class DynamicIcon(LucideIconComponent):
"""A DynamicIcon component."""

tag = "DynamicIcon"

name: Var[str]

def _get_imports(self):
_imports = super()._get_imports()
if self.library:
_imports.pop(self.library)
_imports["lucide-react/dynamic"] = [ImportVar("DynamicIcon", install=False)]
return _imports
Lendemor marked this conversation as resolved.
Show resolved Hide resolved


LUCIDE_ICON_LIST = [
"a_arrow_down",
"a_arrow_up",
Expand Down Expand Up @@ -846,6 +866,7 @@ def create(cls, *children, **props) -> Component:
"house",
"house_plug",
"house_plus",
"house_wifi",
"ice_cream_bowl",
"ice_cream_cone",
"id_card",
Expand Down Expand Up @@ -1534,6 +1555,7 @@ def create(cls, *children, **props) -> Component:
"trending_up_down",
"triangle",
"triangle_alert",
"triangle_dashed",
"triangle_right",
"trophy",
"truck",
Expand Down
49 changes: 49 additions & 0 deletions reflex/components/lucide/icon.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,53 @@ class Icon(LucideIconComponent):
"""
...

class DynamicIcon(LucideIconComponent):
@overload
@classmethod
def create( # type: ignore
cls,
*children,
name: Optional[Union[Var[str], str]] = None,
style: Optional[Style] = None,
key: Optional[Any] = None,
id: Optional[Any] = None,
class_name: Optional[Any] = None,
autofocus: Optional[bool] = None,
custom_attrs: Optional[Dict[str, Union[Var, Any]]] = None,
on_blur: Optional[EventType[[], BASE_STATE]] = None,
on_click: Optional[EventType[[], BASE_STATE]] = None,
on_context_menu: Optional[EventType[[], BASE_STATE]] = None,
on_double_click: Optional[EventType[[], BASE_STATE]] = None,
on_focus: Optional[EventType[[], BASE_STATE]] = None,
on_mount: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_down: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_enter: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_leave: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_move: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_out: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_over: Optional[EventType[[], BASE_STATE]] = None,
on_mouse_up: Optional[EventType[[], BASE_STATE]] = None,
on_scroll: Optional[EventType[[], BASE_STATE]] = None,
on_unmount: Optional[EventType[[], BASE_STATE]] = None,
**props,
) -> "DynamicIcon":
"""Create the component.

Args:
*children: The children of the component.
style: The style of the component.
key: A unique key for the component.
id: The id for the component.
class_name: The class name for the component.
autofocus: Whether the component should take the focus once the page is loaded
custom_attrs: custom attribute
**props: The props of the component.

Returns:
The component.
"""
...

LUCIDE_ICON_LIST = [
"a_arrow_down",
"a_arrow_up",
Expand Down Expand Up @@ -889,6 +936,7 @@ LUCIDE_ICON_LIST = [
"house",
"house_plug",
"house_plus",
"house_wifi",
"ice_cream_bowl",
"ice_cream_cone",
"id_card",
Expand Down Expand Up @@ -1577,6 +1625,7 @@ LUCIDE_ICON_LIST = [
"trending_up_down",
"triangle",
"triangle_alert",
"triangle_dashed",
"triangle_right",
"trophy",
"truck",
Expand Down
Loading