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(button): add aria labels for accessibility #3377

Merged
merged 6 commits into from
Aug 4, 2023

Conversation

dsporysz-box
Copy link
Contributor

Add an optional aria-label property to CloseButton and Button. Note that the "aria-label" property in CloseButton has a default value "close".

@dsporysz-box dsporysz-box requested a review from a team as a code owner July 27, 2023 09:49
@@ -92,6 +95,7 @@ class Button extends React.Component<ButtonProps> {
let button = (
// eslint-disable-next-line react/button-has-type
<button
aria-label={ariaLabel}
Copy link
Contributor

Choose a reason for hiding this comment

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

the Button component uses the rest functionality to allow additional HTML attributes. this is how other components have been adding aria labels:


<Button aria-label={sortMessage} className="be-btn-sort" type="button" {...rest}>

are we able to continue using this pattern? any specific reason for the explicit prop now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for showing me examples. I made changes to use the rest functionality.

/** Custom class for the close button */
className?: string,
/** onClick handler for the close button */
onClick?: Function,
}

const CloseButton = ({ className, onClick }: Props) => {
const CloseButton = ({ className, onClick, ariaLabel = 'close' }: Props) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

the Close message should be translated since this is read by screen readers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the hardcoded value and I used the IntlShape for close message translation. Thanks a lot!

/** Custom class for the close button */
className?: string,
/** onClick handler for the close button */
onClick?: Function,
}

const CloseButton = ({ className, onClick }: Props) => {
const CloseButton = ({ className, onClick, ariaLabel = 'close' }: Props) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these props are sorted alphabetically

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's been removed. Thanks!

/** Custom class for the close button */
className?: string,
/** onClick handler for the close button */
onClick?: Function,
}

const CloseButton = ({ className, onClick }: Props) => {
const CloseButton = ({ className, onClick, ariaLabel = 'close' }: Props) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

any reason why ariaLabel is a prop in this component? i.e. is there a scenario in which a parent component would pass a different label than "Close"?

if not, then you could pass the attribute directly to Button without making a new prop in CloseButton

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've found a proper close message for CloseButton in common/messages.js. Now the label is properly translated and I've removed aria-label from props.

import './CloseButton.scss';

export interface CloseButtonProps {
intl: IntlShape;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this list is sorted alphabetically

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorted.

/** Custom class for the close button */
className?: string;
/** onClick handler for the close button */
onClick?: Function;
}

const CloseButton = ({ className, onClick }: CloseButtonProps) => {
const CloseButton = ({ intl, className, onClick }: CloseButtonProps) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we sort these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorted.

@@ -37,4 +37,12 @@ describe('components/close-button/CloseButton', () => {
expect(handleClick).toHaveBeenCalledTimes(1);
});
});

describe('accesability properties', () => {
test('should have an accesiblity label', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

could we move this into the render() describe above? this test probably doesn't need to be in its own describe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved it.

src/components/close-button/__tests__/CloseButton.test.tsx Outdated Show resolved Hide resolved
@@ -27,4 +34,5 @@ const CloseButton = ({ className, onClick }: CloseButtonProps) => {
);
};

export default CloseButton;
export { CloseButton as CloseButtonBase };
Copy link
Contributor

Choose a reason for hiding this comment

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

usually we do this export so that we can test the component without the injectIntl HOC. do we need to update the import in the test?

Copy link
Contributor Author

@dsporysz-box dsporysz-box Aug 4, 2023

Choose a reason for hiding this comment

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

I removed an unused export, leaving just the one with injectIntl.

Copy link
Contributor

@tjuanitas tjuanitas left a comment

Choose a reason for hiding this comment

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

lgtm thank you for the changes!

@mergify mergify bot merged commit 9a9b5e3 into box:master Aug 4, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants