# All files / src/_hooks ListNavigation.tsx

97.82% Statements 45/46
86.66% Branches 13/15
100% Functions 15/15
96.96% Lines 32/33

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
    17519x

    21x

    9x

    9x

    19x
    157x
    157x
    157x

    157x

    157x

    157x

    31x
    10x

    21x

    1x

    8x

    3x

    4x

    2x

    3x

    157x

    19x
    2x

    2x

    152x
    5x

    19x

    19x

    19x
    2x

    19x
    3x
     import React from 'react'

    /* TYPES */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface ListNavigationConfig {
      circular?: boolean
      initialIndex?: number
      size?: number
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface ListNavigationApi {
      clear: () => void
      decrement: () => void
      decrementBy: (amount: number) => void
      increment: () => void
      incrementBy: (amount: number) => void
      index: number
      reset: () => void
      set: (index: number) => void
      setSize: (index: number) => void
      size: number
    }

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

    interface RenderProps extends ListNavigationConfig {
      children: (props: ListNavigationApi) => {}
    }

    function clamp(max: number, n: number) {
      return Math.min(max - 1, Math.max(0, n))
    }

    function wrap(max: number, n: number) {
      Iif (max === 0) {
        return n
      }

      return n < 0 ? max + n : n % max
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useListNavigation({
      circular = false,
      initialIndex = 0,
      size: initialSize = 0,
    }: ListNavigationConfig): ListNavigationApi {
      const [index, setIndex] = React.useState(initialIndex)

      const [size, setSize] = React.useState(initialSize)

      const set = React.useCallback(
        function (i: number) {
          if (size === 0) {
            setIndex(i)
          } else {
            setIndex(clamp(size, circular ? wrap(size, i) : i))
          }
        },
        [circular, size]
      )

      function clear() {
        set(0)
      }

      function increment() {
        set(index + 1)
      }

      function incrementBy(amount: number) {
        set(index + amount)
      }

      function decrement() {
        set(index - 1)
      }

      function decrementBy(amount: number) {
        set(index - amount)
      }

      function reset() {
        set(initialIndex)
      }

      return {
        clear,
        decrement,
        decrementBy,
        increment,
        incrementBy,
        index,
        reset,
        set,
        setSize,
        size,
      }
    }

    /* RENDER PROP */

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

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

    /* CONTEXT */

    const noop = (method: string) => () =>
      console.warn(
        `Could not call \`${method}\`, please wrap your component in a \`<ListNavigationProvider>\``
      )

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const defaultApi = {
      clear: noop('clear'),
      decrement: noop('decrement'),
      decrementBy: noop('decrementBy'),
      increment: noop('increment'),
      incrementBy: noop('incrementBy'),
      index: -1,
      reset: noop('reset'),
      set: noop('set'),
      setSize: noop('setSize'),
      size: 0,
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const ListNavigationContext =
      React.createContext<ListNavigationApi>(defaultApi)

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

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