feat: allow multiple class names in view! macro class = (closes #612) (#614)

This commit is contained in:
Greg Johnston 2023-03-03 10:44:15 -05:00 committed by GitHub
parent e29f6a884f
commit c8e6d18139
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 3 deletions

View file

@ -576,6 +576,17 @@ impl<El: ElementDescriptor + 'static> HtmlElement<El> {
}
}
/// Adds a list of classes separated by ASCII whitespace to an element.
#[track_caller]
pub fn classes(self, classes: impl Into<Cow<'static, str>>) -> Self {
let classes = classes.into();
let mut this = self;
for class in classes.split_ascii_whitespace() {
this = this.class(class.to_string(), true);
}
this
}
/// Sets a property on an element.
#[track_caller]
pub fn prop(

View file

@ -107,10 +107,15 @@ pub(crate) fn class_expression(
if force || !HydrationCtx::is_hydrating() {
let class_name = wasm_bindgen::intern(class_name);
if value {
class_list.add_1(class_name).unwrap_throw();
if let Err(e) = class_list.add_1(class_name) {
crate::error!("[HtmlElement::class()] {e:?}");
}
} else {
class_list.remove_1(class_name).unwrap_throw();
if let Err(e) = class_list.remove_1(class_name) {
crate::error!("[HtmlElement::class()] {e:?}");
}
}
}
}

View file

@ -748,7 +748,10 @@ fn element_to_tokens(
None => quote! {},
Some(class) => {
quote! {
.class(#class, true)
.classes(
#[allow(unused_braces)]
#class
)
}
}
};