-
Notifications
You must be signed in to change notification settings - Fork 10
Add wrapperless blocks, hook shims and a block support #3
Conversation
|
||
const noop = () => {}; | ||
|
||
export const useState = (init) => (isView ? useReactState(init) : [init, noop]); |
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'm curious whether your thinking here is to leave these as noop
functions forever or whether it is using a tree shaken smaller version of react instead of the WordPress global? 🤔
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.
The noop functions are for Save
and in the Editor it doesn't make much sense to do tree-shaking (at least is not an urgent priority). In the frontend we need the real hooks.
By the way, I'd love to not use globals in the frontend. They are not needed for backwards compatibility so we can switch to regular imports 🙂
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.
Ahh yeah, I mistook that as something that was being used on the frontend. And was confused about why we don't want/need reactivity on there.
|
||
return ( | ||
<> | ||
<div {...blockProps}> |
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.
Funny thing, if we remove blockProps
, React doesn't remove the classes present in the HTML on hydration.
I want to investigate this more.
</div> | ||
); | ||
const save = (Comp) => ({ attributes }) => { | ||
const blockProps = useBlockProps.save(); |
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 think we somehow need to expose the useBlockProps.save
funciton call to the developer in the frontent/block.js
file because that is the only way to add additional attributes like the className to it.
Mayve we can make it completely hidden though. What if the developer would be able to import a Block
component that works like this:
function BlockSave(props) {
const { attributes } = props;
return (
<Block className="custom-class">
<Title message={attributes.message} />
</Block>
);
}
which would call useBlockProps.save
under the hood and spread all the props into the useBlockProps.save
callback
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.
Yes, that's definitely an implementation detail that should be hidden because it doesn't add any value to the developer. Let's see if we can get rid of it 🙂
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 added a Readme. Feel free to add more items to the list.
I'm going to merge this one to continue opening other PRs on top of it. |
Continuing the tests here, I've added support for:
Block
component can control the wrapperdiv
as well.useState
anduseEffect
inBlock
using @fabiankaegy's trick. This will need to replace replaced with something natively supported by Gutenberg. I'd like to take a look at the custom serializer with @gziolo.has-text-color
... class names) because I thought React would remove them, but it doesn't seem to do so. I need to investigate more.The block now hides/shows the children to test that it can destroy and recreate them while preserving hydration correctly.