event.ts 623 B

123456789101112131415
  1. export const composeEventHandlers = <E>(theirsHandler?: (event: E) => boolean | void, oursHandler?: (event: E) => void, { checkForDefaultPrevented = true } = {}) => {
  2. const handleEvent = (event: E) => {
  3. const shouldPrevent = theirsHandler?.(event)
  4. if (checkForDefaultPrevented === false || !shouldPrevent) {
  5. return oursHandler?.(event)
  6. }
  7. }
  8. return handleEvent
  9. }
  10. type WhenMouseHandler = (e: PointerEvent) => any
  11. export const whenMouse = (handler: WhenMouseHandler): WhenMouseHandler => {
  12. return (e: PointerEvent) => (e.pointerType === 'mouse' ? handler(e) : undefined)
  13. }