2023-09-27 17:34:41 -04:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-09-27 17:34:41 -04:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-09-27 17:34:41 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
2024-12-11 09:27:55 +00:00
|
|
|
type ComponentType,
|
|
|
|
|
type FC,
|
|
|
|
|
type SVGAttributes,
|
2023-09-27 17:34:41 -04:00
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
} from "react";
|
|
|
|
|
import {
|
|
|
|
|
Root as DialogRoot,
|
|
|
|
|
Portal as DialogPortal,
|
|
|
|
|
Overlay as DialogOverlay,
|
|
|
|
|
Content as DialogContent,
|
|
|
|
|
Close as DialogClose,
|
|
|
|
|
Title as DialogTitle,
|
|
|
|
|
} from "@radix-ui/react-dialog";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import { Text } from "@vector-im/compound-web";
|
|
|
|
|
|
|
|
|
|
import styles from "./Toast.module.css";
|
|
|
|
|
import overlayStyles from "./Overlay.module.css";
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
/**
|
|
|
|
|
* The controlled open state of the toast.
|
|
|
|
|
*/
|
|
|
|
|
open: boolean;
|
|
|
|
|
/**
|
|
|
|
|
* Callback for when the user dismisses the toast.
|
|
|
|
|
*/
|
|
|
|
|
onDismiss: () => void;
|
|
|
|
|
/**
|
|
|
|
|
* A number of milliseconds after which the toast should be automatically
|
|
|
|
|
* dismissed.
|
|
|
|
|
*/
|
|
|
|
|
autoDismiss?: number;
|
|
|
|
|
children: string;
|
|
|
|
|
/**
|
|
|
|
|
* A supporting icon to display within the toast.
|
|
|
|
|
*/
|
|
|
|
|
Icon?: ComponentType<SVGAttributes<SVGElement>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A temporary message shown in an overlay in the center of the screen.
|
|
|
|
|
*/
|
|
|
|
|
export const Toast: FC<Props> = ({
|
|
|
|
|
open,
|
|
|
|
|
onDismiss,
|
|
|
|
|
autoDismiss,
|
|
|
|
|
children,
|
|
|
|
|
Icon,
|
|
|
|
|
}) => {
|
|
|
|
|
const onOpenChange = useCallback(
|
|
|
|
|
(open: boolean) => {
|
|
|
|
|
if (!open) onDismiss();
|
|
|
|
|
},
|
2023-10-11 10:42:04 -04:00
|
|
|
[onDismiss],
|
2023-09-27 17:34:41 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (open && autoDismiss !== undefined) {
|
|
|
|
|
const timeout = setTimeout(onDismiss, autoDismiss);
|
2024-06-04 11:20:25 -04:00
|
|
|
return (): void => clearTimeout(timeout);
|
2023-09-27 17:34:41 -04:00
|
|
|
}
|
|
|
|
|
}, [open, autoDismiss, onDismiss]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DialogRoot open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
<DialogPortal>
|
|
|
|
|
<DialogOverlay
|
|
|
|
|
className={classNames(overlayStyles.bg, overlayStyles.animate)}
|
|
|
|
|
/>
|
2024-08-27 09:45:39 -04:00
|
|
|
<DialogContent aria-describedby={undefined} asChild>
|
2023-09-27 17:34:41 -04:00
|
|
|
<DialogClose
|
|
|
|
|
className={classNames(
|
|
|
|
|
overlayStyles.overlay,
|
|
|
|
|
overlayStyles.animate,
|
2023-10-11 10:42:04 -04:00
|
|
|
styles.toast,
|
2023-09-27 17:34:41 -04:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<DialogTitle asChild>
|
|
|
|
|
<Text as="h3" size="sm" weight="semibold">
|
|
|
|
|
{children}
|
|
|
|
|
</Text>
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
{Icon && <Icon width={20} height={20} aria-hidden />}
|
|
|
|
|
</DialogClose>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</DialogPortal>
|
|
|
|
|
</DialogRoot>
|
|
|
|
|
);
|
|
|
|
|
};
|