Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component way CSS #25

Open
imbolc opened this issue Jan 11, 2023 · 0 comments
Open

Component way CSS #25

imbolc opened this issue Jan 11, 2023 · 0 comments

Comments

@imbolc
Copy link

imbolc commented Jan 11, 2023

I was thinking on a possibility of rendering a <style> tag once with multiple component usage. Here's what I ended up with. What do you think on the approach? Would you consider adding something like this into the markup?

use std::any::type_name;
use std::cell::Cell;
use std::collections::HashSet;

#[derive(Default)]
pub struct CssOnce(Cell<HashSet<&'static str>>);

impl CssOnce {
    fn new() -> Self {
        Self::default()
    }

    fn insert<T>(&self) -> bool {
        let mut inner = self.0.take();
        let inserted = inner.insert(type_name::<T>());
        self.0.set(inner);
        inserted
    }
}

macro_rules! css_once {
    ($rend:ident, $($str:tt)+) => {
        if CssOnce::insert::<Self>($rend) {
            markup::raw(concat!("<style>", $($str),+, "</style>\n"))
        } else {
            markup::raw("")
        }
    };
}

markup::define! {
    Hello<'a>(
        css: &'a CssOnce,
        name: &'a str,
    ) {
        @css_once!(css,
            ".hello { background: blue }"
            ".hello b { color: yellow }"
        )
        p.hello { "Hello, " b { @name } }
    }

    Body<'a>(
        css: &'a CssOnce,
    ) {
        @Hello {css, name: "World"}
        @Hello {css, name: "Self"}
    }
}

fn main() {
    let css = CssOnce::new();
    println!("{}", Body { css: &css });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant