wasm_css/extend_from_style.rs
1// Authors: Robert Lopez
2
3use super::Style;
4use crate::error::WasmCssError;
5
6impl Style {
7 /// Extend `Style` with `Style`, re-rendering if `s2` is not empty.
8 ///
9 /// Returns error if missing access to: `Head`, `Window`, `Document`.
10 ///
11 /// ---
12 /// Example Usage:
13 /// ```
14 ///
15 /// let mut style1 = named_style!(
16 /// "my_style",
17 /// "
18 /// display: flex;
19 /// flex-direction: row;
20 /// "
21 /// )?;
22 ///
23 ///
24 /// let style2 = style!(
25 /// "
26 /// color: red;
27 /// flex-direction: column;
28 /// "
29 /// )?;
30 ///
31 /// style1.extend_from_style(&style2)?;
32 /// ```
33 pub fn extend_from_style(&mut self, s2: &Style) -> Result<(), WasmCssError> {
34 if !s2.components.fields.is_empty() || !s2.components.effects.is_empty() {
35 self.components.fields.extend(s2.components.fields.clone());
36
37 'effect_loop: for effect in s2.components.effects.iter() {
38 for index in 0..self.components.effects.len() {
39 if effect.key == self.components.effects[index].key {
40 self.components.effects[index] = effect.to_owned();
41 continue 'effect_loop;
42 }
43 }
44
45 self.components.effects.push(effect.to_owned());
46 }
47
48 self.css = self.components.to_css(&self.css_name);
49 self.render()?;
50 }
51
52 Ok(())
53 }
54}