event.ts 607 B

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