Skip to content

Instantly share code, notes, and snippets.

@jeremybanka
Last active April 21, 2023 04:37
Show Gist options
  • Save jeremybanka/b1193139f2846ef1a2f7e6a25a9a89b3 to your computer and use it in GitHub Desktop.
Save jeremybanka/b1193139f2846ef1a2f7e6a25a9a89b3 to your computer and use it in GitHub Desktop.
Easy Mouse Button Handlers
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