WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.53k stars 4.21k forks source link

SlotFill: rewrite base Slot to functional, unify rerenderable refs #67153

Closed jsnajdr closed 2 days ago

jsnajdr commented 2 days ago

This is the last SlotFill refactor that I originally included in #67000 and later decided to merge one-by-one.

The main outcome is refactoring the (base) Slot component from a class component to functional. The componentDidMount/DidUpdate code is converted to a useEffect. The render code is the same, it just moved from the render() method to the functional component body.

The class version registered its own instance, i.e., this, to the slot registry. But functional components don't have instances like this. That's why I'm creating a Renderable interface with a rerender method, storing a constant per-instance value to a ref, and registering this interface instead.

The values in the context that were previously of type Component< BaseSlotComponentProps > are now Rerenderable.

It turns out that exactly the same Renderable interface is already used by bubbles-virtually Fill, so I'm reusing the same code (useForceUpdate).

Last minor update I did is renaming the SlotFillBubblesVirtuallySlotRef to SlotRef, to make the name shorter and prevent the code that uses it from having too many linebreaks.

github-actions[bot] commented 2 days ago

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

tyxla commented 2 days ago

It would be nice to get a second opinion, just in case, but it's not a blocker :)

I'm actually taking a look right now.