Unverified Commit bf8cf270 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #1806 from hypothesis/fix-button-pressed-state

Do not set "aria-pressed" attribute for non-toggle buttons
parents 8211e808 fd927d6b
...@@ -96,7 +96,7 @@ function AnnotationActionBar({ ...@@ -96,7 +96,7 @@ function AnnotationActionBar({
)} )}
{showFlagAction && annotation.flagged && ( {showFlagAction && annotation.flagged && (
<Button <Button
isActive={true} isPressed={true}
icon="flag--active" icon="flag--active"
title="Annotation has been reported to the moderators" title="Annotation has been reported to the moderators"
/> />
......
...@@ -21,7 +21,7 @@ export default function Button({ ...@@ -21,7 +21,7 @@ export default function Button({
className = '', className = '',
disabled = false, disabled = false,
icon = '', icon = '',
isActive = false, isPressed,
onClick = () => null, onClick = () => null,
style = {}, style = {},
title, title,
...@@ -35,6 +35,12 @@ export default function Button({ ...@@ -35,6 +35,12 @@ export default function Button({
const baseClassName = buttonText ? 'button--labeled' : 'button--icon-only'; const baseClassName = buttonText ? 'button--labeled' : 'button--icon-only';
const extraProps = {};
if (typeof isPressed === 'boolean') {
// Indicate that this is a toggle button.
extraProps['aria-pressed'] = isPressed;
}
return ( return (
<button <button
className={classnames( className={classnames(
...@@ -44,15 +50,15 @@ export default function Button({ ...@@ -44,15 +50,15 @@ export default function Button({
'button--compact': useCompactStyle, 'button--compact': useCompactStyle,
'button--input': useInputStyle, 'button--input': useInputStyle,
'button--primary': usePrimaryStyle, 'button--primary': usePrimaryStyle,
'is-active': isActive, 'is-active': isPressed,
}, },
className className
)} )}
onClick={onClick} onClick={onClick}
aria-pressed={isActive}
title={title} title={title}
style={style} style={style}
disabled={disabled} disabled={disabled}
{...extraProps}
> >
{icon && <SvgIcon name={icon} className="button__icon" />} {icon && <SvgIcon name={icon} className="button__icon" />}
{buttonText} {buttonText}
...@@ -104,8 +110,13 @@ Button.propTypes = { ...@@ -104,8 +110,13 @@ Button.propTypes = {
*/ */
icon: requiredStringIfButtonTextMissing, icon: requiredStringIfButtonTextMissing,
/** Is this button currently in an "active"/"on" state? */ /**
isActive: propTypes.bool, * Indicate that this is a toggle button (if `isPressed` is a boolean) and
* whether it is pressed.
*
* If omitted, the button is a non-toggle button.
*/
isPressed: propTypes.bool,
/** callback for button clicks */ /** callback for button clicks */
onClick: propTypes.func, onClick: propTypes.func,
......
...@@ -14,7 +14,6 @@ describe('Button', () => { ...@@ -14,7 +14,6 @@ describe('Button', () => {
return mount( return mount(
<Button <Button
icon="fakeIcon" icon="fakeIcon"
isActive={false}
title="My Action" title="My Action"
onClick={fakeOnClick} onClick={fakeOnClick}
{...props} {...props}
...@@ -31,8 +30,8 @@ describe('Button', () => { ...@@ -31,8 +30,8 @@ describe('Button', () => {
$imports.$restore(); $imports.$restore();
}); });
it('adds active className if `isActive` is `true`', () => { it('adds active className if `isPressed` is `true`', () => {
const wrapper = createComponent({ isActive: true }); const wrapper = createComponent({ isPressed: true });
assert.isTrue(wrapper.find('button').hasClass('is-active')); assert.isTrue(wrapper.find('button').hasClass('is-active'));
}); });
...@@ -42,9 +41,16 @@ describe('Button', () => { ...@@ -42,9 +41,16 @@ describe('Button', () => {
assert.equal(wrapper.find('SvgIcon').prop('name'), 'fakeIcon'); assert.equal(wrapper.find('SvgIcon').prop('name'), 'fakeIcon');
}); });
it('sets ARIA `aria-pressed` attribute if `isActive`', () => { [true, false].forEach(isPressed => {
const wrapper = createComponent({ isActive: true }); it('sets `aria-pressed` attribute if `isPressed` is a boolean', () => {
assert.isTrue(wrapper.find('button').prop('aria-pressed')); const wrapper = createComponent({ isPressed });
assert.equal(wrapper.find('button').prop('aria-pressed'), isPressed);
});
});
it('does not set `aria-pressed` attribute if `isPressed` is omitted', () => {
const wrapper = createComponent();
assert.notProperty(wrapper.find('button').props(), 'aria-pressed');
}); });
it('sets `title` to provided `title` prop', () => { it('sets `title` to provided `title` prop', () => {
......
...@@ -119,7 +119,7 @@ describe('TopBar', () => { ...@@ -119,7 +119,7 @@ describe('TopBar', () => {
wrapper.update(); wrapper.update();
assert.isTrue(helpButton.props().isActive); assert.isTrue(helpButton.props().isPressed);
}); });
context('help service handler configured in services', () => { context('help service handler configured in services', () => {
...@@ -228,7 +228,7 @@ describe('TopBar', () => { ...@@ -228,7 +228,7 @@ describe('TopBar', () => {
const wrapper = createTopBar(); const wrapper = createTopBar();
const shareButton = getButton(wrapper, 'share'); const shareButton = getButton(wrapper, 'share');
assert.isTrue(shareButton.prop('isActive')); assert.isTrue(shareButton.prop('isPressed'));
}); });
it('displays search input in the sidebar', () => { it('displays search input in the sidebar', () => {
......
...@@ -105,7 +105,7 @@ function TopBar({ ...@@ -105,7 +105,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="help" icon="help"
isActive={currentActivePanel === uiConstants.PANEL_HELP} isPressed={currentActivePanel === uiConstants.PANEL_HELP}
onClick={requestHelp} onClick={requestHelp}
title="Help" title="Help"
useCompactStyle useCompactStyle
...@@ -135,7 +135,7 @@ function TopBar({ ...@@ -135,7 +135,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="share" icon="share"
isActive={ isPressed={
currentActivePanel === uiConstants.PANEL_SHARE_ANNOTATIONS currentActivePanel === uiConstants.PANEL_SHARE_ANNOTATIONS
} }
onClick={toggleSharePanel} onClick={toggleSharePanel}
...@@ -146,7 +146,7 @@ function TopBar({ ...@@ -146,7 +146,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="help" icon="help"
isActive={currentActivePanel === uiConstants.PANEL_HELP} isPressed={currentActivePanel === uiConstants.PANEL_HELP}
onClick={requestHelp} onClick={requestHelp}
title="Help" title="Help"
useCompactStyle useCompactStyle
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment