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
/// Css chunk, represented either as static or dynamic string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CssGroup {
// &str - can be used as id using which we can find particular rule
CssStatic { value: &'static str },
// string in this case, is a key to hashmap with the class name
CssDynamic { value: String },
CssMedia {
query: String,
rules: Vec<String>,
}
}
/// CSS styles definition for usable in DOM.
///
/// Consists of a vector of css chunks which can be extended.
///
/// ```rust
/// use vertigo::{Css, CssGroup, dom};
///
/// let blue_text = Css::str("color: blue");
/// let black_background = Css::str("background: black");
///
/// let my_styles = Css::str("
/// font-family: courier;
/// font-size: 160%
/// ")
/// .extend(blue_text)
/// .extend(black_background);
///
/// let element = dom! { <div css={my_styles} /> };
/// ```
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Css {
pub groups: Vec<CssGroup>,
}
impl Css {
pub fn str(value: &'static str) -> Self {
Self {
groups: vec![CssGroup::CssStatic { value }],
}
}
pub fn string(value: String) -> Self {
Self {
groups: vec![CssGroup::CssDynamic { value }],
}
}
#[must_use]
pub fn push_str(mut self, value: &'static str) -> Self {
self.groups.push(CssGroup::CssStatic { value });
self
}
pub fn push_string(&mut self, value: String) {
self.groups.push(CssGroup::CssDynamic { value })
}
#[must_use]
pub fn extend(mut self, new_css: Self) -> Self {
for item in new_css.groups {
self.groups.push(item);
}
self
}
}