classnames/
lib.rs

1//!
2//! # Classnames
3//!
4//! An opinionated lLibrary for generating BEM style classnames, in Rust.
5//!
6//! If you don't know BEM, [BEM](http://getbem.com/naming/) it is a set
7//! of naming conventions for CSS names.
8//!
9//! ## Using Classnames
10//!
11//! There are two main things to import ...
12//!
13//!  * `::classnames::classname` - A function for creating classnames.
14//!  * `::classnames::Class` - A trait all classnames implement. Use this for when you want to pass classnames to places that you want to use them.
15//!
16//! The way classnames works, is internally it wraps each Class with
17//! a different Class type. Adding on "\_\_child" and "--attr" classes,
18//! and so on.
19//!
20//! This is to avoid needing to build lots of strings internally,
21//! in order to make it more efficient.
22//!
23//! ### The crux of using Classnames ...
24//!
25//!  1. You call `classname` to create a base classname.
26//!  2. You may then call `.el` to generate a child classname.
27//!  3. You may also call `.attr` to add on any BEM attributes.
28//!  4. You can then add classes together, to allow printing multiple different classnames in a component.
29//!  5. Finally all of the above; the base class, the child names, attributes, and classes added together. All support being turned into a `String`, or being printed with `::std::fmt::Display`. That's how you get the formatted classname out.
30//!
31//! Here is the above again in code ...
32//!
33//! ```
34//! use ::classnames::Class;
35//! use ::classnames::classname;
36//!
37//! fn example() {
38//!   // 1. Prints "banner"
39//!   let base_class = classname("banner");
40//!   println!("{}", base_class);
41//!
42//!   // 2. Prints "banner__header"
43//!   let header = base_class.el("header");
44//!   println!("{}", header);
45//!
46//!   // 3. Prints "banner__header banner__header--bold"
47//!   let bold_header = base_class.el("header").attr("bold");
48//!   println!("{}", bold_header);
49//!
50//!   // 4. Prints "banner pricing-banner"
51//!   let pricing_banner = base_class + classname("pricing-banner");
52//!   println!("{}", pricing_banner);
53//!
54//!   // 5. Prints out HTML with the classes included.
55//!   format!(r#"
56//!       <div class="{base_class}">
57//!         <h1 class="{header_class}">Pricing Information</h1>
58//!
59//!         <p>Example copy</p>
60//!       </div>
61//!   "#, base_class = pricing_banner, header_class = bold_header);
62//! }
63//! ```
64//!
65//! ### Passing classnames to other functions
66//!
67//! All of the internal Classname types implement `::classnames::Class`.
68//! They can be passed by using this type, which you can also wrap in an `Option` for convenience.
69//!
70//! For example ...
71//!
72//! ```
73//! use ::classnames::Class;
74//! use ::classnames::classname;
75//! use ::render::{component, rsx, Render};
76//!
77//! #[component]
78//! pub fn Card<C: Class, Children: Render>(
79//!   class: Option<C>,
80//!   children: Children,
81//! ) -> impl Render {
82//!   let base_class = classname("card");
83//!
84//!   rsx! {
85//!     <div
86//!       class={base_class + class}
87//!     >
88//!       {children}
89//!     </div>
90//!   }
91//! }
92//! ```
93//!
94
95//
96// Internally this crate works by structuring nodes in reverse order.
97//
98// i.e. The following code ...
99//
100//   Node::new("home_page").append("content").append("button")
101//
102// Would produce a structure like this ...
103//
104//  {
105//      base_class: "button",
106//      parent: {
107//          base_class: "content",
108//          parent: {
109//              base_class: "home_page",
110//              parent: None,
111//          }
112//      }
113//  }
114//
115
116mod class;
117pub mod classes;
118
119pub use crate::class::classname;
120pub use crate::class::Class;
121
122#[cfg(test)]
123mod integration {
124    use super::*;
125
126    #[test]
127    fn it_should_print_base_class() {
128        let class = classname("home_page");
129        assert_eq!(class.to_string(), "home_page");
130    }
131
132    #[test]
133    fn it_should_print_base_class_children() {
134        let class = classname("home_page").el("content");
135        assert_eq!(class.to_string(), "home_page__content");
136    }
137
138    #[test]
139    fn it_should_append_classes() {
140        let class = classname("page") + "home_page";
141        assert_eq!(class.to_string(), "page home_page");
142    }
143
144    #[test]
145    fn it_should_append_different_classes() {
146        let class = (classname("page") + classname("home_page").attr("large"))
147            + classname("noscript").el("pane");
148        assert_eq!(
149            class.to_string(),
150            "page home_page home_page--large noscript__pane"
151        );
152    }
153
154    #[test]
155    fn it_should_append_multiple_classes() {
156        let class = classname("page") + "home_page" + "noscript";
157        assert_eq!(class.to_string(), "page home_page noscript");
158    }
159
160    #[test]
161    fn it_should_print_base_class_with_attributes() {
162        let class = classname("home_page").attr("wide");
163        assert_eq!(class.to_string(), "home_page home_page--wide");
164    }
165
166    #[test]
167    fn it_should_print_base_class_with_multiple_attributes() {
168        let class = classname("home_page").attr("wide").attr("bold");
169        assert_eq!(
170            class.to_string(),
171            "home_page home_page--wide home_page--bold"
172        );
173    }
174
175    #[test]
176    fn it_should_print_base_class_children_with_attributes() {
177        let class = classname("home_page").el("content").attr("large");
178        assert_eq!(
179            class.to_string(),
180            "home_page__content home_page__content--large"
181        );
182    }
183
184    #[test]
185    fn it_should_print_base_class_children_with_attributes_with_additions() {
186        let class = classname("home_page").el("content").attr("large") + "noscript";
187        assert_eq!(
188            class.to_string(),
189            "home_page__content home_page__content--large noscript"
190        );
191    }
192
193    #[test]
194    fn it_should_not_print_added_optional_classes_when_none() {
195        let base: Option<classes::BaseClass> = None;
196        let class = classname("page") + "home_page" + base + "noscript";
197        assert_eq!(class.to_string(), "page home_page  noscript");
198    }
199
200    #[test]
201    fn it_should_print_added_optional_classes_when_some() {
202        let base = Some(classname("mobile"));
203        let class = classname("page") + "home_page" + base + "noscript";
204        assert_eq!(class.to_string(), "page home_page mobile noscript");
205    }
206}