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

a11y-when closing accordion from bottom, focus back on summary element #7837

Merged
merged 3 commits into from
Jul 22, 2024
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
2 changes: 2 additions & 0 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export const Accordion: React.FC<AccordionProps> = ({

const closeAccordion = () => {
const details = detailsRef.current;
const summary = summaryRef.current;
if (details) {
const scrollToLoc = details.offsetTop - 48 - 70 - 10; // account for nav heights and 10px buffer
setDetailsOpen(false);
details.animate(collapse, animationTiming);
summary?.focus();
window.scrollTo({
left: 0,
top: scrollToLoc,
Expand Down
5 changes: 4 additions & 1 deletion src/components/Accordion/__tests__/Accordion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ describe('Accordion', () => {
});
});

it('should collapse Accordion when close button is clicked', async () => {
it('should collapse Accordion and refocus on Accordion element when close button is clicked', async () => {
render(component);
const accordionHeading = screen.getByText('Accordion component example');
userEvent.click(accordionHeading);
const detailsEl = await screen.getByRole('group');
const summaryEl = detailsEl.firstChild;

expect(detailsEl).toHaveAttribute('open');

const text = await screen.getByText(content);
Expand All @@ -79,6 +81,7 @@ describe('Accordion', () => {
await waitFor(() => {
expect(text).not.toBeVisible();
expect(detailsEl).not.toHaveAttribute('open');
expect(summaryEl).toHaveFocus();
});
});

Expand Down