wasm_css/
macros.rs

1// Authors: Robert Lopez
2
3/// Create a `Style` from `css`, rendering it to the DOM as a `<style>` in `<head>`.
4/// The `css` is formatted, minfied, and small errors like invalid semi-colons can be
5/// corrected, however invalid CSS itself is not handled by the parser.
6///
7/// Effects remain there order as defined, but their inner fields, as well as the Style's
8/// fields will be ordered in a deterministic fashion to avoid duplication.
9///
10/// *Note*: Trailing semi-colons are required.
11///
12/// Returns error if missing access to: `Head`, `Crypto`(If generating a random ID),
13/// `Window`, `Document`.
14///
15/// ---
16/// Example Usage:
17/// ```
18///
19/// let style: Style = style!(
20///     "
21///         font-size: {}px;
22///
23///         &:hover {
24///             background-color: rgba(111, 111, 111, 0.1);
25///         }
26///
27///         @media (max-width: 800px) {
28///             font-size: {}px;
29///         }
30///     ",
31///     18, 12
32/// )?;
33/// ```
34#[macro_export]
35macro_rules! style {
36    ($css:expr $(, $arg:expr)*) => {
37        $crate::Style::new(&format!($css $(, $arg)*), None)
38    };
39}
40
41/// Create a `Style` from `css` with a `css_name`, rendering it to the DOM as a `<style>` in `<head>`.
42/// The `css` is formatted, minfied, and small errors like invalid semi-colons can be
43/// corrected, however invalid CSS itself is not handled by the parser.
44///
45/// Effects remain there order as defined, but their inner fields, as well as the Style's
46/// fields will be ordered in a deterministic fashion to avoid duplication.
47///
48/// *Note*: Trailing semi-colons are required.
49///
50/// Returns error if missing access to: `Head`, `Crypto`(If generating a random ID),
51/// `Window`, `Document`.
52///
53/// ---
54/// Example Usage:
55/// ```
56///
57/// let style: Style = named_style!(
58///     ".my_class",
59///     "
60///         font-size: {}px;
61///
62///         &:hover {
63///             background-color: rgba(111, 111, 111, 0.1);
64///         }
65///
66///         @media (max-width: 800px) {
67///             font-size: {}px;
68///         }
69///     ",
70///     18, 12
71/// )?;
72/// ```
73#[macro_export]
74macro_rules! named_style {
75    ($css_name:expr, $css:expr $(, $arg:expr)*) => {
76        $crate::Style::new(&format!($css $(, $arg)*), Some($css_name))
77    };
78}
79
80/// Extend `Style` with `css`, re-rendering if `new_css` is not empty.
81///
82/// Returns error if missing access to: `Head`, `Window`, `Document`.
83///
84/// ---
85/// Example Usage:
86/// ```
87///
88/// let mut style = named_style!(".my_class", "font-size: {}px;", 12)?;
89///
90/// extend_style_from_css!(
91///     &mut style,
92///     "
93///         gap: 2rem;
94///         display: flex;
95///     "
96/// )?;
97/// ```
98#[macro_export]
99macro_rules! extend_style_from_css {
100    ($style:expr, $css:expr $(, $arg:expr)*) => {$style.extend_from_css(&format!($css $(, $arg)*))};
101}
102
103/// Reduce a `Style` by removing `keys`, re-rendering if at-least one key is removed.
104///
105/// Returns error if missing access to: `Head`, `Window`, `Document`.
106///
107/// ---
108/// Example Usage:
109/// ```
110///
111/// let mut style = named_style!(
112///     ".my_class",
113///     "
114///         gap: 1rem;
115///         font-size: {}px;
116///
117///         &:hover {{
118///             background-color: red;
119///         }}
120///     ",
121///     12
122/// )?;
123///
124/// reduce_style!(&mut style, "gap", "&:hover")?;
125/// ```
126#[macro_export]
127macro_rules! reduce_style {
128    ($style:expr, $($key:expr),*) => {{
129        let mut keys = vec![];
130        $(vec.push($key.to_string());)*
131
132        $style.reduce(keys)
133    }};
134}