Skip to content

Commit

Permalink
a11y-when closing accordion from bottom, focus back on summary element (
Browse files Browse the repository at this point in the history
#7837)

* when closing accordion from bottom, focus back on summary element

* add test

* remove extra spaces

---------

Co-authored-by: katiegoines <[email protected]>
  • Loading branch information
katiegoines and katiegoines authored Jul 22, 2024
1 parent e7b3a8a commit 279ce2c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
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

0 comments on commit 279ce2c

Please sign in to comment.