@@ -3,26 +3,26 @@ import useKey from '@accessible/use-key'
3
3
import useEvent from '@react-hook/event'
4
4
import useMergedRef from '@react-hook/merged-ref'
5
5
6
- export function useA11yButton <
7
- T extends HTMLElement ,
8
- E extends React . MouseEvent < T , MouseEvent >
9
- > ( target : React . RefObject < T > | T | null , onClick : ( event : E ) => any ) {
6
+ export function useA11yButton < T extends HTMLElement > (
7
+ target : React . RefObject < T > | T | null ,
8
+ onClick : ( event : MouseEvent ) => any
9
+ ) {
10
10
const clickedMouse = React . useRef ( false )
11
11
const setClickedMouse = ( ) => ( clickedMouse . current = true )
12
12
useEvent ( target , 'touchstart' , setClickedMouse )
13
13
useEvent ( target , 'mousedown' , setClickedMouse )
14
+ useEvent ( target , 'click' , ( event ) => {
15
+ // Only fire onClick if the keyboard was not used to initiate the click
16
+ clickedMouse . current && onClick ( event )
17
+ clickedMouse . current = false
18
+ } )
14
19
// @ts -expect-error
15
20
useKey ( target , {
16
21
Enter : onClick ,
17
22
' ' : onClick ,
18
23
} )
19
24
20
25
return {
21
- onClick : ( event : E ) => {
22
- // Only fire onClick if the keyboard was not used to initiate the click
23
- clickedMouse . current && onClick ( event )
24
- clickedMouse . current = false
25
- } ,
26
26
role : 'button' ,
27
27
tabIndex : 0 ,
28
28
} as const
@@ -31,10 +31,10 @@ export function useA11yButton<
31
31
export const Button = ( { children} : ButtonProps ) => {
32
32
const ref = React . useRef ( null )
33
33
const { props} = children
34
- const { onClick , role, tabIndex} = useA11yButton ( ref , props . onClick )
34
+ const { role, tabIndex} = useA11yButton ( ref , props . onClick )
35
35
36
36
return React . cloneElement ( children , {
37
- onClick,
37
+ onClick : undefined ,
38
38
role : props . hasOwnProperty ( 'role' ) ? props . role : role ,
39
39
tabIndex : props . hasOwnProperty ( 'tabIndex' ) ? props . tabIndex : tabIndex ,
40
40
// @ts -expect-error
0 commit comments