Skip to content

Commit 447de33

Browse files
Draft block selectors API documentation
1 parent 1a3e557 commit 447de33

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

docs/reference-guides/block-api/block-metadata.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ Starting in WordPress 5.8 release, we encourage using the `block.json` metadata
2828
"my-plugin/message": "message"
2929
},
3030
"usesContext": [ "groupId" ],
31+
"selectors": {
32+
"root": ".wp-block-my-plugin-notice"
33+
},
3134
"supports": {
3235
"align": true
3336
},
@@ -379,6 +382,18 @@ See [the block context documentation](/docs/reference-guides/block-api/block-con
379382
}
380383
```
381384

385+
### Selectors
386+
387+
- Type: `object`
388+
- Optional
389+
- Localized: No
390+
- Property: `selectors`
391+
- Default: `{}`
392+
393+
Any custom CSS selectors, keyed by `root`, feature, or sub-feature, to be used
394+
when generating block styles for theme.json (global styles) stylesheets.
395+
See the [the selectors documentation](/docs/reference-guides/block-api/block-selectors.md) for more details.
396+
382397
### Supports
383398

384399
- Type: `object`
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Selectors
2+
3+
Block Selectors is the API that allows blocks to customize the CSS selector used
4+
when their styles generated.
5+
6+
A block may customize its CSS selectors at three levels: root, feature, and
7+
subfeature.
8+
9+
## Root Selector
10+
11+
The block's primary CSS selector.
12+
13+
All blocks require a primary CSS selector for their style declarations to be
14+
included under. If one is not provided through the Block Selectors API, a
15+
default is generated in the form of `.wp-block-<name>`.
16+
17+
### Example
18+
```json
19+
{
20+
...
21+
"selectors": {
22+
"root": ".my-custom-block-selector"
23+
}
24+
}
25+
```
26+
27+
## Feature Selectors
28+
29+
Feature selectors relate to styles for a block support e.g. border, color,
30+
typography etc.
31+
32+
A block may wish to apply the styles for specific features to different
33+
elements within a block. An example of this might be to apply colors on the
34+
block's wrapper but apply the typography styles to an inner heading only.
35+
36+
### Example
37+
```json
38+
{
39+
...
40+
"selectors": {
41+
"root": ".my-custom-block-selector",
42+
"color": ".my-custom-block-selector",
43+
"typography": ".my-custom-block-selector > h2"
44+
}
45+
}
46+
```
47+
48+
## Subfeature Selectors
49+
50+
These selectors relate to individual styles provided by a block support e.g.
51+
`background-color`
52+
53+
A subfeature can have styles generated under its own unique selector. This is
54+
especially useful where one block support subfeature can't be applied to the
55+
same element as the support's other subfeatures.
56+
57+
A great example of this is `text-decoration`. Web browsers render this style
58+
differently making it difficult to override if applied to a wrapper element. By
59+
assigning `text-decoration` a custom selector, its style can target only the
60+
elements to which it should be applied.
61+
62+
### Example
63+
```json
64+
{
65+
...
66+
"selectors": {
67+
"root": ".my-custom-block-selector",
68+
"color": ".my-custom-block-selector",
69+
"typography": {
70+
"root": ".my-custom-block-selector > h2",
71+
"text-decoration": ".my-custom-block-selector > h2 span"
72+
}
73+
}
74+
}
75+
```
76+
77+
## Shorthand
78+
79+
Rather than specify a CSS selector for every subfeature, you can set a single
80+
selector as a string value for the relevant feature. This is the approach
81+
demonstrated for the `color` feature in earlier examples above.
82+
83+
## Fallbacks
84+
85+
If a selector hasn't been configured for a specific feature, it will fallback to
86+
the block's root selector. Similarly, if a subfeature hasn't had a custom
87+
selector set, it will fallback to its parent feature's selector, and if not
88+
available, fallback further to the block's root selector.
89+
90+
Rather than repeating selectors for multiple subfeatures, you can simply set the
91+
common selector as the parent feature's `root` selector and only define the
92+
unique selectors for the subfeatures that differ.
93+
94+
### Example
95+
```json
96+
{
97+
...
98+
"selectors": {
99+
"root": ".my-custom-block-selector",
100+
"color": {
101+
"text": ".my-custom-block-selector p"
102+
},
103+
"typography": {
104+
"root": ".my-custom-block-selector > h2",
105+
"text-decoration": ".my-custom-block-selector > h2 span"
106+
}
107+
}
108+
}
109+
```
110+
111+
In the above example, the `color.background-color` subfeature isn't explicitly
112+
set. As the `color` feature also doesn't define a `root` selector,
113+
`color.background-color` would be included under the block's primary root
114+
selector; `.my-custom-block-selector`.
115+
116+
For a subfeature such as `typography.font-size`, it would fallback to its parent
117+
feature's selector given that is present i.e. `.my-custom-block-selector > h2`.

0 commit comments

Comments
 (0)