-
Notifications
You must be signed in to change notification settings - Fork 11
⚛️ Hydrate the blocks with Directives Hydration (using wp-block
and wp-inner-blocks
)
#66
⚛️ Hydrate the blocks with Directives Hydration (using wp-block
and wp-inner-blocks
)
#66
Conversation
wp-block
and wp-inner-blocks
)
For context, using components from different entry points was failing because each entry point had its own instance of |
I consider this PR ready to review. There is a problem with hydration, though, that it is not working correctly (elements are being regenerated). We should investigate that. cc: @luisherranz |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, David!
I think it's ready to merge, and we can continue in other PRs. I've asked a few questions, though.
Also, I'm not sure yet if it'd be better to use the visitor
pattern or Preact options
. I'm inclined to think Preact options
are safer.
src/gutenberg-packages/hydration.js
Outdated
visitor.map = components; | ||
|
||
const dom = document.querySelector('.wp-site-blocks'); | ||
const vdom = toVdom(dom, visitor, createElement, {}).props.children; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need .props.children
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you run hydrate
on the parent element (wp-site-blocks
), but you pass the vdom of the content (wp-site-blocks
works as a wrapper).
We can change this with a better approach in the future. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see. I'm using createRootFragment for that 🙂
return text; | ||
} | ||
if (n.nodeType !== 1) return null; | ||
let nodeName = String(n.nodeName).toLowerCase(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a possibility that a node name is not in lowercase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the node is an Element, it always return the uppercase name (e.g. DIV
for <div>
). 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I was using node.localName
🙂
// The block content comes between two line breaks that seem to be included during block | ||
// serialization, corresponding to those between the block markup and the block content. | ||
// | ||
// They need to be removed here; otherwise, the preact hydration fails. | ||
return sprintf($block_wrapper, substr($block_content, 1, -1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a note here explaining the fix for the Preact hydration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed in Gutenberg itself.
wp-block
and wp-inner-blocks
)wp-block
and wp-inner-blocks
)
wp-block
and wp-inner-blocks
)wp-block
and wp-inner-blocks
)
Tracking issue: #64
This PR uses the
toVdom()
function from preact-markup to hydrate blocks inside a single vDOM tree.It uses
<wp-block>
and<wp-inner-blocks>
tags to detect both block components and children.data-wp-block-props
data-wp-block-attributes
data-wp-block-sourced-attributes
I'm planning to implement these directives in a different PR, to test how it could be done with the preact option hooks API.
data-wp-block-provides-block-context
data-wp-block-uses-block-context