Files
element-call/src/Modal.tsx

154 lines
4.7 KiB
TypeScript
Raw Normal View History

/*
Copyright 2023 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.
*/
import { FC, ReactNode, useCallback } from "react";
2023-02-02 14:32:44 +00:00
import { AriaDialogProps } from "@react-types/dialog";
2022-10-10 09:19:10 -04:00
import { useTranslation } from "react-i18next";
import {
Root as DialogRoot,
Portal as DialogPortal,
Overlay as DialogOverlay,
Content as DialogContent,
Title as DialogTitle,
Close as DialogClose,
} from "@radix-ui/react-dialog";
import { Drawer } from "vaul";
import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
2023-09-27 19:06:10 -04:00
import CloseIcon from "@vector-im/compound-design-tokens/icons/close.svg?react";
import classNames from "classnames";
import { Heading, Glass } from "@vector-im/compound-web";
2023-09-18 11:56:24 -04:00
import styles from "./Modal.module.css";
2023-09-27 17:34:41 -04:00
import overlayStyles from "./Overlay.module.css";
import { useMediaQuery } from "./useMediaQuery";
2021-12-03 11:45:29 -08:00
// TODO: Support tabs
export interface Props extends AriaDialogProps {
title: string;
children: ReactNode;
className?: string;
/**
* The controlled open state of the modal.
*/
// An option to leave the open state uncontrolled is intentionally not
// provided, since modals are always opened due to external triggers, and it
2023-09-18 11:40:10 -04:00
// is the author's belief that controlled components lead to more obvious code.
open: boolean;
/**
* Callback for when the user dismisses the modal. If undefined, the modal
* will be non-dismissable.
*/
onDismiss?: () => void;
}
/**
* A modal, taking the form of a drawer / bottom sheet on touchscreen devices,
* and a dialog box on desktop.
*/
export const Modal: FC<Props> = ({
title,
children,
className,
open,
onDismiss,
...rest
}) => {
2022-10-10 09:19:10 -04:00
const { t } = useTranslation();
2023-09-19 07:04:43 -04:00
// Empirically, Chrome on Android can end up not matching (hover: none), but
// still matching (pointer: coarse) :/
const touchscreen = useMediaQuery("(hover: none) or (pointer: coarse)");
const onOpenChange = useCallback(
(open: boolean) => {
if (!open) onDismiss?.();
},
2023-10-11 10:42:04 -04:00
[onDismiss],
);
2021-12-03 11:45:29 -08:00
if (touchscreen) {
return (
<Drawer.Root
open={open}
onOpenChange={onOpenChange}
dismissible={onDismiss !== undefined}
>
<Drawer.Portal>
2023-09-27 17:34:41 -04:00
<Drawer.Overlay className={classNames(overlayStyles.bg)} />
<Drawer.Content
2023-09-27 17:34:41 -04:00
className={classNames(
className,
overlayStyles.overlay,
styles.modal,
2023-10-11 10:42:04 -04:00
styles.drawer,
2023-09-27 17:34:41 -04:00
)}
{...rest}
2021-12-03 11:45:29 -08:00
>
<div className={styles.content}>
<div className={styles.header}>
<div className={styles.handle} />
<VisuallyHidden asChild>
<Drawer.Title>{title}</Drawer.Title>
</VisuallyHidden>
</div>
<div className={styles.body}>{children}</div>
2021-12-03 11:45:29 -08:00
</div>
</Drawer.Content>
</Drawer.Portal>
</Drawer.Root>
);
} else {
return (
<DialogRoot open={open} onOpenChange={onOpenChange}>
<DialogPortal>
<DialogOverlay
2023-09-27 17:34:41 -04:00
className={classNames(overlayStyles.bg, overlayStyles.animate)}
/>
<DialogContent asChild {...rest}>
<Glass
2023-09-27 17:34:41 -04:00
className={classNames(
className,
overlayStyles.overlay,
overlayStyles.animate,
styles.modal,
2023-10-11 10:42:04 -04:00
styles.dialog,
2023-09-27 17:34:41 -04:00
)}
>
<div className={styles.content}>
<div className={styles.header}>
<DialogTitle asChild>
<Heading as="h2" weight="semibold" size="md">
{title}
</Heading>
</DialogTitle>
{onDismiss !== undefined && (
<DialogClose
className={styles.close}
data-testid="modal_close"
aria-label={t("action.close")}
>
<CloseIcon width={20} height={20} />
</DialogClose>
)}
</div>
<div className={styles.body}>{children}</div>
</div>
</Glass>
</DialogContent>
</DialogPortal>
</DialogRoot>
);
}
};