Skip to content

Commit 42d2a3c

Browse files
authored
fix: Button accessibility (#7420)
* fix: button accessibility * chore: tabindex -1 when disabled
1 parent 1e51ef6 commit 42d2a3c

File tree

1 file changed

+29
-42
lines changed

1 file changed

+29
-42
lines changed

apps/site/components/Common/Button/index.tsx

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
'use client';
22

33
import classNames from 'classnames';
4-
import type {
5-
FC,
6-
AnchorHTMLAttributes,
7-
KeyboardEvent,
8-
MouseEvent,
9-
} from 'react';
4+
import type { FC, AnchorHTMLAttributes, ButtonHTMLAttributes } from 'react';
105

116
import Link from '@/components/Link';
127

138
import styles from './index.module.css';
149

15-
type ButtonProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
10+
type ButtonProps = (
11+
| AnchorHTMLAttributes<HTMLAnchorElement>
12+
| ButtonHTMLAttributes<HTMLButtonElement>
13+
) & {
1614
kind?: 'neutral' | 'primary' | 'secondary' | 'special';
1715
size?: 'default' | 'small';
1816
disabled?: boolean;
@@ -22,55 +20,44 @@ const Button: FC<ButtonProps> = ({
2220
kind = 'primary',
2321
size = 'default',
2422
disabled = false,
25-
href = undefined,
2623
children,
2724
className,
28-
onClick,
2925
...props
3026
}) => {
31-
const onKeyDownHandler = (e: KeyboardEvent<HTMLAnchorElement>) => {
32-
if (disabled) {
33-
e.preventDefault();
34-
return;
35-
}
36-
37-
if (e.key === 'Enter' || e.key === ' ') {
38-
e.preventDefault();
39-
if (typeof onClick === 'function') {
40-
onClick(e as unknown as MouseEvent<HTMLAnchorElement>);
41-
}
42-
}
43-
};
44-
45-
const onClickHandler = (e: MouseEvent<HTMLAnchorElement>) => {
46-
if (disabled) {
47-
e.preventDefault();
48-
return;
49-
}
50-
51-
if (typeof onClick === 'function') {
52-
onClick(e);
53-
}
54-
};
27+
if ('href' in props) {
28+
return (
29+
<Link
30+
role="button"
31+
href={disabled ? undefined : props.href}
32+
aria-disabled={disabled}
33+
className={classNames(
34+
styles.button,
35+
styles[kind],
36+
styles[size],
37+
className
38+
)}
39+
tabIndex={disabled ? -1 : 0}
40+
{...props}
41+
>
42+
{children}
43+
</Link>
44+
);
45+
}
5546

5647
return (
57-
<Link
58-
role="button"
59-
href={disabled ? undefined : href}
60-
aria-disabled={disabled}
48+
<button
49+
disabled={disabled}
6150
className={classNames(
6251
styles.button,
6352
styles[kind],
6453
styles[size],
6554
className
6655
)}
67-
tabIndex={disabled ? -1 : 0} // Ensure focusable if not disabled
68-
onClick={onClickHandler}
69-
onKeyDown={onKeyDownHandler}
70-
{...props}
56+
type="button"
57+
{...(props as ButtonHTMLAttributes<HTMLButtonElement>)}
7158
>
7259
{children}
73-
</Link>
60+
</button>
7461
);
7562
};
7663

0 commit comments

Comments
 (0)