Skip to content

Commit ac65755

Browse files
authored
ENG-6168: Add theme switcher (#3)
* ENG-6168: Add theme switcher * pre-commit
1 parent 3f2a08a commit ac65755

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

demo/demo/demo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def index() -> rx.Component:
1616
position="top-center",
1717
),
1818
),
19+
ui.theme_switcher(class_name="absolute top-4 right-4"),
1920
class_name=ui.cn(
2021
"flex flex-col items-center justify-center h-screen", "bg-secondary-1"
2122
),

reflex_ui/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
_REFLEX_UI_MAPPING = {
66
"components.base.button": ["button"],
77
"components.base.avatar": ["avatar"],
8+
"components.base.theme_switcher": ["theme_switcher"],
89
}
910

1011
_SUBMODULES = {"components", "utils"}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Theme switcher component."""
2+
3+
from reflex.components.component import Component, memo
4+
from reflex.components.core.cond import cond
5+
from reflex.components.el import Button, Div
6+
from reflex.style import LiteralColorMode, color_mode, set_color_mode
7+
8+
from reflex_ui.components.icons.hugeicon import hi
9+
from reflex_ui.utils.twmerge import cn
10+
11+
12+
def theme_switcher_item(mode: LiteralColorMode, icon: str) -> Component:
13+
"""Create a theme switcher item button for a specific mode."""
14+
active_cn = "text-secondary-12 shadow-small bg-secondary-1"
15+
unactive_cn = "hover:text-secondary-12 text-secondary-9"
16+
return Button.create(
17+
hi(icon, class_name="shrink-0", size=14),
18+
on_click=set_color_mode(mode),
19+
class_name=(
20+
"flex items-center cursor-pointer justify-center rounded-sm transition-color size-6",
21+
cond(mode == color_mode, active_cn, unactive_cn),
22+
),
23+
aria_label=f"Switch to {mode} mode",
24+
)
25+
26+
27+
@memo
28+
def theme_switcher(class_name: str = "") -> Component:
29+
"""Theme switcher component."""
30+
return Div.create(
31+
theme_switcher_item("system", "ComputerIcon"),
32+
theme_switcher_item("light", "Sun01Icon"),
33+
theme_switcher_item("dark", "Moon02Icon"),
34+
class_name=cn(
35+
"flex flex-row items-center bg-secondary-3 p-1 rounded-md w-fit",
36+
class_name,
37+
),
38+
)

0 commit comments

Comments
 (0)