akanjs 3.3.0-rc.34 → 3.3.0-rc.35
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/client/device.ts
CHANGED
|
@@ -200,6 +200,7 @@ export class Device {
|
|
|
200
200
|
if (isPageKeyboardAvoidance())
|
|
201
201
|
void import("./keyboardRide").then(({ KeyboardRide }) => {
|
|
202
202
|
Device.#keyboardRide = KeyboardRide;
|
|
203
|
+
KeyboardRide.watchFocus();
|
|
203
204
|
});
|
|
204
205
|
let currentHeight = 0;
|
|
205
206
|
const emitKeyboardHeight = (event: string, height: number) => {
|
package/client/keyboardRide.ts
CHANGED
|
@@ -16,6 +16,23 @@ const isAtEnd = (scroller: HTMLElement) =>
|
|
|
16
16
|
: scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= END_SLACK_PX;
|
|
17
17
|
const endOf = (scroller: HTMLElement) => (isReversed(scroller) ? 0 : scroller.scrollHeight - scroller.clientHeight);
|
|
18
18
|
const isInset = (rider: HTMLElement) => rider.dataset.akanKeyboardRide === "inset";
|
|
19
|
+
const untypedInputs = new Set([
|
|
20
|
+
"button",
|
|
21
|
+
"checkbox",
|
|
22
|
+
"color",
|
|
23
|
+
"file",
|
|
24
|
+
"hidden",
|
|
25
|
+
"image",
|
|
26
|
+
"radio",
|
|
27
|
+
"range",
|
|
28
|
+
"reset",
|
|
29
|
+
"submit",
|
|
30
|
+
]);
|
|
31
|
+
const isTyped = (field: HTMLElement) =>
|
|
32
|
+
field.isContentEditable ||
|
|
33
|
+
field instanceof HTMLTextAreaElement ||
|
|
34
|
+
(field instanceof HTMLInputElement && !untypedInputs.has(field.type));
|
|
35
|
+
const KEPT_HEIGHT_KEY = "akan:keyboard-height";
|
|
19
36
|
|
|
20
37
|
/**
|
|
21
38
|
* Moves the page with a phone keyboard the shell left to it. A layout change on every frame drops frames on a phone, so
|
|
@@ -42,6 +59,20 @@ export class KeyboardRide {
|
|
|
42
59
|
static #carried = new Set<HTMLElement>();
|
|
43
60
|
static #caret: { field: HTMLElement; color: string } | null = null;
|
|
44
61
|
static #movedAt = 0;
|
|
62
|
+
static #measured = new WeakMap<HTMLElement, { height: number; laid: number; settled: number }>();
|
|
63
|
+
static #kept: number | null = null;
|
|
64
|
+
static #isWatching = false;
|
|
65
|
+
|
|
66
|
+
static watchFocus() {
|
|
67
|
+
if (KeyboardRide.#isWatching) return;
|
|
68
|
+
KeyboardRide.#isWatching = true;
|
|
69
|
+
document.addEventListener("focusin", (event) => {
|
|
70
|
+
const field = event.target;
|
|
71
|
+
if (!(field instanceof HTMLElement) || !isTyped(field)) return;
|
|
72
|
+
|
|
73
|
+
setTimeout(() => KeyboardRide.#prepare(field), 0);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
45
76
|
|
|
46
77
|
static move(height: number, durationMs: number) {
|
|
47
78
|
if (height === KeyboardRide.#target) return;
|
|
@@ -62,9 +93,7 @@ export class KeyboardRide {
|
|
|
62
93
|
if (KeyboardRide.#timer) clearTimeout(KeyboardRide.#timer);
|
|
63
94
|
KeyboardRide.#timer = null;
|
|
64
95
|
|
|
65
|
-
const shown =
|
|
66
|
-
(element) => element.getClientRects().length > 0,
|
|
67
|
-
);
|
|
96
|
+
const shown = KeyboardRide.#shown();
|
|
68
97
|
|
|
69
98
|
const lists = isOverlay ? shown.filter(isInset) : [];
|
|
70
99
|
const riders = shown.filter((rider) => !lists.includes(rider));
|
|
@@ -77,19 +106,18 @@ export class KeyboardRide {
|
|
|
77
106
|
rider.style.transform = "";
|
|
78
107
|
}
|
|
79
108
|
KeyboardRide.#releaseInsets(lists);
|
|
80
|
-
|
|
81
|
-
const scrollers = KeyboardRide.#scrollersAtEnd(riders);
|
|
82
109
|
const listBoxes = lists.map((list) => KeyboardRide.#scrollerOf(list) ?? list);
|
|
83
110
|
const previous = KeyboardRide.#committed;
|
|
84
111
|
const isHeld = !isOverlay && height > previous;
|
|
85
|
-
KeyboardRide.#commit(height, scrollers);
|
|
86
|
-
const settled = riders.map(bottomOf);
|
|
87
|
-
const listsSettled = listBoxes.map(bottomOf);
|
|
88
|
-
if (isHeld || isOverlay) KeyboardRide.#commit(isOverlay ? 0 : previous, scrollers);
|
|
89
|
-
const laid = isHeld || isOverlay ? riders.map(bottomOf) : settled;
|
|
90
|
-
|
|
91
112
|
const active = document.activeElement;
|
|
92
113
|
const lead = riders.findIndex((rider) => !!active && rider.contains(active));
|
|
114
|
+
const isRising = isOverlay && height > 0;
|
|
115
|
+
const known = isRising && (lead >= 0 || !lists.length) ? KeyboardRide.#knownOf(riders, height) : null;
|
|
116
|
+
const { settled, laid, listsSettled } =
|
|
117
|
+
known ?? KeyboardRide.#measure({ riders, listBoxes, height, previous, isHeld, isOverlay });
|
|
118
|
+
if (isRising && !known) KeyboardRide.#remember(riders, height, laid, settled);
|
|
119
|
+
if (height > 0) KeyboardRide.#keep(height);
|
|
120
|
+
|
|
93
121
|
const leadRise = lead < 0 ? null : (laid[lead] ?? 0) - (settled[lead] ?? 0);
|
|
94
122
|
|
|
95
123
|
KeyboardRide.#hideCaret(lead >= 0 && active instanceof HTMLElement ? active : null);
|
|
@@ -125,6 +153,96 @@ export class KeyboardRide {
|
|
|
125
153
|
}, durationMs);
|
|
126
154
|
}
|
|
127
155
|
|
|
156
|
+
static #shown() {
|
|
157
|
+
return [...document.querySelectorAll<HTMLElement>(RIDE_SELECTOR)].filter(
|
|
158
|
+
(element) => element.getClientRects().length > 0,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
static #measure({
|
|
163
|
+
riders,
|
|
164
|
+
listBoxes,
|
|
165
|
+
height,
|
|
166
|
+
previous,
|
|
167
|
+
isHeld,
|
|
168
|
+
isOverlay,
|
|
169
|
+
}: {
|
|
170
|
+
riders: HTMLElement[];
|
|
171
|
+
listBoxes: HTMLElement[];
|
|
172
|
+
height: number;
|
|
173
|
+
previous: number;
|
|
174
|
+
isHeld: boolean;
|
|
175
|
+
isOverlay: boolean;
|
|
176
|
+
}) {
|
|
177
|
+
|
|
178
|
+
const scrollers = KeyboardRide.#scrollersAtEnd(riders);
|
|
179
|
+
KeyboardRide.#commit(height, scrollers);
|
|
180
|
+
const settled = riders.map(bottomOf);
|
|
181
|
+
const listsSettled = listBoxes.map(bottomOf);
|
|
182
|
+
if (isHeld || isOverlay) KeyboardRide.#commit(isOverlay ? 0 : previous, scrollers);
|
|
183
|
+
const laid = isHeld || isOverlay ? riders.map(bottomOf) : settled;
|
|
184
|
+
return { settled, laid, listsSettled };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static #knownOf(riders: HTMLElement[], height: number) {
|
|
188
|
+
if (!riders.length) return null;
|
|
189
|
+
const laid = riders.map(bottomOf);
|
|
190
|
+
const settled: number[] = [];
|
|
191
|
+
for (const [idx, rider] of riders.entries()) {
|
|
192
|
+
const known = KeyboardRide.#measured.get(rider);
|
|
193
|
+
if (!known || known.height !== height || Math.abs(known.laid - (laid[idx] ?? 0)) > 0.5) return null;
|
|
194
|
+
settled.push(known.settled);
|
|
195
|
+
}
|
|
196
|
+
return { settled, laid, listsSettled: [] as number[] };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static #remember(riders: HTMLElement[], height: number, laid: number[], settled: number[]) {
|
|
200
|
+
for (const [idx, rider] of riders.entries())
|
|
201
|
+
KeyboardRide.#measured.set(rider, { height, laid: laid[idx] ?? 0, settled: settled[idx] ?? 0 });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
static #prepare(field: HTMLElement) {
|
|
205
|
+
const height = KeyboardRide.#keptHeight();
|
|
206
|
+
if (!height || KeyboardRide.#target > 0 || KeyboardRide.#timer || document.activeElement !== field) return;
|
|
207
|
+
if (!field.closest(OVERLAY_SELECTOR)) return;
|
|
208
|
+
const riders = KeyboardRide.#shown().filter((rider) => !isInset(rider));
|
|
209
|
+
if (!riders.some((rider) => rider.contains(field))) return;
|
|
210
|
+
const root = document.documentElement;
|
|
211
|
+
root.toggleAttribute("data-akan-keyboard", true);
|
|
212
|
+
root.toggleAttribute("data-akan-keyboard-overlay", true);
|
|
213
|
+
if (!KeyboardRide.#knownOf(riders, height)) {
|
|
214
|
+
const { settled, laid } = KeyboardRide.#measure({
|
|
215
|
+
riders,
|
|
216
|
+
listBoxes: [],
|
|
217
|
+
height,
|
|
218
|
+
previous: 0,
|
|
219
|
+
isHeld: false,
|
|
220
|
+
isOverlay: true,
|
|
221
|
+
});
|
|
222
|
+
KeyboardRide.#remember(riders, height, laid, settled);
|
|
223
|
+
}
|
|
224
|
+
root.toggleAttribute("data-akan-keyboard", false);
|
|
225
|
+
root.toggleAttribute("data-akan-keyboard-overlay", false);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static #keep(height: number) {
|
|
229
|
+
if (height === KeyboardRide.#kept) return;
|
|
230
|
+
KeyboardRide.#kept = height;
|
|
231
|
+
try {
|
|
232
|
+
localStorage.setItem(KEPT_HEIGHT_KEY, String(height));
|
|
233
|
+
} catch {}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
static #keptHeight() {
|
|
237
|
+
if (KeyboardRide.#kept === null)
|
|
238
|
+
try {
|
|
239
|
+
KeyboardRide.#kept = Number(localStorage.getItem(KEPT_HEIGHT_KEY)) || 0;
|
|
240
|
+
} catch {
|
|
241
|
+
KeyboardRide.#kept = 0;
|
|
242
|
+
}
|
|
243
|
+
return KeyboardRide.#kept;
|
|
244
|
+
}
|
|
245
|
+
|
|
128
246
|
static #hideCaret(field: HTMLElement | null) {
|
|
129
247
|
if (KeyboardRide.#caret?.field !== field) KeyboardRide.#showCaret();
|
|
130
248
|
if (!field || KeyboardRide.#caret) return;
|
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import { type StreamError } from "./StreamBoundary.d.ts";
|
|
3
|
+
interface StreamDeferredProps<Value> {
|
|
4
|
+
of: PromiseLike<Value>;
|
|
5
|
+
fallback: ReactNode;
|
|
6
|
+
error?: StreamError;
|
|
7
|
+
keyOf: (thenable: object) => string;
|
|
8
|
+
children: (value: Value) => ReactNode;
|
|
9
|
+
}
|
|
10
|
+
export declare const StreamDeferred: <Value>({ of, fallback, error, keyOf, children }: StreamDeferredProps<Value>) => import("react").JSX.Element;
|
|
11
|
+
export {};
|
package/ui/Load/Stream.tsx
CHANGED
|
@@ -2,6 +2,7 @@ import type { PromiseOrObject } from "akanjs/base";
|
|
|
2
2
|
import { isThenable } from "akanjs/common";
|
|
3
3
|
import { type ReactNode, Suspense, type Usable, use } from "react";
|
|
4
4
|
import { StreamBoundary, type StreamError } from "./StreamBoundary";
|
|
5
|
+
import { StreamDeferred } from "./StreamDeferred";
|
|
5
6
|
|
|
6
7
|
interface StreamProps<Value> {
|
|
7
8
|
of: PromiseOrObject<Value>;
|
|
@@ -28,6 +29,13 @@ const resetKeyOf = (thenable: object) => {
|
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
export default function Stream<Value>({ of, fallback = null, error, children }: StreamProps<Value>) {
|
|
32
|
+
|
|
33
|
+
if (isThenable(of) && process.env.AKAN_PUBLIC_RENDER_ENV === "csr")
|
|
34
|
+
return (
|
|
35
|
+
<StreamDeferred of={of as PromiseLike<Value>} fallback={fallback} error={error} keyOf={resetKeyOf}>
|
|
36
|
+
{children}
|
|
37
|
+
</StreamDeferred>
|
|
38
|
+
);
|
|
31
39
|
return isThenable(of) ? (
|
|
32
40
|
<StreamBoundary resetKey={resetKeyOf(of)} error={error}>
|
|
33
41
|
<Suspense fallback={fallback}>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { type ReactNode, Suspense, type Usable, use, useDeferredValue } from "react";
|
|
3
|
+
import { StreamBoundary, type StreamError } from "./StreamBoundary";
|
|
4
|
+
|
|
5
|
+
interface StreamDeferredProps<Value> {
|
|
6
|
+
of: PromiseLike<Value>;
|
|
7
|
+
fallback: ReactNode;
|
|
8
|
+
error?: StreamError;
|
|
9
|
+
keyOf: (thenable: object) => string;
|
|
10
|
+
children: (value: Value) => ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export const StreamDeferred = <Value,>({ of, fallback, error, keyOf, children }: StreamDeferredProps<Value>) => {
|
|
13
|
+
const shown = useDeferredValue(of);
|
|
14
|
+
return (
|
|
15
|
+
<StreamBoundary resetKey={keyOf(shown)} error={error}>
|
|
16
|
+
<Suspense fallback={fallback}>
|
|
17
|
+
<Resolve of={shown}>{children}</Resolve>
|
|
18
|
+
</Suspense>
|
|
19
|
+
</StreamBoundary>
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
interface ResolveProps<Value> {
|
|
24
|
+
of: PromiseLike<Value>;
|
|
25
|
+
children: (value: Value) => ReactNode;
|
|
26
|
+
}
|
|
27
|
+
const Resolve = <Value,>({ of, children }: ResolveProps<Value>) => children(use(of as Usable<Value>));
|