# All files / src/_utils triggerListeners.ts

100% Statements 30/30
100% Branches 12/12
75% Functions 3/4
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    8960x

    60x

    1535x
    1535x

    1535x
    9210x
    9210x

    1535x
    9210x

    1535x
    1535x

    1535x

    1535x
    1535x

    1535x

    1535x
    1535x

    1535x
    1535x

    1535x

    1535x
    1535x

    1535x
    1535x

    1535x

     import { Scope, useEventListener } from '../_hooks/EventListener'
    import { TriggerVariant } from '../_hooks/Trigger'

    interface Config {
      value?: boolean
      off: (event: any) => any
      on: (event: any) => any
      trigger: TriggerVariant | TriggerVariant[]
      scope: Scope
    }

    function noop() {}

    const availableTriggers = [
      'click',
      'hover',
      'none',
      'contextmenu',
      'focus',
      'always',
    ]

    export function triggerListeners({ value, off, on, trigger, scope }: Config) {
      const targetTriggers = Array.isArray(trigger) ? trigger : [trigger]

      const handlerOrNoop = (fn: (event: any) => any, trigger: TriggerVariant) => {
        const isTargetTrigger = targetTriggers.includes(trigger)
        return {
          handler: isTargetTrigger ? fn : noop,
          noopHandler: !isTargetTrigger,
        }
      }

      availableTriggers.forEach((trigger) => {
        switch (trigger) {
          case 'click':
            const handler = handlerOrNoop(value ? off : on, trigger)
            useEventListener({
              dependencies: [handler],
              event: 'click',
              scope,
              ...handler,
            })
            break

          case 'contextmenu':
            const ctxMenuHandler = handlerOrNoop(value ? off : on, trigger)
            useEventListener({
              dependencies: [ctxMenuHandler],
              event: 'contextmenu',
              scope,
              ...ctxMenuHandler,
            })
            break

          case 'hover':
            const mouseLeaveHandler = handlerOrNoop(off, trigger)
            useEventListener({
              event: 'mouseleave',
              scope,
              ...mouseLeaveHandler,
            })
            const mouseEnterHandler = handlerOrNoop(on, trigger)
            useEventListener({
              event: 'mouseenter',
              scope,
              ...mouseEnterHandler,
            })
            break

          case 'focus':
            const blurHandler = handlerOrNoop(off, trigger)
            useEventListener({
              event: 'blur',
              scope,
              ...blurHandler,
            })

            const focusHandler = handlerOrNoop(on, trigger)
            useEventListener({
              event: 'focus',
              scope,
              ...focusHandler,
            })
            break
        }
      })
    }