# All files / src/_hooks Anchor.tsx

98.18% Statements 54/55
65.21% Branches 15/23
100% Functions 7/7
97.72% Lines 43/44

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
    181
    182
    183
    184
    185
    186
    187
    188
    189
    1902x
    2x
    2x

    2x

    2x

    2x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x

    5x

    5x
    5x
    2x

    2x

    1x

    2x

    2x

    2x

    5x

    5x

    5x

    5x
    5x

    5x

    2x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    2x

    2x
    1x

    2x
    2x

     import React, { PropsWithChildren } from 'react'
    import { getBoundingRect } from '../_utils/dom'
    import { getAnchorPosition } from '../_utils/getAnchorPosition'
    import { Placement } from '../_utils/types'
    import { useLayoutEventListener } from './EventListener'

    /* TYPES */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface AnchorConfig {
      canFlip?: boolean
      dependencies?: Array<any>
      onLayout?: (anchor: AnchorApi) => any
      overlayRef: React.RefObject<HTMLElement>
      padding?: number
      placement?: Placement
      targetRef: React.RefObject<HTMLElement>
      shrinkOverlay?: boolean
      isFlyout?: boolean
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface AnchorProps extends PropsWithChildren<AnchorConfig> {}

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export interface AnchorApi {
      left: number
      /**
       * @deprecated
       *
       * use `shrinkOverlay` boolean property to set `minWidth`
       *
       * @deprecatedSince 10.20.0
       */
      minWidth: number
      placement: Placement
      top: number
    }

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const initAnchor: AnchorApi = {
      left: 0,
      minWidth: 0,
      placement: 'bottom-left',
      top: 0,
    }

    /* HOOK */
    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function useAnchor({
      canFlip = true,
      dependencies = [],
      onLayout = () => {},
      padding = 0,
      placement = 'bottom-left',
      overlayRef,
      targetRef,
      shrinkOverlay = false,
      isFlyout = false,
    }: AnchorConfig): AnchorApi {
      const [anchor, setAnchor] = React.useState<AnchorApi>({
        ...initAnchor,
        placement,
      })

      const viewChangeHandler = () => {
        if (overlayRef.current && targetRef.current) {
          const target = getBoundingRect(targetRef.current)

          if (!shrinkOverlay) {
            // we should set minWidth before calculation
            overlayRef.current.style.minWidth = `${target.width}px`
          }

          const newAnchor = getAnchorPosition(
            placement,
            padding,
            undefined,
            target,
            getBoundingRect(overlayRef.current),
            canFlip,
            isFlyout
          )
          Iif (newAnchor.placement !== anchor.placement) {
            setAnchor(newAnchor)
          }
          onLayout(newAnchor)
        }
      }

      useLayoutEventListener({
        event: 'scroll',
        handler: viewChangeHandler,
        options: {
          capture: true,
        },
      })

      useLayoutEventListener({
        event: 'resize',
        handler: viewChangeHandler,
      })

      useLayoutEventListener({
        event: 'load',
        handler: viewChangeHandler,
      })

      React.useLayoutEffect(() => {
        viewChangeHandler()
      }, [overlayRef, ...dependencies])

      return anchor
    }

    /* RENDER PROP */

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function Anchor({
      children,
      overlayRef,
      targetRef,
      ...props
    }: AnchorProps) {
      const anchor = useAnchor({
        ...props,
        overlayRef,
        targetRef,
      })

      const content =
        typeof children === 'function'
          ? (children as (props: AnchorApi) => React.ReactElement)(anchor)
          : (children as React.ReactElement)

      return (
        content &&
        React.cloneElement(content, {
          ...content.props,
          placement: anchor.placement,
          ref: overlayRef,
        })
      )
    }

    /* CONTEXT */
    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export const AnchorContext = React.createContext(initAnchor)

    /**
     * @deprecatedSince 9
     * @deprecated Never officially documented/supported
     */
    export function AnchorProvider({ children, ...props }: AnchorProps) {
      return (
        <AnchorContext.Provider value={useAnchor(props)}>
          {children}
        </AnchorContext.Provider>
      )
    }

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