1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
//! # About stylance
//!
//! Stylance is a scoped CSS library for rust.
//!
//! Use it in conjunction with [stylance-cli](https://crates.io/crates/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
//! ```scss
//! // 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");
//!
//! 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.
//!
//! ```css
//! .contents :global(.paragraph) {
//! color: blue;
//! }
//! ```
//!
//! This will expand to
//! ```css
//! .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](https://crates.io/crates/stylance-cli).
//!
//!
#![cfg_attr(docsrs, feature(doc_cfg))]
#[doc(hidden)]
pub mod internal {
pub use stylance_macros::*;
pub struct NormalizeOptionStr<'a>(Option<&'a str>);
impl<'a> From<&'a str> for NormalizeOptionStr<'a> {
fn from(value: &'a str) -> Self {
NormalizeOptionStr::<'a>(Some(value))
}
}
impl<'a> From<&'a String> for NormalizeOptionStr<'a> {
fn from(value: &'a String) -> Self {
NormalizeOptionStr::<'a>(Some(value.as_ref()))
}
}
impl<'a, T> From<Option<&'a T>> for NormalizeOptionStr<'a>
where
T: AsRef<str> + ?Sized,
{
fn from(value: Option<&'a T>) -> Self {
Self(value.map(AsRef::as_ref))
}
}
impl<'a, T> From<&'a Option<T>> for NormalizeOptionStr<'a>
where
T: AsRef<str>,
{
fn from(value: &'a Option<T>) -> Self {
Self(value.as_ref().map(AsRef::as_ref))
}
}
pub fn normalize_option_str<'a>(value: impl Into<NormalizeOptionStr<'a>>) -> Option<&'a str> {
value.into().0
}
pub fn join_opt_str_slice(slice: &[Option<&str>]) -> String {
let mut iter = slice.iter().flat_map(|c| *c);
let first = match iter.next() {
Some(first) => first,
None => return String::new(),
};
let size = iter.clone().map(|v| v.len()).sum::<usize>() + slice.len() - 1;
let mut result = String::with_capacity(size);
result.push_str(first);
for v in iter {
result.push(' ');
result.push_str(v);
}
result
}
}
/// 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
/// ```rust
/// import_style!([pub] module_identifier, style_path);
/// ```
/// - 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!(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";
/// }
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "nightly")))]
#[macro_export]
macro_rules! import_style {
($ident:ident, $str:expr) => {
mod $ident {
::stylance::internal::import_style_classes_rel!($str);
}
};
(pub $ident:ident, $str:expr) => {
pub mod $ident {
::stylance::internal::import_style_classes_rel!($str);
}
};
}
/// Reads a css file at compile time and generates a module containing the classnames found inside that css file.
///
/// ### Syntax
/// ```rust
/// import_crate_style!([pub] module_identifier, style_path);
/// ```
/// - 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 must be relative to the cargo manifest directory (The directory that has Cargo.toml).
///
/// ### Example
/// ```rust
/// stylance::import_crate_style!(pub style, "path/from/manifest_dir/to/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";
/// }
/// ```
#[macro_export]
macro_rules! import_crate_style {
($ident:ident, $str:expr) => {
mod $ident {
::stylance::internal::import_style_classes!($str);
}
};
(pub $ident:ident, $str:expr) => {
pub mod $ident {
::stylance::internal::import_style_classes!($str);
}
};
}
/// Utility trait for combining tuples of class names into a single string.
pub trait JoinClasses {
/// Join all elements of the tuple into a single string separating them with a single space character.
///
/// Option elements of the tuple will be skipped if they are None.
///
/// ### Example
///
/// ```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 { // Conditional class
/// 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"
/// ```
fn join_classes(self) -> String;
}
macro_rules! impl_join_classes_for_tuples {
(($($types:ident),*), ($($idx:tt),*)) => {
impl<'a, $($types),*> JoinClasses for ($($types,)*)
where
$($types: Into<internal::NormalizeOptionStr<'a>>),*
{
fn join_classes(self) -> String {
let list = &[
$(internal::normalize_option_str(self.$idx)),*
];
internal::join_opt_str_slice(list)
}
}
};
}
impl_join_classes_for_tuples!(
(T1, T2), //
(0, 1)
);
impl_join_classes_for_tuples!(
(T1, T2, T3), //
(0, 1, 2)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4), //
(0, 1, 2, 3)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5), //
(0, 1, 2, 3, 4)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6), //
(0, 1, 2, 3, 4, 5)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7), //
(0, 1, 2, 3, 4, 5, 6)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8), //
(0, 1, 2, 3, 4, 5, 6, 7)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9),
(0, 1, 2, 3, 4, 5, 6, 7, 8)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
);
impl_join_classes_for_tuples!(
(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
);
/// Utility macro for joining multiple class names.
///
/// The macro accepts `&str` `&String` and any refs of `T` where `T` implements `AsRef<str>`
///
/// It also accepts `Option` of those types, `None` values will be filtered from the list.
///
/// Example
///
/// ```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)
/// );
/// ```
#[macro_export]
macro_rules! classes {
($($exp:expr),+) => {
::stylance::JoinClasses::join_classes(($($exp),*))
};
}