Skip to content

Commit 383ee7c

Browse files
katiekleinkatiegoines
andauthored
when clicking on toc entry, focus on respective heading in content (#7845)
Co-authored-by: katiegoines <[email protected]>
1 parent d24c120 commit 383ee7c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/components/MDXComponents/MDXHeading.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const MDXHeading = (props) => {
55
const { level, children, id } = props;
66

77
return (
8-
<Heading level={level} id={id}>
8+
<Heading level={level} id={id} tabIndex={-1}>
99
{/* Only output heading links for h2 and h3 \ */}
1010
{level == 2 || level == 3 ? (
1111
<Link href={`#${id}`}>{children}</Link>

src/components/MDXComponents/__tests__/MDXHeading.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { render, screen } from '@testing-library/react';
22
import { MDXHeading } from '../MDXHeading';
3+
import { TableOfContents } from '../../TableOfContents/index';
4+
import userEvent from '@testing-library/user-event';
35

46
describe('MDXHeading', () => {
57
it('should render H2 with string and anchor link', () => {
@@ -53,4 +55,28 @@ describe('MDXHeading', () => {
5355
expect(heading).toHaveTextContent(props.children);
5456
expect(link).not.toBeInTheDocument();
5557
});
58+
59+
it('should shift focus to in-content heading on TOC click', async () => {
60+
const props = {
61+
level: 2,
62+
children: 'Test heading',
63+
id: 'test-heading'
64+
};
65+
render(<MDXHeading {...props} />);
66+
67+
const heading = screen.queryByRole('heading', { level: 2 });
68+
const tocHeadings = [
69+
{ linkText: 'Test heading', hash: 'test-heading', level: 'h2' }
70+
];
71+
72+
const tableOfContents = <TableOfContents headers={tocHeadings} />;
73+
render(tableOfContents);
74+
75+
const tocEntry = await screen.findByRole('heading', {
76+
name: 'Test heading'
77+
});
78+
79+
userEvent.click(tocEntry);
80+
expect(heading).toHaveFocus();
81+
});
5682
});

src/styles/base.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ ol:not([class]) {
187187
.amplify-heading--2,
188188
.amplify-heading--3 {
189189
scroll-margin-top: 9rem;
190+
191+
&:focus-visible {
192+
transition: none;
193+
outline: 2px solid var(--amplify-colors-border-focus);
194+
outline-offset: 2px;
195+
}
190196
}
191197
}
192198

0 commit comments

Comments
 (0)