MCPcopy Create free account
hub / github.com/acking-you/static_flow / theme_toggle

Function theme_toggle

crates/frontend/src/components/theme_toggle.rs:22–99  ·  view source on GitHub ↗
(props: &ThemeToggleProps)

Source from the content-addressed store, hash-verified

20
21#[function_component(ThemeToggle)]
22pub fn theme_toggle(props: &ThemeToggleProps) -> Html {
23 let ThemeToggleProps {
24 class,
25 } = props;
26 let theme_state = use_state(is_dark_theme);
27
28 let onclick = {
29 let theme_state = theme_state.clone();
30 Callback::from(move |_| {
31 if let Some(win) = web_sys::window() {
32 let _ =
33 js_sys::Reflect::get(&win, &wasm_bindgen::JsValue::from_str("__toggleTheme"))
34 .ok()
35 .and_then(|f| f.dyn_into::<js_sys::Function>().ok())
36 .and_then(|func| func.call0(&wasm_bindgen::JsValue::NULL).ok());
37 }
38 theme_state.set(is_dark_theme());
39 })
40 };
41
42 let label = if *theme_state { t::SWITCH_TO_LIGHT } else { t::SWITCH_TO_DARK };
43
44 // Dark active -> reveal the sun (clicking goes to light); light active ->
45 // reveal the moon. The hidden glyph rotates + scales out while the visible
46 // one rotates + scales in, so the theme swap cross-fades on-token instead of
47 // hard-cutting one FontAwesome character to another.
48 let (sun_anim, moon_anim) = if *theme_state {
49 ("opacity-100 rotate-0 scale-100", "opacity-0 -rotate-90 scale-50")
50 } else {
51 ("opacity-0 rotate-90 scale-50", "opacity-100 rotate-0 scale-100")
52 };
53
54 let button_class = classes!(
55 "group",
56 "btn-fluent-icon",
57 "border",
58 "border-[var(--border)]",
59 "bg-transparent",
60 "hover:bg-[var(--surface-alt)]",
61 "transition-all",
62 "duration-[var(--motion-fast)]",
63 "ease-[var(--ease-snap)]",
64 class.clone()
65 );
66
67 html! {
68 <button
69 type="button"
70 class={button_class}
71 {onclick}
72 aria-label={label}
73 title={label}
74 aria-pressed={(*theme_state).to_string()}
75 >
76 <span class="relative inline-flex h-[1.25em] w-[1.25em] items-center justify-center">
77 <i
78 class={classes!(
79 "fas", "fa-sun", "fa-lg", "absolute",

Callers

nothing calls this directly

Calls 3

is_dark_themeFunction · 0.85
cloneMethod · 0.80
okMethod · 0.80

Tested by

no test coverage detected