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

Global requires 'static & doesn't support Classes #78

Closed
simbleau opened this issue Jul 4, 2022 · 4 comments
Closed

Global requires 'static & doesn't support Classes #78

simbleau opened this issue Jul 4, 2022 · 4 comments

Comments

@simbleau
Copy link
Contributor

simbleau commented Jul 4, 2022

css: StyleSource<'static>,

Is it really necessary this is static? Or can we allow <Global css={}> to take Classes as input?

My main concern is trying to apply a theme with a little more modularity than the example.

My use case:

fn global_css() -> StyleSource<'static> {
    css!(
        r#"
            :root {
                --fs: 1rem;
                --fw: 400;
                --fh: 700
            }
        "#,
    )
}

#[function_component(App)]
fn app() -> Html {
    let theme = use_theme();
    let theme_css: StyleSource<'static> = css!(
        r#"
        :root {
            background-color: ${bg};
            color: ${fg};
        }
        "#,
        bg = theme.bg1.to_css(),
        fg = theme.fg1.to_css(),
    );
    let root_classes = classes!(global_css(), theme_css);

    html! {
        <>
        <Global css={ root_classes } />
        ...
        </>
    }
}
@simbleau
Copy link
Contributor Author

simbleau commented Jul 4, 2022

Also I'll note in my example, I would have preferred to use global_style!() and use & instead of :root, but I couldn't get it to work.

@simbleau simbleau changed the title Global requires 'static Global requires 'static & doesn't support Classes Jul 4, 2022
@simbleau
Copy link
Contributor Author

simbleau commented Jul 4, 2022

Ok, I feel a little silly.
Apparently this works:

<Global css={ global::get_style() } />
<Global css={ theme_css } />

Whichs begs, wouldn't this make sense to be permissible?

<Global css={ classes!(global::get_style(), theme_css) } />

@WorldSEnder
Copy link
Collaborator

WorldSEnder commented Jul 4, 2022

Is it really necessary this is static? Or can we allow to take Classes as input?

No, and this lifetime parameter has been removed on master already

Ok, I feel a little silly. Apparently this works:

<Global css={ global::get_style() } />
<Global css={ theme_css } />

Whichs begs, wouldn't this make sense to be permissible?

<Global css={ classes!(global::get_style(), theme_css) } />

classes! != StyleSource. Think of StyleSource like a potential class that doesn't have a classname yet. The missing utility is one that combines multiple style sources into one, much like classes! converts multiple yew::Classes together.

The conversion that often takes place, converting a StyleSource into Classes is the magic sauce that checks for such a stylesheet to exist, otherwise generates a new classname and mounts a <style> element in the document. The combinator for StyleSource could either merge multiple sheets (such that only one classname is ultimately generated) or keep them in a list (such that potentially multiple class names are generated, but deduplication could be a bit better).

<Global css={ global::get_style() } />
<Global css={ theme_css } />

You already found the easy workaround in this case ;)

@simbleau
Copy link
Contributor Author

simbleau commented Jul 4, 2022

Awesome. Perhaps justification for a sources! macro in the future.

@simbleau simbleau closed this as completed Jul 4, 2022
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

2 participants