Skip to content

Commit 008e23f

Browse files
committed
fix: mount stateful component into the shadown dom, so stylesheet can be scoped
1 parent 056918d commit 008e23f

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

crates/core/src/dom/component/stateful_component.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ where
114114
}
115115

116116
fn view(&self) -> Node<Self::MSG> {
117-
<Self as Component>::view(self)
117+
Component::view(self)
118118
}
119119

120120
fn stylesheet() -> Vec<String> {
@@ -145,7 +145,11 @@ where
145145
let mut program = Program::from_rc_app(Rc::clone(&app));
146146
let children: Vec<Node<MSG>> = children.into_iter().collect();
147147
let mount_event = on_component_mount(move |me| {
148-
program.mount(&me.target_node.as_node(), MountProcedure::append());
148+
program.mount(&me.target_node.as_node(), MountProcedure::append_to_shadow());
149+
let stylesheet = <COMP as Component>::stylesheet().join("\n");
150+
log::info!("stylesheet: {}", stylesheet);
151+
program.inject_style_to_mount(&stylesheet);
152+
program.inject_style_to_mount(&program.app_context.dynamic_style());
149153
program.update_dom().expect("update dom");
150154
});
151155
Node::Leaf(Leaf::StatefulComponent(StatefulModel {

crates/core/src/dom/dom_node.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ impl From<web_sys::Node> for DomNode {
137137
parent: Rc::new(None),
138138
}
139139
}
140+
Node::DOCUMENT_FRAGMENT_NODE => {
141+
let fragment: web_sys::DocumentFragment = node.unchecked_into();
142+
DomNode {
143+
inner: DomInner::Fragment {
144+
fragment,
145+
children: Rc::new(RefCell::new(vec![])),
146+
},
147+
parent: Rc::new(None),
148+
}
149+
}
140150
_node_type => todo!("for: {_node_type:?}"),
141151
}
142152
}
@@ -838,6 +848,7 @@ where
838848
.borrow_mut()
839849
.attribute_changed(dom_attr.name, dom_attr.value);
840850
}
851+
841852
// the component children is manually appended to the StatefulComponent
842853
// here to allow the conversion of dom nodes with its event
843854
// listener and removing the generics msg

crates/core/src/dom/program.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,27 +353,25 @@ where
353353
.expect("mount node")
354354
.clone(),
355355
MountTarget::ShadowRoot => {
356-
/*
357356
let mount_element: web_sys::Element = self
358357
.mount_node
359358
.borrow()
360359
.as_ref()
361360
.expect("mount node")
362-
.clone()
363-
.unchecked_into();
361+
.as_element();
362+
364363
mount_element
365364
.attach_shadow(&web_sys::ShadowRootInit::new(web_sys::ShadowRootMode::Open))
366365
.expect("unable to attached shadow");
367366
let mount_shadow = mount_element.shadow_root().expect("must have a shadow");
367+
let shadow_node: web_sys::Node = mount_shadow.unchecked_into();
368368

369-
*self.mount_node.borrow_mut() = Some(mount_shadow.unchecked_into());
369+
*self.mount_node.borrow_mut() = Some(DomNode::from(shadow_node));
370370
self.mount_node
371371
.borrow()
372372
.as_ref()
373373
.expect("mount_node")
374374
.clone()
375-
*/
376-
todo!("shadow onhold!..")
377375
}
378376
};
379377

examples/experimentals/src/button.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ impl Component for Button {
4545
<div class="external_children" on_mount=|me|Msg::ExternContMounted(me.target_node)></div>
4646
</button>
4747
}
48+
49+
fn stylesheet() -> Vec<String> {
50+
vec![jss! {
51+
"button": {
52+
background: "#EE88E5",
53+
color: "light blue",
54+
padding: "10px 10px",
55+
margin: "10px 10px",
56+
border: 0,
57+
font_size: "1.5rem",
58+
border_radius: "5px",
59+
}
60+
}]
61+
}
4862
}
4963

5064
impl StatefulComponent for Button {

examples/experimentals/src/date_time.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ where
157157
}]
158158
}
159159

160+
fn style(&self) -> Vec<String>{
161+
vec![".just{some:dynamc_style}".to_string()]
162+
}
163+
160164
fn observed_attributes() -> Vec<AttributeName> {
161165
vec!["date", "time", "interval"]
162166
}

0 commit comments

Comments
 (0)