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

update ToggleSwitch to support autofocus #3319

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ktravers
Copy link
Contributor

@ktravers ktravers commented Feb 7, 2025

Authors: Please fill out this form carefully and completely.

Reviewers: By approving this Pull Request you are approving the code change, as well as its deployment and mitigation plans. Please read this description carefully. If you feel there is anything unclear or missing, please ask for updates.

Related to https://github.com/github/accessibility-audits/issues/10006
Required by https://github.com/github/github/pull/361376

What are you trying to accomplish?

This PR updates the ToggleSwitch component to allow the autofocus property to be applied to its button element. Doing so will allow this component to be fully accessible when used in conjunction with the Rails turbo framework. See https://github.com/github/accessibility-audits/issues/10006 for more context.

Screenshots

toggle-switch.mov

Integration

No prod changes required.

List the issues that this change affects.

Risk Assessment

  • Low risk the change is small, highly observable, and easily rolled back.

What approach did you choose and why?

I explored two approaches for allowing users to optionally apply the autofocus attribute to the ToggleSwitch button element:

  1. Add generic button_arguments argument, allowing users to pass any supported attribute to the button
  2. Add specific autofocus argument, so users can apply this single attribute directly

I opted to use the second approach, as it is the minimum viable change required to make this component accessible when used with turbo-rails. Using a more surgical, targeted approach seemed safer and easier to maintain/test than the more permissive, open-ended first approach. This also seemed more in line with Primer view component design principles.

Anything you want to highlight for special attention from reviewers?

  • What do reviewers think of the two approaches described above? Would folks prefer to ship the more permissive button_arguments approach? 🤔 I didn't see this kind of argument in many existing components; however, it does resemble generic system arguments, which are allowed for all components (if not the child elements they render, necessarily).
  • Are there any concerns around supporting autofocus on a toggle switch button? It is an allowed attribute for buttons in general, but I wasn't sure if there was some reason we didn't already support it here.
  • Should this be a minor release instead of a patch?

Accessibility

  • No new axe scan violation - This change does not introduce any new axe scan violations.

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Added/updated previews (Lookbook)
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@ktravers ktravers self-assigned this Feb 7, 2025
Copy link

changeset-bot bot commented Feb 7, 2025

🦋 Changeset detected

Latest commit: 154e1ac

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/view-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ktravers ktravers changed the title update ToggleSwitch to support button arguments update ToggleSwitch to support autofocus Feb 7, 2025
@ktravers ktravers marked this pull request as ready for review February 10, 2025 19:25
@ktravers ktravers requested a review from a team as a code owner February 10, 2025 19:25
@ktravers ktravers requested a review from camertron February 10, 2025 19:25
@@ -62,6 +62,10 @@ def with_bad_csrf_token
def with_turbo
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, turbo: true))
end

Copy link
Contributor Author

@ktravers ktravers Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this new preview be snapshotted? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say yes, but only if it's visually different from other previews.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only visual difference would be whatever focus styles are in place. I've got aggressive settings on my local machine when it comes to focus, so it's always visually distinct, so I'm not sure if that's actually true for this component as well or just me 😅 Lemme take a quick look at the CSS and/or disable my focus stuff to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, AFAICT ToggleSwitch has focus styling, but it's only visible when :focus-visible is applied via the browser. So I don't think we need a separate preview 👍

@ktravers ktravers added the a11y eng Requires input from PVC accessibility engineering experts label Feb 10, 2025
Copy link
Contributor

@camertron camertron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! I actually think we should implement both of the approaches you outlined 😄 IMO, autofocus: should be an argument to ToggleSwitch.new, but so should button_arguments:. We actually do this in a number of places when we want to accept arguments for child elements.

Inside the constructor, we can do something like:

@button_arguments = button_arguments
@button_arguments[:autofocus] = true if autofocus
@button_arguments[:aria] = merge_aria(
  @button_arguments,
  { aria: { pressed: on? } }
)

@@ -62,6 +62,10 @@ def with_bad_csrf_token
def with_turbo
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path, turbo: true))
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say yes, but only if it's visually different from other previews.

@ktravers
Copy link
Contributor Author

I actually think we should implement both of the approaches you outlined 😄 IMO, autofocus: should be an argument to ToggleSwitch.new, but so should button_arguments

@camertron Cool, that sounds good to me! Would it be ok to ship this PR as is to unblock https://github.com/github/github/pull/361376, then implement button_arguments in a separate fast follow PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a11y eng Requires input from PVC accessibility engineering experts patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants