Skip to main content

leptos_use_css_var/
options.rs

1use crate::IntoElementMaybeSignal;
2use default_struct_builder::DefaultBuilder;
3#[cfg(not(feature = "ssr"))]
4use leptos::prelude::*;
5use std::marker::PhantomData;
6
7/// Options for [`use_css_var_with_options`](crate::use_css_var_with_options).
8#[derive(DefaultBuilder)]
9pub struct UseCssVarOptions<El, M>
10where
11    El: IntoElementMaybeSignal<web_sys::Element, M>,
12{
13    /// The target element to read the variable from and set the variable on.
14    /// Defaults to the `document.documentElement`.
15    pub(crate) target: El,
16
17    /// The initial value of the variable before it is read. Also the default value
18    /// if the variable isn't defined on the target. Defaults to "".
19    #[builder(into)]
20    pub(crate) initial_value: String,
21
22    /// If `true` use a `MutationObserver` to monitor variable changes. Defaults to `false`.
23    pub(crate) observe: bool,
24
25    #[builder(skip)]
26    _marker: PhantomData<M>,
27}
28
29#[cfg(feature = "ssr")]
30impl<M> Default for UseCssVarOptions<Option<web_sys::Element>, M>
31where
32    Option<web_sys::Element>: IntoElementMaybeSignal<web_sys::Element, M>,
33{
34    fn default() -> Self {
35        Self {
36            target: None,
37            initial_value: "".into(),
38            observe: false,
39            _marker: PhantomData,
40        }
41    }
42}
43
44#[cfg(not(feature = "ssr"))]
45impl<M> Default for UseCssVarOptions<web_sys::Element, M>
46where
47    web_sys::Element: IntoElementMaybeSignal<web_sys::Element, M>,
48{
49    fn default() -> Self {
50        Self {
51            target: document().document_element().expect("No document element"),
52            initial_value: "".into(),
53            observe: false,
54            _marker: PhantomData,
55        }
56    }
57}