leptos-use/examples/use_css_var/src/main.rs

60 lines
1.7 KiB
Rust
Raw Normal View History

2023-07-03 16:10:58 +01:00
use leptos::html::Div;
2023-06-16 04:51:33 +01:00
use leptos::*;
use leptos_use::docs::demo_or_body;
use leptos_use::{use_css_var_with_options, UseCssVarOptions};
#[component]
2023-07-27 18:06:36 +01:00
fn Demo() -> impl IntoView {
let el = create_node_ref::<Div>();
2023-06-16 04:51:33 +01:00
let (color, set_color) =
2023-07-27 18:06:36 +01:00
use_css_var_with_options("--color", UseCssVarOptions::default().target(el));
2023-06-16 04:51:33 +01:00
let switch_color = move |_| {
if color.get() == "#df8543" {
set_color.set("#7fa998".to_string());
2023-06-16 04:51:33 +01:00
} else {
set_color.set("#df8543".to_string());
2023-06-16 04:51:33 +01:00
}
};
2023-07-27 18:06:36 +01:00
let elv = create_node_ref::<Div>();
let (key, set_key) = create_signal("--color".to_string());
let (color_val, _) = use_css_var_with_options(key, UseCssVarOptions::default().target(elv));
2023-06-16 04:51:33 +01:00
let change_var = move |_| {
if key.get() == "--color" {
set_key.set("--color-one".to_string());
2023-06-16 04:51:33 +01:00
} else {
set_key.set("--color".to_string());
2023-06-16 04:51:33 +01:00
}
};
let style = move || {
format!(
"--color: #7fa998; --color-one: #df8543; color: {}",
color_val.get()
2023-06-16 04:51:33 +01:00
)
};
2023-07-27 18:06:36 +01:00
view! { <div>
2023-06-16 04:51:33 +01:00
<div node_ref=el style="--color: #7fa998; color: var(--color)">
"Sample text, " {color}
</div>
<button on:click=switch_color>"Change color value"</button>
</div>
<div>
<div node_ref=elv style=style class="mt-4">
"Sample text, " {key} ": " {color_val}
</div>
<button on:click=change_var>"Change color variable"</button>
</div>
}
}
fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();
2023-07-27 18:06:36 +01:00
mount_to(demo_or_body(), || {
view! { <Demo /> }
2023-06-16 04:51:33 +01:00
})
}