pub struct StyleProvider<T> { /* private fields */ }
Expand description
Manages dynamically inserted styles. You should usually have exactly one. Generated classnames are only unique for a fixed StyleProvider.
You will typically use StyleProvider in comination with the crate::make_styles!
macro.
§Example
make_styles! {
(_theme: EmptyTheme) -> MyClasses {
".my_class > span" {
color: "red",
},
}
}
fn main() {
let style_provider = StyleProvider::quickstart_web(EmptyTheme);
// inject the css styles
let cls = style_provider.add_classes::<MyClasses>();
let elem: &web_sys::Element = todo!(); // Some element
elem.set_class_name(&cls.my_class);
// inject it again; no change; will return the same classes
let cls2 = style_provider.add_classes::<MyClasses>();
assert_eq!(cls.my_class, cls2.my_class);
}
Implementations§
Source§impl<T: Theme> StyleProvider<T>
impl<T: Theme> StyleProvider<T>
Sourcepub fn quickstart_web(theme: T) -> Self
pub fn quickstart_web(theme: T) -> Self
Quickly sets up a StyleProvider for the given theme at the active document.
It will create a new style
tag to the head and use it to mount new styles.
Sourcepub fn add_classes<C>(&self) -> Cwhere
C: Classes<Theme = T>,
pub fn add_classes<C>(&self) -> Cwhere
C: Classes<Theme = T>,
Mount new styles and returns the dynamically generated classnames. If this style is already mounted, it won’t be mounted again. The classnames will be the same as last time.
Sourcepub fn update_theme(&self, theme: T)
pub fn update_theme(&self, theme: T)
Change the theme. All styles will be recomputed, but the classnames will not change.
Sourcepub fn use_styles<'a, C>(&self, cx: &'a ScopeState) -> &'a Cwhere
C: Classes<Theme = T>,
Available on crate feature dioxus
only.
pub fn use_styles<'a, C>(&self, cx: &'a ScopeState) -> &'a Cwhere
C: Classes<Theme = T>,
dioxus
only.A convenience hook to mount styles and cache the classnames. Note that the style will only be mounted once, even if you use this hook from multiple components or your components will be used multiple times. The classnames will be the same every time, as long as the same StyleProvider is used.
Trait Implementations§
Source§impl<T: Clone> Clone for StyleProvider<T>
impl<T: Clone> Clone for StyleProvider<T>
Source§fn clone(&self) -> StyleProvider<T>
fn clone(&self) -> StyleProvider<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more