2020-10-19 17:14:28 +03:00
|
|
|
import clsx from "clsx";
|
2020-12-27 18:26:30 +02:00
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
2020-03-13 15:32:47 -04:00
|
|
|
import { t } from "../i18n";
|
|
|
|
import useIsMobile from "../is-mobile";
|
2020-04-08 21:31:40 +09:00
|
|
|
import { KEYS } from "../keys";
|
2020-03-13 15:32:47 -04:00
|
|
|
import "./Dialog.scss";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { back, close } from "./icons";
|
|
|
|
import { Island } from "./Island";
|
|
|
|
import { Modal } from "./Modal";
|
2020-03-13 15:32:47 -04:00
|
|
|
|
2020-11-05 21:05:58 +01:00
|
|
|
const useRefState = <T,>() => {
|
|
|
|
const [refValue, setRefValue] = useState<T | null>(null);
|
|
|
|
const refCallback = useCallback((value: T) => {
|
|
|
|
setRefValue(value);
|
|
|
|
}, []);
|
|
|
|
return [refValue, refCallback] as const;
|
|
|
|
};
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const Dialog = (props: {
|
2020-03-13 15:32:47 -04:00
|
|
|
children: React.ReactNode;
|
|
|
|
className?: string;
|
2020-12-27 17:07:05 -05:00
|
|
|
small?: boolean;
|
2020-03-13 15:32:47 -04:00
|
|
|
onCloseRequest(): void;
|
|
|
|
title: React.ReactNode;
|
2020-12-27 18:26:30 +02:00
|
|
|
autofocus?: boolean;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-11-05 21:05:58 +01:00
|
|
|
const [islandNode, setIslandNode] = useRefState<HTMLDivElement>();
|
2020-04-08 21:31:40 +09:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-11-05 21:05:58 +01:00
|
|
|
if (!islandNode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const focusableElements = queryFocusableElements(islandNode);
|
2020-04-08 21:31:40 +09:00
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
if (focusableElements.length > 0 && props.autofocus !== false) {
|
2020-04-08 21:31:40 +09:00
|
|
|
// If there's an element other than close, focus it.
|
|
|
|
(focusableElements[1] || focusableElements[0]).focus();
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
2020-04-08 21:31:40 +09:00
|
|
|
if (event.key === KEYS.TAB) {
|
2020-11-05 21:05:58 +01:00
|
|
|
const focusableElements = queryFocusableElements(islandNode);
|
2020-04-08 21:31:40 +09:00
|
|
|
const { activeElement } = document;
|
|
|
|
const currentIndex = focusableElements.findIndex(
|
|
|
|
(element) => element === activeElement,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (currentIndex === 0 && event.shiftKey) {
|
|
|
|
focusableElements[focusableElements.length - 1].focus();
|
|
|
|
event.preventDefault();
|
|
|
|
} else if (
|
|
|
|
currentIndex === focusableElements.length - 1 &&
|
|
|
|
!event.shiftKey
|
|
|
|
) {
|
|
|
|
focusableElements[0].focus();
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-08 21:31:40 +09:00
|
|
|
|
2020-11-05 21:05:58 +01:00
|
|
|
islandNode.addEventListener("keydown", handleKeyDown);
|
2020-04-08 21:31:40 +09:00
|
|
|
|
2020-11-05 21:05:58 +01:00
|
|
|
return () => islandNode.removeEventListener("keydown", handleKeyDown);
|
2020-12-27 18:26:30 +02:00
|
|
|
}, [islandNode, props.autofocus]);
|
2020-04-08 21:31:40 +09:00
|
|
|
|
2020-11-05 21:05:58 +01:00
|
|
|
const queryFocusableElements = (node: HTMLElement) => {
|
|
|
|
const focusableElements = node.querySelectorAll<HTMLElement>(
|
2020-04-08 21:31:40 +09:00
|
|
|
"button, a, input, select, textarea, div[tabindex]",
|
|
|
|
);
|
|
|
|
|
|
|
|
return focusableElements ? Array.from(focusableElements) : [];
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-04-08 21:31:40 +09:00
|
|
|
|
2020-03-13 15:32:47 -04:00
|
|
|
return (
|
|
|
|
<Modal
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("Dialog", props.className)}
|
2020-03-13 15:32:47 -04:00
|
|
|
labelledBy="dialog-title"
|
2020-12-27 17:07:05 -05:00
|
|
|
maxWidth={props.small ? 550 : 800}
|
2020-03-13 15:32:47 -04:00
|
|
|
onCloseRequest={props.onCloseRequest}
|
|
|
|
>
|
2021-01-03 11:50:41 +02:00
|
|
|
<Island ref={setIslandNode}>
|
2021-01-17 17:46:23 +01:00
|
|
|
<h2 id="dialog-title" className="Dialog__title">
|
2020-03-13 15:32:47 -04:00
|
|
|
<span className="Dialog__titleContent">{props.title}</span>
|
|
|
|
<button
|
|
|
|
className="Modal__close"
|
|
|
|
onClick={props.onCloseRequest}
|
|
|
|
aria-label={t("buttons.close")}
|
|
|
|
>
|
|
|
|
{useIsMobile() ? back : close}
|
|
|
|
</button>
|
2021-01-17 17:46:23 +01:00
|
|
|
</h2>
|
2021-01-03 11:50:41 +02:00
|
|
|
<div className="Dialog__content">{props.children}</div>
|
2020-03-13 15:32:47 -04:00
|
|
|
</Island>
|
|
|
|
</Modal>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|