@tristan2828/ui-foundation 4.89.0 → 4.91.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/conventions/docs/add-an-entity.md +72 -27
- package/conventions/docs/cell-patterns.md +22 -16
- package/conventions/docs/design-language.md +47 -42
- package/dist/api/gateway/errors.js +11 -2
- package/dist/components/app/action-error.d.ts +11 -0
- package/dist/components/app/action-error.js +15 -0
- package/dist/components/app/app-shell.js +2 -1
- package/dist/components/app/delete-action.d.ts +28 -0
- package/dist/components/app/delete-action.js +61 -0
- package/dist/components/app/job-status.d.ts +17 -0
- package/dist/components/app/job-status.js +7 -0
- package/dist/components/app/notify.d.ts +17 -0
- package/dist/components/app/notify.js +46 -0
- package/dist/components/app/offline-notice.d.ts +6 -0
- package/dist/components/app/offline-notice.js +19 -0
- package/dist/components/ui/skeleton.d.ts +5 -1
- package/dist/components/ui/skeleton.js +17 -2
- package/dist/components/ui/sonner.js +28 -5
- package/dist/hooks/use-action.d.ts +29 -0
- package/dist/hooks/use-action.js +68 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/dist/lib/connection.d.ts +8 -0
- package/dist/lib/connection.js +49 -0
- package/package.json +1 -1
- package/styles/index.css +21 -0
- package/styles/theme.css +144 -137
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// "You're offline" across the top of the app (4.90, the dialogs and
|
|
3
|
+
// feedback standard D9), while the server can't be reached: the browser
|
|
4
|
+
// says it's offline, or a request got no answer at all. Gone as soon as
|
|
5
|
+
// the browser is back online or any request gets an answer. AppShell
|
|
6
|
+
// draws it above every page; an app without the shell can render it
|
|
7
|
+
// itself. One bar instead of each save failing on its own: a save made
|
|
8
|
+
// meanwhile still shows its own error beside it.
|
|
9
|
+
//
|
|
10
|
+
// A warning-tone band (the Notice's wash, edge to edge), in a live region
|
|
11
|
+
// that's always there, so the bar is announced when it appears.
|
|
12
|
+
import { WifiOffIcon } from 'lucide-react';
|
|
13
|
+
import { cn } from 'cn';
|
|
14
|
+
import { useOffline } from '../../lib/connection.js';
|
|
15
|
+
export function OfflineNotice({ offline: forced, className }) {
|
|
16
|
+
const detected = useOffline();
|
|
17
|
+
const offline = forced ?? detected;
|
|
18
|
+
return (_jsx("div", { role: "status", "data-slot": "offline-notice", className: "empty:hidden", children: offline && (_jsxs("div", { "data-state": "offline", className: cn('flex items-center justify-center gap-2 border-b border-warning-text/30 bg-warning/10 px-4 py-1.5 text-center type-body text-foreground dark:bg-warning/20', className), children: [_jsx(WifiOffIcon, { "aria-hidden": "true", className: "size-4 shrink-0 text-warning-text" }), _jsx("span", { children: "You\u2019re offline. Changes won\u2019t save until you\u2019re back." })] })) }));
|
|
19
|
+
}
|
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
declare function Skeleton({ className, delay, ...props }: React.ComponentProps<"div"> & {
|
|
3
|
+
/** Milliseconds before it shows. 300 unless set; 0 shows it at once. */
|
|
4
|
+
delay?: number;
|
|
5
|
+
}): React.JSX.Element;
|
|
2
6
|
export { Skeleton };
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import * as React from "react";
|
|
2
3
|
import { cn } from "cn";
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
// `delay` (4.90, the dialogs and feedback standard D7): nothing shows for
|
|
5
|
+
// the first 300ms, so a fast load just appears instead of flashing grey
|
|
6
|
+
// shapes; a slow one shows its shape after that. It keeps its room while
|
|
7
|
+
// it waits (invisible, not absent), so nothing moves when it shows.
|
|
8
|
+
// `delay={0}` shows it at once. DataTable's loading state, made of these,
|
|
9
|
+
// waits the same way.
|
|
10
|
+
function Skeleton({ className, delay = 300, ...props }) {
|
|
11
|
+
const [shown, setShown] = React.useState(delay <= 0);
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
if (delay <= 0)
|
|
14
|
+
return;
|
|
15
|
+
const timer = window.setTimeout(() => setShown(true), delay);
|
|
16
|
+
return () => window.clearTimeout(timer);
|
|
17
|
+
}, [delay]);
|
|
18
|
+
const visible = shown || delay <= 0;
|
|
19
|
+
return (_jsx("div", { "data-slot": "skeleton", "data-shown": visible || undefined, className: cn("animate-pulse rounded-md bg-muted", !visible && "invisible", className), ...props }));
|
|
5
20
|
}
|
|
6
21
|
export { Skeleton };
|
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// The app's toaster (4.90, the dialogs and feedback standard D5).
|
|
3
|
+
//
|
|
4
|
+
// - Each icon in its tone's `*-text` shade (a green check, a red octagon,
|
|
5
|
+
// an amber triangle, a blue i), through `--toast-icon-*` (styles/
|
|
6
|
+
// index.css: sonner shadows the tokens' own names inside the toaster),
|
|
7
|
+
// which clears 3:1 on the popover surface
|
|
8
|
+
// in every theme and palette, so a success and an error are told apart
|
|
9
|
+
// at a glance. The words stay in the popover's foreground.
|
|
10
|
+
// - Top centre from 48rem up, where the eye already is and clear of the
|
|
11
|
+
// bars at the bottom; at the bottom on a phone, in thumb reach, above
|
|
12
|
+
// whatever is pinned there: `--toast-bottom-offset` (EntityForm's stuck
|
|
13
|
+
// bar sets it on <html>) or else AppShell's bottom bar
|
|
14
|
+
// (`--toast-bottom-base`, styles/index.css).
|
|
15
|
+
// - Toasts last 4 seconds, Undo too. An error from `notify.failed` stays
|
|
16
|
+
// until it's closed.
|
|
17
|
+
//
|
|
18
|
+
// What a toast says and when one shows: `notify` (components/app/notify.ts).
|
|
2
19
|
import { useTheme } from "next-themes";
|
|
3
20
|
import { Toaster as Sonner } from "sonner";
|
|
4
21
|
import { CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from "lucide-react";
|
|
22
|
+
import { useIsMobile } from "../../hooks/use-mobile.js";
|
|
23
|
+
// sonner's own gap from the window's edge is 24px (16px on its narrow
|
|
24
|
+
// layout); 16px here, plus whatever is pinned to the bottom.
|
|
25
|
+
const EDGE = "16px";
|
|
26
|
+
const BOTTOM = `calc(var(--toast-bottom-offset, var(--toast-bottom-base, 0px)) + ${EDGE})`;
|
|
5
27
|
const Toaster = ({ ...props }) => {
|
|
6
28
|
const { theme = "system" } = useTheme();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
29
|
+
const phone = useIsMobile();
|
|
30
|
+
return (_jsx(Sonner, { theme: theme, className: "toaster group", position: phone ? "bottom-center" : "top-center", offset: { top: EDGE, bottom: BOTTOM, left: EDGE, right: EDGE }, mobileOffset: { top: EDGE, bottom: BOTTOM, left: EDGE, right: EDGE }, duration: 4000, icons: {
|
|
31
|
+
success: (_jsx(CircleCheckIcon, { className: "size-4 text-(--toast-icon-success)" })),
|
|
32
|
+
info: (_jsx(InfoIcon, { className: "size-4 text-(--toast-icon-info)" })),
|
|
33
|
+
warning: (_jsx(TriangleAlertIcon, { className: "size-4 text-(--toast-icon-warning)" })),
|
|
34
|
+
error: (_jsx(OctagonXIcon, { className: "size-4 text-(--toast-icon-error)" })),
|
|
12
35
|
loading: (_jsx(Loader2Icon, { className: "size-4 animate-spin" })),
|
|
13
36
|
}, style: {
|
|
14
37
|
"--normal-bg": "var(--popover)",
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
type AsyncAction<TArgs extends unknown[], TResult> = (...args: TArgs) => Promise<TResult>;
|
|
2
|
+
export type UseActionOptions<TResult> = {
|
|
3
|
+
/** The lead of the toast when it fails off screen: "Couldn't mark The Long Walk as read". */
|
|
4
|
+
failed?: string;
|
|
5
|
+
/** A toast's words when it finishes after the component was left. No toast when it's on screen. */
|
|
6
|
+
doneAway?: (result: TResult) => string;
|
|
7
|
+
};
|
|
8
|
+
export type ActionOutcome<TResult> = {
|
|
9
|
+
ok: true;
|
|
10
|
+
value: TResult;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
export type ActionState<TArgs extends unknown[] = unknown[], TResult = unknown> = {
|
|
16
|
+
/** Runs it. Never rejects: resolves `{ ok: true, value }`, or `{ ok: false, error }` (also in `error`). */
|
|
17
|
+
run: (...args: TArgs) => Promise<ActionOutcome<TResult>>;
|
|
18
|
+
/** The last run again, with the same arguments. */
|
|
19
|
+
retry: () => Promise<ActionOutcome<TResult>>;
|
|
20
|
+
/** Clears the error. */
|
|
21
|
+
reset: () => void;
|
|
22
|
+
pending: boolean;
|
|
23
|
+
/** Why the last run failed, in the gateway's words; null when it didn't. */
|
|
24
|
+
error: string | null;
|
|
25
|
+
};
|
|
26
|
+
export declare function useAction<TArgs extends unknown[], TResult>(action: AsyncAction<TArgs, TResult> | {
|
|
27
|
+
mutateAsync: AsyncAction<TArgs, TResult>;
|
|
28
|
+
}, { failed, doneAway }?: UseActionOptions<TResult>): ActionState<TArgs, TResult>;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// One action's run, its pending state and its error, kept beside the
|
|
2
|
+
// button that started it (4.90, the dialogs and feedback standard D6).
|
|
3
|
+
//
|
|
4
|
+
// const markRead = useAction(markReadMutation, { failed: "Couldn't mark The Long Walk as read" })
|
|
5
|
+
// <Button onClick={() => markRead.run(book.id)} disabled={markRead.pending}>…</Button>
|
|
6
|
+
// <ActionError action={markRead} />
|
|
7
|
+
//
|
|
8
|
+
// - `error` is the reason in the gateway's words, for ActionError to draw
|
|
9
|
+
// as a destructive Notice with Try again (`retry`, the last run again).
|
|
10
|
+
// - If the component is gone when it fails (its page was left), the error
|
|
11
|
+
// becomes a toast that stays: "<failed>: <reason>".
|
|
12
|
+
// - `doneAway`: what a toast says when it finishes after the component was
|
|
13
|
+
// left (a job's "Checked 12 · 2 deals"). On screen, the component shows
|
|
14
|
+
// its own result and no toast.
|
|
15
|
+
//
|
|
16
|
+
// Takes a TanStack mutation (its `mutateAsync`) or any async function.
|
|
17
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
18
|
+
import { toast } from 'sonner';
|
|
19
|
+
import { messageOf } from '../components/app/edit-in-place-store.js';
|
|
20
|
+
import { notify } from '../components/app/notify.js';
|
|
21
|
+
export function useAction(action, { failed, doneAway } = {}) {
|
|
22
|
+
const [pending, setPending] = useState(false);
|
|
23
|
+
const [error, setError] = useState(null);
|
|
24
|
+
const latest = useRef({ action, failed, doneAway });
|
|
25
|
+
const lastArgs = useRef(null);
|
|
26
|
+
const mounted = useRef(true);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
latest.current = { action, failed, doneAway };
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
mounted.current = true;
|
|
32
|
+
return () => {
|
|
33
|
+
mounted.current = false;
|
|
34
|
+
};
|
|
35
|
+
}, []);
|
|
36
|
+
const run = useCallback(async (...args) => {
|
|
37
|
+
const { action: current } = latest.current;
|
|
38
|
+
const call = typeof current === 'function' ? current : current.mutateAsync;
|
|
39
|
+
lastArgs.current = args;
|
|
40
|
+
setPending(true);
|
|
41
|
+
setError(null);
|
|
42
|
+
try {
|
|
43
|
+
const result = await call(...args);
|
|
44
|
+
if (mounted.current)
|
|
45
|
+
setPending(false);
|
|
46
|
+
else {
|
|
47
|
+
const words = latest.current.doneAway?.(result);
|
|
48
|
+
if (words)
|
|
49
|
+
toast.success(words);
|
|
50
|
+
}
|
|
51
|
+
return { ok: true, value: result };
|
|
52
|
+
}
|
|
53
|
+
catch (thrown) {
|
|
54
|
+
const reason = messageOf(thrown);
|
|
55
|
+
if (mounted.current) {
|
|
56
|
+
setPending(false);
|
|
57
|
+
setError(reason);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
notify.failed(`${latest.current.failed ?? "Couldn't finish"}: ${reason}`);
|
|
61
|
+
}
|
|
62
|
+
return { ok: false, error: reason };
|
|
63
|
+
}
|
|
64
|
+
}, []);
|
|
65
|
+
const retry = useCallback(() => run(...(lastArgs.current ?? [])), [run]);
|
|
66
|
+
const reset = useCallback(() => setError(null), []);
|
|
67
|
+
return { run, retry, reset, pending, error };
|
|
68
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -29,6 +29,13 @@ export { usePrinting } from './components/app/print-context.js';
|
|
|
29
29
|
export { RecordDialogRoute, RecordLink, type RecordDialogRouteProps, type RecordLinkProps, } from './components/app/record-dialog.js';
|
|
30
30
|
export { recordDialogState, useNewRecordDialog, useOpenRecord, useRecordDialog, type NewRecordDialogContextValue, type RecordDialogBackground, type RecordDialogContextValue, type RecordDialogState, } from './components/app/record-dialog-context.js';
|
|
31
31
|
export { FormDialog, type FormDialogProps } from './components/app/form-dialog.js';
|
|
32
|
+
export { DeleteAction, type DeleteActionProps, type DeleteKind } from './components/app/delete-action.js';
|
|
33
|
+
export { notify, doneMessage, pastTense, type NotifyDoneOptions } from './components/app/notify.js';
|
|
34
|
+
export { useAction, type ActionOutcome, type ActionState, type UseActionOptions } from './hooks/use-action.js';
|
|
35
|
+
export { ActionError, type ActionErrorProps } from './components/app/action-error.js';
|
|
36
|
+
export { JobStatus, type JobProgress, type JobStatusProps } from './components/app/job-status.js';
|
|
37
|
+
export { OfflineNotice, type OfflineNoticeProps } from './components/app/offline-notice.js';
|
|
38
|
+
export { useOffline } from './lib/connection.js';
|
|
32
39
|
export { editInPlace, type EditControlProps, type EditInPlace, type EditInPlaceList, type EditInPlaceListOptions, type EditInPlaceOptions, type EditKind, type EditListAddProps, type EditListItemProps, } from './components/app/edit-in-place.js';
|
|
33
40
|
export { inPlace, type InPlaceDateConfig, type InPlaceFieldsOptions, type InPlaceLinkConfig, type InPlaceLinkValue, type InPlaceMoneyConfig, type InPlaceMonthDayConfig, type InPlaceNumberConfig, type InPlaceOption, type InPlacePart, type InPlaceRecord, type InPlaceReferenceConfig, type InPlaceSaved, type InPlaceSelectConfig, type InPlaceTextConfig, } from './components/app/edit-in-place-presets.js';
|
|
34
41
|
export { linkHost, linkHref, linkRule } from './lib/link-href.js';
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,15 @@ export { usePrinting } from './components/app/print-context.js';
|
|
|
33
33
|
export { RecordDialogRoute, RecordLink, } from './components/app/record-dialog.js';
|
|
34
34
|
export { recordDialogState, useNewRecordDialog, useOpenRecord, useRecordDialog, } from './components/app/record-dialog-context.js';
|
|
35
35
|
export { FormDialog } from './components/app/form-dialog.js';
|
|
36
|
+
// The dialogs and feedback standard (4.90): one delete, toasts' words, an
|
|
37
|
+
// action's error beside it, a job's progress, and the offline bar.
|
|
38
|
+
export { DeleteAction } from './components/app/delete-action.js';
|
|
39
|
+
export { notify, doneMessage, pastTense } from './components/app/notify.js';
|
|
40
|
+
export { useAction } from './hooks/use-action.js';
|
|
41
|
+
export { ActionError } from './components/app/action-error.js';
|
|
42
|
+
export { JobStatus } from './components/app/job-status.js';
|
|
43
|
+
export { OfflineNotice } from './components/app/offline-notice.js';
|
|
44
|
+
export { useOffline } from './lib/connection.js';
|
|
36
45
|
export { editInPlace, } from './components/app/edit-in-place.js';
|
|
37
46
|
export { inPlace, } from './components/app/edit-in-place-presets.js';
|
|
38
47
|
export { linkHost, linkHref, linkRule } from './lib/link-href.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A request got an answer (any status): the server can be reached. */
|
|
2
|
+
export declare function reportAnswered(): void;
|
|
3
|
+
/** A request got no answer at all (fetch rejected): the server can't be reached. */
|
|
4
|
+
export declare function reportUnreachable(): void;
|
|
5
|
+
/** Whether the server can't be reached right now. */
|
|
6
|
+
export declare function isOffline(): boolean;
|
|
7
|
+
/** Whether the server can't be reached right now, re-rendering when that changes. */
|
|
8
|
+
export declare function useOffline(): boolean;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Whether the app can reach its server (4.90, the dialogs and feedback
|
|
2
|
+
// standard D9): what OfflineNotice reads. Offline when the browser says so
|
|
3
|
+
// (`navigator.onLine`, its `offline` event) or when a request couldn't get
|
|
4
|
+
// an answer at all (safeFetch's network failure); online again when the
|
|
5
|
+
// browser says so or when any request gets an answer. One store for the
|
|
6
|
+
// whole app, so the notice shows once however many saves fail.
|
|
7
|
+
import { useSyncExternalStore } from 'react';
|
|
8
|
+
let offline = false;
|
|
9
|
+
const listeners = new Set();
|
|
10
|
+
function set(next) {
|
|
11
|
+
if (next === offline)
|
|
12
|
+
return;
|
|
13
|
+
offline = next;
|
|
14
|
+
for (const listener of listeners)
|
|
15
|
+
listener();
|
|
16
|
+
}
|
|
17
|
+
/** A request got an answer (any status): the server can be reached. */
|
|
18
|
+
export function reportAnswered() {
|
|
19
|
+
set(false);
|
|
20
|
+
}
|
|
21
|
+
/** A request got no answer at all (fetch rejected): the server can't be reached. */
|
|
22
|
+
export function reportUnreachable() {
|
|
23
|
+
set(true);
|
|
24
|
+
}
|
|
25
|
+
let watching = false;
|
|
26
|
+
function watchBrowser() {
|
|
27
|
+
if (watching || typeof window === 'undefined')
|
|
28
|
+
return;
|
|
29
|
+
watching = true;
|
|
30
|
+
if (typeof navigator !== 'undefined' && navigator.onLine === false)
|
|
31
|
+
offline = true;
|
|
32
|
+
window.addEventListener('offline', () => set(true));
|
|
33
|
+
window.addEventListener('online', () => set(false));
|
|
34
|
+
}
|
|
35
|
+
function subscribe(listener) {
|
|
36
|
+
watchBrowser();
|
|
37
|
+
listeners.add(listener);
|
|
38
|
+
return () => {
|
|
39
|
+
listeners.delete(listener);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Whether the server can't be reached right now. */
|
|
43
|
+
export function isOffline() {
|
|
44
|
+
return offline;
|
|
45
|
+
}
|
|
46
|
+
/** Whether the server can't be reached right now, re-rendering when that changes. */
|
|
47
|
+
export function useOffline() {
|
|
48
|
+
return useSyncExternalStore(subscribe, isOffline, () => false);
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.91.0",
|
|
4
4
|
"description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
package/styles/index.css
CHANGED
|
@@ -675,3 +675,24 @@
|
|
|
675
675
|
--form-bar-safe-area: 0px;
|
|
676
676
|
}
|
|
677
677
|
}
|
|
678
|
+
|
|
679
|
+
/* Toasts on a phone sit at the bottom (4.90, ui/sonner.tsx), above
|
|
680
|
+
whatever is pinned there: EntityForm's stuck bar sets
|
|
681
|
+
--toast-bottom-offset itself; failing that, the shell's bottom bar is
|
|
682
|
+
the room to clear, or the phone's safe area. */
|
|
683
|
+
:root {
|
|
684
|
+
--toast-bottom-base: env(safe-area-inset-bottom, 0px);
|
|
685
|
+
/* The tones' text shades, resolved here: sonner's toaster declares its
|
|
686
|
+
own --success-text, --info-text and --warning-text, which would win
|
|
687
|
+
inside it. A custom property's var() resolves where it's declared,
|
|
688
|
+
so these carry the theme's and the palette's values in. */
|
|
689
|
+
--toast-icon-success: var(--success-text);
|
|
690
|
+
--toast-icon-info: var(--info-text);
|
|
691
|
+
--toast-icon-warning: var(--warning-text);
|
|
692
|
+
--toast-icon-error: var(--destructive-text);
|
|
693
|
+
}
|
|
694
|
+
@media (width < 48rem) {
|
|
695
|
+
:root:has([data-slot="app-shell-bottom-bar"]) {
|
|
696
|
+
--toast-bottom-base: calc(var(--spacing) * 16 + 1px + env(safe-area-inset-bottom));
|
|
697
|
+
}
|
|
698
|
+
}
|