swarm-ui: a toast waits while the pointer is on it
A toast left after four seconds (eight for an error) whether it had been read or not. The countdown stops while the pointer rests on it and runs on for at least 1.5 seconds after the pointer leaves.
This commit is contained in:
parent
868b8743d8
commit
963ec65bbc
2 changed files with 56 additions and 5 deletions
|
|
@ -1,26 +1,48 @@
|
|||
// <ToastHost> — renders the toasts `showToast` raised, stacked top-right
|
||||
// above the page. Mounted once, at the app root. A toast removes itself after
|
||||
// its duration; a click dismisses it sooner. An error is announced as an
|
||||
// alert, the others as a status.
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
// its duration, unless the pointer is over it (the countdown stops while it
|
||||
// rests there, and runs on at least a moment after the pointer leaves); a click
|
||||
// dismisses it sooner. An error is announced as an alert, the others as a status.
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import {
|
||||
currentToasts,
|
||||
dismissToast,
|
||||
resumeDelay,
|
||||
subscribeToasts,
|
||||
timeLeft,
|
||||
type Toast,
|
||||
} from "./toasts.js";
|
||||
import "./Toast.css";
|
||||
|
||||
function ToastItem({ toast }: { toast: Toast }) {
|
||||
// What is left to run, when this run of the timer began, and the timer.
|
||||
const left = useRef(toast.duration);
|
||||
const since = useRef(0);
|
||||
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
||||
function run(ms: number) {
|
||||
since.current = Date.now();
|
||||
timer.current = setTimeout(() => dismissToast(toast.id), ms);
|
||||
}
|
||||
function pause() {
|
||||
if (timer.current === undefined) return;
|
||||
clearTimeout(timer.current);
|
||||
timer.current = undefined;
|
||||
left.current = timeLeft(left.current, Date.now() - since.current);
|
||||
}
|
||||
function resume() {
|
||||
if (timer.current === undefined) run(resumeDelay(left.current));
|
||||
}
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => dismissToast(toast.id), toast.duration);
|
||||
return () => clearTimeout(timer);
|
||||
run(toast.duration);
|
||||
return () => clearTimeout(timer.current);
|
||||
}, [toast.id, toast.duration]);
|
||||
return (
|
||||
<div
|
||||
class={`ui-toast ui-toast-${toast.tone}`}
|
||||
role={toast.tone === "error" ? "alert" : "status"}
|
||||
onClick={() => dismissToast(toast.id)}
|
||||
onMouseEnter={pause}
|
||||
onMouseLeave={resume}
|
||||
>
|
||||
{toast.message}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -20,6 +20,18 @@ export function toastDuration(tone: ToastTone): number {
|
|||
return tone === "error" ? 8000 : 4000;
|
||||
}
|
||||
|
||||
/** What is left of a toast's time after it has been up `shownMs`. */
|
||||
export function timeLeft(duration: number, shownMs: number): number {
|
||||
return Math.max(0, duration - shownMs);
|
||||
}
|
||||
|
||||
/** How long a toast the pointer left has to go: its time left, but never less
|
||||
* than a moment, so one let go at the last second can still be read. */
|
||||
export const RESUME_FLOOR_MS = 1500;
|
||||
export function resumeDelay(left: number): number {
|
||||
return Math.max(RESUME_FLOOR_MS, left);
|
||||
}
|
||||
|
||||
let toasts: Toast[] = [];
|
||||
let nextId = 1;
|
||||
const listeners = new Set<() => void>();
|
||||
|
|
|
|||
Loading…
Reference in a new issue