dom.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //! Implementation of a renderer for Dioxus on the web.
  2. //!
  3. //! Outstanding todos:
  4. //! - Passive event listeners
  5. //! - no-op event listener patch for safari
  6. //! - tests to ensure dyn_into works for various event types.
  7. //! - Partial delegation?
  8. use std::{any::Any, rc::Rc};
  9. use dioxus_core::Runtime;
  10. use dioxus_core::{ElementId, Template};
  11. use dioxus_interpreter_js::unified_bindings::Interpreter;
  12. use rustc_hash::FxHashMap;
  13. use wasm_bindgen::{closure::Closure, JsCast};
  14. use web_sys::{Document, Event, Node};
  15. use crate::{load_document, virtual_event_from_websys_event, Config, WebEventConverter};
  16. pub struct WebsysDom {
  17. #[allow(dead_code)]
  18. pub(crate) root: Node,
  19. pub(crate) document: Document,
  20. pub(crate) templates: FxHashMap<Template, u16>,
  21. pub(crate) interpreter: Interpreter,
  22. #[cfg(feature = "mounted")]
  23. pub(crate) runtime: Rc<Runtime>,
  24. #[cfg(feature = "mounted")]
  25. pub(crate) queued_mounted_events: Vec<ElementId>,
  26. // We originally started with a different `WriteMutations` for collecting templates during hydration.
  27. // When profiling the binary size of web applications, this caused a large increase in binary size
  28. // because diffing code in core is generic over the `WriteMutation` object.
  29. //
  30. // The fact that diffing is generic over WriteMutations instead of dynamic dispatch or a vec is nice
  31. // because we can directly write mutations to sledgehammer and avoid the runtime and binary size overhead
  32. // of dynamic dispatch
  33. //
  34. // Instead we now store a flag to see if we should be writing templates at all if hydration is enabled.
  35. // This has a small overhead, but it avoids dynamic dispatch and reduces the binary size
  36. //
  37. // NOTE: running the virtual dom with the `write_mutations` flag set to true is different from running
  38. // it with no mutation writer because it still assigns ids to nodes, but it doesn't write them to the dom
  39. #[cfg(feature = "hydrate")]
  40. pub(crate) skip_mutations: bool,
  41. #[cfg(feature = "hydrate")]
  42. pub(crate) suspense_hydration_ids: crate::hydration::SuspenseHydrationIds,
  43. }
  44. impl WebsysDom {
  45. pub fn new(cfg: Config, runtime: Rc<Runtime>) -> Self {
  46. let (document, root) = match cfg.root {
  47. crate::cfg::ConfigRoot::RootName(rootname) => {
  48. // eventually, we just want to let the interpreter do all the work of decoding events into our event type
  49. // a match here in order to avoid some error during runtime browser test
  50. let document = load_document();
  51. let root = match document.get_element_by_id(&rootname) {
  52. Some(root) => root,
  53. None => {
  54. web_sys::console::error_1(
  55. &format!("element '#{}' not found. mounting to the body.", rootname)
  56. .into(),
  57. );
  58. document.create_element("body").ok().unwrap()
  59. }
  60. };
  61. (document, root.unchecked_into())
  62. }
  63. crate::cfg::ConfigRoot::RootNode(root) => {
  64. let document = match root.owner_document() {
  65. Some(document) => document,
  66. None => load_document(),
  67. };
  68. (document, root)
  69. }
  70. };
  71. let interpreter = Interpreter::default();
  72. // The closure type we pass to the dom may be invoked recursively if one event triggers another. For example,
  73. // one event could focus another element which triggers the focus event of the new element like inhttps://github.com/DioxusLabs/dioxus/issues/2882.
  74. // The Closure<dyn Fn(_)> type can invoked recursively, but Closure<dyn FnMut()> cannot
  75. let handler: Closure<dyn Fn(&Event)> = Closure::wrap(Box::new({
  76. let runtime = runtime.clone();
  77. move |web_sys_event: &web_sys::Event| {
  78. let name = web_sys_event.type_();
  79. let element = walk_event_for_id(web_sys_event);
  80. let bubbles = web_sys_event.bubbles();
  81. let Some((element, target)) = element else {
  82. return;
  83. };
  84. let data = virtual_event_from_websys_event(web_sys_event.clone(), target);
  85. let event = dioxus_core::Event::new(Rc::new(data) as Rc<dyn Any>, bubbles);
  86. runtime.handle_event(name.as_str(), event.clone(), element);
  87. // Prevent the default action if the user set prevent default on the event
  88. let prevent_default = !event.default_action_enabled();
  89. // Prevent forms from submitting and redirecting
  90. if name == "submit" {
  91. // On forms the default behavior is not to submit, if prevent default is set then we submit the form
  92. if !prevent_default {
  93. web_sys_event.prevent_default();
  94. }
  95. } else if prevent_default {
  96. web_sys_event.prevent_default();
  97. }
  98. }
  99. }));
  100. let _interpreter = interpreter.base();
  101. _interpreter.initialize(
  102. root.clone().unchecked_into(),
  103. handler.as_ref().unchecked_ref(),
  104. );
  105. dioxus_html::set_event_converter(Box::new(WebEventConverter));
  106. handler.forget();
  107. Self {
  108. document,
  109. root,
  110. interpreter,
  111. templates: FxHashMap::default(),
  112. #[cfg(feature = "mounted")]
  113. runtime,
  114. #[cfg(feature = "mounted")]
  115. queued_mounted_events: Default::default(),
  116. #[cfg(feature = "hydrate")]
  117. skip_mutations: false,
  118. #[cfg(feature = "hydrate")]
  119. suspense_hydration_ids: Default::default(),
  120. }
  121. }
  122. }
  123. fn walk_event_for_id(event: &web_sys::Event) -> Option<(ElementId, web_sys::Element)> {
  124. let target = event
  125. .target()
  126. .expect("missing target")
  127. .dyn_into::<web_sys::Node>()
  128. .expect("not a valid node");
  129. walk_element_for_id(&target)
  130. }
  131. fn walk_element_for_id(target: &Node) -> Option<(ElementId, web_sys::Element)> {
  132. let mut current_target_element = target.dyn_ref::<web_sys::Element>().cloned();
  133. loop {
  134. match (
  135. current_target_element
  136. .as_ref()
  137. .and_then(|el| el.get_attribute("data-dioxus-id").map(|f| f.parse())),
  138. current_target_element,
  139. ) {
  140. // This node is an element, and has a dioxus id, so we can stop walking
  141. (Some(Ok(id)), Some(target)) => return Some((ElementId(id), target)),
  142. // Walk the tree upwards until we actually find an event target
  143. (None, target_element) => {
  144. let parent = match target_element.as_ref() {
  145. Some(el) => el.parent_element(),
  146. // if this is the first node and not an element, we need to get the parent from the target node
  147. None => target.parent_element(),
  148. };
  149. match parent {
  150. Some(parent) => current_target_element = Some(parent),
  151. _ => return None,
  152. }
  153. }
  154. // This node is an element with an invalid dioxus id, give up
  155. _ => return None,
  156. }
  157. }
  158. }