### Stylance Usage Examples Source: https://docs.rs/stylance/0.7.4/stylance/index.html Demonstrates how to use the stylance library to import and utilize scoped CSS modules in Rust code. ```APIDOC ## Usage Examples ### Importing CSS Files Create a `.module.css` file: ```css // src/component1/style.module.css .header { color: red; } .contents { border: 1px solid black; } ``` Import the CSS file in your Rust code: ```rust stylance::import_crate_style!(style, "src/component1/style.module.css"); stylance::import_style!(style2, "style2.module.css"); fn use_style() { println!("{} {}", style::header, style2::content); } ``` ### Accessing Global Class Names Use `:global(.class)` to reference external class names: ```css .contents :global(.paragraph) { color: blue; } ``` This will expand to: ```css .contents-539306b .paragraph { color: blue; } ``` ``` -------------------------------- ### Example usage of import_style! macro Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Provides a practical example of using the `import_style!` macro to import a CSS file and access its class names. ```rust stylance::import_style!(#[allow(dead_code)] pub style, "style.css"); fn use_style() { println!("{}", style::header); } ``` -------------------------------- ### Example usage of import_style Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_style.html Demonstrates importing a CSS file and using the generated class names in Rust code. ```rust // style.css is located in the same directory as this rust file. stylance::import_style!(#[allow(dead_code)] pub style, "style.css"); fn use_style() { println!("{}", style::header); } ``` -------------------------------- ### Stylance Macro Usage Example Source: https://docs.rs/stylance/0.7.4/stylance/macro.classes.html Demonstrates how to use the 'classes!' macro with static strings, module-scoped styles, and conditionally included classes using Option. ```rust let active_tab = 0; // set to 1 to disable the active class! let classes_string = classes!( "some-global-class", my_style::header, module_style::header, // conditionally activate a global style if active_tab == 0 { Some(my_style::active) } else { None } // The same can be expressed with then_some: (active_tab == 0).then_some(my_style::active) ); ``` -------------------------------- ### Implement JoinClasses for Tuples Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Demonstrates how to use the join_classes method with a tuple containing various types of class names, including global, scoped, conditional, and Option types. Ensure that the necessary imports and styles are set up. ```rust import_crate_style!(style, "tests/style.module.scss"); let current_page = 10; // Some variable to use in the condition let class_name = ( "header", // Global classname style::style1, // Stylance scoped classname if current_page == 10 { Some("active1") } else { None }, (current_page == 11).then_some("active2"), // Same as above but much nicer ) .join_classes(); // class_name is "header style1-a331da9 active1" ``` -------------------------------- ### Define a CSS Module Source: https://docs.rs/stylance/0.7.4/stylance Create a .module.css file to define scoped styles for your components. ```css // src/component1/style.module.css .header { color: red; } .contents { border: 1px solid black; } ``` -------------------------------- ### Macro: classes! Source: https://docs.rs/stylance/0.7.4/stylance/macro.classes.html A utility macro for joining multiple class names, supporting &str, &String, types implementing AsRef, and Option types. ```APIDOC ## classes! ### Description Utility macro for joining multiple class names. The macro accepts `&str`, `&String`, and any refs of `T` where `T` implements `AsRef`. It also accepts `Option` of those types; `None` values will be filtered from the list. ### Usage ```rust let classes_string = classes!( "some-global-class", my_style::header, module_style::header, (active_tab == 0).then_some(my_style::active) ); ``` ``` -------------------------------- ### Stylance Macros Source: https://docs.rs/stylance/0.7.4/stylance/index.html Documentation for the macros provided by the stylance library. ```APIDOC ## Macros ### `classes` Utility macro for joining multiple class names. ### `import_crate_style` Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file. ### `import_style` Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file. The path is relative to the file that called the macro. ``` -------------------------------- ### Macro: classes Source: https://docs.rs/stylance/0.7.4/stylance Utility macro for joining multiple class names into a single string. ```APIDOC ## classes!(...) ### Description Utility macro for joining multiple class names into a single string for use in HTML templates or components. ``` -------------------------------- ### Macro: import_style! Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file. ```APIDOC ## import_style! ### Description Reads a CSS file at compile time and generates a module containing the classnames found inside that CSS file. The path is relative to the file that called the macro. ### Syntax `import_style!([#[attribute]] [pub] module_identifier, style_path);` ### Parameters - **#[attribute]** (meta) - Optional - Attributes to be added to the generated module (e.g., #[allow(dead_code)]). - **pub** (keyword) - Optional - Makes the generated module public. - **module_identifier** (ident) - Required - The name of the module generated by this macro. - **style_path** (string literal) - Required - The path to a CSS file inside your rust crate, relative to the file where this macro was called. ### Request Example ```rust stylance::import_style!(#[allow(dead_code)] pub style, "style.css"); fn use_style() { println!("{}", style::header); } ``` ### Response #### Expands into ```rust pub mod style { pub const header: &str = "header-539306b"; pub const contents: &str = "contents-539306b"; } ``` -------------------------------- ### Macro: import_crate_style Source: https://docs.rs/stylance/0.7.4/stylance Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file, using a path relative to the crate root. ```APIDOC ## import_crate_style!(module_name, "path/to/style.module.css") ### Description Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file. The path is relative to the crate root. ### Request Example stylance::import_crate_style!(style, "src/component1/style.module.css"); ``` -------------------------------- ### Stylance Usage Source: https://docs.rs/stylance/0.7.4/index.html Demonstrates how to use Stylance to import and reference CSS class names within Rust code. ```APIDOC ## Usage Create a `.module.css` file inside your rust source directory: ```css // src/component1/style.module.css .header { color: red; } .contents { border: 1px solid black; } ``` Then import that file from your rust code: ```rust stylance::import_crate_style!(style, "src/component1/style.module.css"); stylance::import_style!(style2, "style2.module.css"); fn use_style() { println!("{} {}", style::header, style2::content); } ``` ### 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. ```css .contents :global(.paragraph) { color: blue; } ``` This will expand to: ```css .contents-539306b .paragraph { color: blue; } ``` ``` -------------------------------- ### Define the classes! macro Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html The macro implementation that joins expressions into a single string using ::stylance::JoinClasses. ```rust #[macro_export] macro_rules! classes { () => { "" }; ($($exp:expr),+$(,)?) => { ::stylance::JoinClasses::join_classes([$($exp.into()),*].as_slice()) }; } ``` -------------------------------- ### JoinClasses for 15-element tuple Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Implements JoinClasses for tuples containing up to 15 elements. Each element must be convertible into MaybeStr. ```rust impl<'a, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> JoinClasses for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) where T1: Into>, T2: Into>, T3: Into>, T4: Into>, T5: Into>, T6: Into>, T7: Into>, T8: Into>, T9: Into>, T10: Into>, T11: Into>, T12: Into>, T13: Into>, T14: Into>, T15: Into>, { fn join_classes(self) -> String } ``` -------------------------------- ### JoinClasses for 17-element tuple Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Implements JoinClasses for tuples containing up to 17 elements. Each element must be convertible into MaybeStr. ```rust impl<'a, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17> JoinClasses for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17) where T1: Into>, T2: Into>, T3: Into>, T4: Into>, T5: Into>, T6: Into>, T7: Into>, T8: Into>, T9: Into>, T10: Into>, T11: Into>, T12: Into>, T13: Into>, T14: Into>, T15: Into>, T16: Into>, T17: Into>, { fn join_classes(self) -> String } ``` -------------------------------- ### JoinClasses for 16-element tuple Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Implements JoinClasses for tuples containing up to 16 elements. Each element must be convertible into MaybeStr. ```rust impl<'a, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> JoinClasses for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) where T1: Into>, T2: Into>, T3: Into>, T4: Into>, T5: Into>, T6: Into>, T7: Into>, T8: Into>, T9: Into>, T10: Into>, T11: Into>, T12: Into>, T13: Into>, T14: Into>, T15: Into>, T16: Into>, { fn join_classes(self) -> String } ``` -------------------------------- ### JoinClasses for 13-element tuple Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Implements JoinClasses for tuples containing up to 13 elements. Each element must be convertible into MaybeStr. ```rust impl<'a, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> JoinClasses for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) where T1: Into>, T2: Into>, T3: Into>, T4: Into>, T5: Into>, T6: Into>, T7: Into>, T8: Into>, T9: Into>, T10: Into>, T11: Into>, T12: Into>, T13: Into>, { fn join_classes(self) -> String } ``` -------------------------------- ### Stylance Traits and Macros Source: https://docs.rs/stylance/0.7.4/stylance/all.html This section details the available traits and macros within the stylance crate. ```APIDOC ## Stylance API Documentation ### Traits #### JoinClasses This trait is used for joining CSS classes. ### Macros #### classes This macro is used for defining CSS classes. #### import_crate_style This macro is used for importing styles from the crate. #### import_style This macro is used for importing styles. ``` -------------------------------- ### Syntax for import_style Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_style.html The syntax structure for invoking the import_style macro. ```rust import_style!([#[attribute]] [pub] module_identifier, style_path); ``` -------------------------------- ### Macro: import_style Source: https://docs.rs/stylance/0.7.4/stylance Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file, using a path relative to the file that called the macro. ```APIDOC ## import_style!(module_name, "path/to/style.module.css") ### Description Reads a CSS file at compile time and generates a module containing the class names found inside that CSS file. The path is relative to the file that called the macro. ### Request Example stylance::import_style!(style2, "style2.module.css"); ``` -------------------------------- ### JoinClasses for 14-element tuple Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Implements JoinClasses for tuples containing up to 14 elements. Each element must be convertible into MaybeStr. ```rust impl<'a, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> JoinClasses for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) where T1: Into>, T2: Into>, T3: Into>, T4: Into>, T5: Into>, T6: Into>, T7: Into>, T8: Into>, T9: Into>, T10: Into>, T11: Into>, T12: Into>, T13: Into>, T14: Into>, { fn join_classes(self) -> String } ``` -------------------------------- ### Macro for Implementing JoinClasses for Tuples Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html A macro that generates `impl JoinClasses` for tuples of varying sizes, ensuring each element can be converted into `internal::MaybeStr`. ```rust macro_rules! impl_join_classes_for_tuples { (($($types:ident),*), ($($idx:tt),*)) => { impl<'a, $($types),*> JoinClasses for ($($types,)*) where $($types: Into>),* { fn join_classes(self) -> String { internal::join_maybe_str_slice([ $((self.$idx).into()),* ].as_slice()) } } }; } ``` -------------------------------- ### Use Global Class Names Source: https://docs.rs/stylance/0.7.4/stylance Wrap class names in :global() to prevent stylance from scoping them. ```css .contents :global(.paragraph) { color: blue; } ``` ```css .contents-539306b .paragraph { color: blue; } ``` -------------------------------- ### Import CSS Classnames into Rust Module Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Use this macro to read a CSS file at compile time and generate a Rust module containing the classnames found within. Optionally prefix attributes and make the generated module public. ```rust macro_rules! import_crate_style { ($(#[$meta:meta])* $vis:vis $ident:ident, $str:expr) => { $(#[$meta])* $vis mod $ident { ::stylance::internal::import_style_classes!($str); } }; } ``` -------------------------------- ### Define import_style macro signature Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_style.html The macro definition for import_style. ```rust macro_rules! import_style { ($(#[$meta:meta])* $vis:vis $ident:ident, $str:expr) => { ... }; } ``` -------------------------------- ### Import CSS Stylesheet Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_crate_style.html Use this macro to import a CSS file into your Rust project. The generated module will contain constants for each class name found in the CSS file. Ensure the path is relative to the Cargo.toml file. ```rust stylance::import_crate_style!(pub style, "path/from/manifest_dir/to/style.css"); fn use_style() { println!("{}", style::header); } ``` -------------------------------- ### Define CSS Classes in .module.css Source: https://docs.rs/stylance/0.7.4/stylance/index.html Define CSS rules within a .module.css file. These rules will be scoped by stylance. ```css .header { color: red; } .contents { border: 1px solid black; } ``` -------------------------------- ### Stylance Traits Source: https://docs.rs/stylance/0.7.4/stylance/index.html Documentation for the traits provided by the stylance library. ```APIDOC ## Traits ### `JoinClasses` Utility trait for combining tuples of class names into a single string. ``` -------------------------------- ### Internal join_opt_str_iter function Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Internal helper function to join an iterator of string slices with spaces, optimizing for capacity. ```rust fn join_opt_str_iter<'a, Iter>(iter: &mut Iter) -> String where Iter: Iterator + Clone, { let Some(first) = iter.next() else { return String::new(); }; let size = first.len() + iter.clone().map(|v| v.len() + 1).sum::(); let mut result = String::with_capacity(size); result.push_str(first); for v in iter { result.push(' '); result.push_str(v); } debug_assert_eq!(result.len(), size); result } ``` -------------------------------- ### Generated Module Structure Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_crate_style.html This shows the structure of the module generated by the `import_crate_style` macro. It contains public constants for each CSS class, with values representing the class names. ```rust pub mod style { pub const header: &str = "header-539306b"; pub const contents: &str = "contents-539306b"; } ``` -------------------------------- ### Import CSS Styles into Rust Code Source: https://docs.rs/stylance/0.7.4/stylance/index.html Use stylance macros to import CSS files into your Rust code. `import_crate_style` reads from the crate root, while `import_style` reads relative to the calling file. ```rust stylance::import_crate_style!(style, "src/component1/style.module.css"); stylance::import_style!(style2, "style2.module.css"); fn use_style() { println!("{} {}", style::header, style2::content); } ``` -------------------------------- ### import_style! Macro Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_style.html The import_style! macro reads a CSS file at compile time and generates a Rust module containing the classnames found in that CSS file. The path to the CSS file is relative to the file where the macro is called. ```APIDOC ## Macro import_style ### Description 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. ### Syntax ``` import_style!([#[attribute]] [pub] module_identifier, style_path); ``` - Optionally prefix attributes that will be added to the generated module. Particularly common is `#[allow(dead_code)]` to silence warnings from unused class names. - Optionally add pub keyword before `module_identifier` to make the generated module public. - `module_identifier`: This will be used as the name of the module generated by this macro. - `style_path`: This should be a string literal with the path to a css file inside your rust crate. The path is relative to the file where this macro was called from. ### Example ```rust // style.css is located in the same directory as this rust file. stylance::import_style!(#[allow(dead_code)] pub style, "style.css"); fn use_style() { println!("{}", style::header); } ``` ### Expands into ```rust pub mod style { pub const header: &str = "header-539306b"; pub const contents: &str = "contents-539306b"; } ``` ``` -------------------------------- ### import_crate_style Macro Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_crate_style.html The import_crate_style macro generates a module from a CSS file, allowing class names to be accessed as constants in Rust code. ```APIDOC ## import_crate_style ### Description Reads a CSS file at compile time and generates a module containing the classnames found inside that CSS file. ### Syntax `import_crate_style!([#[attribute]] [pub] module_identifier, style_path);` ### Parameters - **attribute** (meta) - Optional - Attributes to be added to the generated module (e.g., #[allow(dead_code)]). - **pub** (keyword) - Optional - Makes the generated module public. - **module_identifier** (ident) - Required - The name of the module to be generated. - **style_path** (string literal) - Required - The path to the CSS file, relative to the Cargo manifest directory. ### Request Example ```rust stylance::import_crate_style!(pub style, "path/from/manifest_dir/to/style.css"); fn use_style() { println!("{}", style::header); } ``` ### Expansion Example ```rust pub mod style { pub const header: &str = "header-539306b"; pub const contents: &str = "contents-539306b"; } ``` ``` -------------------------------- ### Define import_crate_style Macro Source: https://docs.rs/stylance/0.7.4/stylance/macro.import_crate_style.html This is the macro definition for `import_crate_style`. It is used to read CSS files at compile time and generate a module with class names. ```rust macro_rules! import_crate_style { ($(#[$meta:meta])* $vis:vis $ident:ident, $str:expr) => { ... }; } ``` -------------------------------- ### Stylance Macro Definition Source: https://docs.rs/stylance/0.7.4/stylance/macro.classes.html Defines the 'classes!' macro for joining class names. It accepts zero or more expressions, with an optional trailing comma. ```rust macro_rules! classes { () => { ... }; ($($exp:expr),+$(,)?) => { ... }; } ``` -------------------------------- ### Join Classes from Tuple into String Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Implement the `JoinClasses` trait for tuples to combine class names into a single space-separated string. Skips `None` values in `Option` elements. ```rust impl JoinClasses for &[internal::MaybeStr<'_>] { fn join_classes(self) -> String { internal::join_maybe_str_slice(self) } } ``` -------------------------------- ### JoinClasses::join_classes Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Concatenates elements of a tuple into a single string representation. ```APIDOC ## fn join_classes(self) -> String ### Description Concatenates the elements of the tuple into a single String. Each element in the tuple must implement Into>. ### Parameters - **self** (Tuple) - Required - A tuple of size 13 to 17 where each element is convertible to MaybeStr. ### Response - **String** - The joined string representation of the tuple elements. ``` -------------------------------- ### Access Global Class Names with :global() Source: https://docs.rs/stylance/0.7.4/stylance/index.html Use the `:global()` pseudo-class in your .module.css files to reference external class names without scoping. This instructs stylance to leave the specified class name untouched. ```css .contents :global(.paragraph) { color: blue; } ``` -------------------------------- ### Internal join_maybe_str_slice function Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Internal function to join a slice of `MaybeStr` into a single space-separated string. ```rust pub fn join_maybe_str_slice(slice: &[MaybeStr<'_>]) -> String { let mut iter = slice.iter().flat_map(|c| c.0); join_opt_str_iter(&mut iter) } ``` -------------------------------- ### Expanded Global Class Name Source: https://docs.rs/stylance/0.7.4/stylance/index.html This is how the CSS with `:global(.paragraph)` is transformed by stylance, showing the generated scoped class name combined with the global class. ```css .contents-539306b .paragraph { color: blue; } ``` -------------------------------- ### Define JoinClasses Trait Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html Defines the JoinClasses trait with a single required method, join_classes. This trait is intended for types that can be converted into a single String of class names. ```rust pub trait JoinClasses { // Required method fn join_classes(self) -> String; } ``` -------------------------------- ### Internal MaybeStr struct and From implementations Source: https://docs.rs/stylance/0.7.4/src/stylance/lib.rs.html Internal `MaybeStr` struct used for handling optional string slices, with `From` trait implementations for various string types. ```rust pub struct MaybeStr<'a>(Option<&'a str>); impl<'a> From<&'a str> for MaybeStr<'a> { fn from(value: &'a str) -> Self { MaybeStr::<'a>(Some(value)) } } impl<'a> From<&'a String> for MaybeStr<'a> { fn from(value: &'a String) -> Self { MaybeStr::<'a>(Some(value.as_ref())) } } impl<'a, T> From> for MaybeStr<'a> where T: AsRef + ?Sized, { fn from(value: Option<&'a T>) -> Self { Self(value.map(AsRef::as_ref)) } } impl<'a, T> From<&'a Option> for MaybeStr<'a> where T: AsRef, { fn from(value: &'a Option) -> Self { Self(value.as_ref().map(AsRef::as_ref)) } } ``` -------------------------------- ### Trait: JoinClasses Source: https://docs.rs/stylance/0.7.4/stylance/trait.JoinClasses.html The JoinClasses trait allows for the concatenation of multiple class names into a single string, which is useful for dynamic CSS class generation. ```APIDOC ## Trait: JoinClasses ### Description Utility trait for combining tuples of class names into a single string. It joins all elements of the tuple into a single string, separating them with a single space character. Optional elements (None) are skipped. ### Method fn join_classes(self) -> String ### Request Example ```rust import_crate_style!(style, "tests/style.module.scss"); let current_page = 10; let class_name = ( "header", style::style1, if current_page == 10 { Some("active1") } else { None }, (current_page == 11).then_some("active2"), ).join_classes(); // class_name is "header style1-a331da9 active1" ``` ### Response - **Return Value** (String) - A space-separated string of all valid class names provided in the tuple. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.