Merge branch 'leptos_dom_v2' of https://github.com/jquesada2016/leptos into leptos_dom_v2

This commit is contained in:
Jose Quesada 2022-12-17 11:57:25 -06:00
commit 234e1cda4e
6 changed files with 40 additions and 12 deletions

View file

@ -94,6 +94,7 @@ where
use leptos_dom::DynChild;
let cached_id = HydrationCtx::peak();
let _space_for_inner = HydrationCtx::id();
DynChild::new(move || {
if context.ready() {

View file

@ -110,9 +110,11 @@ where
let prev_child = RefCell::new(None);
let cached_id = HydrationCtx::peak();
let _space_for_inner = HydrationCtx::id();
DynChild::new(move || {
if context.ready() {
leptos_dom::warn!("<Transition/> ready and continuing from {}", cached_id);
HydrationCtx::continue_from(cached_id);
let current_child = child(cx).into_view(cx);

View file

@ -125,9 +125,12 @@ impl IntoView for ComponentRepr {
impl ComponentRepr {
/// Creates a new [`Component`].
pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
let name = name.into();
Self::new_with_id(name, HydrationCtx::id())
}
let id = HydrationCtx::id();
/// Creates a new [`Component`] with the given hydration ID.
pub fn new_with_id(name: impl Into<Cow<'static, str>>, id: usize) -> Self {
let name = name.into();
let markers = (
Comment::new(Cow::Owned(format!("</{name}>")), id, true),
@ -169,6 +172,7 @@ impl ComponentRepr {
id,
}
}
}
/// A user-defined `leptos` component.

View file

@ -1,6 +1,6 @@
use leptos_reactive::Scope;
use crate::{ComponentRepr, IntoView, View};
use crate::{ComponentRepr, IntoView, View, HydrationCtx};
/// Trait for converting any iterable into a [`Fragment`].
pub trait IntoFragment {
@ -20,7 +20,10 @@ where
/// Represents a group of [`views`](View).
#[derive(Debug, Clone)]
pub struct Fragment(Vec<View>);
pub struct Fragment {
id: usize,
nodes: Vec<View>
}
impl FromIterator<View> for Fragment {
fn from_iter<T: IntoIterator<Item = View>>(iter: T) -> Self {
@ -37,21 +40,34 @@ impl From<View> for Fragment {
impl Fragment {
/// Creates a new [`Fragment`] from a [`Vec<Node>`].
pub fn new(nodes: Vec<View>) -> Self {
Self(nodes)
Self::new_with_id(HydrationCtx::id(), nodes)
}
/// Creates a new [`Fragment`] with the given hydration ID from a [`Vec<Node>`].
pub fn new_with_id(id: usize, nodes: Vec<View>) -> Self {
Self {
id,
nodes
}
}
/// Gives access to the [View] children contained within the fragment.
pub fn as_children(&self) -> &[View] {
&self.0
&self.nodes
}
/// Returns the fragment's hydration ID.
pub fn id(&self) -> usize {
self.id
}
}
impl IntoView for Fragment {
#[cfg_attr(debug_assertions, instrument(level = "trace", name = "</>", skip_all, fields(children = self.0.len())))]
#[cfg_attr(debug_assertions, instrument(level = "trace", name = "</>", skip_all, fields(children = self.nodes.len())))]
fn into_view(self, cx: leptos_reactive::Scope) -> View {
let mut frag = ComponentRepr::new("");
let mut frag = ComponentRepr::new_with_id("", self.id);
frag.children = self.0;
frag.children = self.nodes;
frag.into_view(cx)
}

View file

@ -29,7 +29,8 @@ impl HydrationCtx {
unsafe { ID }
}
pub(crate) fn id() -> usize {
/// Increments the current hydration `id` and returns it
pub fn id() -> usize {
unsafe {
let id = ID;

View file

@ -31,7 +31,8 @@ pub fn Routes(
});
let mut branches = Vec::new();
let children = children(cx)
let frag = children(cx);
let children = frag
.as_children()
.iter()
.filter_map(|child| {
@ -198,7 +199,10 @@ pub fn Routes(
})
});
move || root.get()
Fragment::new_with_id(
frag.id(),
vec![(move || root.get()).into_view(cx)]
)
}
#[derive(Clone, Debug, PartialEq)]