#css #html-css #web #html

jss

Create dynamic css easily using json notation

12 releases

0.6.2 Jul 23, 2023
0.6.1 Jul 10, 2023
0.5.1 Mar 25, 2022
0.5.0 Feb 24, 2022
0.1.0 Aug 19, 2021

#992 in Web programming

Download history 819/week @ 2024-07-23 1837/week @ 2024-07-30 1484/week @ 2024-08-06 1055/week @ 2024-08-13 826/week @ 2024-08-20 1239/week @ 2024-08-27 1705/week @ 2024-09-03 1791/week @ 2024-09-10 975/week @ 2024-09-17 1580/week @ 2024-09-24 1261/week @ 2024-10-01 1200/week @ 2024-10-08 728/week @ 2024-10-15 860/week @ 2024-10-22 546/week @ 2024-10-29 406/week @ 2024-11-05

2,802 downloads per month
Used in 26 crates (via duid-core)

MIT license

59KB
1.5K SLoC

jss!

Latest Version Build Status MIT licensed

This crate provides an easy way to write dynamic css using json notation. This gives you more convenient than you think.

Considering using a dynamic width for our layer class

.layer {
 width: 10px;
}

You will have to write it using the format! macro

let width = 10;
let css = format!("
.layer {{
    width: {}px;
}}
", width);

let expected = r#"
.layer {
    width: 10px;
}
"#;
assert_eq!(expected,css);

Oh!, we forgot that escaping braces in rust strings is done with braces and we will have double braces all over our dynamic css. It will just get worse when there are more variables added into it, keeping track the order of the format argument.

jss! to the rescue:

use jss::prelude::*;

let width = 10;
let css = jss!{
    ".layer": {
     width: px(width),
    }
};

let expected = ".layer{width:10px;}";
assert_eq!(expected,css);

Non-identifier style names can be written with snake_case, or using quotes on them.

use jss::prelude::*;

let css = jss!(
    ".layer": {
        border: "1px solid green",
        background_color: "red",
        "width": percent(100),
        "border-color": "red!important",
        margin: px(5) + " auto"
    },

    ".hide .layer": {
        opacity: 0,
    },
);

let expected = ".layer{border:1px solid green;background-color:red;width:100%;border-color:red!important;margin:5px auto;}.hide .layer{opacity:0;}";
assert_eq!(expected, css);

Use of name spaces in class selector to prevent collision with similar class names in other components.

use jss::{jss_ns, units::percent};
let css = jss::jss_ns_pretty!("frame",
    ".": {
        display: "block",
    },

    ".layer": {
        background_color: "red",
        border: "1px solid green",
    },

    "@media screen and (max-width: 800px)": {
      ".layer": {
        width: percent(100),
      }
    },

    ".hide .layer": {
        opacity: 0,
    },
);

let expected = r#"
.frame {
    display: block;
}
.frame__layer {
    background-color: red;
    border: 1px solid green;
}
@media screen and (max-width: 800px) {

    .frame__layer {
        width: 100%;
    }

}
.frame__hide .frame__layer {
    opacity: 0;
}
"#;
assert_eq!(expected, css);

Feature strict will prevent you from making typo on the style name. Using invalid style names will panic.

cargo test all --features = "strict"
use jss::prelude::*;

let width = 10;
let css = jss!{
    ".layer": {
     "not-soo-awesome-style-name": px(width), // panicked at 'invalid style name: not-soo-awesome-style-name'
    }
};

License: MIT

Dependencies

~0.7–1.3MB
~27K SLoC