-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Block.json: Refactor and stabilize selectors API #46496
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
0239107
Allow selectors when registering blocks
aaronrobertshaw 8d22173
Add tests for updated selectors API
aaronrobertshaw 13e743e
Add selectors to block.json schema
aaronrobertshaw 974f188
Update image block to use selectors API
aaronrobertshaw 3858633
Add global function to get block CSS selectors
aaronrobertshaw b19da22
Update theme.json class to support selectors API
aaronrobertshaw bb99093
Add filter ensuring selectors metadata is available
aaronrobertshaw 4776e71
Draft block selectors API documentation
aaronrobertshaw a7d49bb
Test root block selector generation for core block
aaronrobertshaw e354e2e
Fix typo in null duotone selector test
aaronrobertshaw e35ca22
Make duotone use color.duotone path
aaronrobertshaw 045e256
Remove unnecessary selectors properties for fallback tests
aaronrobertshaw fd51b64
Add extra test to cover feature fallback to generated default selector
aaronrobertshaw b0c53a8
Fix typos and improve wording for docs
aaronrobertshaw a67f109
Fix up checks for whether selectors API in use
aaronrobertshaw 5561ed7
Add selectors default value in block type api
aaronrobertshaw e133f6a
Improve block metadata selectors docs
aaronrobertshaw 9edfa55
Note when selectors polyfill can be removed
aaronrobertshaw a42484d
Add note on selectors API purpose to block.json schema
aaronrobertshaw f11ccd7
Relocate new global function to 6.3 compat file
aaronrobertshaw efaef5e
Remove unnecessary space
aaronrobertshaw 75a1f6a
Move duotone under the filters feature
aaronrobertshaw 08b2604
Fix duotone fallback and selector function tests
aaronrobertshaw 991dae9
Add editorSelectors API
aaronrobertshaw 28d39fa
Switch to ternary for editor duotone selector check
aaronrobertshaw 8aa8f24
Fix typo
aaronrobertshaw 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 hidden or 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 hidden or 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,145 @@ | ||
| # Selectors | ||
|
|
||
| Block Selectors is the API that allows blocks to customize the CSS selector used | ||
| when their styles are generated. | ||
aaronrobertshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| A block may customize its CSS selectors at three levels: root, feature, and | ||
| subfeature. Each may also be overridden with editor-only selectors. | ||
|
|
||
| ## Root Selector | ||
|
|
||
| The root selector is the block's primary CSS selector. | ||
|
|
||
| All blocks require a primary CSS selector for their style declarations to be | ||
| included under. If one is not provided through the Block Selectors API, a | ||
| default is generated in the form of `.wp-block-<name>`. | ||
|
|
||
| ### Example | ||
| ```json | ||
| { | ||
| ... | ||
| "selectors": { | ||
| "root": ".my-custom-block-selector" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Feature Selectors | ||
|
|
||
| Feature selectors relate to styles for a block support, e.g. border, color, | ||
| typography, etc. | ||
|
|
||
| A block may wish to apply the styles for specific features to different | ||
| elements within a block. An example might be using colors on the block's wrapper | ||
| but applying the typography styles to an inner heading only. | ||
|
|
||
| ### Example | ||
| ```json | ||
| { | ||
| ... | ||
| "selectors": { | ||
| "root": ".my-custom-block-selector", | ||
| "color": ".my-custom-block-selector", | ||
| "typography": ".my-custom-block-selector > h2" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Subfeature Selectors | ||
|
|
||
| These selectors relate to individual styles provided by a block support e.g. | ||
| `background-color` | ||
|
|
||
| A subfeature can have styles generated under its own unique selector. This is | ||
| especially useful where one block support subfeature can't be applied to the | ||
| same element as the support's other subfeatures. | ||
|
|
||
| A great example of this is `text-decoration`. Web browsers render this style | ||
| differently, making it difficult to override if added to a wrapper element. By | ||
| assigning `text-decoration` a custom selector, its style can target only the | ||
| elements to which it should be applied. | ||
|
|
||
| ### Example | ||
| ```json | ||
| { | ||
| ... | ||
| "selectors": { | ||
| "root": ".my-custom-block-selector", | ||
| "color": ".my-custom-block-selector", | ||
| "typography": { | ||
| "root": ".my-custom-block-selector > h2", | ||
| "text-decoration": ".my-custom-block-selector > h2 span" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Shorthand | ||
|
|
||
| Rather than specify a CSS selector for every subfeature, you can set a single | ||
| selector as a string value for the relevant feature. This is the approach | ||
| demonstrated for the `color` feature in the earlier examples above. | ||
|
|
||
| ## Fallbacks | ||
|
|
||
| A selector that hasn't been configured for a specific feature will fall back to | ||
| the block's root selector. Similarly, if a subfeature hasn't had a custom | ||
| selector set, it will fall back to its parent feature's selector and, if unavailable, fall back further to the block's root selector. | ||
|
|
||
| Rather than repeating selectors for multiple subfeatures, you can set the | ||
| common selector as the parent feature's `root` selector and only define the | ||
| unique selectors for the subfeatures that differ. | ||
|
|
||
| ### Example | ||
| ```json | ||
| { | ||
| ... | ||
| "selectors": { | ||
| "root": ".my-custom-block-selector", | ||
| "color": { | ||
| "text": ".my-custom-block-selector p" | ||
| }, | ||
| "typography": { | ||
| "root": ".my-custom-block-selector > h2", | ||
| "text-decoration": ".my-custom-block-selector > h2 span" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The `color.background-color` subfeature isn't explicitly set in the above | ||
| example. As the `color` feature also doesn't define a `root` selector, | ||
| `color.background-color` would be included under the block's primary root | ||
| selector, `.my-custom-block-selector`. | ||
|
|
||
| For a subfeature such as `typography.font-size`, it would fallback to its parent | ||
| feature's selector given that is present, i.e. `.my-custom-block-selector > h2`. | ||
|
|
||
| ## Editor-only Selectors | ||
|
|
||
| There are scenarios in which a block might need different markup within the | ||
| editor compared to the frontend e.g. inline cropping of the Image block. Some | ||
| generated styles may then need to be applied to different, or multiple, | ||
| elements. | ||
|
|
||
| Continuing with the Image cropping example, the image border styles need to also | ||
| be applied to the cropping area. If the selector for the cropping area is added | ||
| to the normal `selectors` config for the block, it would be output unnecessarily | ||
| on the frontend. | ||
|
|
||
| To avoid this, and include the selector for the editor only, the selectors for the border feature can be | ||
| overridden via the `editorSelectors` config. | ||
|
|
||
| ### Example | ||
| ```json | ||
| { | ||
| ... | ||
| "selectors": { | ||
| "root": ".wp-block-image", | ||
| "border": ".wp-block-image img" | ||
| }, | ||
| "editorSelectors": { | ||
| "border": ".wp-block-image img, .wp-block-image .wp-block-image__crop-area" | ||
aaronrobertshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| } | ||
| ``` | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.