Trait Classes

Source
pub trait Classes: Sized + 'static {
    type Theme: Theme;

    // Required methods
    fn generate(theme: &Self::Theme, css: &mut String, counter: &mut u64);
    fn new(start: u64) -> Self;

    // Provided method
    fn use_style(cx: &ScopeState) -> &Self { ... }
}
Expand description

This trait will be implemented by the classnames-struct generated by the make_styles! macro. You probably won’t implement it yourself unless you need something very specific which the macro cannot handle.

Example

struct MyClasses {
    active: String,
    disabled: String,
}

impl Classes for MyClasses {
    type Theme = EmptyTheme;

    fn generate(_: &Self::Theme, css: &mut String, counter: &mut u64) {
        use core::fmt::Write;
        writeln!(css, "css-{counter} {{ background-color: transparent; }}").unwrap();
        *counter += 1;
        writeln!(css, "css-{counter} {{ background-color: #f0f0f0; }}").unwrap();
        *counter += 1;
    }

    fn new(start: u64) -> Self {
        MyClasses {
            active: format!("css-{}", start),
            disabled: format!("css-{}", start + 1),
        }
    }
}

Required Associated Types§

Source

type Theme: Theme

The Theme which this style depend on

Required Methods§

Source

fn generate(theme: &Self::Theme, css: &mut String, counter: &mut u64)

Generate the CSS rules. Use the provided counter to obtain unique classnames and increment it accordingly. The content of the rules may depend on the given theme, but the classnames must be the same whenever this method is called. The classnames must only depend on the counter, because they are cached.

It is important that the number of classes does not change either. The runtime will panic if a second call to generate returns a different counter than the first time.

Usually, this method will introduce an arbitrary number n of the css classes and increment the counter by exactly n. This n is usually a fixed constant

Source

fn new(start: u64) -> Self

The styles generated in Self::generate use unreadable classnames. The struct implementing this type should provide the user a way to access those classnames. The start parameter is the same as the counter param used in Self::generate, which is necessary because the dynamic classnames will depend on it.

Provided Methods§

Source

fn use_style(cx: &ScopeState) -> &Self

Available on crate feature dioxus only.

Mount this style and return a reference to the classnames (which are represented by Self). 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, which is taken from the current context.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§