Last active
April 21, 2023 04:37
-
-
Save jeremybanka/b1193139f2846ef1a2f7e6a25a9a89b3 to your computer and use it in GitHub Desktop.
Easy Mouse Button Handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { MouseEventHandler } from "react" | |
export type MakeMouseHandlers = ( | |
a: Partial< | |
Record< | |
| `onClickL` | |
| `onClickM` | |
| `onClickR` | |
| `onMouseDownL` | |
| `onMouseDownM` | |
| `onMouseDownR` | |
| `onMouseUpL` | |
| `onMouseUpM` | |
| `onMouseUpR`, | |
MouseEventHandler | |
> | |
> | |
) => { | |
onClick: MouseEventHandler | |
onMouseDown: MouseEventHandler | |
onMouseUp: MouseEventHandler | |
} | |
export const makeMouseHandlers: MakeMouseHandlers = ({ | |
onClickL, | |
onClickM, | |
onClickR, | |
onMouseDownL, | |
onMouseDownM, | |
onMouseDownR, | |
onMouseUpL, | |
onMouseUpM, | |
onMouseUpR, | |
}) => ({ | |
onClick: (e) => [onClickL, onClickM, onClickR][e.button]?.(e), | |
onMouseDown: (e) => [onMouseDownL, onMouseDownM, onMouseDownR][e.button]?.(e), | |
onMouseUp: (e) => [onMouseUpL, onMouseUpM, onMouseUpR][e.button]?.(e), | |
onContextMenu: (e) => e.preventDefault(), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment