Skip to content

Secondary and tertiary buttons have no focus ring when not fully disabled #56149

@andrewhayward

Description

@andrewhayward

What problem does this address?

Buttons support the ability to retain focus while being disabled, via the __experimentalIsFocusable prop. However, when this is enabled on secondary and tertiary buttons, they do not have a focus ring. For example, the "Reset filters" button here is focused and only mostly disabled, but that is not visibly evident.

A toolbar with a search field, two buttons and a dropdown toggle. One button (circled in red) is disabled and focused, but has no focus ring.

This is caused by an issue of specificity in the CSS, with .is-secondary and .is-tertiary overriding previously established focus styling for buttons using box-shadow and outline:

&:disabled,
&[aria-disabled="true"],
&[aria-disabled="true"]:hover {
color: $gray-600;
background: transparent;
transform: none;
opacity: 1;
box-shadow: none;
outline: none;
}

&:focus:not(:disabled) {
box-shadow: 0 0 0 var(--wp-admin-border-width-focus) $components-color-accent;
// Windows High Contrast mode will show this outline, but not the box-shadow.
outline: 3px solid transparent;
}

What is your proposed solution?

The fix could be as straightforward as changing the scope or specificity of one of the other rules, to allow for the focus styling to propagate. This is probably most easily achieved by moving the box-shadow and outline declarations into a sub-block:

diff --git a/packages/components/src/button/style.scss b/packages/components/src/button/style.scss
index 03273056cf..4b11d9169a 100644
--- a/packages/components/src/button/style.scss
+++ b/packages/components/src/button/style.scss
@@ -129,8 +129,11 @@
                        background: transparent;
                        transform: none;
                        opacity: 1;
-                       box-shadow: none;
-                       outline: none;
+
+                       &:not(:focus) {
+                               box-shadow: none;
+                               outline: none;
+                       }
                }
        }

This will at least allow mostly-disabled buttons to pick up the focus styling applied to other controls:

A toolbar with a search field, two buttons and a dropdown toggle. One button is disabled and focused, and has a focus ring.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done 🎉

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions