Skip to content

Commit 2bd07f3

Browse files
authored
fix(accessible-button): use not-allowed cursor on disabled (#1114)
* fix(accessible-button): use not-allowed cursor on disabled * chore: remove useless css
1 parent 21ef9e4 commit 2bd07f3

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

src/components/buttons/accessible-button/accessible-button.js

+48-36
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,55 @@ import React from 'react';
33
import { css } from '@emotion/core';
44
import vars from '../../../../materials/custom-properties';
55

6-
const AccessibleButton = React.forwardRef((props, ref) => (
7-
<button
8-
id={props.id}
9-
ref={ref}
10-
type={props.type}
11-
aria-label={props.label}
12-
onClick={props.onClick}
13-
css={css`
14-
border: none;
15-
background: none;
16-
display: inline-block;
17-
outline: 0;
18-
padding: 0;
19-
margin: 0;
20-
white-space: nowrap;
21-
cursor: pointer;
22-
color: inherit;
23-
font: inherit;
24-
font-size: ${vars.fontSizeDefault};
25-
font-family: inherit;
6+
const AccessibleButton = React.forwardRef((props, ref) => {
7+
const { onClick } = props;
268

27-
&:disabled {
28-
pointer-events: none;
29-
}
30-
`}
31-
// Allow to override the styles by passing a `className` prop.
32-
// Custom styles can also be passed using the `css` prop from emotion.
33-
// https://emotion.sh/docs/css-prop#style-precedence
34-
className={props.className}
35-
disabled={props.isDisabled}
36-
aria-disabled={props.isDisabled}
37-
{...(props.isToggleButton ? { 'aria-pressed': props.isToggled } : {})}
38-
{...props.buttonAttributes}
39-
>
40-
{props.children}
41-
</button>
42-
));
9+
const handleClick = React.useCallback(
10+
event => {
11+
if (!props.isDisabled) return onClick(event);
12+
// eslint-disable-next-line no-useless-return, consistent-return
13+
return;
14+
},
15+
[onClick, props.isDisabled]
16+
);
17+
return (
18+
<button
19+
id={props.id}
20+
ref={ref}
21+
type={props.type}
22+
aria-label={props.label}
23+
onClick={handleClick}
24+
css={css`
25+
border: none;
26+
background: none;
27+
display: inline-block;
28+
outline: 0;
29+
padding: 0;
30+
margin: 0;
31+
white-space: nowrap;
32+
cursor: pointer;
33+
color: inherit;
34+
font: inherit;
35+
font-size: ${vars.fontSizeDefault};
36+
font-family: inherit;
37+
38+
&:disabled {
39+
cursor: not-allowed;
40+
}
41+
`}
42+
// Allow to override the styles by passing a `className` prop.
43+
// Custom styles can also be passed using the `css` prop from emotion.
44+
// https://emotion.sh/docs/css-prop#style-precedence
45+
className={props.className}
46+
disabled={props.isDisabled}
47+
aria-disabled={props.isDisabled}
48+
{...(props.isToggleButton ? { 'aria-pressed': props.isToggled } : {})}
49+
{...props.buttonAttributes}
50+
>
51+
{props.children}
52+
</button>
53+
);
54+
});
4355
AccessibleButton.displayName = 'AccessibleButton';
4456
AccessibleButton.propTypes = {
4557
id: PropTypes.string,

src/components/buttons/accessible-button/accessible-button.spec.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ describe('rendering', () => {
3131
expect(container).toContainElement(ref.current);
3232
});
3333
it('should be marked as "disabled"', () => {
34+
const onClick = jest.fn();
3435
const { getByLabelText } = render(
35-
<AccessibleButton {...props} isDisabled={true} />
36-
);
37-
expect(getByLabelText('test-button')).toHaveAttribute('disabled');
38-
expect(getByLabelText('test-button')).toHaveAttribute(
39-
'aria-disabled',
40-
'true'
36+
<AccessibleButton {...props} onClick={onClick} isDisabled={true} />
4137
);
38+
const button = getByLabelText('test-button');
39+
40+
expect(button).toHaveAttribute('disabled');
41+
expect(button).toHaveAttribute('aria-disabled', 'true');
42+
43+
button.click();
44+
expect(onClick).not.toHaveBeenCalled();
4245
});
4346
it('should be marked as "active"', () => {
4447
const { getByLabelText } = render(

src/components/buttons/secondary-button/secondary-button.styles.js

-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const getStateStyles = (isDisabled, isActive, theme) => {
88
box-shadow: 0 0 0 1px ${vars.colorNeutral} inset;
99
background-color: ${vars.colorAccent98};
1010
color: ${vars.colorNeutral60};
11-
pointer-events: none;
12-
cursor: not-allowed;
1311
`;
1412
switch (theme) {
1513
case 'info':
@@ -38,7 +36,6 @@ const getStateStyles = (isDisabled, isActive, theme) => {
3836
box-shadow: 0 0 0 1px ${vars.colorNeutral} inset;
3937
background-color: ${vars.colorAccent98};
4038
color: ${vars.colorNeutral60};
41-
pointer-events: none;
4239
`,
4340
];
4441
switch (theme) {

0 commit comments

Comments
 (0)