/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import { useEffect, useRef, useState } from 'react'; import * as React from 'react'; import { createPortal } from 'react-dom'; export default function DropDown({ buttonLabel, buttonAriaLabel, buttonClassName, buttonIconClassName, children, stopCloseOnClickSelf, }: { buttonAriaLabel?: string; buttonClassName: string; buttonIconClassName?: string; buttonLabel?: string; children: JSX.Element | string | (JSX.Element | string)[]; stopCloseOnClickSelf?: boolean; }): JSX.Element { const dropDownRef = useRef(null); const buttonRef = useRef(null); const [showDropDown, setShowDropDown] = useState(false); useEffect(() => { const button = buttonRef.current; const dropDown = dropDownRef.current; if (showDropDown && button !== null && dropDown !== null) { const { top, left } = button.getBoundingClientRect(); dropDown.style.top = `${top + 40}px`; dropDown.style.left = `${Math.min( left, window.innerWidth - dropDown.offsetWidth - 20 )}px`; if (dropDown.offsetHeight + top > window.innerHeight) { dropDown.style.top = `${top - dropDown.offsetHeight - 10}px`; } } }, [dropDownRef, buttonRef, showDropDown]); useEffect(() => { const button = buttonRef.current; if (button !== null && showDropDown) { const handle = (event: MouseEvent) => { const target = event.target; if (stopCloseOnClickSelf) { if (dropDownRef.current.contains(target as Node)) return; } if (!button.contains(target as Node)) { setShowDropDown(false); } }; document.addEventListener('click', handle); return () => { document.removeEventListener('click', handle); }; } }, [dropDownRef, buttonRef, showDropDown, stopCloseOnClickSelf]); return ( <> {showDropDown && createPortal(
{children}
, document.body )} ); }