Skip to content
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

Allow :focus-visible pseudo-selector to be set in theme.json #68521

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8261.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8261

* https://github.com/WordPress/gutenberg/pull/68521
2 changes: 1 addition & 1 deletion docs/how-to-guides/themes/global-settings-and-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ h3 {
{% end %}
##### Element pseudo selectors

Pseudo selectors `:hover`, `:focus`, `:visited`, `:active`, `:link`, `:any-link` are supported by Gutenberg.
Pseudo selectors `:hover`, `:focus`, `:focus-visible`, `:visited`, `:active`, `:link`, `:any-link` are supported by Gutenberg.

```json
"elements": {
Expand Down
13 changes: 9 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ class WP_Theme_JSON_Gutenberg {
/**
* Defines which pseudo selectors are enabled for which elements.
*
* The order of the selectors should be: link, any-link, visited, hover, focus, active.
* The order of the selectors should be: link, any-link, visited, hover, focus, focus-visible, active.
* This is to ensure the user action (hover, focus and active) styles have a higher
* specificity than the visited styles, which in turn have a higher specificity than
* the unvisited styles.
Expand All @@ -573,10 +573,11 @@ class WP_Theme_JSON_Gutenberg {
*
* @since 6.1.0
* @since 6.2.0 Added support for `:link` and `:any-link`.
* @since 6.8.0 Added support for `:focus-visible`.
*/
const VALID_ELEMENT_PSEUDO_SELECTORS = array(
'link' => array( ':link', ':any-link', ':visited', ':hover', ':focus', ':active' ),
'button' => array( ':link', ':any-link', ':visited', ':hover', ':focus', ':active' ),
'link' => array( ':link', ':any-link', ':visited', ':hover', ':focus', ':focus-visible', ':active' ),
'button' => array( ':link', ':any-link', ':visited', ':hover', ':focus', ':focus-visible', ':active' ),
);

/**
Expand Down Expand Up @@ -2931,7 +2932,11 @@ static function ( $split_selector ) use ( $clean_style_variation_selector ) {
array_filter(
$element_pseudo_allowed,
static function ( $pseudo_selector ) use ( $selector ) {
return str_contains( $selector, $pseudo_selector );
/*
* Check if the pseudo selector is in the current selector,
* ensuring it is not followed by a dash (e.g., :focus should not match :focus-visible).
*/
return preg_match( '/' . preg_quote( $pseudo_selector, '/' ) . '(?!-)/', $selector ) === 1;
}
)
);
Expand Down
4 changes: 4 additions & 0 deletions schemas/json/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,9 @@
":focus": {
"$ref": "#/definitions/stylesPropertiesComplete"
},
":focus-visible": {
"$ref": "#/definitions/stylesPropertiesComplete"
},
":hover": {
"$ref": "#/definitions/stylesPropertiesComplete"
},
Expand All @@ -1729,6 +1732,7 @@
":active",
":any-link",
":focus",
":focus-visible",
":hover",
":link",
":visited"
Expand Down
Loading