wasm-css 0.2.0

Ergonomic WASM CSS Framework
Documentation
// Authors: Robert Lopez

use super::Style;
use crate::error::WasmCssError;

impl Style {
    /// Extend `Style` with `Style`, re-rendering if `s2` is not empty.
    ///
    /// Returns error if missing access to: `Head`, `Window`, `Document`.
    ///
    /// ---
    /// Example Usage:
    /// ```
    ///
    /// let mut style1 = named_style!(
    ///     "my_style",
    ///     "
    ///         display: flex;
    ///         flex-direction: row;
    ///     "
    /// )?;
    ///
    ///
    /// let style2 = style!(
    ///     "
    ///         color: red;
    ///         flex-direction: column;
    ///     "
    /// )?;
    ///
    /// style1.extend_from_style(&style2)?;
    /// ```
    pub fn extend_from_style(&mut self, s2: &Style) -> Result<(), WasmCssError> {
        if !s2.components.fields.is_empty() || !s2.components.effects.is_empty() {
            self.components.fields.extend(s2.components.fields.clone());

            'effect_loop: for effect in s2.components.effects.iter() {
                for index in 0..self.components.effects.len() {
                    if effect.key == self.components.effects[index].key {
                        self.components.effects[index] = effect.to_owned();
                        continue 'effect_loop;
                    }
                }

                self.components.effects.push(effect.to_owned());
            }

            self.css = self.components.to_css(&self.css_name);
            self.render()?;
        }

        Ok(())
    }
}