Macro rcss::css

source ·
css!() { /* proc-macro */ }
Expand description

Api that mimic js “CSS modules”, it generate struct with css classes as fields. Output instance of this struct as result of macro call.

Usefull to avoid runtime errors, and to have autocompletion in IDE. Internally wraps rename all css classes with random names, keeping original names as prefix.

Example:

use rcss::*;
let css = rcss::css! {
   .my-class {
     color: red;
  }
};

// Note: css.my_class is in snake_case, uses feature = "auto-snake-case"
assert!(css.my_class.contains("my-class"));
// WARN: runtime panic if class is not found, feature = "indexed-classes"
assert!(css["my-class"].contains("my-class"));

// Example of usage
let html = format!(r#"<div class="{}">Hello</div>"#, css.my_class);
// Note: This style is only used for current style, and doesn't combine well with extend.
let html = format!("<style>{}</style>\n{}", css.scope_style(), html);
assert_eq!(html, "<style>.my-class-Mlfe{color:red}</style>\n\
<div class=\"my-class-Mlfe\">Hello</div>");

Generate CSS scope object based on css. Don’t use this macro directly, use rcss crate instead, since bundler will ignore macro that used directly.