mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
* build: enable consistent type imports eslint rule * change to warn * fix the warning in example and excalidraw-app * fix packages * enable type annotations and throw error for the rule
63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import type { CSSProperties } from "react";
|
|
import { useCallback, useEffect, useRef } from "react";
|
|
import { CloseIcon } from "./icons";
|
|
import "./Toast.scss";
|
|
import { ToolButton } from "./ToolButton";
|
|
|
|
const DEFAULT_TOAST_TIMEOUT = 5000;
|
|
|
|
export const Toast = ({
|
|
message,
|
|
onClose,
|
|
closable = false,
|
|
// To prevent autoclose, pass duration as Infinity
|
|
duration = DEFAULT_TOAST_TIMEOUT,
|
|
style,
|
|
}: {
|
|
message: string;
|
|
onClose: () => void;
|
|
closable?: boolean;
|
|
duration?: number;
|
|
style?: CSSProperties;
|
|
}) => {
|
|
const timerRef = useRef<number>(0);
|
|
const shouldAutoClose = duration !== Infinity;
|
|
const scheduleTimeout = useCallback(() => {
|
|
if (!shouldAutoClose) {
|
|
return;
|
|
}
|
|
timerRef.current = window.setTimeout(() => onClose(), duration);
|
|
}, [onClose, duration, shouldAutoClose]);
|
|
|
|
useEffect(() => {
|
|
if (!shouldAutoClose) {
|
|
return;
|
|
}
|
|
scheduleTimeout();
|
|
return () => clearTimeout(timerRef.current);
|
|
}, [scheduleTimeout, message, duration, shouldAutoClose]);
|
|
|
|
const onMouseEnter = shouldAutoClose
|
|
? () => clearTimeout(timerRef?.current)
|
|
: undefined;
|
|
const onMouseLeave = shouldAutoClose ? scheduleTimeout : undefined;
|
|
return (
|
|
<div
|
|
className="Toast"
|
|
onMouseEnter={onMouseEnter}
|
|
onMouseLeave={onMouseLeave}
|
|
style={style}
|
|
>
|
|
<p className="Toast__message">{message}</p>
|
|
{closable && (
|
|
<ToolButton
|
|
icon={CloseIcon}
|
|
aria-label="close"
|
|
type="icon"
|
|
onClick={onClose}
|
|
className="close"
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|