Skip to content
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

fix: add close button accessible name props to ToastNotification #462

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/components/ToastNotification/ToastNotification.stories.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ const toastNotificationArgTypes = {
},
},
},
closeButtonLabel: {
description: 'Accessible name of the close button',
control: { type: 'text' },
table: {
type: {
summary: 'string',
},
defaultValue: {
summary: 'undefined',
},
},
},
};

export { toastNotificationArgTypes };
Expand Down
5 changes: 3 additions & 2 deletions src/components/ToastNotification/ToastNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import Text from '../Text';
* The ToastNotification component.
*/
const ToastNotification: FC<Props> = (props: Props) => {
const { className, id, style, content, leadingVisual, buttonGroup, onClose } = props;
const { className, id, style, content, leadingVisual, buttonGroup, onClose, closeButtonLabel } =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this case gives us the perfect opportunity to have a default arialabel for the button "Close notification". X buttons in notification toasts, if pressent, will always need an aria label. So if we are making this prop optional (which is good imo), then we should add a default value.

props;

return (
<ModalContainer
Expand All @@ -37,7 +38,7 @@ const ToastNotification: FC<Props> = (props: Props) => {
)}
{onClose && (
<div className={classnames(className, STYLE.closeButton)}>
<ButtonCircle size={20} ghost onPress={onClose}>
<ButtonCircle size={20} ghost onPress={onClose} aria-label={closeButtonLabel}>
<Icon name="cancel" weight="bold" scale={16} />
</ButtonCircle>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/components/ToastNotification/ToastNotification.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ export interface Props {
* Callback when user clicks on the close button.
*/
onClose?: (event: PressEvent) => void;

/**
* Close button's aria-label
*/
closeButtonLabel?: string;
}
21 changes: 19 additions & 2 deletions src/components/ToastNotification/ToastNotification.unit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ describe('<ToastNotification />', () => {

const style = { color: 'pink' };

const container = await mountAndWait(<ToastNotification style={style} content={exampleContent} />);
const container = await mountAndWait(
<ToastNotification style={style} content={exampleContent} />
);

expect(container).toMatchSnapshot();
});
Expand Down Expand Up @@ -158,14 +160,29 @@ describe('<ToastNotification />', () => {
});

it('should wrap the onClose inside when onClose is provided', async () => {
expect.assertions(1);
expect.assertions(2);

const wrapper = await mountAndWait(
<ToastNotification onClose={onClose} content={exampleContent} />
);
const element = wrapper.find('.md-toast-notification-close-button').getDOMNode();
const button = wrapper.find('.md-toast-notification-close-button button');

expect(element).toBeDefined();
expect(button.props()['aria-label']).toBe(undefined);
});

it('should have label of the close button when both onClose and closeButtonLabel defined', async () => {
expect.assertions(2);

const wrapper = await mountAndWait(
<ToastNotification onClose={onClose} closeButtonLabel={'close'} content={exampleContent} />
);
const element = wrapper.find('.md-toast-notification-close-button').getDOMNode();
const button = wrapper.find('.md-toast-notification-close-button button');

expect(element).toBeDefined();
expect(button.props()['aria-label']).toBe('close');
});

it('should wrap Icon inside leadingVisual when Icon is provided', async () => {
Expand Down
Loading