| 1 |
/* eslint-disable @typescript-eslint/consistent-type-definitions */ |
| 2 |
import { useMemo, useRef, useState } from 'react'; |
| 3 |
import { createPopper as defaultCreatePopper, Options as PopperOptions, VirtualElement } from '../popper-core/popper'; |
| 4 |
import { useIsomorphicLayoutEffect } from '../../../../helpers/useIsomorphicLayout'; |
| 5 |
|
| 6 |
type $Shape<T extends object> = Partial<T>; |
| 7 |
|
| 8 |
const isEqual = (a: any, b: any) => JSON.stringify(a) === JSON.stringify(b); |
| 9 |
|
| 10 |
/** |
| 11 |
* Simple ponyfill for Object.fromEntries |
| 12 |
*/ |
| 13 |
const fromEntries = (entries: [string, any][]) => |
| 14 |
entries.reduce((acc: any, [key, value]) => { |
| 15 |
acc[key] = value; |
| 16 |
return acc; |
| 17 |
}, {}); |
| 18 |
|
| 19 |
type Options = $Shape< |
| 20 |
PopperOptions & { |
| 21 |
createPopper: typeof defaultCreatePopper; |
| 22 |
} |
| 23 |
>; |
| 24 |
|
| 25 |
type State = { |
| 26 |
styles: { |
| 27 |
[key: string]: $Shape<CSSStyleDeclaration>; |
| 28 |
}; |
| 29 |
attributes: { |
| 30 |
[key: string]: { |
| 31 |
[key: string]: string; |
| 32 |
}; |
| 33 |
}; |
| 34 |
}; |
| 35 |
|
| 36 |
const EMPTY_MODIFIERS: any = []; |
| 37 |
|
| 38 |
function isReferenceConnected(reference: Element | VirtualElement): boolean { |
| 39 |
if (reference instanceof Element) { |
| 40 |
return reference.isConnected; |
| 41 |
} |
| 42 |
const { contextElement } = reference; |
| 43 |
if (contextElement instanceof Element) { |
| 44 |
return contextElement.isConnected; |
| 45 |
} |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
export const usePopper = ( |
| 50 |
referenceElement: (Element | VirtualElement) | null | undefined, |
| 51 |
popperElement: HTMLElement | null | undefined, |
| 52 |
options: Options = {} |
| 53 |
) => { |
| 54 |
const prevOptions = useRef<PopperOptions | null | undefined>(null); |
| 55 |
|
| 56 |
const optionsWithDefaults = { |
| 57 |
onFirstUpdate: options.onFirstUpdate, |
| 58 |
placement: options.placement || 'bottom', |
| 59 |
strategy: options.strategy || 'absolute', |
| 60 |
modifiers: options.modifiers || EMPTY_MODIFIERS |
| 61 |
}; |
| 62 |
|
| 63 |
const [state, setState] = useState<State>({ |
| 64 |
styles: { |
| 65 |
popper: { |
| 66 |
position: optionsWithDefaults.strategy, |
| 67 |
left: '0', |
| 68 |
top: '0' |
| 69 |
} |
| 70 |
}, |
| 71 |
attributes: {} |
| 72 |
}); |
| 73 |
|
| 74 |
const updateStateModifier = useMemo( |
| 75 |
() => ({ |
| 76 |
name: 'updateState', |
| 77 |
enabled: true, |
| 78 |
phase: 'write', |
| 79 |
// eslint-disable-next-line no-shadow |
| 80 |
fn: ({ state }: any) => { |
| 81 |
const elements = Object.keys(state.elements); |
| 82 |
|
| 83 |
setState({ |
| 84 |
styles: fromEntries(elements.map(element => [element, state.styles[element] || {}])), |
| 85 |
attributes: fromEntries(elements.map(element => [element, state.attributes[element]])) |
| 86 |
}); |
| 87 |
}, |
| 88 |
requires: ['computeStyles'] |
| 89 |
}), |
| 90 |
[] |
| 91 |
); |
| 92 |
|
| 93 |
const popperOptions = useMemo(() => { |
| 94 |
const newOptions = { |
| 95 |
onFirstUpdate: optionsWithDefaults.onFirstUpdate, |
| 96 |
placement: optionsWithDefaults.placement, |
| 97 |
strategy: optionsWithDefaults.strategy, |
| 98 |
modifiers: [...optionsWithDefaults.modifiers, updateStateModifier, { name: 'applyStyles', enabled: false }] |
| 99 |
}; |
| 100 |
|
| 101 |
if (isEqual(prevOptions.current, newOptions)) { |
| 102 |
return prevOptions.current || newOptions; |
| 103 |
} else { |
| 104 |
prevOptions.current = newOptions; |
| 105 |
return newOptions; |
| 106 |
} |
| 107 |
}, [ |
| 108 |
optionsWithDefaults.onFirstUpdate, |
| 109 |
optionsWithDefaults.placement, |
| 110 |
optionsWithDefaults.strategy, |
| 111 |
optionsWithDefaults.modifiers, |
| 112 |
updateStateModifier |
| 113 |
]); |
| 114 |
|
| 115 |
const popperInstanceRef = useRef<any>(undefined); |
| 116 |
|
| 117 |
useIsomorphicLayoutEffect(() => { |
| 118 |
if (popperInstanceRef && popperInstanceRef.current) { |
| 119 |
popperInstanceRef.current.setOptions(popperOptions); |
| 120 |
} |
| 121 |
}, [popperOptions]); |
| 122 |
|
| 123 |
useIsomorphicLayoutEffect(() => { |
| 124 |
if (referenceElement == null || popperElement == null) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
if (!isReferenceConnected(referenceElement) || !popperElement.isConnected) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
const createPopper = options.createPopper || defaultCreatePopper; |
| 133 |
const popperInstance = createPopper(referenceElement, popperElement, popperOptions); |
| 134 |
|
| 135 |
popperInstanceRef.current = popperInstance; |
| 136 |
|
| 137 |
return () => { |
| 138 |
popperInstance.destroy(); |
| 139 |
popperInstanceRef.current = null; |
| 140 |
}; |
| 141 |
}, [referenceElement, popperElement, options.createPopper]); |
| 142 |
|
| 143 |
return { |
| 144 |
state: popperInstanceRef.current ? popperInstanceRef.current.state : null, |
| 145 |
styles: state.styles, |
| 146 |
attributes: state.attributes, |
| 147 |
update: popperInstanceRef.current ? popperInstanceRef.current.update : null, |
| 148 |
forceUpdate: popperInstanceRef.current ? popperInstanceRef.current.forceUpdate : null |
| 149 |
}; |
| 150 |
}; |