Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting class on SVG elements #398

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions packages/sycamore-macro/src/view/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,7 @@ impl Codegen {
}
} else {
quote! {
::sycamore::generic_node::GenericNode::set_attribute(
&__el,
#name,
#quoted_text,
);
::sycamore::generic_node::GenericNode::set_attribute(&__el, #name, #quoted_text);
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/sycamore-reactive/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<T> Signal<T> {
}

/// Set the current value of the state wrapped in a [`Rc`] _without_ triggering subscribers.
///
///
/// See the documentation for [`Signal::set_rc()`] for more information.
///
/// Make sure you know what you are doing because this can make state inconsistent.
Expand Down
24 changes: 17 additions & 7 deletions packages/sycamore/src/generic_node/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@ use super::SycamoreElement;
extern "C" {
#[wasm_bindgen(extends = Node)]
pub(super) type NodeWithId;

#[wasm_bindgen(method, getter, js_name = "$$$nodeId")]
pub fn node_id(this: &NodeWithId) -> Option<usize>;

#[wasm_bindgen(method, setter, js_name = "$$$nodeId")]
pub fn set_node_id(this: &NodeWithId, id: usize);
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Element)]
type ElementTrySetClassName;
#[wasm_bindgen(method, catch, setter, js_name = "className")]
fn try_set_class_name(this: &ElementTrySetClassName, class_name: &str) -> Result<(), JsValue>;

#[wasm_bindgen(extends = Document)]
type DocumentCreateTextNodeInt;

#[wasm_bindgen(method, js_name = "createTextNode")]
pub fn create_text_node_int(this: &DocumentCreateTextNodeInt, num: i32) -> web_sys::Text;
}
Expand Down Expand Up @@ -226,7 +225,18 @@ impl GenericNode for DomNode {
}

fn set_class_name(&self, value: &str) {
self.node.unchecked_ref::<Element>().set_class_name(value);
if self
.node
.unchecked_ref::<ElementTrySetClassName>()
.try_set_class_name(value)
.is_err()
{
// Node is a SVG element.
self.node
.unchecked_ref::<Element>()
.set_attribute("class", value)
.unwrap_throw();
}
}

fn add_class(&self, class: &str) {
Expand Down
1 change: 1 addition & 0 deletions packages/sycamore/tests/web/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod keyed;
pub mod portal;
pub mod reconcile;
pub mod render;
pub mod svg;

use sycamore::html;
use sycamore::prelude::*;
Expand Down
13 changes: 13 additions & 0 deletions packages/sycamore/tests/web/svg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::*;

#[wasm_bindgen_test]
fn issue_391_svg_with_class_should_not_use_classname() {
sycamore::render_to(
|cx| {
view! { cx,
svg(class="my-class")
}
},
&test_container(),
);
}