Struct wasm_css::Style

source ·
pub struct Style {
    pub css: String,
    pub class: String,
    pub global: bool,
    pub effects: HashMap<String, String>,
    pub components: HashMap<String, String>,
}
Expand description

Represents a CSS Style

Fields§

§css: String§class: String§global: bool§effects: HashMap<String, String>§components: HashMap<String, String>

Implementations§

source§

impl Style

source

pub fn new( css: String, target: Option<String>, global: bool, ) -> Result<Self, WasmCssError>

Construct a Style from a CSS string

global simply removes the . from the class name, making it a global style, and requires you provide a target E.x. “html”

  • See style! and global_style! for built-in formatting

  • Generates random class name if one is not provided

  • Returns WasmCssError if unable to locate Crypto when generating uuid

  • Returns WasmCssError if unable to locate Document/Window/Head

  • Returns WasmCssError if global is true and target is None


Example Usage:


let style: Style = Style::new(
    "
        gap: 10rem;
        display: flex;
        flex-direction: column;
    ",
    None,
)?;
source

pub fn class(&self) -> &String

Returns this Styles class name

source

pub fn append(&mut self, source: &Style) -> Result<(), WasmCssError>

Append another Style to this one, overwriting any fields on self matching with source, or adding them if they did not previously exist, updating the Style


Example Usage:


let mut style_one = style!("color: red; font-size: 10rem;")?;

let style_two = style!("font-size: 30rem; background-color: red;")?;

style_one.append(&style_two)?;

// style_one.css: "color: red; font-size: 30rem; background-color: red;"
source

pub fn insert<V: Into<String>>( &mut self, key: &str, value: V, ) -> Result<(), WasmCssError>

Insert a new CSS line into the Style

  • Skips invalid keys

Example Usage:


let mut style = style!("color: red; font-size: 10rem;")?;

style.insert("font-weight", "bold");
source

pub fn remove(&mut self, key: &str) -> Result<(), WasmCssError>

Remove a CSS line from Style


Example Usage:


let mut style = style!("color: red; font-size: 10rem;")?;

style.remove("color", "red")?;
source

pub fn insert_many<V: Into<String>>( &mut self, components: Vec<(&str, V)>, ) -> Result<(), WasmCssError>

Insert many CSS lines into the Style

  • Skips invalid keys

Example Usage:


let mut style = style!("color: red; font-size: 10rem;")?;

style.insert_many(vec![
    ("font-weight", "bold"),
    ("background-color", "red")
])?;
source

pub fn remove_many(&mut self, keys: Vec<&str>) -> Result<(), WasmCssError>

Remove CSS lines from Style


Example Usage:


let mut style = style!("color: red; font-size: 10rem;")?;

style.remove("color", "red")?;
source

pub fn insert_effect<E: Into<String>>( &mut self, effect: E, ) -> Result<(), WasmCssError>

Add a effect to the Style

Effect meaning sub-components to a class like MediaQuery, Hover, Etc.

The ID of the effect becomes its tag component.


Example Usage:


let mut style = style!("color: red;")?;

// Effect ID = '&:hover'
style.insert_effect("&:hover { font-size: 10rem; }")?;
source

pub fn remove_effect(&mut self, id: &str) -> Result<(), WasmCssError>

Remove a effect from the Style

Effect meaning sub-components to a class like MediaQuery, Hover, Etc.

The ID or name of the added effect is just its tag.


Example Usage:


let mut style = style!("color: red;")?;

// Effect ID = '&:hover'
style.insert_effect("&:hover { font-size: 10rem; };")?;

style.remove_effect("&:hover")?;
source

pub fn insert_effect_many<E: Into<String>>( &mut self, effects: Vec<E>, ) -> Result<(), WasmCssError>

Add many effects to the Style

Effect meaning sub-components to a class like MediaQuery, Hover, Etc.

The ID of the effect becomes its tag component.


Example Usage:


let mut style = style!("color: red;")?;

// Effect IDs = '&:hover' & '@media (max-width: 400px)'
style.insert_effect_many(vec![
    "&:hover { font-size: 10rem; }",
    "@media (max-width: 400px) { color: blue; }"
])?;
source

pub fn remove_effect_many( &mut self, effect_ids: Vec<&str>, ) -> Result<(), WasmCssError>

Remove many effects from the Style

Effect meaning sub-components to a class like MediaQuery, Hover, Etc.

The ID of the effect becomes its tag component.


Example Usage:


let mut style = style!("color: red;")?;

// Effect IDs = '&:hover' & '@media (max-width: 400px)'
style.insert_effect_many(vec![
    "&:hover { font-size: 10rem; }",
    "@media (max-width: 400px) { color: blue; }"
])?;

style.remove_effect_many(vec!["&:hover", "@media (max-width: 400px)"])?;

Trait Implementations§

source§

impl Clone for Style

source§

fn clone(&self) -> Style

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Style

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl UnwindSafe for Style

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.