# All files / src/_hooks Trigger.tsx

97.36% Statements 37/38
100% Branches 12/12
85.71% Functions 6/7
96.77% Lines 30/31

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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    18160x
    60x

    60x

    60x
    1535x
    1535x
    1535x
    1535x
    1535x
    1535x
    1535x

    1535x

    1535x

    1535x

    1535x

    1535x

    60x
    132x
    132x
    132x

    132x

    132x

    132x

    180x

    60x

    60x

    60x
    1x

    60x
    2x

     import React from 'react'
    import { triggerListeners } from '../_utils/triggerListeners'
    import { RenderProp } from '../_utils/types'
    import { useHotkey } from './Hotkey'

    /* TYPES */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export type TriggerVariant =
      | 'click'
      | 'hover'
      | 'none'
      | 'contextmenu'
      | 'focus'
      | 'always'

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TriggerConfig {
      disable: React.EventHandler<any>
      disableKeys?: string[]
      enable: React.EventHandler<any>
      enableKeys?: string[]
      isEnabled?: boolean
      trigger?: TriggerVariant | TriggerVariant[]
      triggerRef: React.RefObject<HTMLElement>
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TriggerProps extends TriggerConfig {
      children: React.ReactNode
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TriggerRenderProps
      extends TriggerConfig,
        RenderProp<TriggerApi> {}

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface TriggerApi {
      disable: React.EventHandler<any>
      enable: React.EventHandler<any>
      isVisible: boolean
      toggle: React.EventHandler<any>
    }

    /* HOOK */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useTrigger({
      disable,
      disableKeys = [],
      enable,
      enableKeys = [],
      isEnabled = false,
      trigger = 'click',
      triggerRef,
    }: TriggerConfig): TriggerApi {
      triggerListeners({
        off: disable,
        on: enable,
        scope: triggerRef,
        trigger,
        value: isEnabled,
      })

      useHotkey({
        key: enableKeys,
        handler: enable,
        preventDefault: true,
        scope: triggerRef,
      })

      useHotkey({
        key: disableKeys,
        handler: disable,
        preventDefault: true,
        scope: triggerRef,
      })

      // prevent keyup event from firing on FF
      useHotkey({
        event: 'keyup',
        key: [...enableKeys, ...disableKeys],
        handler: () => {},
        preventDefault: true,
        scope: triggerRef,
      })

      return {
        disable,
        enable,
        isVisible: isEnabled,
        toggle: isEnabled ? disable : enable,
      }
    }

    /* RENDER PROP */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function Trigger({
      children,
      triggerRef,
      ...props
    }: TriggerRenderProps) {
      const trigger = useTrigger({ ...props, triggerRef })

      const element =
        typeof children === 'function'
          ? (children as (props: TriggerApi) => React.ReactElement)(trigger)
          : (children as React.ReactElement)

      return React.cloneElement(element, {
        open: trigger.isVisible,
      })
    }

    /* CONTEXT */

    const noop: (method: string) => React.EventHandler<any> = (method) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in an \`<TriggerProvider>\``
      )

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const initTrigger: TriggerApi = {
      disable: noop('disable'),
      enable: noop('enable'),
      isVisible: false,
      toggle: noop('toggle'),
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const TriggerContext = React.createContext(initTrigger)

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function TriggerProvider({ children, ...props }: TriggerProps) {
      return (
        <TriggerContext.Provider value={useTrigger(props)}>
          {children}
        </TriggerContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useTriggerContext() {
      return React.useContext(TriggerContext)
    }