Skip to content

a11y- Scrollable table is missing role="region" #7943

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions src/components/MDXComponents/MDXTable.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
import { ScrollView } from '@aws-amplify/ui-react';
import { HTMLAttributes, ReactNode } from 'react';
import { HTMLAttributes, ReactNode, useEffect, useRef, useState } from 'react';

interface MDXTableProps extends HTMLAttributes<HTMLTableElement> {
children: ReactNode;
}

export const MDXTable: React.FC<MDXTableProps> = ({ children, ...props }) => {
const ref = useRef<HTMLTableElement>(null);
const [caption, setCaption] = useState('');

useEffect(() => {
const getHeading = (el) => {
if (el?.previousElementSibling?.tagName.startsWith('H')) {
setCaption(el.previousElementSibling.textContent);
} else {
getHeading(el?.previousElementSibling);
}
};
getHeading(ref.current);
}, []);

const tableId = caption.includes(' ') ? caption.split(' ').join('') : caption;

return (
<ScrollView
tabIndex={0}
aria-label="Scrollable table"
aria-labelledby={'table:' + tableId}
className="scrollview"
role="region"
ref={ref}
>
<table {...props}>{children}</table>
<table {...props}>
<caption id={'table:' + tableId}>Table: {caption}</caption>
{children}
</table>
</ScrollView>
);
};
99 changes: 51 additions & 48 deletions src/components/MDXComponents/__tests__/MDXTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,57 @@ import { MDXTable } from '../MDXTable';
describe('MDXTable', () => {
it('should render table', () => {
render(
<MDXTable>
<thead>
<tr>
<th align="left"></th>
<th align="left">Amplify Authenticator</th>
<th align="left">Amplify Libraries</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">
<strong>Description</strong>
</td>
<td align="left">
Open source drop-in UI component for authentication
</td>
<td align="left">
Low-level building blocks for implementing authentication
</td>
</tr>
<tr>
<td align="left">
<strong>Benefits</strong>
</td>
<td align="left">
Automatically integrates with your existing Amplify configuration
and allows you to easily add the entire authentication flow to
your application. You can then customize themes to adjust colors
and styling as needed.
</td>
<td align="left">
Gives you full control over the UI and logic implementation.
</td>
</tr>
<tr>
<td align="left">
<strong>Constraints</strong>
</td>
<td align="left">
Dependent on Amplify CLI for provisioning resources.
</td>
<td align="left">
Requires the building of screens and frontend logic to enable the
sign-in and registration experiences.
</td>
</tr>
</tbody>
</MDXTable>
<div>
<h3>Heading</h3>
<MDXTable>
<thead>
<tr>
<th align="left"></th>
<th align="left">Amplify Authenticator</th>
<th align="left">Amplify Libraries</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">
<strong>Description</strong>
</td>
<td align="left">
Open source drop-in UI component for authentication
</td>
<td align="left">
Low-level building blocks for implementing authentication
</td>
</tr>
<tr>
<td align="left">
<strong>Benefits</strong>
</td>
<td align="left">
Automatically integrates with your existing Amplify
configuration and allows you to easily add the entire
authentication flow to your application. You can then customize
themes to adjust colors and styling as needed.
</td>
<td align="left">
Gives you full control over the UI and logic implementation.
</td>
</tr>
<tr>
<td align="left">
<strong>Constraints</strong>
</td>
<td align="left">
Dependent on Amplify CLI for provisioning resources.
</td>
<td align="left">
Requires the building of screens and frontend logic to enable
the sign-in and registration experiences.
</td>
</tr>
</tbody>
</MDXTable>
</div>
);

const tableHeading = screen.getByText('Amplify Authenticator');
Expand Down
4 changes: 4 additions & 0 deletions src/styles/table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ table:not([class]) {
background-color: var(--amplify-colors-neutral-10);
}
}
caption {
visibility: hidden;
line-height: 0;
}
}

.scrollview {
Expand Down
Loading