2020-04-08 21:31:40 +09:00
|
|
|
import React, { useEffect, useRef } from "react";
|
2020-03-13 15:32:47 -04:00
|
|
|
import { Modal } from "./Modal";
|
|
|
|
import { Island } from "./Island";
|
|
|
|
import { t } from "../i18n";
|
|
|
|
import useIsMobile from "../is-mobile";
|
|
|
|
import { back, close } from "./icons";
|
2020-04-08 21:31:40 +09:00
|
|
|
import { KEYS } from "../keys";
|
2020-03-13 15:32:47 -04:00
|
|
|
|
|
|
|
import "./Dialog.scss";
|
|
|
|
|
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;
|
|
|
|
maxWidth?: number;
|
|
|
|
onCloseRequest(): void;
|
|
|
|
title: React.ReactNode;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-04-08 21:31:40 +09:00
|
|
|
const islandRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const focusableElements = queryFocusableElements();
|
|
|
|
|
|
|
|
if (focusableElements.length > 0) {
|
|
|
|
// If there's an element other than close, focus it.
|
|
|
|
(focusableElements[1] || focusableElements[0]).focus();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!islandRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const focusableElements = queryFocusableElements();
|
|
|
|
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
|
|
|
|
|
|
|
const node = islandRef.current;
|
|
|
|
node.addEventListener("keydown", handleKeyDown);
|
|
|
|
|
|
|
|
return () => node.removeEventListener("keydown", handleKeyDown);
|
|
|
|
}, []);
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const queryFocusableElements = () => {
|
2020-04-08 21:31:40 +09:00
|
|
|
const focusableElements = islandRef.current?.querySelectorAll<HTMLElement>(
|
|
|
|
"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
|
|
|
|
className={`${props.className ?? ""} Dialog`}
|
|
|
|
labelledBy="dialog-title"
|
|
|
|
maxWidth={props.maxWidth}
|
|
|
|
onCloseRequest={props.onCloseRequest}
|
|
|
|
>
|
2020-04-08 21:31:40 +09:00
|
|
|
<Island padding={4} ref={islandRef}>
|
2020-03-13 15:32:47 -04:00
|
|
|
<h2 id="dialog-title" className="Dialog__title">
|
|
|
|
<span className="Dialog__titleContent">{props.title}</span>
|
|
|
|
<button
|
|
|
|
className="Modal__close"
|
|
|
|
onClick={props.onCloseRequest}
|
|
|
|
aria-label={t("buttons.close")}
|
|
|
|
>
|
|
|
|
{useIsMobile() ? back : close}
|
|
|
|
</button>
|
|
|
|
</h2>
|
|
|
|
{props.children}
|
|
|
|
</Island>
|
|
|
|
</Modal>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|