Stylers

Important Note

Installtion

cargo add stylers

Prerequisite

[build-dependencies] stylers = { version = "*" } ```

fn main() { build(Some(String::from("./target/main.css"))); } `` - In the above case output css file will be generated in the./target/main.csspath. You can include thatmain.cssfile in yourindex.html` file.(Or If you are using a build like Trunk.rs you have to follow appropriate methods to include the main.css file to your project).

You can find the importance of these new changes here.

Leptos Example

Note :Leptos version > 0.4.9 has some new changes. But stylers works the same way in all versions of leptos

style!

```rust

[component]

fn Hello(cx: Scope, name: &'static str) -> impl IntoView { let stylerclass = style! { #two{ color: blue; } div.one{ color: red; content: rawstr(r#"\hello"#); font: "1.3em/1.2" Arial, Helvetica, sans-serif; } div { border: 1px solid black; margin: 25px 50px 75px 100px; background-color: lightblue; } h2 { color: purple; } @media only screen and (max-width: 1000px) { h3 { background-color: lightblue; color: blue } } };

view! {cx, class = styler_class,
    <div class="one">
        <h1 id="two">"Hello"</h1>
        <h2>"World"</h2>
        <h2>{name}</h2>
        <h3>"Hello Kanna"</h3>
    </div>
}

} ```

style_sheet!

```rust

[component]

fn Hello(cx: Scope, name: &'static str) -> impl IntoView { let classname = stylesheet!("./hello.css"); view! {cx, class = class_name,

"Hello"

"World"

} } - In the above casehello.css``file is inside theroot` directory of the project.

style_str!

```rust

[component]

pub fn GreenButton(cx: Scope) -> impl IntoView { let (classname, styleval) = style_str! { button { background-color: green; border-radius: 8px; border-style: none; box-sizing: border-box; color: yellow; cursor: pointer; display: inline-block; font-family: r#"Haas Grot Text R Web"#, r#"Helvetica Neue"#, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 500; height: 40px; line-height: 20px; list-style: none; margin: 0; outline: none; padding: 10px 16px; position: relative; text-align: center; text-decoration: none; transition: color 100ms; vertical-align: baseline; user-select: none; -webkit-user-select: none; } button:hover{ background-color: yellow; color: green; } };

view! {cx, class = class_name,
    <style>{style_val}</style>
    <button>"I am green button"</button>
}

} ```

stylesheetstr!

```rust

[component]

pub fn BlueButton(cx: Scope) -> impl IntoView { let (classname, styleval) = stylesheetstr!("./src/button.css");

view! {cx, class = class_name,
    <style>{style_val}</style>
    <button>"I am blue button"</button>
}

} - In this casebutton.css``file is inside thesrc` directory of the project.

Custom pseudo classes

css div :deep(h3) { color: orange; }

Output

css div.l-243433 h3{color: orange;}

css :deep(h3 div) { color: orange; }

Ouput

css h3 div{color: orange;}

How it works:

Edge cases handled for style! macros

rust style!( div{ content: raw_str(r#"\hello"#); font: "1.3em/1.2" Arial; } )

Output

css div.l-23432{ content: "\hello"; font: 1.3em/1.2 Arial; }

Optional build process using Trunk(Only when you use style! or style_sheet! macro )