akanjs 3.3.0-rc.34 → 3.3.0-rc.36

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) => {
@@ -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 = [...document.querySelectorAll<HTMLElement>(RIDE_SELECTOR)].filter(
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.34",
3
+ "version": "3.3.0-rc.36",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -15,5 +15,6 @@
15
15
  */
16
16
  export declare class KeyboardRide {
17
17
  #private;
18
+ static watchFocus(): void;
18
19
  static move(height: number, durationMs: number): void;
19
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 {};
@@ -0,0 +1,2 @@
1
+ import type { PathContextType } from "akanjs/client";
2
+ export declare const isSamePathValue: (shown: PathContextType, next: PathContextType) => boolean;
@@ -2,14 +2,14 @@
2
2
  export declare const badgeRecipe: (variants?: Omit<({
3
3
  size?: "lg" | "md" | "sm" | "xs" | undefined;
4
4
  outline?: boolean | undefined;
5
- variant?: "default" | "error" | "info" | "warning" | "success" | "primary" | "outline" | "secondary" | "accent" | "neutral" | undefined;
5
+ variant?: "error" | "default" | "info" | "warning" | "success" | "primary" | "outline" | "secondary" | "accent" | "neutral" | undefined;
6
6
  } & {
7
7
  class?: import("tailwind-variants/lite").ClassValue;
8
8
  className?: never;
9
9
  }) | ({
10
10
  size?: "lg" | "md" | "sm" | "xs" | undefined;
11
11
  outline?: boolean | undefined;
12
- variant?: "default" | "error" | "info" | "warning" | "success" | "primary" | "outline" | "secondary" | "accent" | "neutral" | undefined;
12
+ variant?: "error" | "default" | "info" | "warning" | "success" | "primary" | "outline" | "secondary" | "accent" | "neutral" | undefined;
13
13
  } & {
14
14
  class?: never;
15
15
  className?: import("tailwind-variants/lite").ClassValue;
@@ -2,7 +2,7 @@
2
2
  export declare const buttonRecipe: (variants?: Omit<({
3
3
  size?: "xl" | "lg" | "md" | "sm" | "xs" | "icon" | undefined;
4
4
  outline?: boolean | undefined;
5
- variant?: "default" | "link" | "info" | "warning" | "success" | "destructive" | "primary" | "outline" | "secondary" | "accent" | "neutral" | "ghost" | undefined;
5
+ variant?: "link" | "default" | "info" | "warning" | "success" | "destructive" | "primary" | "outline" | "secondary" | "accent" | "neutral" | "ghost" | undefined;
6
6
  shape?: "default" | "circle" | "square" | undefined;
7
7
  } & {
8
8
  class?: import("tailwind-variants/lite").ClassValue;
@@ -10,7 +10,7 @@ export declare const buttonRecipe: (variants?: Omit<({
10
10
  }) | ({
11
11
  size?: "xl" | "lg" | "md" | "sm" | "xs" | "icon" | undefined;
12
12
  outline?: boolean | undefined;
13
- variant?: "default" | "link" | "info" | "warning" | "success" | "destructive" | "primary" | "outline" | "secondary" | "accent" | "neutral" | "ghost" | undefined;
13
+ variant?: "link" | "default" | "info" | "warning" | "success" | "destructive" | "primary" | "outline" | "secondary" | "accent" | "neutral" | "ghost" | undefined;
14
14
  shape?: "default" | "circle" | "square" | undefined;
15
15
  } & {
16
16
  class?: never;
@@ -2,14 +2,14 @@
2
2
  export declare const inputRecipe: (variants?: Omit<({
3
3
  kind?: "field" | "select" | "area" | undefined;
4
4
  size?: "xl" | "lg" | "md" | "sm" | "xs" | undefined;
5
- tone?: "default" | "error" | "primary" | undefined;
5
+ tone?: "error" | "default" | "primary" | undefined;
6
6
  } & {
7
7
  class?: import("tailwind-variants/lite").ClassValue;
8
8
  className?: never;
9
9
  }) | ({
10
10
  kind?: "field" | "select" | "area" | undefined;
11
11
  size?: "xl" | "lg" | "md" | "sm" | "xs" | undefined;
12
- tone?: "default" | "error" | "primary" | undefined;
12
+ tone?: "error" | "default" | "primary" | undefined;
13
13
  } & {
14
14
  class?: never;
15
15
  className?: import("tailwind-variants/lite").ClassValue;
@@ -17,8 +17,8 @@ export declare const useCsrRefs: () => {
17
17
  pageContentRef: import("react").RefObject<HTMLDivElement | null>;
18
18
  prevPageContentRef: import("react").RefObject<HTMLDivElement | null>;
19
19
  onBack: import("react").RefObject<{
20
- none?: (() => Promise<void>) | undefined;
21
20
  stack?: (() => Promise<void>) | undefined;
21
+ none?: (() => Promise<void>) | undefined;
22
22
  fade?: (() => Promise<void>) | undefined;
23
23
  bottomUp?: (() => Promise<void>) | undefined;
24
24
  scaleOut?: (() => Promise<void>) | undefined;
@@ -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>));
@@ -54,6 +54,7 @@ import { readRscRoutePatch, subscribeRscRoutePatch } from "../../client/rscRoute
54
54
  import { FrameRootVars } from "../../webkit/FrameRootVars";
55
55
  import { useTransitionStore } from "../../webkit/useTransitionStore";
56
56
  import { getFrameCssVars, isResponsiveFrame } from "./Common";
57
+ import { isSamePathValue } from "./isSamePathValue";
57
58
  import { Messages } from "./Messages";
58
59
  import { Reconnect } from "./Reconnect";
59
60
 
@@ -185,12 +186,13 @@ export const ClientPathWrapper = ({
185
186
  renderLayouts: [],
186
187
  };
187
188
  const csr = useCsr();
189
+ const isPending = pageType === "pending";
188
190
  const registerFrameSlot = useCallback(
189
191
  (slot: Parameters<NonNullable<typeof csr.registerFrameSlot>>[1]) =>
190
192
  typeof csr.registerFrameSlot === "function"
191
- ? csr.registerFrameSlot(pathRoute.path, slot, pageType === "pending" ? "pending" : "active")
193
+ ? csr.registerFrameSlot(pathRoute.path, slot, isPending ? "pending" : "active")
192
194
  : () => undefined,
193
- [csr.registerFrameSlot, pathRoute.path, pageType],
195
+ [csr.registerFrameSlot, pathRoute.path, isPending],
194
196
  );
195
197
 
196
198
  const [gestureEnabled, setGestureEnabled] = useState(true);
@@ -247,8 +249,11 @@ export const ClientPathWrapper = ({
247
249
  registerFrameSlot,
248
250
  ],
249
251
  );
252
+
253
+ const shownValue = useRef(pathValue);
254
+ if (activity !== "hidden" && !isSamePathValue(shownValue.current, pathValue)) shownValue.current = pathValue;
250
255
  return (
251
- <pathContext.Provider value={pathValue}>
256
+ <pathContext.Provider value={shownValue.current}>
252
257
  <pageActivityContext.Provider value={pageActivity}>
253
258
  <Frame
254
259
  {...bindProps}
@@ -0,0 +1,13 @@
1
+ import type { PathContextType } from "akanjs/client";
2
+
3
+ export const isSamePathValue = (shown: PathContextType, next: PathContextType) =>
4
+ shown.pageKey === next.pageKey &&
5
+ shown.location.href === next.location.href &&
6
+ shown.location.hash === next.location.hash &&
7
+ shown.location.pathRoute.path === next.location.pathRoute.path &&
8
+ shown.location.pathRoute.renderPage === next.location.pathRoute.renderPage &&
9
+ shown.location.entryId === next.location.entryId &&
10
+ shown.followsRouter === next.followsRouter &&
11
+ shown.prefix === next.prefix &&
12
+ shown.gestureEnabled === next.gestureEnabled &&
13
+ shown.registerFrameSlot === next.registerFrameSlot;
@@ -241,11 +241,19 @@ export const useCsrNavigation = (input: CsrNavigationInput) => {
241
241
  (kind: "push" | "replace", href: string, { scrollToTop }: RouteOptions = {}) => {
242
242
  const fromLocation = getCurrentLocation();
243
243
  const target = getLocation(href);
244
-
244
+ const cached = target.pathRoute.pageState.cache
245
+ ? nav.history.current.cachedLocationMap.get(target.pathRoute.path)
246
+ : undefined;
247
+
245
248
  const toLocation =
246
249
  kind === "replace" && target.pathRoute.path === fromLocation.pathRoute.path
247
250
  ? { ...target, entryId: fromLocation.entryId }
248
- : target;
251
+ : cached &&
252
+ cached.pathname === target.pathname &&
253
+ cached.search === target.search &&
254
+ cached.hash === target.hash
255
+ ? cached
256
+ : target;
249
257
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
250
258
  const slides =
251
259
  CsrStack.keyOf(toLocation) !== CsrStack.keyOf(fromLocation) &&