MCPcopy Create free account
hub / github.com/DioxusLabs/dioxus-components / theme_seed

Function theme_seed

preview/src/theme.rs:7–48  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5const CHANNEL_NAME: &str = "dx-theme";
6
7pub fn theme_seed() {
8 _ = document::eval(&format!(
9 r#"
10 (function () {{
11 if (window.__dx_theme_seeded) return;
12 window.__dx_theme_seeded = true;
13
14 const COOKIE_NAME = '{COOKIE_NAME}';
15 const CHANNEL_NAME = '{CHANNEL_NAME}';
16
17 function getCookie(name) {{
18 const prefix = name + '=';
19 const parts = document.cookie.split(';');
20 for (let p of parts) {{
21 p = p.trim();
22 if (p.startsWith(prefix)) return decodeURIComponent(p.slice(prefix.length));
23 }}
24 return null;
25 }}
26
27 function apply(theme) {{
28 if (theme === 'dark' || theme === 'light') {{
29 document.documentElement.setAttribute('data-theme', theme);
30 }} else {{
31 document.documentElement.removeAttribute('data-theme');
32 }}
33 }}
34
35 apply(getCookie(COOKIE_NAME));
36
37 try {{
38 const ch = new BroadcastChannel(CHANNEL_NAME);
39 ch.addEventListener('message', (event) => {{
40 const data = event.data;
41 apply(data && data.theme);
42 }});
43 window.__dx_theme_channel = ch;
44 }} catch (_) {{}}
45 }})();
46 "#,
47 ));
48}
49
50pub fn set_theme(dark_mode: bool) {
51 let theme = if dark_mode { "dark" } else { "light" };

Callers 1

AppLayoutFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected