import { useCallback, useRef } from 'react' import { createPopper } from '@popperjs/core' import type { Instance, Options } from '@popperjs/core' interface UsePopperOutput { popper: Instance | undefined initPopper: (reference: HTMLElement, popper: HTMLElement, options: Partial) => void destroyPopper: () => void updatePopper: (options?: Partial) => void } export const usePopper = (): UsePopperOutput => { const _popper = useRef(undefined) const el = useRef(null) const initPopper = useCallback( (reference: HTMLElement, popper: HTMLElement, options: Partial) => { _popper.current?.destroy() _popper.current = createPopper(reference, popper, options) el.current = popper }, [] ) const destroyPopper = useCallback(() => { const popperInstance = _popper.current if (popperInstance && el.current) { popperInstance.destroy() } _popper.current = undefined }, []) const updatePopper = useCallback((options?: Partial) => { const popperInstance = _popper.current if (popperInstance && options) { popperInstance.setOptions(options) } if (popperInstance) { popperInstance.update() } }, []) return { popper: _popper.current, initPopper, destroyPopper, updatePopper, } }