Added dynatos_html::ElementWithClass.

This commit is contained in:
Filipe Rodrigues 2024-02-04 14:01:12 +00:00
parent e972ae461f
commit d9d9366080
Signed by: zenithsiz
SSH Key Fingerprint: SHA256:Mb5ppb3Sh7IarBO/sBTXLHbYEOz37hJAlslLQPPAPaU
6 changed files with 72 additions and 1 deletions

View File

@ -1,6 +1,7 @@
{
"cSpell.words": [
"Dynatos",
"itertools",
"popstate",
"unsize"
],

17
Cargo.lock generated
View File

@ -61,7 +61,9 @@ version = "0.1.0"
dependencies = [
"duplicate",
"extend",
"itertools",
"js-sys",
"tracing",
"wasm-bindgen",
"web-sys",
]
@ -114,6 +116,12 @@ dependencies = [
"web-sys",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "extend"
version = "1.2.0"
@ -150,6 +158,15 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]]
name = "js-sys"
version = "0.3.67"

View File

@ -25,6 +25,7 @@ dynatos-util = { path = "dynatos-util" }
anyhow = "1.0.79"
duplicate = "1.0.0"
extend = "1.2.0"
itertools = "0.12.1"
js-sys = "0.3.67"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View File

@ -7,6 +7,8 @@ edition = "2021"
duplicate = { workspace = true }
extend = { workspace = true }
itertools = { workspace = true }
js-sys = { workspace = true }
tracing = { workspace = true }
wasm-bindgen = { workspace = true }
web-sys = { workspace = true }

View File

@ -7,7 +7,7 @@
pub mod html;
// Imports
use wasm_bindgen::JsValue;
use {itertools::Itertools, wasm_bindgen::JsValue};
/// Extension trait to set the text content in a builder-style.
#[extend::ext_sized(name = NodeWithTextContent)]
@ -152,3 +152,36 @@ where
.map(|()| self)
}
}
/// Extension trait to *append* a class in a builder-style.
#[extend::ext_sized(name = ElementWithClass)]
pub impl<T> T
where
T: AsRef<web_sys::Element>,
{
fn with_class<C>(self, class: C) -> Self
where
C: AsRef<str>,
{
self.with_classes([class])
}
fn with_classes<I, C>(self, classes: I) -> Self
where
I: IntoIterator<Item = C>,
C: AsRef<str>,
{
// Append all classes to the existing class name and set it.
// TODO: Not allocate the classes here.
let classes = classes.into_iter().collect::<Vec<_>>();
let class_name = self
.as_ref()
.class_name()
.split_whitespace()
.chain(classes.iter().map(C::as_ref))
.join(" ");
self.as_ref().set_class_name(&class_name);
self
}
}

17
examples/Cargo.lock generated
View File

@ -88,7 +88,9 @@ version = "0.1.0"
dependencies = [
"duplicate",
"extend",
"itertools",
"js-sys",
"tracing",
"wasm-bindgen",
"web-sys",
]
@ -141,6 +143,12 @@ dependencies = [
"web-sys",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "extend"
version = "1.2.0"
@ -177,6 +185,15 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "itertools"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
[[package]]
name = "js-sys"
version = "0.3.67"