Go to file
Mario Carbajal b36b00a860 only create one tag when releasing stylance. 2024-01-08 11:32:44 -03:00
internal Fix nightly feature always enabled. 2024-01-08 11:16:33 -03:00
stylance only create one tag when releasing stylance. 2024-01-08 11:32:44 -03:00
stylance-cli stylance-cli REAME.md 2024-01-08 11:07:08 -03:00
.gitignore css parsing mostly working 2024-01-05 13:09:31 -03:00
Cargo.lock rename internal libs 2024-01-08 10:36:46 -03:00
Cargo.toml rename internal libs 2024-01-08 10:36:46 -03:00
README.md Small readme fix 2024-01-08 11:07:19 -03:00
release.toml only create one tag when releasing stylance. 2024-01-08 11:32:44 -03:00

README.md

Stylance

Stylance is a library and tool for working with scoped CSS in rust inspired by CSS modules.

Usage

Stylance is divided in two parts:

  1. Rust proc macros for importing scoped/hashed class names from css files as constants into your rust code.
  2. A cli tool that finds all css modules in your crate and generates an output css file with hashed class names.

Proc macro

Add stylance to your rust cargo.toml:

cargo add stylance

Then use the import_crate_style proc read a css/scss file and bring the classes from within that file as constants.

css/scss file:

/* src/component/card/card.module.scss */
.header {
  background-color: red;
}

rust file:

// src/component/card/card.rs

// Import a css file's classes:
stylance::import_crate_style!(my_style, "src/component/card/card.module.scss");

fn use_style() {
	// Use the classnames:
	println!("{}", my_style::header) // prints header-f45126d
}

All classnames found inside the file src/component/card/card.module.scss will be included as constants inside a module named as the identifier passed as first argument to import_style.

The proc macro has no other effects, generating the modified css file is done using the stylance cli.

Stylance cli

Install stylance cli

cargo install stylance-cli

Run stylance cli:

stylance --output ./bundled.scss ./path/to/crate/dir/

This will find all the files ending with .module.scss and .module.cssand bundle them into ./bundled.scss, all classes will be modified to include a hash that matches the one the import_crate_style! macro produces.

Resulting output.scss:

.header-f45126d {
  background-color: red;
}

During development it is convenient to use sylance cli in watch mode:

stylance --watch --output ./bundled.scss ./path/to/crate/dir/

The stylance process will then watch the .module.css files for changes and automatically rebuild the output file.