Skip to content

[AC-3395] Support aria attributes as alternative to label in ListItemGroup #3052

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

Merged
merged 7 commits into from
Apr 30, 2025
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
5 changes: 5 additions & 0 deletions .changeset/clever-icons-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sumup-oss/circuit-ui": patch
---

Allowed more flexibility on the accessibility requirements of the ListItemGroup component by making the `label` prop optional and accepting `aria-label` or `aria-labelledby` attributes as alternatives to provide context.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { createRef } from 'react';

import { screen, render, axe, type RenderFn } from '../../util/test-utils.js';
Expand Down Expand Up @@ -55,6 +55,16 @@ describe('ListItemGroup', () => {
expect(screen.getByText('Group label')).toBeVisible();
});

it('should render a ListItemGroup with aria-label', () => {
renderListItemGroup(render, {
...baseProps,
label: undefined,
'aria-label': 'Group label',
});
expect(screen.queryByRole('label')).not.toBeInTheDocument();
expect(screen.getByRole('list')).toBeInTheDocument();
});

it('should render a ListItemGroup with a details line', () => {
renderListItemGroup(render, {
...baseProps,
Expand Down Expand Up @@ -87,4 +97,20 @@ describe('ListItemGroup', () => {
const actual = await axe(container);
expect(actual).toHaveNoViolations();
});

it('should throw accessibility error when there is no aria attribute or label', () => {
const props = {
...baseProps,
label: undefined,
'aria-label': undefined,
'aria-labelledby': undefined,
'aria-hidden': undefined,
} as unknown as ListItemGroupProps;
// Silence the console.error output and switch to development mode to throw the error
vi.spyOn(console, 'error').mockImplementation(() => undefined);
process.env.NODE_ENV = 'development';
expect(() => render(<ListItemGroup {...props} />)).toThrow();
process.env.NODE_ENV = 'test';
vi.restoreAllMocks();
});
});
42 changes: 24 additions & 18 deletions packages/circuit-ui/components/ListItemGroup/ListItemGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
type ReactNode,
} from 'react';

import { AccessibilityError } from '../../util/errors.js';
import {
AccessibilityError,
isSufficientlyLabelled,
} from '../../util/errors.js';
import { Body } from '../Body/index.js';
import { ListItem, type ListItemProps } from '../ListItem/index.js';
import { isString } from '../../util/type-check.js';
Expand All @@ -48,8 +51,9 @@ export interface BaseProps {
items: ItemProps[];
/**
* Display a main label/headline describing the group.
* `aria-label` or `aria-labelledby` can alternatively be used to provide accessibility information.
*/
label: ReactNode;
label?: ReactNode;
/**
* Visually hide the label. This should only be used in rare cases and only
* if the purpose of the field can be inferred from other context.
Expand Down Expand Up @@ -89,11 +93,11 @@ export const ListItemGroup = forwardRef<HTMLDivElement, ListItemGroupProps>(
if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV !== 'test' &&
!label
!isSufficientlyLabelled(label, props)
) {
throw new AccessibilityError(
'ListItemGroup',
'The `label` prop is missing. This is an accessibility requirement. Pass `hideLabel` if you intend to hide the label visually.',
'The `label` prop, `aria-label` or `aria-labelledby` is missing. This is an accessibility requirement. Pass `hideLabel` if you intend to hide the label visually.',
);
}

Expand All @@ -104,20 +108,22 @@ export const ListItemGroup = forwardRef<HTMLDivElement, ListItemGroupProps>(
ref={ref}
>
<div className={classes.header}>
<div
className={clsx(
classes.label,
hideLabel && utilClasses.hideVisually,
)}
>
{isString(label) ? (
<Body as="h4" size="s">
{label}
</Body>
) : (
label
)}
</div>
{label && (
<div
className={clsx(
classes.label,
hideLabel && utilClasses.hideVisually,
)}
>
{isString(label) ? (
<Body as="h4" size="s">
{label}
</Body>
) : (
label
)}
</div>
)}
{details && (
<div className={classes.details}>
{isString(details) ? <Body size="s">{details}</Body> : details}
Expand Down