-
Notifications
You must be signed in to change notification settings - Fork 305
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
Conversation
src/components/button/Button.tsx
Outdated
@@ -92,6 +95,7 @@ class Button extends React.Component<ButtonProps> { | |||
let button = ( | |||
// eslint-disable-next-line react/button-has-type | |||
<button | |||
aria-label={ariaLabel} |
There was a problem hiding this comment.
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:
...rest |
<Button aria-label={sortMessage} className="be-btn-sort" type="button" {...rest}> |
aria-label={label} |
are we able to continue using this pattern? any specific reason for the explicit prop now?
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it.
@@ -27,4 +34,5 @@ const CloseButton = ({ className, onClick }: CloseButtonProps) => { | |||
); | |||
}; | |||
|
|||
export default CloseButton; | |||
export { CloseButton as CloseButtonBase }; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
03b425f
to
f1a4459
Compare
There was a problem hiding this 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!
Add an optional aria-label property to CloseButton and Button. Note that the "aria-label" property in CloseButton has a default value "close".