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

feat(Accordion): improve accessibility #1113

Merged
merged 8 commits into from
May 16, 2024

Conversation

marcoskolodny
Copy link
Collaborator

@marcoskolodny marcoskolodny commented May 6, 2024

Issue: Link

I've tried to use details/summary HTML markup, but there are some things that were not great about it:

  • Behavior in browsers differs (specially Safari), making it trickier to implement.
  • Screen reader doesn't consider as interactive. When you press on the Accordion's header, it jumps to elements inside it directly.
  • AFAIK, there are no simple ways to animate the opening/closing of the accordion's content. I've found only hacks that require handling JS events and other tricky things.

I believe that the current implementation is way more clean. In terms of accessibility, I've compared summary/details with it, and our implementation reads everything that the summary one reads.

Changes on this PR:

  • Disable animations if the user's device has reduced motion accessibility setting turned on
  • Allow passing role to Accordion/AccordionItem
  • Fix logic for rendering last divider in a list of Accordion items
  • Code cleanup

Copy link

github-actions bot commented May 6, 2024

Size stats

master this branch diff
Total JS 10.7 MB 10.7 MB +704 B
JS without icons 1.92 MB 1.93 MB +704 B
Lib overhead 52.1 kB 52.1 kB 0 B
Lib overhead (gzip) 14 kB 14 kB 0 B

Copy link

github-actions bot commented May 6, 2024

Accessibility report
✔️ No issues found

ℹ️ You can run this locally by executing yarn audit-accessibility.

Copy link

github-actions bot commented May 6, 2024

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-8lf8cnzw7-flows-projects-65bb050e.vercel.app

Built with commit 597da91.
This pull request is being automatically deployed with vercel-action

@@ -24,13 +25,15 @@ const ACCORDION_TRANSITION_DURATION_IN_MS = 400;

type AccordionContextType = {
index: ReadonlyArray<number>;
toogle: (item: number) => void;
toggle: (item: number) => void;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

typo: toogle -> toggle

ref={ref}
dataAttributes={{'component-name': 'AccordionItem', ...dataAttributes}}
/>
({dataAttributes, role = 'listitem', ...props}, ref) => (
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding list/listitem roles by default (similar to Rows)

Copy link
Contributor

Choose a reason for hiding this comment

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

This solves the relationship between the accordion group and each of the accordions in case they have relation between them, since they could also be only regions of content with no relation between them.

How the relationship between the header and the content can be defined?

I see two cases:

  • They are a group of controls: The accordion item acts as a fieldset where the header title is the legend and the controls are inside the collapsable panel.
  • They are a definition list (usually used for FAQs): The list of accordions acs as a definition list so the accordion header should have the role: "term" and the content of the collapsable section, the role: "definition"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure I understood you. Currently, the relation between the header and the content is made by using aria-controls in the header and aria-labelledby + role=region in the panel.

Is that what you meant?

We are following the accessibility specs described in https://www.w3.org/WAI/ARIA/apg/patterns/accordion/examples/accordion/

The fieldset tag is meant to be used to group fields inside a form. An accordion is not the case.

Also, the accordion content does not necessarily "define" the header. For example, the header can say something like "click to see more". In that case, it would be confusing to define them as term/definition.

I suggest we should stick to the specs described in the link I'm providing in this comment (the current implementation is doing that)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Discussed offline. Alex will investigate about best practices regarding this and will come back to us with some updates. In case some changes are necessary, we can iterate on it in a different PR.

src/accordion.tsx Outdated Show resolved Hide resolved
)
);

type AccordionBaseProps = {
children: React.ReactNode;
dataAttributes?: DataAttributes;
onChange?: (index: number, value: boolean) => void;
role?: string;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Initially, I added "list" and "listitem" as default roles for Accordion and AccordionItem, but after discussing with @aweell, he pointed out that it may not be a good idea.

For example, if the accordions are being used to divide some content of a document in different sections, it's not the best idea to use a list for them.

We've decided to make it customizable, but without setting any default values

Copy link
Contributor

Choose a reason for hiding this comment

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

Specs are updated accordingly

atabel

This comment was marked as resolved.

@marcoskolodny marcoskolodny requested a review from atabel May 10, 2024 09:33
Comment on lines +72 to +76
'@media': {
['(prefers-reduced-motion)']: {
transition: 'none',
},
},
Copy link
Member

Choose a reason for hiding this comment

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

nice 👍

@marcoskolodny marcoskolodny added this pull request to the merge queue May 16, 2024
Merged via the queue into master with commit f85142c May 16, 2024
11 checks passed
@marcoskolodny marcoskolodny deleted the WEB-1816-accordion-accessibility branch May 16, 2024 10:17
tuentisre pushed a commit that referenced this pull request May 17, 2024
# [15.8.0](v15.7.0...v15.8.0) (2024-05-17)

### Bug Fixes

* **Image:** fix bad aspect ratio cases ([#1119](#1119)) ([2bc69e1](2bc69e1))
* **TextFields:** Use textError color for errors ([#1125](#1125)) ([e9c5abd](e9c5abd))

### Features

* **Accordion:** improve accessibility ([#1113](#1113)) ([f85142c](f85142c))
@tuentisre
Copy link
Collaborator

🎉 This PR is included in version 15.8.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants