@cloudlet/toast 1.4.0 → 1.5.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/CHANGELOG.md +165 -0
- package/README.md +3 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +19 -15
- package/dist/agent.js.map +1 -1
- package/dist/center-react.d.ts +286 -0
- package/dist/center-react.d.ts.map +1 -0
- package/dist/center-react.js +2 -0
- package/dist/center.d.ts +3 -0
- package/dist/center.d.ts.map +1 -0
- package/dist/center.js +1 -0
- package/dist/core/agent-port.d.ts +2 -0
- package/dist/core/agent-port.d.ts.map +1 -1
- package/dist/core/agent-port.js +3 -0
- package/dist/core/agent-port.js.map +1 -1
- package/dist/react/card.d.ts +4 -0
- package/dist/react/card.d.ts.map +1 -1
- package/dist/react/card.js +3 -8
- package/dist/react/card.js.map +1 -1
- package/dist/react/center-backdrop.d.ts +69 -0
- package/dist/react/center-backdrop.d.ts.map +1 -0
- package/dist/react/center-backdrop.js +81 -0
- package/dist/react/center-backdrop.js.map +1 -0
- package/dist/react/center-clear.d.ts +16 -0
- package/dist/react/center-clear.d.ts.map +1 -0
- package/dist/react/center-clear.js +24 -0
- package/dist/react/center-clear.js.map +1 -0
- package/dist/react/center-gestures.d.ts +47 -0
- package/dist/react/center-gestures.d.ts.map +1 -0
- package/dist/react/center-gestures.js +74 -0
- package/dist/react/center-gestures.js.map +1 -0
- package/dist/react/center-motion.d.ts +174 -0
- package/dist/react/center-motion.d.ts.map +1 -0
- package/dist/react/center-motion.js +344 -0
- package/dist/react/center-motion.js.map +1 -0
- package/dist/react/center.d.ts +15 -0
- package/dist/react/center.d.ts.map +1 -0
- package/dist/react/center.js +1287 -0
- package/dist/react/center.js.map +1 -0
- package/dist/react/focus.d.ts +7 -0
- package/dist/react/focus.d.ts.map +1 -0
- package/dist/react/focus.js +14 -0
- package/dist/react/focus.js.map +1 -0
- package/dist/react/gestures.d.ts +33 -2
- package/dist/react/gestures.d.ts.map +1 -1
- package/dist/react/gestures.js +181 -149
- package/dist/react/gestures.js.map +1 -1
- package/dist/react/host.d.ts.map +1 -1
- package/dist/react/host.js +287 -283
- package/dist/react/host.js.map +1 -1
- package/dist/react/motion.d.ts +18 -11
- package/dist/react/motion.d.ts.map +1 -1
- package/dist/react/motion.js +6 -3
- package/dist/react/motion.js.map +1 -1
- package/dist/react/reduced.d.ts +24 -0
- package/dist/react/reduced.d.ts.map +1 -0
- package/dist/react/reduced.js +35 -0
- package/dist/react/reduced.js.map +1 -0
- package/dist/react/zoom.d.ts +12 -0
- package/dist/react/zoom.d.ts.map +1 -0
- package/dist/react/zoom.js +21 -0
- package/dist/react/zoom.js.map +1 -0
- package/dist/react.d.ts +4 -0
- package/dist/react.d.ts.map +1 -1
- package/dist/styles.css +1 -1
- package/package.json +2 -1
- package/src/agent.ts +11 -6
- package/src/center-react.ts +300 -0
- package/src/center.ts +27 -0
- package/src/core/agent-port.ts +3 -0
- package/src/react/card.tsx +5 -6
- package/src/react/center-backdrop.tsx +151 -0
- package/src/react/center-clear.ts +56 -0
- package/src/react/center-gestures.ts +143 -0
- package/src/react/center-motion.ts +504 -0
- package/src/react/center.tsx +1557 -0
- package/src/react/focus.ts +18 -0
- package/src/react/gestures.ts +127 -37
- package/src/react/host.tsx +17 -5
- package/src/react/motion.ts +16 -9
- package/src/react/reduced.ts +52 -0
- package/src/react/zoom.ts +26 -0
- package/src/react.ts +4 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
4
|
+
import type { CloudletSpring, CloudletTimedSpring } from './react.js';
|
|
5
|
+
|
|
6
|
+
// The notification center's React interface. `center.ts` re-exports it, as the root entry does
|
|
7
|
+
// `react.ts`, so the subpath stays readable on the server while these values are client code.
|
|
8
|
+
|
|
9
|
+
/** A button of a notification; a press calls `onAction(item.id, action.id)`. */
|
|
10
|
+
export interface NotificationItemAction {
|
|
11
|
+
/** Unique among the notification's actions. */
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly label: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* One notification of the app's list. The app owns the list: it loads it from wherever it likes (a
|
|
18
|
+
* REST endpoint, a WebSocket, local state) and gives the center a new array whenever it changes;
|
|
19
|
+
* the center never changes it itself.
|
|
20
|
+
*/
|
|
21
|
+
export interface NotificationItem {
|
|
22
|
+
/** Stable and unique in the list: the center follows a notification by its id across arrays. */
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly title: ReactNode;
|
|
25
|
+
readonly description?: ReactNode;
|
|
26
|
+
/** When it happened: a `Date` or milliseconds since the epoch. The list shows the newest first. */
|
|
27
|
+
readonly time: Date | number;
|
|
28
|
+
/** Notifications with the same nonempty `group` stack into one group; without it one stands alone. */
|
|
29
|
+
readonly group?: string;
|
|
30
|
+
/**
|
|
31
|
+
* `success`, `error`, `info`, `warning` or `loading` draw the Toaster's status icon and tint; any
|
|
32
|
+
* other string is the app's own kind (`data-kind` on the card) and draws no icon of its own.
|
|
33
|
+
*/
|
|
34
|
+
readonly kind?: string;
|
|
35
|
+
/** `false` marks the notification unread (`data-read="false"` and a dot); omitted, it reads as read. */
|
|
36
|
+
readonly read?: boolean;
|
|
37
|
+
/** Replaces the kind's icon. */
|
|
38
|
+
readonly icon?: ReactNode;
|
|
39
|
+
readonly actions?: readonly NotificationItemAction[];
|
|
40
|
+
/** The app's own data, passed back untouched in `renderNotification`. */
|
|
41
|
+
readonly data?: unknown;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The parts of the default card, for `renderNotification` to place as it likes. The center keeps
|
|
46
|
+
* the card's frame (`li`, its motion and swipe); what the function returns is its content.
|
|
47
|
+
*/
|
|
48
|
+
export interface NotificationParts {
|
|
49
|
+
/** The status icon or `item.icon`; null without either. */
|
|
50
|
+
readonly icon: ReactNode;
|
|
51
|
+
/** The title as the card's heading, with `titleId`. */
|
|
52
|
+
readonly title: ReactElement;
|
|
53
|
+
/** The description in its part; null without one. */
|
|
54
|
+
readonly description: ReactNode;
|
|
55
|
+
/** The action buttons in their row; null without actions. */
|
|
56
|
+
readonly actions: ReactNode;
|
|
57
|
+
/**
|
|
58
|
+
* The time and, in its place on hover and focus, the clear button. Leave it out and the card has
|
|
59
|
+
* no clear button: it still clears by swipe and with its group.
|
|
60
|
+
*/
|
|
61
|
+
readonly time: ReactElement;
|
|
62
|
+
/** The id of the title's heading, for the app's own `aria-labelledby`. */
|
|
63
|
+
readonly titleId: string;
|
|
64
|
+
/** Asks the app to dismiss this notification, as its clear button does (`onDismiss`). */
|
|
65
|
+
readonly dismiss: () => void;
|
|
66
|
+
/** Runs an action of this notification, as its button does (`onAction`). */
|
|
67
|
+
readonly action: (actionId: string) => void;
|
|
68
|
+
/** The default content: the head (icon, title, time) and the body (description, actions). */
|
|
69
|
+
readonly content: ReactElement;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface NotificationCenterLabels {
|
|
73
|
+
/** The panel's visible heading and accessible name, "Notifications" by default. */
|
|
74
|
+
readonly title: string;
|
|
75
|
+
/** The panel's close button. */
|
|
76
|
+
readonly close: string;
|
|
77
|
+
/** The button that clears one card, in place of its time. */
|
|
78
|
+
readonly clear: string;
|
|
79
|
+
/** The line of an open center without notifications. */
|
|
80
|
+
readonly empty: string;
|
|
81
|
+
/** The hidden word before a card's title that tells a screen reader its kind; an empty string says nothing. */
|
|
82
|
+
readonly kind: (kind: string) => string;
|
|
83
|
+
/** The hidden word before the title of an unread notification, "Unread" by default. */
|
|
84
|
+
readonly unread: string;
|
|
85
|
+
/** A card's time: `time` and `now` are milliseconds since the epoch. "now", "2m", "1h", "3d", then a date, by default. */
|
|
86
|
+
readonly time: (time: number, now: number) => string;
|
|
87
|
+
/** A group's name from its key (the notifications' `group`): the key as it is by default. */
|
|
88
|
+
readonly group: (key: string) => string;
|
|
89
|
+
/** The button under a collapsed stack that expands it: "Deploys · 2 more" by default; `count` is the notifications under the top one. */
|
|
90
|
+
readonly more: (label: string, count: number) => string;
|
|
91
|
+
/** The accessible name of a group of `count` notifications: "Deploys, 3 notifications" by default. */
|
|
92
|
+
readonly stack: (label: string, count: number) => string;
|
|
93
|
+
/** The button that collapses an expanded group, beside its name. */
|
|
94
|
+
readonly showLess: string;
|
|
95
|
+
/** The two-step button in an expanded group's heading row that clears the group: "Clear" by default. */
|
|
96
|
+
readonly clearGroup: string;
|
|
97
|
+
/** The same button armed, and the polite status then: "Clear 3" by default; `count` is the group's notifications. */
|
|
98
|
+
readonly clearGroupConfirm: (count: number) => string;
|
|
99
|
+
/**
|
|
100
|
+
* The accessible name of a collapsed stack's clear button, in place of its top card's time: "Clear
|
|
101
|
+
* Deploys" by default. Armed, it reads `clearGroupConfirm`.
|
|
102
|
+
*/
|
|
103
|
+
readonly clearStack: (label: string) => string;
|
|
104
|
+
/** The two-step button in the panel's header that clears every notification, shown from two: "Clear all" by default. */
|
|
105
|
+
readonly clearAll: string;
|
|
106
|
+
/** The same button armed, and the polite status then: "Clear 5" by default; `count` is every notification of the list. */
|
|
107
|
+
readonly clearAllConfirm: (count: number) => string;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Styling hooks of the notification center; see `NotificationCenterProps.classNames`. */
|
|
111
|
+
export type NotificationCenterPart = 'center' | 'header' | 'heading' | 'dismiss' | 'list' | 'card' | 'time' | 'clear' | 'empty'
|
|
112
|
+
| 'group' | 'group-header' | 'group-title' | 'show-less' | 'group-list' | 'edge' | 'more' | 'clear-all' | 'group-clear' | 'stack-clear'
|
|
113
|
+
| 'icon' | 'title' | 'body' | 'description' | 'actions' | 'action' | 'unread';
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The notification center's motion constants: durations in ms at `timeScale: 1`, offsets and blur
|
|
117
|
+
* in CSS px. Reported by the laboratory's tuning panel; see `NotificationCenterMotion.overrides`.
|
|
118
|
+
*/
|
|
119
|
+
export interface NotificationCenterMotionConstants {
|
|
120
|
+
/** The drawer's position spring opening. */
|
|
121
|
+
readonly open: CloudletSpring;
|
|
122
|
+
/** The drawer's position spring closing. */
|
|
123
|
+
readonly close: CloudletSpring;
|
|
124
|
+
/** How far past the column's width a closed card lies, in px. */
|
|
125
|
+
readonly travel: number;
|
|
126
|
+
/** The share of the way each next card trails the previous one by (0.12 = 12 %), up to the eighth card. */
|
|
127
|
+
readonly lag: number;
|
|
128
|
+
/** A card arriving in the open center: scale from `scale` on the spring, opacity over `fade` ms. */
|
|
129
|
+
readonly enter: CloudletTimedSpring & { readonly scale: number; readonly fade: number };
|
|
130
|
+
/** A card leaving the list. */
|
|
131
|
+
readonly exit: CloudletTimedSpring;
|
|
132
|
+
/** The other cards moving to their new places. */
|
|
133
|
+
readonly reflow: CloudletSpring;
|
|
134
|
+
/** The time and the clear button in its place: swap duration and blur radius in px. */
|
|
135
|
+
readonly swap: number;
|
|
136
|
+
readonly swapBlur: number;
|
|
137
|
+
/** Reduced motion: opening, closing and the list change opacity only, for this long. */
|
|
138
|
+
readonly reduced: number;
|
|
139
|
+
/**
|
|
140
|
+
* A group of two notifications or more. Collapsed, up to two edges lie under its top card, each
|
|
141
|
+
* `step` px lower than the one above and narrower by `shrink` of the card's width (0.06 = 6 %, so
|
|
142
|
+
* 0.94 and 0.88). Its cards move between the stack and the list on `expand` and `collapse`; an
|
|
143
|
+
* edge's content shows over `reveal` ms from a `blur` px blur, as the group's heading row and the
|
|
144
|
+
* button under the stack swap; the n-th card of the group starts once the first has covered
|
|
145
|
+
* n × `cascade` of its way (0.06 = 6 % per card, up to the eighth; 0 moves them together).
|
|
146
|
+
*/
|
|
147
|
+
readonly stack: {
|
|
148
|
+
readonly expand: CloudletSpring;
|
|
149
|
+
readonly collapse: CloudletSpring;
|
|
150
|
+
readonly step: number;
|
|
151
|
+
readonly shrink: number;
|
|
152
|
+
readonly reveal: number;
|
|
153
|
+
readonly blur: number;
|
|
154
|
+
readonly cascade: number;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The notification center's backdrop constants for `backdrop="blur"`; see
|
|
160
|
+
* `NotificationCenterMotion.overrides`. `layers` is how many blur layers stand under the tone: from
|
|
161
|
+
* the strongest (`--cloudlet-center-blur`, at the edge) each next one, nearer the column's inner
|
|
162
|
+
* side, has half the radius; one layer is a single blur of 0.7 × `--cloudlet-center-blur` fading in
|
|
163
|
+
* from the band's inner edge. Without an override, an Apple engine (`navigator.vendor`) gets one.
|
|
164
|
+
* `scrollTint` shows the tone alone while the page scrolls under the open center: a scroll anywhere
|
|
165
|
+
* in the document but the panel's own list fades the blur layers out over 120 ms, and 150 ms without
|
|
166
|
+
* one fades them back (at once under reduced motion).
|
|
167
|
+
*/
|
|
168
|
+
export interface NotificationCenterBackdropConstants {
|
|
169
|
+
/** An integer from 1 to 8; 4 by default. */
|
|
170
|
+
readonly layers: number;
|
|
171
|
+
/** `false` by default. */
|
|
172
|
+
readonly scrollTint: boolean;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
type PartialMotion<T> = { readonly [K in keyof T]?: T[K] extends number ? number : PartialMotion<T[K]> };
|
|
176
|
+
/**
|
|
177
|
+
* Partial `NotificationCenterMotionConstants`; nested springs and the stack's fields may be partial
|
|
178
|
+
* too. `backdrop` takes `NotificationCenterBackdropConstants` fields: `layers` an integer from 1 to
|
|
179
|
+
* 8, `scrollTint` a boolean; any other value keeps the default.
|
|
180
|
+
*/
|
|
181
|
+
export type NotificationCenterMotionOverrides = PartialMotion<NotificationCenterMotionConstants> & {
|
|
182
|
+
readonly backdrop?: Partial<NotificationCenterBackdropConstants>;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export interface NotificationCenterMotion {
|
|
186
|
+
/**
|
|
187
|
+
* Debugging and laboratory aid: slows every transition (0.5 = twice as slow).
|
|
188
|
+
* Default 1; a non-finite or nonpositive value falls back to 1. Not recommended in production.
|
|
189
|
+
*/
|
|
190
|
+
readonly timeScale?: number;
|
|
191
|
+
/**
|
|
192
|
+
* Debugging and laboratory channel: replaces the motion constants live, for transitions started
|
|
193
|
+
* after the change. A field that is not a finite number in range (stiffness and mass > 0, the rest
|
|
194
|
+
* ≥ 0) keeps its default. Not a styling API: the values may change between releases.
|
|
195
|
+
*/
|
|
196
|
+
readonly overrides?: NotificationCenterMotionOverrides;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Whether the center is open, uncontrolled or controlled; for `useNotificationCenter` and the panel. */
|
|
200
|
+
export interface NotificationCenterOpenOptions {
|
|
201
|
+
/** Controls the open state; pair it with `onOpenChange`. */
|
|
202
|
+
readonly open?: boolean | undefined;
|
|
203
|
+
/** The first open state of an uncontrolled center; `false` by default. */
|
|
204
|
+
readonly defaultOpen?: boolean | undefined;
|
|
205
|
+
/** Called with the next state when a trigger, Escape, a press outside, the close button or a swipe asks to open or close. */
|
|
206
|
+
readonly onOpenChange?: ((open: boolean) => void) | undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface NotificationCenterProps extends NotificationCenterOpenOptions {
|
|
210
|
+
/**
|
|
211
|
+
* The center's open state and its triggers, from `useNotificationCenter()`. Without it the panel
|
|
212
|
+
* keeps its own state from `open`, `defaultOpen` and `onOpenChange`, and has no trigger of its own.
|
|
213
|
+
*/
|
|
214
|
+
readonly center?: UseNotificationCenterResult;
|
|
215
|
+
/**
|
|
216
|
+
* The app's notifications, in any order (the panel shows the newest first). Give a new array when
|
|
217
|
+
* it changes: the panel animates what arrived, left and moved. Of two with one id the first counts.
|
|
218
|
+
*/
|
|
219
|
+
readonly notifications: readonly NotificationItem[];
|
|
220
|
+
/**
|
|
221
|
+
* A card's clear button or a swipe asks to dismiss it. Without it a card has no clear button and
|
|
222
|
+
* does not swipe; with only `onClear`, these call `onClear([id])`.
|
|
223
|
+
*/
|
|
224
|
+
readonly onDismiss?: ((id: string) => void) | undefined;
|
|
225
|
+
/**
|
|
226
|
+
* A group's two-step Clear (its ids) or the header's Clear all (every id of the list) asks to clear
|
|
227
|
+
* these notifications. Without it the two-step buttons call `onDismiss` for each id, and with
|
|
228
|
+
* neither callback there are none.
|
|
229
|
+
*/
|
|
230
|
+
readonly onClear?: ((ids: readonly string[]) => void) | undefined;
|
|
231
|
+
/** A press of one of a notification's `actions`. The card stays: the app removes it if it wants. */
|
|
232
|
+
readonly onAction?: ((id: string, actionId: string) => void) | undefined;
|
|
233
|
+
/**
|
|
234
|
+
* Unread notifications (`read: false`) the person has seen: those listed when the panel opens and
|
|
235
|
+
* those that arrive while it is open (on a visible page), each id once per opening.
|
|
236
|
+
*/
|
|
237
|
+
readonly onRead?: ((ids: readonly string[]) => void) | undefined;
|
|
238
|
+
/** Draws a card's content from the default parts; the center keeps its frame, motion and swipe. */
|
|
239
|
+
readonly renderNotification?: ((item: NotificationItem, parts: NotificationParts) => ReactNode) | undefined;
|
|
240
|
+
/** Physical: the panel stays at that edge in RTL too. Default `right`. */
|
|
241
|
+
readonly side?: 'left' | 'right';
|
|
242
|
+
readonly theme?: 'system' | 'light' | 'dark';
|
|
243
|
+
readonly dir?: 'ltr' | 'rtl';
|
|
244
|
+
readonly labels?: Partial<NotificationCenterLabels>;
|
|
245
|
+
/** Extra classes per part, added to the own `cloudlet-*` classes (never replacing them). */
|
|
246
|
+
readonly classNames?: Partial<Record<NotificationCenterPart, string>>;
|
|
247
|
+
/**
|
|
248
|
+
* What lies under the panel's column and a band past it, at its edge: `blur` (the default: the
|
|
249
|
+
* page blurred progressively, strongest at the edge, under a tone of `--cloudlet-center-blur-tint`;
|
|
250
|
+
* one blur layer in Safari and other Apple engines; the tone of `tint` in a window narrower than
|
|
251
|
+
* 48rem and where `backdrop-filter` is unsupported), `tint` (a tone, `--cloudlet-center-tint` of
|
|
252
|
+
* `--cloudlet-background`, even under the column and fading out across the band) or `none` (nothing: the panel's header and
|
|
253
|
+
* empty line then stand on the page, whose contrast is the app's). Any other value is `blur`.
|
|
254
|
+
* Reduced transparency draws the tone of `tint` in place of `blur`: `data-reduced-transparency` on
|
|
255
|
+
* an ancestor of the panel (it is portalled into the body, so on `<body>` or `<html>`; by presence,
|
|
256
|
+
* live) and the system's `prefers-reduced-transparency: reduce`; an app cannot turn the blur on
|
|
257
|
+
* against the system. The panel's root says the app's result in `data-backdrop` (`tint` under the
|
|
258
|
+
* attribute; the system's setting is CSS and stays out of it), and with `blur` how many blur layers
|
|
259
|
+
* it has in `data-backdrop-layers`. Read on every render.
|
|
260
|
+
*/
|
|
261
|
+
readonly backdrop?: 'blur' | 'tint' | 'none';
|
|
262
|
+
/** Read on every render. */
|
|
263
|
+
readonly motion?: NotificationCenterMotion;
|
|
264
|
+
/**
|
|
265
|
+
* The app's own reduced-motion setting. `true` removes motion whatever the system says; `false`
|
|
266
|
+
* and omitting it follow the system and `data-reduced-motion` on an ancestor of the panel (it is
|
|
267
|
+
* portalled into the body, so on `<body>` or `<html>`; the attribute counts by presence). An app
|
|
268
|
+
* cannot turn motion on against the system's `prefers-reduced-motion: reduce`. Read on every render.
|
|
269
|
+
*/
|
|
270
|
+
readonly reducedMotion?: boolean;
|
|
271
|
+
/** The nonce of the panel's own `<style>`, whose CSSOM rules carry its motion under a strict CSP. Read when the panel opens. */
|
|
272
|
+
readonly nonce?: string;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Spread on the app's own button: it opens and closes the center. A center takes as many buttons
|
|
277
|
+
* as these props are spread on; focus returns to the one that opened it.
|
|
278
|
+
*/
|
|
279
|
+
export interface NotificationCenterTriggerProps {
|
|
280
|
+
/** Returns a cleanup for the element under React 19 and later, which call it in place of `null`. */
|
|
281
|
+
readonly ref: (element: HTMLElement | null) => (() => void) | undefined;
|
|
282
|
+
readonly 'aria-expanded': boolean;
|
|
283
|
+
/** The panel's id while it is open. */
|
|
284
|
+
readonly 'aria-controls': string | undefined;
|
|
285
|
+
readonly 'aria-haspopup': 'dialog';
|
|
286
|
+
readonly onClick: (event?: { readonly currentTarget: EventTarget | null }) => void;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export interface UseNotificationCenterResult {
|
|
290
|
+
readonly open: boolean;
|
|
291
|
+
/** Opens or closes the center, as the app; a controlled center asks through `onOpenChange`. The same function every render. */
|
|
292
|
+
readonly setOpen: (open: boolean) => void;
|
|
293
|
+
readonly triggerProps: NotificationCenterTriggerProps;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** Public component and hook signatures. */
|
|
297
|
+
export type NotificationCenterComponent = (props: NotificationCenterProps) => ReactElement | null;
|
|
298
|
+
export type UseNotificationCenter = (options?: NotificationCenterOpenOptions) => UseNotificationCenterResult;
|
|
299
|
+
|
|
300
|
+
export { NotificationCenter, useNotificationCenter } from './react/center.js';
|
package/src/center.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The notification center's entry. It carries no client directive itself: the panel and the hook
|
|
2
|
+
// come through the client facade `center-react.ts`, as the root's React exports come through
|
|
3
|
+
// `react.ts`, so the subpath can be imported on the server. The root entry never imports this one,
|
|
4
|
+
// so an app without a center does not load its code.
|
|
5
|
+
//
|
|
6
|
+
// The center shows the app's own notifications: an array the app loads from wherever it likes and
|
|
7
|
+
// keeps up to date. It never changes the array itself; dismissing, clearing, actions and reading
|
|
8
|
+
// are callbacks, and the panel animates the difference between one array and the next.
|
|
9
|
+
|
|
10
|
+
export { NotificationCenter, useNotificationCenter } from './center-react.js';
|
|
11
|
+
export type {
|
|
12
|
+
NotificationCenterBackdropConstants,
|
|
13
|
+
NotificationCenterComponent,
|
|
14
|
+
NotificationCenterLabels,
|
|
15
|
+
NotificationCenterMotion,
|
|
16
|
+
NotificationCenterMotionConstants,
|
|
17
|
+
NotificationCenterMotionOverrides,
|
|
18
|
+
NotificationCenterOpenOptions,
|
|
19
|
+
NotificationCenterPart,
|
|
20
|
+
NotificationCenterProps,
|
|
21
|
+
NotificationCenterTriggerProps,
|
|
22
|
+
NotificationItem,
|
|
23
|
+
NotificationItemAction,
|
|
24
|
+
NotificationParts,
|
|
25
|
+
UseNotificationCenter,
|
|
26
|
+
UseNotificationCenterResult,
|
|
27
|
+
} from './center-react.js';
|
package/src/core/agent-port.ts
CHANGED
|
@@ -78,6 +78,8 @@ export interface ToasterPort<Slot> {
|
|
|
78
78
|
* events themselves only after its state listeners.
|
|
79
79
|
*/
|
|
80
80
|
readonly subscribe: (listener: (commit: PortCommit<Slot>) => void) => () => void;
|
|
81
|
+
/** Hands an error of the reader's own listeners to the controller's diagnostics, one each. */
|
|
82
|
+
readonly report: (error: unknown) => void;
|
|
81
83
|
/** Opens the Island on the top notification, unless a person is busy with the Toaster. */
|
|
82
84
|
readonly open: () => Changed | Rejection<'disposed' | 'no-host' | 'empty' | 'busy'>;
|
|
83
85
|
/** Collapses the Island, unless it holds a draft. */
|
|
@@ -144,6 +146,7 @@ export function createToasterPort<Slot>(controller: object): ToasterPort<Slot> {
|
|
|
144
146
|
closedBy: commit.closedBy,
|
|
145
147
|
});
|
|
146
148
|
}),
|
|
149
|
+
report: (error) => { k.failure('listener-error', 'subscribe', error); },
|
|
147
150
|
open() {
|
|
148
151
|
if (k.disposed) return reject('disposed');
|
|
149
152
|
if (k.lease === null) return reject('no-host');
|
package/src/react/card.tsx
CHANGED
|
@@ -73,7 +73,7 @@ function control(name: string, children: ReactNode, onClick: () => void, props?:
|
|
|
73
73
|
return <button type="button" data-cloudlet-control={name} {...props} onClick={onClick}>{children}</button>;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
function glyph(path: string) {
|
|
76
|
+
export function glyph(path: string) {
|
|
77
77
|
return <svg aria-hidden="true" viewBox="0 0 16 16"><path d={path} /></svg>;
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -87,7 +87,7 @@ export function partClasses(classNames: ToasterProps['classNames']): PartClass {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/** 16 px stroke glyphs drawn in the status colour. */
|
|
90
|
-
const glyphs: Record<Exclude<NotificationKind, 'loading'>, string> = {
|
|
90
|
+
export const glyphs: Record<Exclude<NotificationKind, 'loading'>, string> = {
|
|
91
91
|
success: 'M4.5 8.5 7 11l4.5-5.5',
|
|
92
92
|
error: 'm5.5 5.5 5 5m0-5-5 5',
|
|
93
93
|
info: 'M8 7.5V11M8 5h.01',
|
|
@@ -174,7 +174,7 @@ export function SlidePreview({ notification, labels, part }: { notification: Not
|
|
|
174
174
|
{content.action?.kind === 'command' && lookalike(content.action.label, true)}
|
|
175
175
|
{content.action?.kind === 'link' && <a className={`${action} cloudlet-primary`}>{content.action.label}</a>}
|
|
176
176
|
{retryable(operation) && lookalike(labels.retry)}
|
|
177
|
-
{operation?.canCancel && lookalike(
|
|
177
|
+
{operation?.canCancel && lookalike(labels.cancel)}
|
|
178
178
|
</div>
|
|
179
179
|
{notification.action.status === 'failed' && <p className="cloudlet-action-error">{notification.action.message}</p>}
|
|
180
180
|
</div>
|
|
@@ -213,9 +213,8 @@ export function Body({ notification, form, bridge, lease, labels, navigate, slot
|
|
|
213
213
|
onClick={event => activateLink(event.nativeEvent)}
|
|
214
214
|
onAuxClick={event => { if (event.button === 1) activateLink(event.nativeEvent); }}>{command.label}</a>}
|
|
215
215
|
{retryable(operation) && control('retry', labels.retry, () => bridge.retryOperation(lease, handle, operation.token), { className: neutral })}
|
|
216
|
-
{
|
|
217
|
-
|
|
218
|
-
}, { className: neutral, 'aria-disabled': operation.cancelRequested })}
|
|
216
|
+
{/* `canCancel` turns false once cancellation is requested: the button leaves and the title says so. */}
|
|
217
|
+
{operation?.canCancel && control('cancel', labels.cancel, () => bridge.cancelOperation(lease, handle, operation.token), { className: neutral })}
|
|
219
218
|
</div>
|
|
220
219
|
{action.status === 'failed' && <p id={`${id}-action-error`} className="cloudlet-action-error">{action.message}</p>}
|
|
221
220
|
</div>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { ReactElement, RefObject } from 'react';
|
|
3
|
+
import type { NotificationCenterBackdropConstants } from '../center-react.js';
|
|
4
|
+
import { watchAncestorAttribute } from './reduced.js';
|
|
5
|
+
|
|
6
|
+
// The notification center's backdrop: what lies under its column and a band past it, at the panel's
|
|
7
|
+
// edge. One container, the root's first drawn child, holds the backdrop's layers, the tone last; it
|
|
8
|
+
// stands at the edge for the panel's whole height and does not travel with the drawer. The layers
|
|
9
|
+
// fade in and out with the drawer's position, each on its own (the panel's motion writes one CSSOM
|
|
10
|
+
// rule for the container's children): the container itself never takes `opacity`, `filter`,
|
|
11
|
+
// `mask`, `clip-path` or `will-change`, nor does the root, since any of them would cut a blur in
|
|
12
|
+
// the container off from the page under it.
|
|
13
|
+
//
|
|
14
|
+
// `blur` draws blur layers under the tone; `tint` draws the tone; `none` draws nothing. The layers'
|
|
15
|
+
// boxes, radii and masks are CSS (styles.css) on the tokens of `.cloudlet-host`, so an app restyles
|
|
16
|
+
// the backdrop without JavaScript; the markup only says how many layers there are and which is
|
|
17
|
+
// which. A window narrower than 48rem and a browser without `backdrop-filter` hide the layers by
|
|
18
|
+
// CSS and keep the tone.
|
|
19
|
+
//
|
|
20
|
+
// Reduced transparency turns the blur into the tone, never the other way: the app's way is `tint`
|
|
21
|
+
// or `data-reduced-transparency` on any ancestor of the root (Safari does not tell the page the
|
|
22
|
+
// system's setting); the system's `prefers-reduced-transparency: reduce` is CSS.
|
|
23
|
+
|
|
24
|
+
/** The backdrop the app asks for: anything but `tint` and `none` is `blur`, the default. */
|
|
25
|
+
export type CenterBackdrop = 'blur' | 'tint' | 'none';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The `backdrop` prop as the panel uses it: `tint` and `none` as given, anything else `blur`; with
|
|
29
|
+
* the app's reduced transparency (`reduced`), `tint` in place of `blur`.
|
|
30
|
+
*/
|
|
31
|
+
export function resolveBackdrop(value: unknown, reduced = false): CenterBackdrop {
|
|
32
|
+
if (value === 'tint' || value === 'none') return value;
|
|
33
|
+
return reduced ? 'tint' : 'blur';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The ancestor attribute by which an app asks for reduced transparency, by presence. */
|
|
37
|
+
export const REDUCED_TRANSPARENCY = 'data-reduced-transparency';
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether an ancestor of the panel's root has `data-reduced-transparency`, live. The panel is
|
|
41
|
+
* portalled into `document.body`, so its first render reads from there and draws no blur layers it
|
|
42
|
+
* would drop at once; the root's own ancestors are watched once it is in the document.
|
|
43
|
+
*/
|
|
44
|
+
export function useReducedTransparency(rootRef: RefObject<HTMLElement | null>): boolean {
|
|
45
|
+
const [reduced, setReduced] = useState(() => typeof document !== 'undefined' && !!document.body?.closest(`[${REDUCED_TRANSPARENCY}]`));
|
|
46
|
+
useLayoutEffect(() => {
|
|
47
|
+
const root = rootRef.current;
|
|
48
|
+
if (!root) return;
|
|
49
|
+
const ancestors = watchAncestorAttribute(root, REDUCED_TRANSPARENCY, () => setReduced(ancestors.present()));
|
|
50
|
+
setReduced(ancestors.present());
|
|
51
|
+
return () => ancestors.dispose();
|
|
52
|
+
}, [rootRef]);
|
|
53
|
+
return reduced;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The backdrop's constants, the one place to change them: four blur layers (one if the frame budget
|
|
58
|
+
* asks for it) and the blur kept while the page scrolls under the center (`scrollTint: true` shows
|
|
59
|
+
* the tone alone then, see `BACKDROP_SCROLL`).
|
|
60
|
+
*/
|
|
61
|
+
export const CENTER_BACKDROP: NotificationCenterBackdropConstants = { layers: 4, scrollTint: false };
|
|
62
|
+
|
|
63
|
+
/** The most blur layers the styles place (each has its rule, see styles.css). */
|
|
64
|
+
export const MAX_BACKDROP_LAYERS = 8;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* True in an Apple engine (Safari on macOS, every browser on iOS and iPadOS): a stack of masked
|
|
68
|
+
* blur layers there shows a stale picture of the page, so it gets one layer.
|
|
69
|
+
*/
|
|
70
|
+
export function appleEngine(): boolean {
|
|
71
|
+
const vendor = typeof navigator === 'undefined' ? undefined : navigator.vendor;
|
|
72
|
+
return typeof vendor === 'string' && vendor.startsWith('Apple');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Resolves `NotificationCenterMotion.overrides.backdrop` (a debugging channel) onto the constants
|
|
77
|
+
* with the motion's contract: `layers` an integer from 1 to `MAX_BACKDROP_LAYERS`, `scrollTint` a
|
|
78
|
+
* boolean, anything else keeps its default; unknown keys are ignored. Without a valid `layers`, an
|
|
79
|
+
* Apple engine (`apple`) gets one layer.
|
|
80
|
+
*/
|
|
81
|
+
export function resolveCenterBackdrop(overrides: unknown, apple: boolean): NotificationCenterBackdropConstants {
|
|
82
|
+
const source = overrides && typeof overrides === 'object' ? overrides as Record<string, unknown> : {};
|
|
83
|
+
const layers = source.layers;
|
|
84
|
+
const scrollTint = source.scrollTint;
|
|
85
|
+
return {
|
|
86
|
+
layers: Number.isInteger(layers) && (layers as number) >= 1 && (layers as number) <= MAX_BACKDROP_LAYERS
|
|
87
|
+
? layers as number
|
|
88
|
+
: apple ? 1 : CENTER_BACKDROP.layers,
|
|
89
|
+
scrollTint: typeof scrollTint === 'boolean' ? scrollTint : CENTER_BACKDROP.scrollTint,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The CSSOM selector, under the panel's root, of every layer the drawer's position fades. */
|
|
94
|
+
export const BACKDROP_LAYERS = ' > .cloudlet-center-backdrop > *';
|
|
95
|
+
/** The CSSOM selector, under the panel's root, of the blur layers alone (the scroll tint fades them). */
|
|
96
|
+
export const BACKDROP_BLURS = ' > .cloudlet-center-backdrop > .cloudlet-center-blur';
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The scroll tint's numbers (`scrollTint`): the blur layers fade out over `fade` ms when the page
|
|
100
|
+
* starts to scroll under the open center and back in over `fade` ms once it has not scrolled for
|
|
101
|
+
* `idle` ms; the tone stays.
|
|
102
|
+
*/
|
|
103
|
+
export const BACKDROP_SCROLL = { fade: 120, idle: 150 } as const;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The scroll tint: while the center is `open` and `enabled` (blur layers and `scrollTint`), a scroll
|
|
107
|
+
* anywhere in the document but inside the panel (its own list) hides the blur layers through
|
|
108
|
+
* `setBlur` and shows them again after `BACKDROP_SCROLL.idle` ms without one. No listener and no
|
|
109
|
+
* timer otherwise.
|
|
110
|
+
* A center that closes keeps its blur layers hidden while it leaves; one that opens again, or turns
|
|
111
|
+
* the tint off while open, shows them.
|
|
112
|
+
*/
|
|
113
|
+
export function useScrollTint(open: boolean, enabled: boolean, rootRef: RefObject<HTMLElement | null>, setBlur: (shown: boolean) => void): void {
|
|
114
|
+
const hidden = useRef(false);
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
const show = () => { if (hidden.current) { hidden.current = false; setBlur(true); } };
|
|
117
|
+
if (!open) return;
|
|
118
|
+
show();
|
|
119
|
+
const root = rootRef.current;
|
|
120
|
+
if (!enabled || !root) return;
|
|
121
|
+
const doc = root.ownerDocument;
|
|
122
|
+
const view = doc.defaultView!;
|
|
123
|
+
let timer: number | undefined;
|
|
124
|
+
const onScroll = (event: Event) => {
|
|
125
|
+
const target = event.target;
|
|
126
|
+
if (target instanceof view.Node && root.contains(target)) return;
|
|
127
|
+
if (!hidden.current) { hidden.current = true; setBlur(false); }
|
|
128
|
+
if (timer !== undefined) view.clearTimeout(timer);
|
|
129
|
+
timer = view.setTimeout(() => { timer = undefined; show(); }, BACKDROP_SCROLL.idle);
|
|
130
|
+
};
|
|
131
|
+
doc.addEventListener('scroll', onScroll, { capture: true, passive: true });
|
|
132
|
+
return () => {
|
|
133
|
+
doc.removeEventListener('scroll', onScroll, { capture: true });
|
|
134
|
+
if (timer !== undefined) view.clearTimeout(timer);
|
|
135
|
+
};
|
|
136
|
+
}, [open, enabled, rootRef, setBlur]);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The backdrop's container and its layers; nothing for `none`. Hidden from assistive technology and
|
|
141
|
+
* the pointer. With `blur`, `layers` blur layers come first, the weakest (nearest the column's inner
|
|
142
|
+
* side) first and the strongest, at the edge, last: `data-cloudlet-layer` counts from the edge (0).
|
|
143
|
+
*/
|
|
144
|
+
export function CenterBackdropLayers({ backdrop, layers }: { readonly backdrop: CenterBackdrop; readonly layers: number }): ReactElement | null {
|
|
145
|
+
if (backdrop === 'none') return null;
|
|
146
|
+
const blur = backdrop === 'blur' ? Array.from({ length: layers }, (_, index) => layers - 1 - index) : [];
|
|
147
|
+
return <div className="cloudlet-center-backdrop" aria-hidden="true">
|
|
148
|
+
{blur.map(level => <div key={level} className="cloudlet-center-blur" data-cloudlet-layer={level} />)}
|
|
149
|
+
<div className="cloudlet-center-tone" />
|
|
150
|
+
</div>;
|
|
151
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
// The notification center's two-step clear buttons: "Clear all" in the panel's header, "Clear" in
|
|
4
|
+
// an expanded group's heading row and the clear button of a collapsed stack. The first press arms a
|
|
5
|
+
// button and shows what the second one clears; a second press within `CLEAR_CONFIRM` clears. A
|
|
6
|
+
// button disarms when its time runs out, when focus leaves it and when it leaves the panel (the group
|
|
7
|
+
// collapsed or expanded, shrank to one card, the center to one notification, the center closed).
|
|
8
|
+
// One button is armed at a time: arming another disarms the first.
|
|
9
|
+
|
|
10
|
+
/** How long an armed button waits for its second press, as the Island's Clear all. */
|
|
11
|
+
export const CLEAR_CONFIRM = 3000;
|
|
12
|
+
|
|
13
|
+
export interface TwoStepClear {
|
|
14
|
+
/** The id of the armed button, if any. */
|
|
15
|
+
readonly armed: string | null;
|
|
16
|
+
/** A press of the button `id`: arms it, or runs `clear` when it is armed already. */
|
|
17
|
+
readonly press: (id: string, clear: () => void) => void;
|
|
18
|
+
/** Disarms the button `id` (focus left it), or whichever is armed without an id. */
|
|
19
|
+
readonly disarm: (id?: string) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The armed state of the panel's two-step buttons. `present` names the buttons the panel shows in
|
|
24
|
+
* this render; `active` is false while the center is closed (or closing).
|
|
25
|
+
*/
|
|
26
|
+
export function useTwoStepClear(present: readonly string[], active: boolean): TwoStepClear {
|
|
27
|
+
const [armed, setArmed] = useState<string | null>(null);
|
|
28
|
+
const current = useRef<string | null>(null);
|
|
29
|
+
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
30
|
+
const set = useCallback((id: string | null) => {
|
|
31
|
+
clearTimeout(timer.current);
|
|
32
|
+
timer.current = undefined;
|
|
33
|
+
current.current = id;
|
|
34
|
+
setArmed(id);
|
|
35
|
+
}, []);
|
|
36
|
+
const disarm = useCallback((id?: string) => {
|
|
37
|
+
if (current.current === null || (id !== undefined && current.current !== id)) return;
|
|
38
|
+
set(null);
|
|
39
|
+
}, [set]);
|
|
40
|
+
const press = useCallback((id: string, clear: () => void) => {
|
|
41
|
+
if (current.current === id) {
|
|
42
|
+
set(null);
|
|
43
|
+
clear();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
set(id);
|
|
47
|
+
timer.current = setTimeout(() => disarm(id), CLEAR_CONFIRM);
|
|
48
|
+
}, [set, disarm]);
|
|
49
|
+
// The armed button left with this commit: the next one starts unarmed.
|
|
50
|
+
useLayoutEffect(() => {
|
|
51
|
+
const id = current.current;
|
|
52
|
+
if (id !== null && (!active || !present.includes(id))) set(null);
|
|
53
|
+
});
|
|
54
|
+
useEffect(() => () => clearTimeout(timer.current), []);
|
|
55
|
+
return { armed, press, disarm };
|
|
56
|
+
}
|