Skip to content

Commit

Permalink
feat(overlay-alert): add aria props
Browse files Browse the repository at this point in the history
  • Loading branch information
chburket committed Sep 18, 2024
1 parent 9ec4e89 commit b7f2d10
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/components/OverlayAlert/OverlayAlert.stories.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ const overlayAlertArgTypes = {
},
},
},
ariaLabelledby: {
description: 'Provides the ariaLabelledby within this component as a `string`.',
control: { type: 'text' },
table: {
type: {
summary: 'string',
},
defaultValue: {
summary: 'undefined',
},
},
},
ariaDescribedby: {
description: 'Provides the ariaDescribedby within this component as a `string`.',
control: { type: 'text' },
table: {
type: {
summary: 'string',
},
defaultValue: {
summary: 'undefined',
},
},
},
};

export { overlayAlertArgTypes };
Expand Down
10 changes: 8 additions & 2 deletions src/components/OverlayAlert/OverlayAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const OverlayAlert: FC<Props> = (props: Props) => {
focusLockProps = DEFAULTS.FOCUS_LOCK_PROPS,
onClose,
ariaLabel,
ariaLabelledby,
ariaDescribedby,
...other
} = props;
const id = useRef(uuidV4());
Expand All @@ -44,6 +46,10 @@ const OverlayAlert: FC<Props> = (props: Props) => {
[shouldCloseOnEsc]
);

const ariaLabelledbyProp = ariaLabelledby ?? (title ? id.current : undefined);
const ariaDescribedbyProp =
ariaDescribedby ?? (details && !children ? detailsId.current : undefined);

return (
<Overlay
className={classnames(className, STYLE.wrapper)}
Expand All @@ -56,8 +62,8 @@ const OverlayAlert: FC<Props> = (props: Props) => {
round={75}
color={modalColor}
aria-label={ariaLabel}
aria-labelledby={title ? id.current : undefined}
aria-describedby={details && !children ? detailsId.current : undefined}
aria-labelledby={ariaLabelledbyProp}
aria-describedby={ariaDescribedbyProp}
focusLockProps={focusLockProps}
>
<div>
Expand Down
10 changes: 10 additions & 0 deletions src/components/OverlayAlert/OverlayAlert.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ export interface Props extends OverlayProps {
* ariaLabel for this OverlayAlert which will be passed onto ModalContainer.
*/
ariaLabel?: string;

/**
* ariaLabeledby for this OverlayAlert which will be passed onto ModalContainer.
*/
ariaLabelledby?: string;

/**
* ariaDescribedby for this OverlayAlert which will be passed onto ModalContainer.
*/
ariaDescribedby?: string;
}
14 changes: 12 additions & 2 deletions src/components/OverlayAlert/OverlayAlert.unit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,23 @@ describe('<OverlayAlert />', () => {
expect(container).toMatchSnapshot();
});

it('should match snapshot with ariaLabel', () => {
it('should match snapshot with aria props', () => {
expect.assertions(1);

const ariaLabel = 'test-aria-label';
const ariaLabelledby = 'test-aria-labelledby';
const ariaDescribedby = 'test-aria-describedby';
const children = 'example-children';

const container = mount(<OverlayAlert ariaLabel={ariaLabel}>{children}</OverlayAlert>);
const container = mount(
<OverlayAlert
ariaLabel={ariaLabel}
ariaLabelledby={ariaLabelledby}
ariaDescribedby={ariaDescribedby}
>
{children}
</OverlayAlert>
);

expect(container).toMatchSnapshot();
});
Expand Down
8 changes: 7 additions & 1 deletion src/components/OverlayAlert/OverlayAlert.unit.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ exports[`<OverlayAlert /> snapshot should match snapshot with actions 1`] = `
</OverlayAlert>
`;

exports[`<OverlayAlert /> snapshot should match snapshot with ariaLabel 1`] = `
exports[`<OverlayAlert /> snapshot should match snapshot with aria props 1`] = `
<OverlayAlert
ariaDescribedby="test-aria-describedby"
ariaLabel="test-aria-label"
ariaLabelledby="test-aria-labelledby"
>
<Overlay
className="md-overlay-alert-wrapper"
Expand All @@ -189,7 +191,9 @@ exports[`<OverlayAlert /> snapshot should match snapshot with ariaLabel 1`] = `
onKeyDown={[Function]}
>
<ModalContainer
aria-describedby="test-aria-describedby"
aria-label="test-aria-label"
aria-labelledby="test-aria-labelledby"
className="md-overlay-alert-modal-container"
focusLockProps={
Object {
Expand All @@ -208,7 +212,9 @@ exports[`<OverlayAlert /> snapshot should match snapshot with ariaLabel 1`] = `
hidden={true}
/>
<div
aria-describedby="test-aria-describedby"
aria-label="test-aria-label"
aria-labelledby="test-aria-labelledby"
aria-modal={true}
className="md-overlay-alert-modal-container md-modal-container-wrapper"
data-color="primary"
Expand Down

0 comments on commit b7f2d10

Please sign in to comment.