Skip to content

Commit 279ce2c

Browse files
katiekleinkatiegoines
andauthored
a11y-when closing accordion from bottom, focus back on summary element (#7837)
* when closing accordion from bottom, focus back on summary element * add test * remove extra spaces --------- Co-authored-by: katiegoines <[email protected]>
1 parent e7b3a8a commit 279ce2c

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/components/Accordion/Accordion.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ export const Accordion: React.FC<AccordionProps> = ({
6969

7070
const closeAccordion = () => {
7171
const details = detailsRef.current;
72+
const summary = summaryRef.current;
7273
if (details) {
7374
const scrollToLoc = details.offsetTop - 48 - 70 - 10; // account for nav heights and 10px buffer
7475
setDetailsOpen(false);
7576
details.animate(collapse, animationTiming);
77+
summary?.focus();
7678
window.scrollTo({
7779
left: 0,
7880
top: scrollToLoc,

src/components/Accordion/__tests__/Accordion.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ describe('Accordion', () => {
6565
});
6666
});
6767

68-
it('should collapse Accordion when close button is clicked', async () => {
68+
it('should collapse Accordion and refocus on Accordion element when close button is clicked', async () => {
6969
render(component);
7070
const accordionHeading = screen.getByText('Accordion component example');
7171
userEvent.click(accordionHeading);
7272
const detailsEl = await screen.getByRole('group');
73+
const summaryEl = detailsEl.firstChild;
74+
7375
expect(detailsEl).toHaveAttribute('open');
7476

7577
const text = await screen.getByText(content);
@@ -79,6 +81,7 @@ describe('Accordion', () => {
7981
await waitFor(() => {
8082
expect(text).not.toBeVisible();
8183
expect(detailsEl).not.toHaveAttribute('open');
84+
expect(summaryEl).toHaveFocus();
8285
});
8386
});
8487

0 commit comments

Comments
 (0)