element-call-Github/src/Modal.tsx

202 lines
4.9 KiB
TypeScript
Raw Normal View History

/*
Copyright 2022 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* eslint-disable jsx-a11y/no-autofocus */
import React, { useRef, useMemo, ReactNode } from "react";
2021-12-04 03:45:29 +08:00
import {
useOverlay,
usePreventScroll,
useModal,
OverlayContainer,
OverlayProps,
2021-12-04 03:45:29 +08:00
} from "@react-aria/overlays";
import {
OverlayTriggerState,
useOverlayTriggerState,
} from "@react-stately/overlays";
2021-12-04 03:45:29 +08:00
import { useDialog } from "@react-aria/dialog";
import { FocusScope } from "@react-aria/focus";
import { ButtonAria, useButton } from "@react-aria/button";
import classNames from "classnames";
import { AriaDialogProps } from "@react-types/dialog";
2021-12-04 03:45:29 +08:00
import { ReactComponent as CloseIcon } from "./icons/Close.svg";
import styles from "./Modal.module.css";
2022-08-06 04:16:59 +08:00
export interface ModalProps extends OverlayProps, AriaDialogProps {
title: string;
children: ReactNode;
className?: string;
mobileFullScreen?: boolean;
onClose?: () => void;
}
export function Modal({
title,
children,
className,
mobileFullScreen,
onClose,
...rest
}: ModalProps) {
2021-12-04 03:45:29 +08:00
const modalRef = useRef();
const { overlayProps, underlayProps } = useOverlay(
{ ...rest, onClose },
modalRef
);
2021-12-04 03:45:29 +08:00
usePreventScroll();
const { modalProps } = useModal();
const { dialogProps, titleProps } = useDialog(rest, modalRef);
2021-12-04 03:45:29 +08:00
const closeButtonRef = useRef();
const { buttonProps: closeButtonProps } = useButton(
{
onPress: () => onClose(),
},
closeButtonRef
);
2021-12-04 03:45:29 +08:00
return (
<OverlayContainer>
<div className={styles.modalOverlay} {...underlayProps}>
<FocusScope contain restoreFocus autoFocus>
<div
{...overlayProps}
{...dialogProps}
{...modalProps}
ref={modalRef}
2021-12-11 02:54:18 +08:00
className={classNames(
styles.modal,
{ [styles.mobileFullScreen]: mobileFullScreen },
className
)}
2021-12-04 03:45:29 +08:00
>
<div className={styles.modalHeader}>
<h3 {...titleProps}>{title}</h3>
<button
{...closeButtonProps}
ref={closeButtonRef}
className={styles.closeButton}
>
<CloseIcon />
</button>
</div>
{children}
</div>
</FocusScope>
</div>
</OverlayContainer>
);
}
interface ModalContentProps {
children: ReactNode;
className?: string;
}
export function ModalContent({
children,
className,
...rest
}: ModalContentProps) {
2021-12-04 03:45:29 +08:00
return (
<div className={classNames(styles.content, className)} {...rest}>
{children}
</div>
);
}
2021-12-07 09:34:10 +08:00
export function useModalTriggerState(): {
modalState: OverlayTriggerState;
modalProps: { isOpen: boolean; onClose: () => void };
} {
2021-12-07 09:34:10 +08:00
const modalState = useOverlayTriggerState({});
const modalProps = useMemo(
() => ({ isOpen: modalState.isOpen, onClose: modalState.close }),
[modalState]
);
return { modalState, modalProps };
}
export function useToggleModalButton(
modalState: OverlayTriggerState,
ref: React.RefObject<HTMLButtonElement>
): ButtonAria<React.ButtonHTMLAttributes<HTMLButtonElement>> {
2021-12-07 09:34:10 +08:00
return useButton(
{
onPress: () => modalState.toggle(),
},
ref
);
}
export function useOpenModalButton(
modalState: OverlayTriggerState,
ref: React.RefObject<HTMLButtonElement>
): ButtonAria<React.ButtonHTMLAttributes<HTMLButtonElement>> {
2021-12-07 09:34:10 +08:00
return useButton(
{
onPress: () => modalState.open(),
},
ref
);
}
export function useCloseModalButton(
modalState: OverlayTriggerState,
ref: React.RefObject<HTMLButtonElement>
): ButtonAria<React.ButtonHTMLAttributes<HTMLButtonElement>> {
2021-12-07 09:34:10 +08:00
return useButton(
{
onPress: () => modalState.close(),
},
ref
);
}
interface ModalTriggerProps {
children: ReactNode;
}
export function ModalTrigger({ children }: ModalTriggerProps) {
const { modalState, modalProps } = useModalTriggerState();
2021-12-07 09:34:10 +08:00
const buttonRef = useRef();
const { buttonProps } = useToggleModalButton(modalState, buttonRef);
if (
!Array.isArray(children) ||
children.length > 2 ||
typeof children[1] !== "function"
) {
throw new Error(
"ModalTrigger must have two props. The first being a button and the second being a render prop."
);
}
const [modalTrigger, modal] = children;
return (
<>
<modalTrigger.type
{...modalTrigger.props}
{...buttonProps}
ref={buttonRef}
/>
{modalState.isOpen && modal(modalProps)}
</>
);
}