Crate stylance

source ·
Expand description

§About stylance

Stylance is a scoped CSS library for rust.

Use it in conjunction with stylance-cli.

§Feature flags

  • nightly: Enables importing styles with paths relative to the rust file where the macro was called.

§Usage

Create a .module.css file inside your rust source directory

// src/component1/style.module.css

.header {
    color: red;
}

.contents {
    border: 1px solid black;
}

Then import that file from your rust code:

stylance::import_crate_style!(style, "src/component1/style.module.css");

fn use_style() {
    println!("{}", style::header);
}

§Accessing non-scoped global class names with :global(.class)

Sometimes you may want to use an external classname in your .module.css file.

For this you can wrap the global class name with :global(), this instructs stylance to leave that class name alone.

.contents :global(.paragraph) {
    color: blue;
}

This will expand to

.contents-539306b .paragraph {
    color: blue;
}

§Transforming and bundling your .module.css files

To transform your .module.css and .module.scss into a bundled css file use stylance-cli.

Macros§

  • Utility macro for joining multiple class names.
  • Reads a css file at compile time and generates a module containing the classnames found inside that css file.
  • Reads a css file at compile time and generates a module containing the classnames found inside that css file. Path is relative to the file that called the macro.

Traits§

  • Utility trait for combining tuples of class names into a single string.