css!() { /* proc-macro */ }Expand description
Define &str constants for each class in a CSS file.
This defines 2 modules:
mod classwith constants or functions (depending onauto_mount) for each CSS class. For a CSS class calledmy-css-class, a constant calledMY_CSS_CLASSor a function calledmy_css_classwill be defined.mod stylesheetwith:- An
fn text() -> &'static strthat gets the content of the stylesheet. fn mount()andfn mount_dom<D: Document>()that lazily callDefaultDom::mount_in_headorD:mount_in_headonce, respectively. This ensures the stylesheet is in the head. Any subsequent calls to either function will have no effect.
- An
The macro takes two forms. Firstly it can take a single string literal which
is the path to the CSS/SCSS/SASS file. The path is relative to the
$CARGO_MANIFEST_DIR environment variable.
Alternatively, named parameters can be specified.
§Parameters
Parameters take the form:
css!(
path = "my-css-file.css",
syntax = "css",
prefix = "prefix",
include_prefixes = ["included-"],
exclude_prefixes = ["excluded-"],
auto_mount,
validate,
transpile = (
minify,
pretty,
modules,
nesting,
browsers = (
android = (1, 0, 0),
chrome = (1, 0, 0),
edge = (1, 0, 0),
firefox = (1, 0, 0),
ie = (1, 0, 0),
ios_saf = (1, 0, 0),
opera = (1, 0, 0),
safari = (1, 0, 0),
samsung = (1, 0, 0),
)
)
);All are optional, but one of path or content must be specified.
pathis the path to the CSS/SCSS/SASS file. The syntax is determined from the extension.syntaxexplicitly specifies the syntax. It must be one of “css”, “scss”, or “sass”. The default is “css”. It overrides any syntax inferred frompath.contentis the css content.prefix: only classes starting withprefixshould be included. Their Rust names will have the prefix stripped.include_prefixes: a list of prefixes to include, without stripping the prefix. Rust constants will only be defined for classes starting with one or more of these prefixes.exclude_prefixes: a list of prefixes to exclude. No Rust constants will be defined for a class starting with any of these prefixes.exclude_prefixestakes precedence overinclude_prefixes.auto_mount: Generate a function for each CSS class that will callstylesheet::mountbefore returning the class name.validate: validate the CSS. Requires crate featurecss-transpile.transpile: transpile the CSS with lightningcss. Requires crate featurecss-transpile.
§transpile
minify: Minify the CSS returned bystylesheet(). Minification also adds/removes vendor prefixes, so it’s a good idea to keep this the same between debug and release builds. Useprettyif you want legible CSS in debug.pretty: Pretty print the final output. This is the default unless minify is specified.modules: Enable CSS Modules to locally scope class identifiers, via lightningcss. Composition is unsupported.nesting: Allow CSS nesting.browsersis a comma seperated list of the minimum supported browser versions. This will add vendor prefixes to the CSS fromstylesheet(). The version is a paranthesized,seperated string of major, minor, and patch versions. For example, to support firefox 110 + and chrome 111+, usebrowsers =( firefox = (110, 0, 0), chrome = (111, 0, 0) ).
§Examples
Define private constants for all CSS classes:
css!("my-css-file.css");
assert_eq!(class::MY_CLASS, "my-class");Define private constants for all content CSS classes:
css!(content = r#"
.my-class {
color: hotpink;
}
"#);
assert_eq!(class::MY_CLASS, "my-class");
assert_eq!(stylesheet::text(), r#"
.my-class {
color: hotpink;
}
"#);Include classes starting with border-, except classes starting with
border-excluded-:
css!(
path = "my-css-file.css",
prefix = "border-",
exclude_prefixes = ["border-excluded-"]
);
assert_eq!(class::SMALL, "border-small");This won’t compile because exclude_prefixes takes precedence over
include_prefixes:
ⓘ
css!(
path = "my-css-file.css",
include_prefixes = ["border-"]
exclude_prefixes = ["border-excluded-"]
);
assert_eq!(class::BORDER_EXCLUDED_HUGE, "border-excluded-huge");