Crates.io | semester |
lib.rs | semester |
version | 1.0.3 |
source | src |
created_at | 2022-10-01 11:32:16.629816 |
updated_at | 2023-01-07 00:41:57.230569 |
description | High efficiency classnames macro |
homepage | |
repository | https://github.com/Lucretiel/semester |
max_upload_size | |
id | 677761 |
size | 19,118 |
Semester is a declarative CSS conditional class name joiner, in the style of React's classnames. It's intended for use in web frameworks (like Yew) and HTML template engines (like horrorshow) as an efficient and compile-time checked way to conditionally activate or deactivate CSS classes on HTML elements.
Semester provides two similar macros, classes
and static_classes
, for creating sets of classes. Each one has a slightly different mode of operation, but they both use the same simple syntax and perform the same checks and guarantees:
impl Classes
:use semester::{classes, Classes as _};
let classes = classes!(
"class1",
"class2",
"class3"
);
assert_eq!(classes.render(), "class1 class2 class3");
use semester::{classes, Classes as _};
let classes = classes!(
"always",
"yup": 10 == 10,
"nope": 10 == 15,
);
assert_eq!(classes.render(), "always yup");
semester
will render all of the enabled classes in declaration order, separated by a single space. It will do its best to pre-compute parts of the output (for instance, by concatenating all the consecutive unconditional classes), and you can go further and use static_classes
, which pre-computes every possible combination of classes at compile time.
Besides render
, semester
provides several other ways to access the class set, so you can use whichever one makes the most sense for your use case. See the Classes
and StaticClasses
traits for details. semester
is no_std
and will generally only allocate on specific methods like render
and to_string
.
Additionally, semester
performs several compile time correctness checks on your classes:
// This does not compile
use semester::classes;
classes!("null\0class")
// This does not compile
use semester::classes;
classes!("class pair")
// This does not compile
use semester::classes;
classes!("")
<
>
&
'
"
// This does not compile
use semester::classes;
classes!("<injected-class>")
semester
can't detect mutually exclusive conditions, so it prevents duplicates unconditionally.// This does not compile
use semester::classes;
let x = 10;
classes!(
"class1": x == 10,
"class1": x != 10,
);
License: MPL-2.0