Site | GitHub | API | Coverage Report
Scoped CSS for Rust web frameworks like Leptos.
Macro | Description |
---|---|
style! |
Enable writing css inside rust functions directly. |
style_str! |
Same as style! macro but returns the tuple (class_name, style_val). |
style_sheet! |
Extract css from an external css file and importing that into the rust functions. |
style_sheet_str! |
Same as style_sheet! macro but returns the tuple (class_name, style_val). |
This crate owes its existence to the stylers repository created by Abishek P, without which this crate would never have been possible.
The reason for this crate’s development was because stylers has a reliance on the Rust nightly
build. This bugged me and I wanted to see if an alternate approach was feasible.
If you’re choosing between style4rs & stylers, the following comparision may help. Also, an additional crate styled exists, which requires stylist. At the time of writing[0] stylist depends on the nightly build too; they haven’t been analysed further.
style4rs | stylers | styled / stylist | Comments | |
---|---|---|---|---|
Rust build | Stable ✓ | Nightly ☹ | Nightly ☹ | |
Minified CSS | ✓ | x | ? | |
macros | ||||
style! | ✓ | ✓ | ✓ | |
style_sheet! | ✓ | ✓ | ? | |
style_str! | ✓ | ✓ | ? | |
style_sheet_str! | ✓ | ✓ | ? | |
css validation | ✓ | ✓+ | ? | [1] |
Misc | ||||
custom raw_str function |
x | ✓ | ? | [2] |
Specific CSS handling | ||||
::deep | Passed-through | Handled | ? | [3] |
@document | Passed-through | Handled | ? | [3] |
Released ? | ||||
Release version | Not in crates.io | 1.0.0-alpha | 0.2.0 / 0.13.0 |
style4rs
highlights syntactic errors around the entire CSS block (with an error message described by lightningcss).stylers
highlights errors at their precise line and also provides semantic checks / hints.raw_str
function is not all valid CSS content is parsable if it conflicts with the rust parsing. E.g. content: "\hello"
results in compile error, whereas content: "\\hello"
results in css with \\
rather than the rust escaped \
. Unicode escape sequences, such as content: "\01F44D"
appear ok.Nix can be used to set up a development environment.
Running > nix develop --impure
will set up Rust with Vscode.
The core code uses the LightningCSS Parser.
Tests in this crate are derived from stylers (and modified to reflect CSS constructs supported differently here). Note that the low coverage stats are misleading; much of the testing relies on compile time and build time invocation and is not instrumented.
See style4rs macro documentation.
Create a build.rs
file to merge all style!
and style_sheet!
css to the target css file.
See style4rs builder documentation.
See Build Scripts - The Cargo Book for further information about build.rs
.
[package]
name = "style4rs-test"
version = "0.1.0"
edition = "2021"
[dependencies]
style4rs = { version = "*" }
[build-dependencies]
style4rs = { version = "*" }