-
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
Deprecating isPressed
in Button
component
#54740
Deprecating isPressed
in Button
component
#54740
Conversation
This change causes a big pile of tests to fail because of the new deprecation warning. Curious as to peoples thoughts about whether it's better to update the tests to expect the warning, or to just update the code to no longer use |
Size Change: +586 B (0%) Total Size: 1.62 MB
ℹ️ View Unchanged
|
Flaky tests detected in 704770c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6276716510
|
Ideally, we should update the code to no longer use the deprecated prop, otherwise, folks will see the warning in their consoles. If it's too much work for a single PR, sometimes we migrate the props to the recommended alternative and only then deprecate the legacy prop. |
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.
This looks good and tests well for me. I see your point on the bug scope of consumers who've already added aria-pressed
but not isPressed
. After a quick search I don't see any cases of that in Gutenberg, so we're probably clear on that front at least internally, and can hope it's a minimal occurrence among third party consumers.
I think addressing the existing usages of isPressed
as @tyxla suggested is the only real thing between this and a merge 👏 It looks like theres 30 ish of them to address, but they're not huge individual changes, so maybe a good fit for this PR? I'll defer to others if they disagree 😄
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.
Looking good!
I think that refactoring other components can happen in one (or more) follow-up PRs.
I would also add the `aria-pressed` control to Storybook for completeness
diff --git a/packages/components/src/button/stories/index.story.tsx b/packages/components/src/button/stories/index.story.tsx
index a1dadab081..61b4c32438 100644
--- a/packages/components/src/button/stories/index.story.tsx
+++ b/packages/components/src/button/stories/index.story.tsx
@@ -26,6 +26,10 @@ const meta: Meta< typeof Button > = {
component: Button,
argTypes: {
// Overrides a limitation of the docgen interpreting our TS types for this as required.
+ 'aria-pressed': {
+ control: { type: 'select' },
+ options: [ undefined, 'true', 'false', 'mixed' ],
+ },
href: { type: { name: 'string', required: false } },
icon: {
control: { type: 'select' },
@@ -131,7 +143,10 @@ export function UnforwardedButton( | |||
'is-small': size === 'small', | |||
'is-compact': size === 'compact', | |||
'is-tertiary': variant === 'tertiary', | |||
'is-pressed': isPressed, | |||
|
|||
'is-pressed': ariaPressed && ariaPressed !== 'false', |
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.
Could we simplify this check?
'is-pressed': ariaPressed && ariaPressed !== 'false', | |
'is-pressed': ariaPressed !== 'false', |
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.
The problem here is that ariaPressed
has a boolean-ish value; it can either be true
/"true"
, false
/"false"
, "mixed"
or undefined
. So we can't just test for "false"
, because that would give false positives for false
and undefined
.
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 wanted to be a bit more explicit, an alternative might be...
[true, "true", "mixed"].contains(ariaPressed)
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.
That variety of value possibilities is a good case for adding some unit tests, by the way.
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.
Thank you for the explanation, @andrewhayward ! For which specific implementation to use, I'll leave it up to you to decide which one you think is the clearest to understand for other developers coming across this component.
That variety of value possibilities is a good case for adding some unit tests, by the way.
I guess we could add a bunch of extra tests checking for false
, 'true'
and 'false'
values too
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.
Added a few extra tests, and altered the logic around this to be a bit more explicit about what is expected.
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.
LGTM 🚀
Feel free to merge once remaining feedback is addressed. Thank you!
@@ -131,7 +143,10 @@ export function UnforwardedButton( | |||
'is-small': size === 'small', | |||
'is-compact': size === 'compact', | |||
'is-tertiary': variant === 'tertiary', | |||
'is-pressed': isPressed, | |||
|
|||
'is-pressed': ariaPressed && ariaPressed !== 'false', |
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.
Thank you for the explanation, @andrewhayward ! For which specific implementation to use, I'll leave it up to you to decide which one you think is the clearest to understand for other developers coming across this component.
That variety of value possibilities is a good case for adding some unit tests, by the way.
I guess we could add a bunch of extra tests checking for false
, 'true'
and 'false'
values too
&[aria-pressed="true"], | ||
&[aria-pressed="mixed"] { |
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.
After seeing #55004, now I think about it, we could keep using the is-pressed
class to assign those styles. Afterall, now the is-pressed
class is assigned when truthyAriaPressedValues.includes( ariaPressed )
Dev noteThe In an effort to move away from custom props that have native HTML equivalents, and to allow greater flexibility in component usage, |
The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
* Editor: remove deprecated isPressed prop in WP 6.5. The isPressed prop is deprecated in WP 6.5, and replaced by the native aria-pressed attribute. WordPress/gutenberg#54740 * Update version * Bump version Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/8847567798 Upstream-Ref: Automattic/jetpack@adace6c
What?
This PR starts the deprecation of
isPressed
on theButton
component, deferring to the nativearia-pressed
attribute instead. While it does not externally change the API, internally it convertsisPressed
intoaria-pressed
, such that using the latter is equivalent to the former. As such,aria-pressed
can be used instead ofisPressed
should a consumer so desire.Why?
When the
isPressed
prop is used onButton
,aria-pressed
is explicitly set accordingly. But sometimesButton
is used for roles other thanbutton
, such asoption
andcheckbox
, wherearia-pressed
is not appropriate. Instead consumers should be able to decide for themselves the appropriate semantics.How?
The
isPressed
prop is marked as deprecated, and transformed intoaria-pressed
. The value ofaria-pressed
is then used instead to determine how the component should be styled.Testing Instructions
Unit tests have been added to confirm that the legacy
isPressed
behaviour continues, and that usingaria-pressed
directly behaves in the same way.The only potential scope for issues is where a consumer has already set
aria-pressed
on aButton
, but notisPressed
, as this could affect the visual appearance due to the addedis-pressed
class.