wasm-css 0.2.0

Ergonomic WASM CSS Framework
Documentation
// Authors: Robert Lopez

/// Create a `Style` from `css`, rendering it to the DOM as a `<style>` in `<head>`.
/// The `css` is formatted, minfied, and small errors like invalid semi-colons can be
/// corrected, however invalid CSS itself is not handled by the parser.
///
/// Effects remain there order as defined, but their inner fields, as well as the Style's
/// fields will be ordered in a deterministic fashion to avoid duplication.
///
/// *Note*: Trailing semi-colons are required.
///
/// Returns error if missing access to: `Head`, `Crypto`(If generating a random ID),
/// `Window`, `Document`.
///
/// ---
/// Example Usage:
/// ```
///
/// let style: Style = style!(
///     "
///         font-size: {}px;
///
///         &:hover {
///             background-color: rgba(111, 111, 111, 0.1);
///         }
///
///         @media (max-width: 800px) {
///             font-size: {}px;
///         }
///     ",
///     18, 12
/// )?;
/// ```
#[macro_export]
macro_rules! style {
    ($css:expr $(, $arg:expr)*) => {
        $crate::Style::new(&format!($css $(, $arg)*), None)
    };
}

/// Create a `Style` from `css` with a `css_name`, rendering it to the DOM as a `<style>` in `<head>`.
/// The `css` is formatted, minfied, and small errors like invalid semi-colons can be
/// corrected, however invalid CSS itself is not handled by the parser.
///
/// Effects remain there order as defined, but their inner fields, as well as the Style's
/// fields will be ordered in a deterministic fashion to avoid duplication.
///
/// *Note*: Trailing semi-colons are required.
///
/// Returns error if missing access to: `Head`, `Crypto`(If generating a random ID),
/// `Window`, `Document`.
///
/// ---
/// Example Usage:
/// ```
///
/// let style: Style = named_style!(
///     ".my_class",
///     "
///         font-size: {}px;
///
///         &:hover {
///             background-color: rgba(111, 111, 111, 0.1);
///         }
///
///         @media (max-width: 800px) {
///             font-size: {}px;
///         }
///     ",
///     18, 12
/// )?;
/// ```
#[macro_export]
macro_rules! named_style {
    ($css_name:expr, $css:expr $(, $arg:expr)*) => {
        $crate::Style::new(&format!($css $(, $arg)*), Some($css_name))
    };
}

/// Extend `Style` with `css`, re-rendering if `new_css` is not empty.
///
/// Returns error if missing access to: `Head`, `Window`, `Document`.
///
/// ---
/// Example Usage:
/// ```
///
/// let mut style = named_style!(".my_class", "font-size: {}px;", 12)?;
///
/// extend_style_from_css!(
///     &mut style,
///     "
///         gap: 2rem;
///         display: flex;
///     "
/// )?;
/// ```
#[macro_export]
macro_rules! extend_style_from_css {
    ($style:expr, $css:expr $(, $arg:expr)*) => {$style.extend_from_css(&format!($css $(, $arg)*))};
}

/// Reduce a `Style` by removing `keys`, re-rendering if at-least one key is removed.
///
/// Returns error if missing access to: `Head`, `Window`, `Document`.
///
/// ---
/// Example Usage:
/// ```
///
/// let mut style = named_style!(
///     ".my_class",
///     "
///         gap: 1rem;
///         font-size: {}px;
///
///         &:hover {{
///             background-color: red;
///         }}
///     ",
///     12
/// )?;
///
/// reduce_style!(&mut style, "gap", "&:hover")?;
/// ```
#[macro_export]
macro_rules! reduce_style {
    ($style:expr, $($key:expr),*) => {{
        let mut keys = vec![];
        $(vec.push($key.to_string());)*

        $style.reduce(keys)
    }};
}