-
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
Block Editor: Add useEditorFeature hook to simplify access to editor features #21646
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/block-editor/src/components/use-editor-feature/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Hook that retrieves the setting for the given editor feature. | ||
* | ||
* @param {string} featureName The name of the feature. | ||
* | ||
* @return {any} Returns the value defined for the setting. | ||
*/ | ||
export default function useEditorFeature( featureName ) { | ||
const setting = useSelect( | ||
( select ) => { | ||
const { getSettings } = select( 'core/block-editor' ); | ||
|
||
return getSettings()[ featureName ]; | ||
}, | ||
[ featureName ] | ||
); | ||
|
||
return setting; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 like the API a lot, and the fact that we can implement any heuristics to get the value of that config in the hook without changing how the API is used.
I'm not sure yet about the naming.
useEditorFeature
,useEditorSetting
,useEditorConfig
? How will it scale to nested configs like typography, colors? Why is__experimentalDisableCustomLineHeight
a top level config and not nested under "typography" (same for drop cap).cc @mtias @aduth @mcsf and others.
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.
🤘
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 thing we're referring to here is the same thing we assign via
<Editor settings={ ... } />
, orblock_editor_settings
filter, I'd very much like to see "setting" or "settings" as part of the name.Do we have nested settings? Can we not? 😛
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 we fly with
useSetting
then we need those nested groups to make it all easier. We discussed groups liketypography
,colors
so far. I'm sure we need more. The only question is whether we name it feature, setting, support or whatever :)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.
for context, this hook is supposed to get its values from the "config" property in the proposed format in the end. #21583 (comment)
This config is probably going to end up in the
block-editor
getSettings selector somehow, I haven't give it much though yet about how, whether it's just1 - 1
or if there's some intermediate mapping that happens.I don't think the block-editor settings and the "config" of a block-editor are the exact same things, block-editor can have more settings and themes can have more "config" though.
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.
It feels like all we need is proper namespacing to make it easy to inject all settings from the
theme.json
config. Wherever we have duplication, we can find a nice way to migrate it behind the scenes before sending it to the front.To give an example, the following would still work on PHP side:
You could also override it with
theme.json
:The challenge would be to pick what takes precedence,
theme.json
orget_theme_support
here, or the top-leveldisableCustomFontSizes
vstypography. disableCustomFontSizes
. Before sending it to the client, we would remove duplicates from the top-level using some conflict resolution strategy.On the client, we need to keep old settings for backward compatibility but I would solve it by doing internal mapping in the
getSettings
selector:I hope I didn't miss anything.