diff --git a/components/Dropdown.tsx b/components/Dropdown.tsx index aa2aaa4..9588551 100644 --- a/components/Dropdown.tsx +++ b/components/Dropdown.tsx @@ -20,12 +20,15 @@ export default function Dropdown({ onToggle, }: DropdownProps) { const dropdownRef = useRef(null); + const buttonRef = useRef(null); const [isOpen, setIsOpen] = useState(controlledIsOpen ?? false); + const [dropdownPosition, setDropdownPosition] = useState({ top: 0, left: 0, width: 0 }); useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (!dropdownRef.current || !(event.target instanceof Node)) return; - if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { + if (dropdownRef.current && !dropdownRef.current.contains(event.target) && + buttonRef.current && !buttonRef.current.contains(event.target)) { if (onToggle) onToggle(false); else setIsOpen(false); } @@ -40,6 +43,17 @@ export default function Dropdown({ }; }, [controlledIsOpen, isOpen, onToggle]); + useEffect(() => { + if ((controlledIsOpen ?? isOpen) && buttonRef.current) { + const rect = buttonRef.current.getBoundingClientRect(); + setDropdownPosition({ + top: rect.bottom + window.scrollY, + left: rect.left + window.scrollX, + width: rect.width + }); + } + }, [controlledIsOpen, isOpen]); + const activeOption = options.find((option) => option.id === activeId) || null; const handleSelect = (option: { id: number }) => { @@ -54,33 +68,44 @@ export default function Dropdown({ }; return ( -
- + + {activeOption ? activeOption.name : label} + + + + + +
{(controlledIsOpen ?? isOpen) && ( -
+
{options.length === 0 ? (
- No streams available + No teams available
) : ( options.map((option) => ( @@ -95,6 +120,6 @@ export default function Dropdown({ )}
)} -
+ ); } \ No newline at end of file