Skip to content

Commit f467238

Browse files
authored
feat(Callout): allow setting title aria-label (#1386)
WEB-2244
1 parent 8e41dd3 commit f467238

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/__tests__/callout-test.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ test('renders an accesible and closable Callout', async () => {
88
const handleCloseSpy = jest.fn();
99
render(
1010
<ThemeContextProvider theme={makeTheme()}>
11-
<Callout title="some title" description="some description" onClose={handleCloseSpy} />
11+
<Callout
12+
aria-label="some label"
13+
title={{text: 'some title', 'aria-label': 'some title aria label'}}
14+
description="some description"
15+
onClose={handleCloseSpy}
16+
/>
1217
</ThemeContextProvider>
1318
);
1419

15-
const callout = screen.getByRole('region');
20+
const callout = screen.getByRole('region', {name: 'some label'});
1621
expect(callout).toBeInTheDocument();
1722

23+
const title = within(callout).getByRole('heading', {name: 'some title aria label'});
24+
expect(title).toBeInTheDocument();
25+
1826
const closeButton = within(callout).getByRole('button', {name: 'Cerrar'});
1927
expect(closeButton).toBeInTheDocument();
2028

@@ -28,6 +36,7 @@ test('renders an accesible and closable Callout with custom close button label',
2836
render(
2937
<ThemeContextProvider theme={makeTheme()}>
3038
<Callout
39+
aria-label="some label"
3140
title="some title"
3241
description="some description"
3342
onClose={handleCloseSpy}
@@ -36,7 +45,7 @@ test('renders an accesible and closable Callout with custom close button label',
3645
</ThemeContextProvider>
3746
);
3847

39-
const callout = screen.getByRole('region');
48+
const callout = screen.getByRole('region', {name: 'some label'});
4049
expect(callout).toBeInTheDocument();
4150

4251
const closeButton = within(callout).getByRole('button', {name: 'custom close label'});

src/callout.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ import type {ButtonLink, ButtonPrimary, ButtonSecondary} from './button';
2121
import type {DataAttributes, HeadingType, RendersNullableElement} from './utils/types';
2222

2323
type Props = {
24-
title?: string;
24+
title?:
25+
| {
26+
text: string;
27+
'aria-label'?: string;
28+
as?: HeadingType;
29+
}
30+
| string;
2531
titleAs?: HeadingType;
2632
description: string;
2733
onClose?: () => void;
@@ -52,13 +58,20 @@ const Callout = ({
5258
}: Props): JSX.Element => {
5359
const variant = useThemeVariant();
5460
const {texts, t} = useTheme();
61+
62+
const isTitleObject = typeof title === 'object';
63+
64+
const titleElementType = isTitleObject ? title?.as || titleAs : titleAs;
65+
const titleAriaLabel = isTitleObject ? title?.['aria-label'] : undefined;
66+
const titleText = isTitleObject ? title?.text : title;
67+
5568
return (
5669
<section
5770
className={classNames(styles.container, styles.background[variant])}
5871
style={applyCssVars({
5972
[mediaStyles.vars.mediaBorderRadius]: vars.borderRadii.mediaSmall,
6073
})}
61-
aria-label={ariaLabel ?? title}
74+
aria-label={ariaLabel}
6275
role={role}
6376
{...getPrefixedDataAttributes(dataAttributes, 'Callout')}
6477
>
@@ -72,8 +85,13 @@ const Callout = ({
7285
<Stack space={16}>
7386
<Inline fullWidth alignItems="flex-start" space="between">
7487
<Stack space={4}>
75-
<Text3 as={titleAs} regular dataAttributes={{testid: 'title'}}>
76-
{title}
88+
<Text3
89+
as={titleElementType}
90+
regular
91+
dataAttributes={{testid: 'title'}}
92+
aria-label={titleAriaLabel}
93+
>
94+
{titleText}
7795
</Text3>
7896
<Text2
7997
as="p"

0 commit comments

Comments
 (0)