|
| 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