import React, { useEffect, useRef } from 'react' import { useTranslations } from 'next-intl' import { getActiveStyleForButton, getHoverStyleForButton } from '@/styles/hover' import { DropDownArrowSvg } from '@/components/icons/DropDownArrowSvg' import { SlideSearchBox } from '@/components/ui/SlideSearchBox' import { CloseButton } from '@/components/ui/CloseButton' import { SearchResults } from '@/components/ui/SearchResults' import { useKeyPress } from '@/utils/hooks/useKeyPress' import { joinClassNames } from '@/utils/joinClassNames' import useCloseOnOutsideClick from '@/utils/hooks/useCloseOnOutsideClick' export const SelectorWrapper = ({ dataTestId, label, placeholder, selectedItem, children, searchStr, onClose, onSearch, open, setOpen, disabled, }) => { const escPressed = useKeyPress('Escape', open) const parentRef = useRef(null) const popoverRef = useRef(null) useEffect(() => { const ref = popoverRef?.current if (!ref) return const { y, height } = ref.getBoundingClientRect() const screen = window.innerHeight if (y + height * 0.67 > screen) { ref.style.position = 'fixed' ref.style.bottom = '4px' document.addEventListener('scroll', () => setOpen(false), { once: true, }) } if (ref.getBoundingClientRect().y < 0) { ref.style.position = 'fixed' ref.style.top = '4px' const search = ref.firstChild.firstChild const inner = search.nextSibling inner.style.height = `${screen - search.offsetHeight - 16}px` } }) const escFunc = () => { if (escPressed) { onClose() } } useEffect(escFunc, [escPressed]) useCloseOnOutsideClick(parentRef, () => onClose()) const t = useTranslations() const buttonClassNames = { flex: 'flex items-center gap-2', space: 'px-2 py-1.5 rounded', border: 'border border-zinc-200 dark:border-transparent', text: `leading-tight ${!label && 'text-lg'}`, open: `${getHoverStyleForButton(selectedItem?.color)} ${ open && getActiveStyleForButton(selectedItem?.color) }`, active: 'active:opacity-80', custom: label || open ? 'bg-transparent' : 'bg-white dark:bg-separator', disabled: `${disabled ? 'hover:cursor-not-allowed' : ''}`, } // TODO: Unify chainImg/icon properties between Chain and Token types const imgSrc = selectedItem?.['chainImg' in selectedItem ? 'chainImg' : 'icon']?.src const itemName = selectedItem?.['symbol' in selectedItem ? 'symbol' : 'name'] return (