akanjs 3.3.0-rc.33 → 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) => {
@@ -7,6 +7,7 @@ const END_SLACK_PX = 2;
7
7
  const GLIDE_ATTRIBUTE = "data-akan-keyboard-glide";
8
8
 
9
9
  const bottomOf = (element: HTMLElement) => element.getBoundingClientRect().bottom;
10
+ const topOf = (element: HTMLElement) => element.getBoundingClientRect().top;
10
11
 
11
12
  const isReversed = (scroller: HTMLElement) => getComputedStyle(scroller).flexDirection === "column-reverse";
12
13
  const isAtEnd = (scroller: HTMLElement) =>
@@ -15,6 +16,23 @@ const isAtEnd = (scroller: HTMLElement) =>
15
16
  : scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight <= END_SLACK_PX;
16
17
  const endOf = (scroller: HTMLElement) => (isReversed(scroller) ? 0 : scroller.scrollHeight - scroller.clientHeight);
17
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";
18
36
 
19
37
  /**
20
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
@@ -25,10 +43,11 @@ const isInset = (rider: HTMLElement) => rider.dataset.akanKeyboardRide === "inse
25
43
  * up, since the shorter box would clip a rider still on its way, and keys going down give the taller box at once while
26
44
  * the riders start from where they were drawn. When the focused field sits in `data-akan-keyboard-overlay`, the page
27
45
  * keeps the whole screen, as a native chat screen does, so what lies under the glass keys stays drawn: the riders go
28
- * where the shorter box would put them and stay there. An `inset` rider (a list's content) is not carried: it takes the
29
- * rise as bottom padding at once, so the list still scrolls to its top, and a list resting at its end scrolls the rise
30
- * with the keys, and back down with them. While it does, its scroller carries `data-akan-keyboard-glide`: a list's own
31
- * keep-at-end logic yields to it.
46
+ * where the shorter box would put them and stay there. An `inset` rider (a list's content) rises as far as the rider
47
+ * holding the focused field and takes that rise as bottom padding at once, so the list still scrolls to its top, and a
48
+ * list resting at its end scrolls the rise with the keys, and back down with them. While it does, its scroller carries
49
+ * `data-akan-keyboard-glide`: a list's own keep-at-end logic yields to it. A list too short to scroll that far moves
50
+ * the rest as a transform on the keys' curve, which a short list's layer affords.
32
51
  */
33
52
  export class KeyboardRide {
34
53
  static #committed = 0;
@@ -37,7 +56,23 @@ export class KeyboardRide {
37
56
  static #timer: ReturnType<typeof setTimeout> | null = null;
38
57
  static #insets = new Map<HTMLElement, { paddingBottom: string; base: string; rise: number }>();
39
58
  static #glides = new Map<HTMLElement, number>();
59
+ static #carried = new Set<HTMLElement>();
60
+ static #caret: { field: HTMLElement; color: string } | null = null;
40
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
+ }
41
76
 
42
77
  static move(height: number, durationMs: number) {
43
78
  if (height === KeyboardRide.#target) return;
@@ -58,9 +93,7 @@ export class KeyboardRide {
58
93
  if (KeyboardRide.#timer) clearTimeout(KeyboardRide.#timer);
59
94
  KeyboardRide.#timer = null;
60
95
 
61
- const shown = [...document.querySelectorAll<HTMLElement>(RIDE_SELECTOR)].filter(
62
- (element) => element.getClientRects().length > 0,
63
- );
96
+ const shown = KeyboardRide.#shown();
64
97
 
65
98
  const lists = isOverlay ? shown.filter(isInset) : [];
66
99
  const riders = shown.filter((rider) => !lists.includes(rider));
@@ -73,19 +106,24 @@ export class KeyboardRide {
73
106
  rider.style.transform = "";
74
107
  }
75
108
  KeyboardRide.#releaseInsets(lists);
76
-
77
- const scrollers = KeyboardRide.#scrollersAtEnd(riders);
78
109
  const listBoxes = lists.map((list) => KeyboardRide.#scrollerOf(list) ?? list);
79
110
  const previous = KeyboardRide.#committed;
80
111
  const isHeld = !isOverlay && height > previous;
81
- KeyboardRide.#commit(height, scrollers);
82
- const settled = riders.map(bottomOf);
83
- const listsSettled = listBoxes.map(bottomOf);
84
- if (isHeld || isOverlay) KeyboardRide.#commit(isOverlay ? 0 : previous, scrollers);
85
- const laid = isHeld || isOverlay ? riders.map(bottomOf) : settled;
112
+ const active = document.activeElement;
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
+
121
+ const leadRise = lead < 0 ? null : (laid[lead] ?? 0) - (settled[lead] ?? 0);
122
+
123
+ KeyboardRide.#hideCaret(lead >= 0 && active instanceof HTMLElement ? active : null);
86
124
  lists.forEach((list, idx) => {
87
125
  const scroller = listEnds[idx] ?? null;
88
- const rise = bottomOf(listBoxes[idx] ?? list) - (listsSettled[idx] ?? 0);
126
+ const rise = leadRise ?? bottomOf(listBoxes[idx] ?? list) - (listsSettled[idx] ?? 0);
89
127
  if (height > 0) KeyboardRide.#raiseList(list, rise, scroller, durationMs);
90
128
  else KeyboardRide.#lowerList(list, scroller, durationMs);
91
129
  });
@@ -94,8 +132,7 @@ export class KeyboardRide {
94
132
  });
95
133
  void root.offsetHeight;
96
134
 
97
- const late = Math.round(performance.now() - KeyboardRide.#movedAt);
98
- const transition = `transform ${durationMs}ms ${nativeKeyboardEasing()} ${late > 0 ? -late : 0}ms`;
135
+ const transition = KeyboardRide.#transitionOf(durationMs);
99
136
  riders.forEach((rider, idx) => {
100
137
  rider.style.transition = transition;
101
138
  rider.style.transform = `translateY(${settled[idx] - laid[idx]}px)`;
@@ -110,9 +147,139 @@ export class KeyboardRide {
110
147
  }
111
148
  void root.offsetHeight;
112
149
  for (const rider of settling) rider.style.transition = "";
150
+ for (const list of KeyboardRide.#carried) list.style.transition = "";
151
+ KeyboardRide.#carried.clear();
152
+ KeyboardRide.#showCaret();
113
153
  }, durationMs);
114
154
  }
115
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
+
246
+ static #hideCaret(field: HTMLElement | null) {
247
+ if (KeyboardRide.#caret?.field !== field) KeyboardRide.#showCaret();
248
+ if (!field || KeyboardRide.#caret) return;
249
+ KeyboardRide.#caret = { field, color: field.style.caretColor };
250
+ field.style.caretColor = "transparent";
251
+ }
252
+
253
+ static #showCaret() {
254
+ if (!KeyboardRide.#caret) return;
255
+ KeyboardRide.#caret.field.style.caretColor = KeyboardRide.#caret.color;
256
+ KeyboardRide.#caret = null;
257
+ }
258
+
259
+ static #transitionOf(durationMs: number) {
260
+ const late = Math.round(performance.now() - KeyboardRide.#movedAt);
261
+ return `transform ${durationMs}ms ${nativeKeyboardEasing()} ${late > 0 ? -late : 0}ms`;
262
+ }
263
+
264
+ static #drawnTopOf(list: HTMLElement) {
265
+ const top = topOf(list);
266
+ if (list.style.transform) {
267
+ list.style.transition = "none";
268
+ list.style.transform = "";
269
+ }
270
+ return top;
271
+ }
272
+
273
+ static #carry(list: HTMLElement, offset: number, durationMs: number) {
274
+ if (!(Math.abs(offset) >= 0.5) || durationMs <= 0) return;
275
+ list.style.transition = "none";
276
+ list.style.transform = `translateY(${offset}px)`;
277
+ void list.offsetHeight;
278
+ list.style.transition = KeyboardRide.#transitionOf(durationMs);
279
+ list.style.transform = "";
280
+ KeyboardRide.#carried.add(list);
281
+ }
282
+
116
283
  static #commit(height: number, scrollers: HTMLElement[]) {
117
284
  const root = document.documentElement;
118
285
  root.style.setProperty("--akan-keyboard-cover", `${height}px`);
@@ -125,11 +292,13 @@ export class KeyboardRide {
125
292
  const held = KeyboardRide.#insets.get(list);
126
293
  const base = held?.base ?? getComputedStyle(list).paddingBottom;
127
294
  if (rise <= 0) return;
295
+ const drawnTop = KeyboardRide.#drawnTopOf(list);
128
296
  KeyboardRide.#insets.set(list, { paddingBottom: held?.paddingBottom ?? list.style.paddingBottom, base, rise });
129
297
  list.style.paddingBottom = `calc(${base} + ${rise}px)`;
130
298
  const outer = KeyboardRide.#scrollerOf(list);
131
299
 
132
300
  if (outer && isReversed(outer)) outer.scrollTop -= rise - (held?.rise ?? 0);
301
+ KeyboardRide.#carry(list, drawnTop - topOf(list), durationMs);
133
302
  if (scroller) KeyboardRide.#glide(scroller, endOf(scroller), durationMs);
134
303
  }
135
304
 
@@ -142,7 +311,11 @@ export class KeyboardRide {
142
311
  KeyboardRide.#insets.delete(list);
143
312
  if (outer && isReversed(outer)) outer.scrollTop += held.rise;
144
313
  };
145
- if (!scroller) return release();
314
+ if (!scroller || (isReversed(scroller) && scroller.scrollHeight - scroller.clientHeight < held.rise)) {
315
+ const drawnTop = KeyboardRide.#drawnTopOf(list);
316
+ release();
317
+ return KeyboardRide.#carry(list, drawnTop - topOf(list), durationMs);
318
+ }
146
319
  const to = isReversed(scroller) ? -held.rise : Math.max(0, scroller.scrollTop - held.rise);
147
320
  KeyboardRide.#glide(scroller, to, durationMs, release);
148
321
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.33",
3
+ "version": "3.3.0-rc.35",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -7,12 +7,14 @@
7
7
  * up, since the shorter box would clip a rider still on its way, and keys going down give the taller box at once while
8
8
  * the riders start from where they were drawn. When the focused field sits in `data-akan-keyboard-overlay`, the page
9
9
  * keeps the whole screen, as a native chat screen does, so what lies under the glass keys stays drawn: the riders go
10
- * where the shorter box would put them and stay there. An `inset` rider (a list's content) is not carried: it takes the
11
- * rise as bottom padding at once, so the list still scrolls to its top, and a list resting at its end scrolls the rise
12
- * with the keys, and back down with them. While it does, its scroller carries `data-akan-keyboard-glide`: a list's own
13
- * keep-at-end logic yields to it.
10
+ * where the shorter box would put them and stay there. An `inset` rider (a list's content) rises as far as the rider
11
+ * holding the focused field and takes that rise as bottom padding at once, so the list still scrolls to its top, and a
12
+ * list resting at its end scrolls the rise with the keys, and back down with them. While it does, its scroller carries
13
+ * `data-akan-keyboard-glide`: a list's own keep-at-end logic yields to it. A list too short to scroll that far moves
14
+ * the rest as a transform on the keys' curve, which a short list's layer affords.
14
15
  */
15
16
  export declare class KeyboardRide {
16
17
  #private;
18
+ static watchFocus(): void;
17
19
  static move(height: number, durationMs: number): void;
18
20
  }
@@ -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 {};
@@ -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>));