-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(POC) Block Bindings: edit external prop value/block attribute #59537
Conversation
* It introduces helper() (get and update) in the API * it populates the block attrobutes by using get() and useMemo * It updates the prop by calling the update() helper * It use the useSource hook to update the attr
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
Size Change: +183 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
import { useEntityProp } from '@wordpress/core-data'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useEntityProp, store as coreStore } from '@wordpress/core-data'; | ||
import { select, dispatch } from '@wordpress/data'; |
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.
We should avoid using global select/dispatch and always rely on useSelect/useDispatch. (we might not be targeting the right store with the global select/dispatch)
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.
We should probably have a lint rule about this at some point :)
@@ -12,33 +12,47 @@ import { store as editorStore } from '../store'; | |||
export default { | |||
name: 'core/post-meta', | |||
label: _x( 'Post Meta', 'block bindings source' ), | |||
useSource( props, sourceAttributes ) { | |||
const { getCurrentPostType } = useSelect( editorStore ); | |||
init( props, sourceAttributes ) { |
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.
When is init run?
What?
This PR allows not only read but edit the external source bound to the block attribute, from the block edit component context.
Why?
Because editing the property value of the external source is required for the Block Binding API.
How?
This PR changes considerably the handler API. There are a few reasons why we should do this. This thread and especially this comment motivates the introductions of these changes:
First, the init() function returns the
value
andplaceholder
valles. Also, theupdate()
helper anduseSource()
react hook:update()
allows updating the prop value of the external source.useSource()
possibilities re-rendering the component when the external value change.Handler API
Reading data workflow
Reading the data from the external source is straightforward since the
value
is not provided by the react hook now.Updating data from the block
It still does uses the effect hook to detect new changes in the external source to re-render the component.
Testing Instructions
Testing Instructions for Keyboard
content
in the core/paragraph block instancesrc
in the core/image blockalt
in the core/image blocktext
in the core/button blockScreenshots or screencast
Screen.Recording.2024-03-04.at.07.54.30.mov