# All files / src/_hooks Event.tsx

100% Statements 25/25
50% Branches 2/4
100% Functions 4/4
100% Lines 16/16

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
    832x
    2x

    2x
    4x
    4x
    4x

    4x

    4x

    4x

    2x
    1x

    1x

    2x

    2x
    1x

    2x
     import React from 'react'
    import { Scope, useEventListener } from './EventListener'

    /* TYPES */

    interface EventConfig {
      event: string
      options?: AddEventListenerOptions
      scope?: Scope
    }

    interface EventApi {
      value: any
    }

    interface Props extends EventConfig {
      children?: React.ReactNode
    }

    interface RenderProps extends EventConfig {
      children: (props: EventApi) => {}
    }

    /* HOOK */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useEvent({
      event,
      options,
      scope = window,
    }: EventConfig): EventApi {
      const [value, handler] = React.useState(null)

      useEventListener({ event, handler, options, scope })

      return {
        value,
      }
    }

    /* RENDER PROP */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function Event({ children, ...props }: RenderProps) {
      const event = useEvent(props)

      return typeof children === 'function' ? children(event) : children
    }

    /* CONTEXT */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const EventContext = React.createContext<EventApi>({
      value: null,
    })

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function EventProvider({ children, ...props }: Props) {
      return (
        <EventContext.Provider value={useEvent(props)}>
          {children}
        </EventContext.Provider>
      )
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const useEventContext = () => React.useContext(EventContext)