Skip to content

Commit 3c274ba

Browse files
faridomarAfovflowd
andauthored
Fix button focus (#7287)
* add changes to MetaBar index.modul.css * add href to button * remove unrelated changes * apply changes to CodeBox file & Button file * add change to CodeBox file * add changes to CodeBox & Button component * import KeyboardEvent & MouseEvent from react at Button index.file * refactor: consolidate React imports into a single line * refactor: consolidate React imports into a single line * refactor: Button component & CodeBox compnent * Update: Button component modified in case of type-saftey at some part * omit unnecessary comment-lines, handle-proper naming * omit unnecessary comment-lines, handle-proper naming * Update apps/site/components/Common/Button/index.tsx Signed-off-by: Claudio W <[email protected]> --------- Signed-off-by: Claudio W <[email protected]> Co-authored-by: Claudio W <[email protected]>
1 parent d572121 commit 3c274ba

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed
Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
'use client';
2+
13
import classNames from 'classnames';
2-
import type { FC, AnchorHTMLAttributes } from 'react';
4+
import type {
5+
FC,
6+
AnchorHTMLAttributes,
7+
KeyboardEvent,
8+
MouseEvent,
9+
} from 'react';
310

411
import Link from '@/components/Link';
512

613
import styles from './index.module.css';
714

815
type ButtonProps = AnchorHTMLAttributes<HTMLAnchorElement> & {
916
kind?: 'neutral' | 'primary' | 'secondary' | 'special';
10-
// We have an extra `disabled` prop as we simulate a button
1117
disabled?: boolean;
1218
};
1319

@@ -17,17 +23,48 @@ const Button: FC<ButtonProps> = ({
1723
href = undefined,
1824
children,
1925
className,
26+
onClick,
2027
...props
21-
}) => (
22-
<Link
23-
role="button"
24-
href={disabled ? undefined : href}
25-
aria-disabled={disabled}
26-
className={classNames(styles.button, styles[kind], className)}
27-
{...props}
28-
>
29-
{children}
30-
</Link>
31-
);
28+
}) => {
29+
const onKeyDownHandler = (e: KeyboardEvent<HTMLAnchorElement>) => {
30+
if (disabled) {
31+
e.preventDefault();
32+
return;
33+
}
34+
35+
if (e.key === 'Enter' || e.key === ' ') {
36+
e.preventDefault();
37+
if (typeof onClick === 'function') {
38+
onClick(e as unknown as MouseEvent<HTMLAnchorElement>);
39+
}
40+
}
41+
};
42+
43+
const onClickHandler = (e: MouseEvent<HTMLAnchorElement>) => {
44+
if (disabled) {
45+
e.preventDefault();
46+
return;
47+
}
48+
49+
if (typeof onClick === 'function') {
50+
onClick(e);
51+
}
52+
};
53+
54+
return (
55+
<Link
56+
role="button"
57+
href={disabled ? undefined : href}
58+
aria-disabled={disabled}
59+
className={classNames(styles.button, styles[kind], className)}
60+
tabIndex={disabled ? -1 : 0} // Ensure focusable if not disabled
61+
onClick={onClickHandler}
62+
onKeyDown={onKeyDownHandler}
63+
{...props}
64+
>
65+
{children}
66+
</Link>
67+
);
68+
};
3269

3370
export default Button;

0 commit comments

Comments
 (0)