small improvement

This commit is contained in:
daniel31x13
2024-04-20 10:49:06 -04:00
parent 8278878673
commit b702aa0401
2 changed files with 26 additions and 6 deletions
+25 -6
View File
@@ -8,19 +8,38 @@ type Props = {
onMount?: (rect: DOMRect) => void; onMount?: (rect: DOMRect) => void;
}; };
function getZIndex(element: HTMLElement): number {
let zIndex = 0;
while (element) {
const zIndexStyle = window
.getComputedStyle(element)
.getPropertyValue("z-index");
const numericZIndex = Number(zIndexStyle);
if (zIndexStyle !== "auto" && !isNaN(numericZIndex)) {
zIndex = numericZIndex;
break;
}
element = element.parentElement as HTMLElement;
}
return zIndex;
}
function useOutsideAlerter( function useOutsideAlerter(
ref: RefObject<HTMLElement>, ref: RefObject<HTMLElement>,
onClickOutside: Function onClickOutside: Function
) { ) {
useEffect(() => { useEffect(() => {
function handleClickOutside(event: Event) { function handleClickOutside(event: MouseEvent) {
if ( const clickedElement = event.target as HTMLElement;
ref.current && if (ref.current && !ref.current.contains(clickedElement)) {
!ref.current.contains(event.target as HTMLInputElement) const refZIndex = getZIndex(ref.current);
) { const clickedZIndex = getZIndex(clickedElement);
onClickOutside(event); if (clickedZIndex <= refZIndex) {
onClickOutside(event);
}
} }
} }
document.addEventListener("mousedown", handleClickOutside); document.addEventListener("mousedown", handleClickOutside);
return () => { return () => {
document.removeEventListener("mousedown", handleClickOutside); document.removeEventListener("mousedown", handleClickOutside);
+1
View File
@@ -75,6 +75,7 @@ export default function App({
<span data-testid="toast-message">{message}</span> <span data-testid="toast-message">{message}</span>
{t.type !== "loading" && ( {t.type !== "loading" && (
<button <button
className="btn btn-xs outline-none btn-circle btn-ghost"
data-testid="close-toast-button" data-testid="close-toast-button"
onClick={() => toast.dismiss(t.id)} onClick={() => toast.dismiss(t.id)}
> >