Skip to content

Commit c2e7086

Browse files
Add documentation for dynamic_icon feature (#1318)
* Add documentation for dynamic_icon feature Co-Authored-By: Alek Petuskey <[email protected]> * Update dynamic icon documentation based on feedback Co-Authored-By: Alek Petuskey <[email protected]> * Fix whitelist.py file Co-Authored-By: Alek Petuskey <[email protected]> * Remove subtitle qualifiers from headings as requested Co-Authored-By: Alek Petuskey <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Alek Petuskey <[email protected]>
1 parent 2a89cf0 commit c2e7086

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

docs/library/data-display/icon.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@ rx.flex(
3636

3737
## Dynamic Icons
3838

39-
It is not possible to use dynamic values as the `tag` prop, because it is used to import the icon from the Lucide library.
40-
If you have a specific subset of icons you want to use dynamically, define an rx.match with them:
39+
There are two ways to use dynamic icons in Reflex:
40+
41+
### Using rx.match
42+
43+
If you have a specific subset of icons you want to use dynamically, you can define an `rx.match` with them:
4144

4245
```python
43-
def dynamic_icon(icon_name):
46+
def dynamic_icon_with_match(icon_name):
4447
return rx.match(
4548
icon_name,
4649
("plus", rx.icon("plus")),
@@ -50,7 +53,7 @@ def dynamic_icon(icon_name):
5053
```
5154

5255
```python exec
53-
def dynamic_icon(icon_name):
56+
def dynamic_icon_with_match(icon_name):
5457
return rx.match(
5558
icon_name,
5659
("plus", rx.icon("plus")),
@@ -59,10 +62,34 @@ def dynamic_icon(icon_name):
5962
)
6063
```
6164

62-
You can then use the `dynamic_icon` function to display the icons dynamically.
65+
### Using Dynamic Icon Tags
66+
67+
Reflex also supports using dynamic values directly as the `tag` prop in `rx.icon()`. This allows you to use any icon from the Lucide library dynamically at runtime.
68+
69+
```python exec
70+
class DynamicIconState(rx.State):
71+
current_icon: str = "heart"
72+
73+
def change_icon(self):
74+
icons = ["heart", "star", "bell", "calendar", "settings"]
75+
import random
76+
self.current_icon = random.choice(icons)
77+
```
6378

6479
```python demo
65-
rx.foreach(["plus", "minus", "equal"], dynamic_icon)
80+
rx.vstack(
81+
rx.heading("Dynamic Icon Example"),
82+
rx.icon(DynamicIconState.current_icon, size=30, color="red"),
83+
rx.button("Change Icon", on_click=DynamicIconState.change_icon),
84+
spacing="4",
85+
align="center",
86+
)
87+
```
88+
89+
Under the hood, when a dynamic value is passed as the `tag` prop to `rx.icon()`, Reflex automatically uses a special `DynamicIcon` component that can load icons at runtime.
90+
91+
```md alert
92+
When using dynamic icons, make sure the icon names are valid. Invalid icon names will cause runtime errors.
6693
```
6794

6895
## Styling
@@ -167,4 +194,4 @@ rx.badge(
167194
radius="full",
168195
color_scheme="gray",
169196
)
170-
```
197+
```

0 commit comments

Comments
 (0)