Skip to content

Commit

Permalink
Add a Html::to_web_sys method (#490)
Browse files Browse the repository at this point in the history
Add a Html::to_web_sys method

BREAKING: This is technically a breaking change but is minor enough that it doesn't warrant a major release.
  • Loading branch information
lukechu10 authored Sep 22, 2022
1 parent 7a741f2 commit 2a7d015
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/sycamore-web/src/dom_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ impl GenericNode for DomNode {

impl Html for DomNode {
const IS_BROWSER: bool = true;

fn to_web_sys(&self) -> web_sys::Node {
self.inner_element()
}
}

/// Render a [`View`] into the DOM.
Expand Down
4 changes: 4 additions & 0 deletions packages/sycamore-web/src/hydrate_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ impl GenericNode for HydrateNode {

impl Html for HydrateNode {
const IS_BROWSER: bool = true;

fn to_web_sys(&self) -> web_sys::Node {
self.inner_element()
}
}

/// Render a [`View`] under a `parent` node by reusing existing nodes (client side
Expand Down
6 changes: 6 additions & 0 deletions packages/sycamore-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ pub trait Html: GenericNode<EventType = Event, PropertyType = JsValue> {
/// A value of `false` does not necessarily mean that it is not being rendered in WASM or even
/// in the browser. It only means that it does not create DOM nodes.
const IS_BROWSER: bool;

/// Convert this node into a raw [`web_sys::Node`].
///
/// For certain backends, this is not possible (e.g. [`SsrNode`]). In that case, calling this
/// will panic at runtime.
fn to_web_sys(&self) -> web_sys::Node;
}

/// Create a generic `Html` node from a `web_sys::Node`.
Expand Down
4 changes: 4 additions & 0 deletions packages/sycamore-web/src/ssr_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ impl GenericNode for SsrNode {

impl Html for SsrNode {
const IS_BROWSER: bool = false;

fn to_web_sys(&self) -> web_sys::Node {
panic!("SsrNode cannot be converted into web_sys::Node");
}
}

/// Write the [`SsrNode`] to a string buffer.
Expand Down

1 comment on commit 2a7d015

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 2a7d015 Previous: 7a741f2 Ratio
reactivity_map_indexed 62685 ns/iter (± 2260) 44801 ns/iter (± 377) 1.40

This comment was automatically generated by workflow using github-action-benchmark.

CC: @lukechu10

Please sign in to comment.