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

feat(web): using None instead of Element #3012

Merged
merged 1 commit into from
Sep 30, 2024
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
14 changes: 12 additions & 2 deletions packages/web/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use wasm_bindgen::JsCast as _;

/// Configuration for the WebSys renderer for the Dioxus VirtualDOM.
///
/// This struct helps configure the specifics of hydration and render destination for WebSys.
Expand All @@ -15,7 +17,7 @@ pub struct Config {

pub(crate) enum ConfigRoot {
RootName(String),
RootElement(web_sys::Element),
RootNode(web_sys::Node),
}

impl Config {
Expand Down Expand Up @@ -52,7 +54,15 @@ impl Config {
///
/// This is akin to calling React.render() on the given element.
pub fn rootelement(mut self, elem: web_sys::Element) -> Self {
self.root = ConfigRoot::RootElement(elem);
self.root = ConfigRoot::RootNode(elem.unchecked_into());
self
}

/// Set the node that Dioxus will use as root.
///
/// This is akin to calling React.render() on the given element.
pub fn rootnode(mut self, node: web_sys::Node) -> Self {
self.root = ConfigRoot::RootNode(node);
self
}

Expand Down
8 changes: 4 additions & 4 deletions packages/web/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use dioxus_core::{ElementId, Template};
use dioxus_interpreter_js::unified_bindings::Interpreter;
use rustc_hash::FxHashMap;
use wasm_bindgen::{closure::Closure, JsCast};
use web_sys::{Document, Element, Event, Node};
use web_sys::{Document, Event, Node};

use crate::{load_document, virtual_event_from_websys_event, Config, WebEventConverter};

pub struct WebsysDom {
#[allow(dead_code)]
pub(crate) root: Element,
pub(crate) root: Node,
pub(crate) document: Document,
pub(crate) templates: FxHashMap<Template, u16>,
pub(crate) interpreter: Interpreter,
Expand Down Expand Up @@ -67,9 +67,9 @@ impl WebsysDom {
document.create_element("body").ok().unwrap()
}
};
(document, root)
(document, root.unchecked_into())
}
crate::cfg::ConfigRoot::RootElement(root) => {
crate::cfg::ConfigRoot::RootNode(root) => {
let document = match root.owner_document() {
Some(document) => document,
None => load_document(),
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/hydration/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl WebsysDom {

// Rehydrate the root scope that was rendered on the server. We will likely run into suspense boundaries.
// Any suspense boundaries we run into are stored for hydration later.
self.start_hydration_at_scope(vdom.base_scope(), vdom, vec![self.root.clone().into()])?;
self.start_hydration_at_scope(vdom.base_scope(), vdom, vec![self.root.clone()])?;

Ok(rx)
}
Expand Down
Loading