@onekeyfe/react-native-native-overlay 6.8.0-alpha.514 → 6.8.0-alpha.538

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.
Files changed (79) hide show
  1. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayHost.kt +37 -5
  2. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayPackage.kt +1 -1
  3. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastCardView.kt +869 -0
  4. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToastSpec.kt +327 -0
  5. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterView.kt +574 -0
  6. package/android/src/main/java/com/onekey/nativeoverlay/NativeOverlayToasterViewManager.kt +54 -0
  7. package/android/src/test/java/com/onekey/nativeoverlay/NativeOverlayAccessibilityIsolationTest.kt +172 -0
  8. package/docs/SPEC.md +172 -11
  9. package/ios/NativeOverlayContainerView.swift +20 -4
  10. package/ios/NativeOverlayToastCardView.swift +572 -0
  11. package/ios/NativeOverlayToastSpec.swift +356 -0
  12. package/ios/NativeOverlayToasterView.swift +519 -0
  13. package/ios/RCTNativeOverlayToasterComponentView.h +11 -0
  14. package/ios/RCTNativeOverlayToasterComponentView.mm +85 -0
  15. package/lib/module/NativeOverlayToasterNativeComponent.ts +24 -0
  16. package/lib/module/OverlayStore.js +20 -3
  17. package/lib/module/OverlayStore.js.map +1 -1
  18. package/lib/module/OverlayView.web.js +9 -18
  19. package/lib/module/OverlayView.web.js.map +1 -1
  20. package/lib/module/index.js +2 -0
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/module/toast/OverlayToastHost.js +42 -0
  23. package/lib/module/toast/OverlayToastHost.js.map +1 -0
  24. package/lib/module/toast/OverlayToastHost.web.js +32 -0
  25. package/lib/module/toast/OverlayToastHost.web.js.map +1 -0
  26. package/lib/module/toast/OverlayToastStore.js +217 -0
  27. package/lib/module/toast/OverlayToastStore.js.map +1 -0
  28. package/lib/module/toast/toastBridge.js +65 -0
  29. package/lib/module/toast/toastBridge.js.map +1 -0
  30. package/lib/module/toast/toastDomContent.web.js +298 -0
  31. package/lib/module/toast/toastDomContent.web.js.map +1 -0
  32. package/lib/module/toast/toastDomRenderer.web.js +695 -0
  33. package/lib/module/toast/toastDomRenderer.web.js.map +1 -0
  34. package/lib/module/toast/toastStyles.web.js +97 -0
  35. package/lib/module/toast/toastStyles.web.js.map +1 -0
  36. package/lib/module/toast/types.js +2 -0
  37. package/lib/module/toast/types.js.map +1 -0
  38. package/lib/module/web/overlayLayers.js +82 -0
  39. package/lib/module/web/overlayLayers.js.map +1 -1
  40. package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts +19 -0
  41. package/lib/typescript/src/NativeOverlayToasterNativeComponent.d.ts.map +1 -0
  42. package/lib/typescript/src/OverlayStore.d.ts +8 -1
  43. package/lib/typescript/src/OverlayStore.d.ts.map +1 -1
  44. package/lib/typescript/src/OverlayView.web.d.ts.map +1 -1
  45. package/lib/typescript/src/index.d.ts +4 -0
  46. package/lib/typescript/src/index.d.ts.map +1 -1
  47. package/lib/typescript/src/toast/OverlayToastHost.d.ts +6 -0
  48. package/lib/typescript/src/toast/OverlayToastHost.d.ts.map +1 -0
  49. package/lib/typescript/src/toast/OverlayToastHost.web.d.ts +6 -0
  50. package/lib/typescript/src/toast/OverlayToastHost.web.d.ts.map +1 -0
  51. package/lib/typescript/src/toast/OverlayToastStore.d.ts +47 -0
  52. package/lib/typescript/src/toast/OverlayToastStore.d.ts.map +1 -0
  53. package/lib/typescript/src/toast/toastBridge.d.ts +11 -0
  54. package/lib/typescript/src/toast/toastBridge.d.ts.map +1 -0
  55. package/lib/typescript/src/toast/toastDomContent.web.d.ts +15 -0
  56. package/lib/typescript/src/toast/toastDomContent.web.d.ts.map +1 -0
  57. package/lib/typescript/src/toast/toastDomRenderer.web.d.ts +8 -0
  58. package/lib/typescript/src/toast/toastDomRenderer.web.d.ts.map +1 -0
  59. package/lib/typescript/src/toast/toastStyles.web.d.ts +2 -0
  60. package/lib/typescript/src/toast/toastStyles.web.d.ts.map +1 -0
  61. package/lib/typescript/src/toast/types.d.ts +240 -0
  62. package/lib/typescript/src/toast/types.d.ts.map +1 -0
  63. package/lib/typescript/src/web/overlayLayers.d.ts +2 -0
  64. package/lib/typescript/src/web/overlayLayers.d.ts.map +1 -1
  65. package/package.json +3 -2
  66. package/react-native.config.js +1 -0
  67. package/src/NativeOverlayToasterNativeComponent.ts +24 -0
  68. package/src/OverlayStore.ts +35 -4
  69. package/src/OverlayView.web.tsx +10 -11
  70. package/src/index.ts +23 -0
  71. package/src/toast/OverlayToastHost.tsx +62 -0
  72. package/src/toast/OverlayToastHost.web.tsx +29 -0
  73. package/src/toast/OverlayToastStore.ts +254 -0
  74. package/src/toast/toastBridge.ts +62 -0
  75. package/src/toast/toastDomContent.web.ts +351 -0
  76. package/src/toast/toastDomRenderer.web.ts +856 -0
  77. package/src/toast/toastStyles.web.ts +97 -0
  78. package/src/toast/types.ts +251 -0
  79. package/src/web/overlayLayers.ts +114 -0
@@ -0,0 +1,254 @@
1
+ import type {
2
+ IOverlayToastDismissReason,
3
+ IOverlayToastEvent,
4
+ IOverlayToastHandle,
5
+ IOverlayToastHandlers,
6
+ IOverlayToastItem,
7
+ IOverlayToastSpec,
8
+ IOverlayToasterConfig,
9
+ } from './types';
10
+
11
+ export interface IOverlayToastSnapshot {
12
+ /** Newest first. */
13
+ items: readonly IOverlayToastItem[];
14
+ config: IOverlayToasterConfig;
15
+ }
16
+
17
+ /** Toasts beyond this many are dropped oldest first. */
18
+ const TOAST_LIMIT = 20;
19
+
20
+ /**
21
+ * The single source of truth for message toasts. Callers push data in; a
22
+ * platform renderer (attached by `OverlayToastHost`) draws it and reports
23
+ * what happened to each card through `handleEvent`.
24
+ *
25
+ * A toast leaves the list only when its renderer reports `close`, so the exit
26
+ * animation always has a card to play on. With no renderer attached there is
27
+ * no animation to wait for and a dismissal removes the toast at once.
28
+ */
29
+ export class OverlayToastStore {
30
+ private items: IOverlayToastItem[] = [];
31
+
32
+ private config: IOverlayToasterConfig = {};
33
+
34
+ private snapshot: IOverlayToastSnapshot = { items: [], config: {} };
35
+
36
+ private readonly handlers = new Map<string, IOverlayToastHandlers>();
37
+
38
+ private readonly dismissReasons = new Map<
39
+ string,
40
+ IOverlayToastDismissReason
41
+ >();
42
+
43
+ private readonly listeners = new Set<() => void>();
44
+
45
+ private renderers = 0;
46
+
47
+ private nextId = 0;
48
+
49
+ private nextGeneration = 0;
50
+
51
+ getSnapshot = (): IOverlayToastSnapshot => this.snapshot;
52
+
53
+ subscribe = (listener: () => void): (() => void) => {
54
+ this.listeners.add(listener);
55
+ return () => {
56
+ this.listeners.delete(listener);
57
+ };
58
+ };
59
+
60
+ /** A renderer is drawing the list; returns its detach function. */
61
+ attachRenderer(): () => void {
62
+ this.renderers += 1;
63
+ let attached = true;
64
+ return () => {
65
+ if (!attached) {
66
+ return;
67
+ }
68
+ attached = false;
69
+ this.renderers -= 1;
70
+ if (this.renderers === 0) {
71
+ // Nothing can report `close` any more: finish pending exits.
72
+ this.items
73
+ .filter((item) => !item.visible)
74
+ .forEach((item) =>
75
+ this.remove(
76
+ item.spec.id,
77
+ this.dismissReasons.get(item.spec.id) ?? 'programmatic'
78
+ )
79
+ );
80
+ }
81
+ };
82
+ }
83
+
84
+ createId(): string {
85
+ this.nextId += 1;
86
+ return `overlay-toast-${this.nextId}`;
87
+ }
88
+
89
+ show(
90
+ spec: IOverlayToastSpec,
91
+ handlers: IOverlayToastHandlers = {}
92
+ ): IOverlayToastHandle {
93
+ const { id } = spec;
94
+ const generation = this.nextGeneration;
95
+ this.nextGeneration += 1;
96
+ const existing = this.items.find((item) => item.spec.id === id);
97
+ this.handlers.set(id, handlers);
98
+ this.dismissReasons.delete(id);
99
+ if (existing) {
100
+ this.items = this.items.map((item) =>
101
+ item === existing
102
+ ? {
103
+ spec,
104
+ visible: true,
105
+ revision: item.revision + 1,
106
+ generation,
107
+ }
108
+ : item
109
+ );
110
+ } else {
111
+ this.items = [
112
+ { spec, visible: true, revision: 0, generation },
113
+ ...this.items,
114
+ ];
115
+ // Past the limit the oldest toasts leave without an animation, as the
116
+ // list they came from did.
117
+ this.items.slice(TOAST_LIMIT).forEach((item) => {
118
+ this.handlers.delete(item.spec.id);
119
+ this.dismissReasons.delete(item.spec.id);
120
+ });
121
+ this.items = this.items.slice(0, TOAST_LIMIT);
122
+ }
123
+ this.emit();
124
+ return { id, dismiss: () => this.dismiss(id) };
125
+ }
126
+
127
+ /**
128
+ * Redraws a toast that is on screen with a new spec (a theme or layout
129
+ * change) without restarting its lifetime. Unknown or leaving ids are
130
+ * ignored.
131
+ */
132
+ update(spec: IOverlayToastSpec): void {
133
+ const existing = this.items.find((item) => item.spec.id === spec.id);
134
+ if (!existing || !existing.visible) {
135
+ return;
136
+ }
137
+ this.items = this.items.map((item) =>
138
+ item === existing ? { ...item, spec, revision: item.revision + 1 } : item
139
+ );
140
+ this.emit();
141
+ }
142
+
143
+ dismiss(id: string): void {
144
+ const item = this.items.find((entry) => entry.spec.id === id);
145
+ if (!item || !item.visible) {
146
+ return;
147
+ }
148
+ this.items = this.items.map((entry) =>
149
+ entry === item ? { ...entry, visible: false } : entry
150
+ );
151
+ this.dismissReasons.set(id, 'programmatic');
152
+ this.handlers.get(id)?.onDismiss?.('programmatic');
153
+ if (this.renderers === 0) {
154
+ this.remove(id, 'programmatic');
155
+ return;
156
+ }
157
+ this.emit();
158
+ }
159
+
160
+ dismissAll(): void {
161
+ this.items.map((item) => item.spec.id).forEach((id) => this.dismiss(id));
162
+ }
163
+
164
+ configure(config: IOverlayToasterConfig): void {
165
+ const next = { ...this.config, ...config };
166
+ const keys = Object.keys(next) as (keyof IOverlayToasterConfig)[];
167
+ if (keys.every((key) => next[key] === this.config[key])) {
168
+ return;
169
+ }
170
+ this.config = next;
171
+ this.emit();
172
+ }
173
+
174
+ /** Renderer → store. */
175
+ handleEvent(event: IOverlayToastEvent): void {
176
+ const item = this.items.find((entry) => entry.spec.id === event.id);
177
+ if (
178
+ !item ||
179
+ (event.generation !== undefined && event.generation !== item.generation)
180
+ ) {
181
+ return;
182
+ }
183
+ const handlers = this.handlers.get(event.id);
184
+ switch (event.type) {
185
+ case 'dismiss': {
186
+ if (!item.visible) {
187
+ // A caller already dismissed it and was told so.
188
+ return;
189
+ }
190
+ this.items = this.items.map((entry) =>
191
+ entry === item ? { ...entry, visible: false } : entry
192
+ );
193
+ this.dismissReasons.set(event.id, event.reason);
194
+ handlers?.onDismiss?.(event.reason);
195
+ this.emit();
196
+ return;
197
+ }
198
+ case 'close':
199
+ this.remove(event.id, event.reason, event.generation);
200
+ return;
201
+ case 'action':
202
+ if (!item.visible) {
203
+ return;
204
+ }
205
+ handlers?.onAction?.(event.actionId);
206
+ return;
207
+ case 'link':
208
+ if (!item.visible) {
209
+ return;
210
+ }
211
+ handlers?.onLink?.(event.link);
212
+ return;
213
+ case 'press':
214
+ if (!item.visible) {
215
+ return;
216
+ }
217
+ handlers?.onPress?.();
218
+ return;
219
+ default:
220
+ }
221
+ }
222
+
223
+ private remove(
224
+ id: string,
225
+ reason: IOverlayToastDismissReason,
226
+ generation?: number
227
+ ): void {
228
+ const item = this.items.find((entry) => entry.spec.id === id);
229
+ if (!item) {
230
+ return;
231
+ }
232
+ // A toast shown again under the same id while its predecessor was
233
+ // leaving stays: the renderer's `close` belongs to the old card.
234
+ if (
235
+ item.visible ||
236
+ (generation !== undefined && generation !== item.generation)
237
+ ) {
238
+ return;
239
+ }
240
+ const handlers = this.handlers.get(id);
241
+ this.handlers.delete(id);
242
+ this.dismissReasons.delete(id);
243
+ this.items = this.items.filter((entry) => entry !== item);
244
+ this.emit();
245
+ handlers?.onClose?.(reason);
246
+ }
247
+
248
+ private emit(): void {
249
+ this.snapshot = { items: this.items, config: this.config };
250
+ this.listeners.forEach((listener) => listener());
251
+ }
252
+ }
253
+
254
+ export const overlayToastStore = new OverlayToastStore();
@@ -0,0 +1,62 @@
1
+ import type {
2
+ IOverlayToastDismissReason,
3
+ IOverlayToastEvent,
4
+ IOverlayToastItem,
5
+ } from './types';
6
+
7
+ /** How a toast that never times out crosses the bridge: JSON has no Infinity. */
8
+ export const TOAST_DURATION_INFINITE = -1;
9
+
10
+ const DISMISS_REASONS: readonly IOverlayToastDismissReason[] = [
11
+ 'timeout',
12
+ 'swipe',
13
+ 'close',
14
+ 'programmatic',
15
+ ];
16
+
17
+ export function serializeToastItems(
18
+ items: readonly IOverlayToastItem[]
19
+ ): string {
20
+ return JSON.stringify(
21
+ items.map((item) => ({
22
+ ...item,
23
+ spec: {
24
+ ...item.spec,
25
+ duration: Number.isFinite(item.spec.duration)
26
+ ? item.spec.duration
27
+ : TOAST_DURATION_INFINITE,
28
+ },
29
+ }))
30
+ );
31
+ }
32
+
33
+ function toDismissReason(value: string): IOverlayToastDismissReason {
34
+ return DISMISS_REASONS.find((reason) => reason === value) ?? 'programmatic';
35
+ }
36
+
37
+ export function toToastEvent(native: {
38
+ toastId: string;
39
+ generation?: number;
40
+ type: string;
41
+ value: string;
42
+ }): IOverlayToastEvent | undefined {
43
+ const { toastId: id, generation, type, value } = native;
44
+ const base =
45
+ typeof generation === 'number' && Number.isFinite(generation)
46
+ ? { id, generation }
47
+ : { id };
48
+ switch (type) {
49
+ case 'dismiss':
50
+ return { ...base, type, reason: toDismissReason(value) };
51
+ case 'close':
52
+ return { ...base, type, reason: toDismissReason(value) };
53
+ case 'action':
54
+ return { ...base, type, actionId: value };
55
+ case 'link':
56
+ return { ...base, type, link: value };
57
+ case 'press':
58
+ return { ...base, type };
59
+ default:
60
+ return undefined;
61
+ }
62
+ }
@@ -0,0 +1,351 @@
1
+ /* cspell:ignore dasharray dashoffset */
2
+ import type {
3
+ IOverlayToastAction,
4
+ IOverlayToastIcon,
5
+ IOverlayToastSpec,
6
+ IOverlayToastText,
7
+ IOverlayToastTextRun,
8
+ } from './types';
9
+
10
+ const SVG_NS = 'http://www.w3.org/2000/svg';
11
+
12
+ // The card geometry of the layouts this replaces.
13
+ const MESSAGE_ICON_GAP = 8;
14
+ const MESSAGE_TEXT_GAP = 4;
15
+ const NOTIFICATION_GAP = 8;
16
+ const NOTIFICATION_TEXT_GAP = 2;
17
+ const ACTIONS_PADDING_TOP = 12;
18
+ const ACTIONS_PADDING_END = 2;
19
+ const ACTIONS_PADDING_BOTTOM = 2;
20
+
21
+ export type IToastContentEvent =
22
+ | { type: 'action'; actionId: string }
23
+ | { type: 'link'; link: string }
24
+ | { type: 'press' };
25
+
26
+ type IEmit = (event: IToastContentEvent) => void;
27
+
28
+ function element(tag: string, marker: string): HTMLElement {
29
+ const node = document.createElement(tag);
30
+ node.setAttribute(marker, '');
31
+ return node;
32
+ }
33
+
34
+ function createIcon(icon: IOverlayToastIcon): Element {
35
+ if (icon.kind === 'image') {
36
+ const image = document.createElement('img');
37
+ image.setAttribute('data-okt-image', '');
38
+ image.src = icon.uri;
39
+ image.alt = '';
40
+ image.draggable = false;
41
+ image.width = icon.size;
42
+ image.height = icon.size;
43
+ image.style.width = `${icon.size}px`;
44
+ image.style.height = `${icon.size}px`;
45
+ if (icon.borderRadius) {
46
+ image.style.borderRadius = `${icon.borderRadius}px`;
47
+ }
48
+ return image;
49
+ }
50
+ const svg = document.createElementNS(SVG_NS, 'svg');
51
+ svg.setAttribute('width', String(icon.size));
52
+ svg.setAttribute('height', String(icon.size));
53
+ svg.setAttribute('aria-hidden', 'true');
54
+ svg.setAttribute('focusable', 'false');
55
+ svg.style.color = icon.color;
56
+ if (icon.kind === 'spinner') {
57
+ // The indeterminate ring of the activity indicator this replaces.
58
+ svg.setAttribute('data-okt-spinner', '');
59
+ svg.style.color = '';
60
+ svg.setAttribute('viewBox', '0 0 32 32');
61
+ const track = document.createElementNS(SVG_NS, 'circle');
62
+ const arc = document.createElementNS(SVG_NS, 'circle');
63
+ [track, arc].forEach((circle) => {
64
+ circle.setAttribute('cx', '16');
65
+ circle.setAttribute('cy', '16');
66
+ circle.setAttribute('r', '14');
67
+ circle.setAttribute('fill', 'none');
68
+ circle.setAttribute('stroke-width', '4');
69
+ circle.style.stroke = icon.color;
70
+ });
71
+ track.style.opacity = '0.2';
72
+ arc.style.strokeDasharray = '80';
73
+ arc.style.strokeDashoffset = '60';
74
+ svg.append(track, arc);
75
+ return svg;
76
+ }
77
+ svg.setAttribute('data-okt-icon', '');
78
+ if (icon.href) {
79
+ const use = document.createElementNS(SVG_NS, 'use');
80
+ use.setAttribute('href', icon.href);
81
+ svg.append(use);
82
+ }
83
+ return svg;
84
+ }
85
+
86
+ function applyRunStyle(node: HTMLElement, run: IOverlayToastTextRun) {
87
+ if (run.color) {
88
+ node.style.color = run.color;
89
+ }
90
+ if (run.fontWeight) {
91
+ node.style.fontWeight = run.fontWeight;
92
+ }
93
+ if (run.fontSize !== undefined) {
94
+ node.style.fontSize = `${run.fontSize}px`;
95
+ }
96
+ if (run.underline) {
97
+ node.style.textDecorationLine = 'underline';
98
+ }
99
+ }
100
+
101
+ function isPlainRun(run: IOverlayToastTextRun): boolean {
102
+ return (
103
+ !run.color &&
104
+ !run.fontWeight &&
105
+ run.fontSize === undefined &&
106
+ !run.underline &&
107
+ run.link === undefined
108
+ );
109
+ }
110
+
111
+ function createParagraph(
112
+ text: IOverlayToastText,
113
+ runs: IOverlayToastTextRun[],
114
+ emit: IEmit
115
+ ): HTMLElement {
116
+ const paragraph = element('span', 'data-okt-text');
117
+ paragraph.style.color = text.color;
118
+ paragraph.style.fontSize = `${text.fontSize}px`;
119
+ paragraph.style.lineHeight = `${text.lineHeight}px`;
120
+ paragraph.style.fontWeight = text.fontWeight;
121
+ if (text.fontFamily) {
122
+ paragraph.style.fontFamily = text.fontFamily;
123
+ }
124
+ if (text.letterSpacing !== undefined) {
125
+ paragraph.style.letterSpacing = `${text.letterSpacing}px`;
126
+ }
127
+ if (text.selectable) {
128
+ paragraph.setAttribute('data-okt-selectable', '');
129
+ }
130
+ if (text.tabularNumbers) {
131
+ paragraph.style.fontVariantNumeric = 'tabular-nums';
132
+ }
133
+ if (text.linkHoverColor) {
134
+ paragraph.style.setProperty('--okt-link-hover', text.linkHoverColor);
135
+ }
136
+ if (text.linkPressedColor) {
137
+ paragraph.style.setProperty('--okt-link-active', text.linkPressedColor);
138
+ }
139
+ if (text.numberOfLines) {
140
+ paragraph.setAttribute('data-okt-clamp', '');
141
+ paragraph.style.setProperty(
142
+ '-webkit-line-clamp',
143
+ String(text.numberOfLines)
144
+ );
145
+ }
146
+ runs.forEach((run) => {
147
+ if (isPlainRun(run)) {
148
+ paragraph.append(document.createTextNode(run.text));
149
+ return;
150
+ }
151
+ const span = element('span', 'data-okt-text');
152
+ span.textContent = run.text;
153
+ applyRunStyle(span, run);
154
+ const { link } = run;
155
+ if (link !== undefined) {
156
+ span.setAttribute('data-okt-link', '');
157
+ span.setAttribute('role', 'link');
158
+ span.tabIndex = 0;
159
+ const open = (event: Event) => {
160
+ event.stopPropagation();
161
+ emit({ type: 'link', link });
162
+ };
163
+ span.addEventListener('click', open);
164
+ span.addEventListener('keydown', (event) => {
165
+ if (event.key === 'Enter') {
166
+ open(event);
167
+ }
168
+ });
169
+ }
170
+ paragraph.append(span);
171
+ });
172
+ return paragraph;
173
+ }
174
+
175
+ function createParagraphs(text: IOverlayToastText, emit: IEmit): HTMLElement {
176
+ const container = element('div', 'data-okt-paragraphs');
177
+ text.paragraphs.forEach((runs) =>
178
+ container.append(createParagraph(text, runs, emit))
179
+ );
180
+ return container;
181
+ }
182
+
183
+ function createAction(action: IOverlayToastAction, emit: IEmit): HTMLElement {
184
+ const button = document.createElement('button');
185
+ button.type = 'button';
186
+ button.setAttribute('data-okt-action', '');
187
+ if (action.testID) {
188
+ button.setAttribute('data-testid', action.testID);
189
+ }
190
+ if (action.icon) {
191
+ const icon = createIcon(action.icon) as SVGElement | HTMLElement;
192
+ icon.style.marginRight = `${action.iconGap ?? 0}px`;
193
+ button.append(icon);
194
+ }
195
+ button.append(document.createTextNode(action.label));
196
+ button.disabled = !!action.disabled;
197
+ const { style } = button;
198
+ style.setProperty('--okt-action-bg', action.backgroundColor);
199
+ if (action.hoverBackgroundColor) {
200
+ style.setProperty('--okt-action-hover', action.hoverBackgroundColor);
201
+ }
202
+ if (action.pressedBackgroundColor) {
203
+ style.setProperty('--okt-action-active', action.pressedBackgroundColor);
204
+ }
205
+ if (action.focusRingColor) {
206
+ style.setProperty('--okt-action-ring', action.focusRingColor);
207
+ }
208
+ style.color = action.color;
209
+ style.fontSize = `${action.fontSize}px`;
210
+ style.lineHeight = `${action.lineHeight}px`;
211
+ style.fontWeight = action.fontWeight;
212
+ if (action.fontFamily) {
213
+ style.fontFamily = action.fontFamily;
214
+ }
215
+ style.padding = `${action.paddingVertical}px ${action.paddingHorizontal}px`;
216
+ style.borderRadius = `${action.borderRadius}px`;
217
+ style.borderWidth = `${action.borderWidth ?? 0}px`;
218
+ if (action.marginHorizontal || action.marginVertical) {
219
+ style.margin = `${action.marginVertical ?? 0}px ${
220
+ action.marginHorizontal ?? 0
221
+ }px`;
222
+ }
223
+ if (action.disabled && action.disabledOpacity !== undefined) {
224
+ style.opacity = String(action.disabledOpacity);
225
+ }
226
+ button.addEventListener('click', (event) => {
227
+ // A press on an action never reaches the row under it.
228
+ event.stopPropagation();
229
+ if (!action.disabled) {
230
+ emit({ type: 'action', actionId: action.id });
231
+ }
232
+ });
233
+ return button;
234
+ }
235
+
236
+ function createActions(
237
+ actions: IOverlayToastAction[],
238
+ align: 'left' | 'right',
239
+ padding: string,
240
+ emit: IEmit
241
+ ): HTMLElement {
242
+ const row = element('div', 'data-okt-actions');
243
+ row.style.justifyContent = align === 'left' ? 'flex-start' : 'flex-end';
244
+ row.style.padding = padding;
245
+ actions.forEach((action) => row.append(createAction(action, emit)));
246
+ return row;
247
+ }
248
+
249
+ /** Builds the body of one card. The caller owns the card element. */
250
+ export function buildToastContent(
251
+ spec: IOverlayToastSpec,
252
+ emit: IEmit
253
+ ): HTMLElement {
254
+ if (spec.layout === 'notification') {
255
+ const row = element('div', 'data-okt-row');
256
+ row.style.flex = '1 1 auto';
257
+ row.style.gap = `${NOTIFICATION_GAP}px`;
258
+ if (spec.pressable) {
259
+ row.setAttribute('data-okt-pressable', '');
260
+ row.setAttribute('role', 'button');
261
+ row.tabIndex = 0;
262
+ row.addEventListener('click', (event) => {
263
+ const target = event.target as HTMLElement | null;
264
+ if (target?.closest?.('button, [data-okt-link]')) {
265
+ return;
266
+ }
267
+ emit({ type: 'press' });
268
+ });
269
+ row.addEventListener('keydown', (event) => {
270
+ const target = event.target as HTMLElement | null;
271
+ if (
272
+ target &&
273
+ target !== row &&
274
+ target.closest?.('button, [data-okt-link]')
275
+ ) {
276
+ return;
277
+ }
278
+ if (event.key === 'Enter' || event.key === ' ') {
279
+ event.preventDefault();
280
+ emit({ type: 'press' });
281
+ }
282
+ });
283
+ }
284
+ const badge = element('div', 'data-okt-badge');
285
+ badge.style.width = `${spec.badgeSize}px`;
286
+ badge.style.height = `${spec.badgeSize}px`;
287
+ badge.style.backgroundColor = spec.badgeColor;
288
+ badge.append(createIcon(spec.icon));
289
+ const column = element('div', 'data-okt-col');
290
+ column.style.gap = `${NOTIFICATION_TEXT_GAP}px`;
291
+ [spec.title, spec.message].forEach((text) => {
292
+ if (text) {
293
+ text.paragraphs.forEach((runs) =>
294
+ column.append(createParagraph(text, runs, emit))
295
+ );
296
+ }
297
+ });
298
+ if (spec.actions?.length) {
299
+ column.append(
300
+ createActions(
301
+ spec.actions,
302
+ spec.actionsAlign ?? 'right',
303
+ `${ACTIONS_PADDING_TOP}px 0 0 0`,
304
+ emit
305
+ )
306
+ );
307
+ }
308
+ row.append(badge, column);
309
+ if (spec.image) {
310
+ row.append(
311
+ createIcon({
312
+ kind: 'image',
313
+ uri: spec.image.uri,
314
+ size: spec.image.size,
315
+ borderRadius: spec.image.borderRadius,
316
+ })
317
+ );
318
+ }
319
+ return row;
320
+ }
321
+
322
+ const root = element('div', 'data-okt-root');
323
+ const row = element('div', 'data-okt-row');
324
+ if (spec.icon) {
325
+ row.style.gap = `${MESSAGE_ICON_GAP}px`;
326
+ const box = element('div', 'data-okt-icon-box');
327
+ box.append(createIcon(spec.icon));
328
+ row.append(box);
329
+ }
330
+ const column = element('div', 'data-okt-col');
331
+ column.style.gap = `${MESSAGE_TEXT_GAP}px`;
332
+ if (spec.title) {
333
+ column.append(createParagraphs(spec.title, emit));
334
+ }
335
+ if (spec.message) {
336
+ column.append(createParagraphs(spec.message, emit));
337
+ }
338
+ if (spec.actions?.length) {
339
+ column.append(
340
+ createActions(
341
+ spec.actions,
342
+ spec.actionsAlign ?? 'right',
343
+ `${ACTIONS_PADDING_TOP}px ${ACTIONS_PADDING_END}px ${ACTIONS_PADDING_BOTTOM}px 0`,
344
+ emit
345
+ )
346
+ );
347
+ }
348
+ row.append(column);
349
+ root.append(row);
350
+ return root;
351
+ }