66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
// The confirmation that drops in at the top of the screen after a save.
|
|
//
|
|
// Two rules it exists to enforce:
|
|
// 1. Success is never optimistic. The toast is driven by the server's answer
|
|
// (`ok: true`), not by the click, so "Gespeichert" cannot appear over a
|
|
// write that failed.
|
|
// 2. An error says what went wrong. The message comes from the action's
|
|
// i18n key, never from a raw provider string or an HTTP code (A.1).
|
|
//
|
|
// Pure/presentational: it renders the copy it is handed. Firing it is the
|
|
// `useSaveToast` hook's job.
|
|
|
|
import { FeatherAlertCircle, FeatherCheckCircle } from "@subframe/core";
|
|
|
|
export type SaveToastVariant = "success" | "error";
|
|
|
|
export type SaveToastProps = {
|
|
variant: SaveToastVariant;
|
|
/** Resolved headline, e.g. "Gespeichert". */
|
|
title: string;
|
|
/** Resolved detail. On an error this is what went wrong and what to do. */
|
|
description?: string | null;
|
|
};
|
|
|
|
const VARIANT = {
|
|
success: {
|
|
frame: "border-border-positive-subtle bg-surface-feedback-subtle-positive",
|
|
icon: "text-icon-feedback-positive",
|
|
title: "text-text-feedback-positive",
|
|
},
|
|
error: {
|
|
frame: "border-border-negative-subtle bg-surface-feedback-subtle-negative",
|
|
icon: "text-icon-feedback-negative",
|
|
title: "text-text-feedback-negative",
|
|
},
|
|
} as const;
|
|
|
|
export function SaveToast({ variant, title, description }: SaveToastProps) {
|
|
const tone = VARIANT[variant];
|
|
return (
|
|
<div
|
|
// `role="status"` for the success and `role="alert"` for the failure: a
|
|
// screen reader should interrupt for a save that did not happen, and not
|
|
// for one that did.
|
|
role={variant === "error" ? "alert" : "status"}
|
|
aria-live={variant === "error" ? "assertive" : "polite"}
|
|
className={`flex w-full max-w-[420px] items-start gap-3 rounded-md border border-solid px-4 py-3 shadow-lg ${tone.frame}`}
|
|
data-ui-id={`ui.saveToast.${variant}`}
|
|
>
|
|
{variant === "success" ? (
|
|
<FeatherCheckCircle className={`mt-0.5 text-body-large-0 font-body-large-0 ${tone.icon}`} aria-hidden />
|
|
) : (
|
|
<FeatherAlertCircle className={`mt-0.5 text-body-large-0 font-body-large-0 ${tone.icon}`} aria-hidden />
|
|
)}
|
|
<div className="flex min-w-0 grow flex-col gap-0.5">
|
|
<span className={`text-body-default-medium-0 font-body-default-medium-0 ${tone.title}`}>{title}</span>
|
|
{description ? (
|
|
<span className="break-words text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">{description}</span>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default SaveToast;
|