akanjs 3.3.0-rc.17 → 3.3.0-rc.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.3.0-rc.17",
3
+ "version": "3.3.0-rc.19",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -0,0 +1,30 @@
1
+ import type { PathRoute } from "akanjs/client";
2
+ type FramePageState = PathRoute["pageState"];
3
+ type Keyframe = Record<string, string>;
4
+ export interface CsrSlideFrame {
5
+ width: number;
6
+ height: number;
7
+ pageState: FramePageState;
8
+ prevPageState: FramePageState;
9
+ }
10
+ interface CsrSlidePlay {
11
+ fromUnit: number;
12
+ toUnit: number;
13
+ duration: number;
14
+ keep?: boolean;
15
+ }
16
+ export declare class CsrSlide {
17
+ #private;
18
+ static readonly easing = "cubic-bezier(0.25, 1, 0.5, 1)";
19
+ private constructor();
20
+ static get supported(): boolean;
21
+ static get isCalm(): boolean;
22
+ static play(frame: CsrSlideFrame, { fromUnit, toUnit, duration, keep }: CsrSlidePlay): CsrSlide | null;
23
+ /** What each part's style is with the page `unit` px from its place: 0 shown, the frame's width gone right. */
24
+ static partsAt({ width, height, pageState, prevPageState }: CsrSlideFrame, unit: number): Record<string, Keyframe>;
25
+ /** Where the page is right now, as the spring's unit, or null when this slide does not move it. */
26
+ unitNow(): number | null;
27
+ get finished(): Promise<undefined>;
28
+ cancel(): void;
29
+ }
30
+ export {};
@@ -0,0 +1,6 @@
1
+ import { type Location } from "akanjs/client";
2
+ export declare class NativeSplash {
3
+ #private;
4
+ static readonly drawTimeoutMs = 4000;
5
+ static hideOnceDrawn(location: Location): () => void;
6
+ }
@@ -4,6 +4,7 @@ export type FrameSlotBucket = "active" | "pending";
4
4
  export type FrameSlotMapByBucket = Record<FrameSlotBucket, FrameSlotMap>;
5
5
  export declare const PENDING_FRAME_READY_TIMEOUT_MS = 80;
6
6
  export declare const PENDING_FRAME_READY_MAX_TIMEOUT_MS = 120;
7
+ export declare const STACK_TRANSITION_DURATION_MS = 350;
7
8
  export declare const KEYBOARD_SHOW_ANIMATION_DURATION_MS = 420;
8
9
  export declare const KEYBOARD_HIDE_ANIMATION_DURATION_MS = 90;
9
10
  export declare const KEYBOARD_SHOW_ANIMATION_EASING = "cubic-bezier(0.16, 1, 0.3, 1)";
package/ui/System/CSR.tsx CHANGED
@@ -26,6 +26,7 @@ import {
26
26
  useState,
27
27
  } from "react";
28
28
  import { createPortal } from "react-dom";
29
+ import { NativeSplash } from "../../webkit/nativeSplash";
29
30
  import { NativeUpdates } from "../../webkit/nativeUpdates";
30
31
  import { RenderLayer } from "../../webkit/RenderLayer";
31
32
  import { animated } from "../animated";
@@ -348,6 +349,7 @@ const CSRFrameSlotTargets = ({ slot }: { slot: FrameSlotTarget }) => {
348
349
  <animated.div
349
350
  key={id}
350
351
  id={id}
352
+ data-page-type={pageType}
351
353
  inert={hidden}
352
354
  aria-hidden={hidden ? true : undefined}
353
355
  className={cn(
@@ -435,6 +437,10 @@ const CSRPageContainer = ({ entry, prefix, layoutStyle }: CSRPageContainerProps)
435
437
  useEffect(() => {
436
438
  if (pageType === "current") NativeUpdates.confirm();
437
439
  }, [pageType]);
440
+ useEffect(
441
+ () => (pageType === "current" ? NativeSplash.hideOnceDrawn(location) : undefined),
442
+ [pageType, location.entryId],
443
+ );
438
444
 
439
445
  const [pageContainers, setPageContainers] = useState(() => document.getElementById("pageContainers"));
440
446
  useLayoutEffect(() => {
@@ -465,6 +471,7 @@ const CSRPageContainer = ({ entry, prefix, layoutStyle }: CSRPageContainerProps)
465
471
  <animated.div
466
472
  id={`pageContainer-${key}`}
467
473
  data-path={pathRoute.path}
474
+ data-page-type={pageType}
468
475
  inert={pageType !== "current"}
469
476
  aria-hidden={pageType === "current" ? undefined : true}
470
477
  style={{
@@ -0,0 +1,130 @@
1
+ "use client";
2
+ import type { PathRoute } from "akanjs/client";
3
+
4
+ type FramePageState = PathRoute["pageState"];
5
+ type Keyframe = Record<string, string>;
6
+
7
+ export interface CsrSlideFrame {
8
+ width: number;
9
+ height: number;
10
+ pageState: FramePageState;
11
+ prevPageState: FramePageState;
12
+ }
13
+
14
+ interface CsrSlidePlay {
15
+ fromUnit: number;
16
+ toUnit: number;
17
+ duration: number;
18
+ keep?: boolean;
19
+ }
20
+
21
+ const lerp = (from: number, to: number, progress: number) => from + (to - from) * progress;
22
+ const px = (value: number) => `${value}px`;
23
+ const shown = (selector: string) => `${selector} > [data-page-type="current"]`;
24
+ const under = (selector: string) => `${selector} > [data-page-type="prev"]`;
25
+ const pageSelector = `${shown("#pageContainers")} .akan-page-content`;
26
+
27
+ export class CsrSlide {
28
+
29
+ static readonly easing = "cubic-bezier(0.25, 1, 0.5, 1)";
30
+ readonly #animations: Animation[];
31
+ readonly #page: HTMLElement | null;
32
+
33
+ private constructor(animations: Animation[], page: HTMLElement | null) {
34
+ this.#animations = animations;
35
+ this.#page = page;
36
+ }
37
+
38
+ static get supported() {
39
+ return typeof document !== "undefined" && typeof document.documentElement.animate === "function";
40
+ }
41
+
42
+ static get isCalm() {
43
+ return typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
44
+ }
45
+
46
+ static play(frame: CsrSlideFrame, { fromUnit, toUnit, duration, keep = false }: CsrSlidePlay): CsrSlide | null {
47
+ if (!CsrSlide.supported || duration <= 0) return null;
48
+ const from = CsrSlide.partsAt(frame, fromUnit);
49
+ const to = CsrSlide.partsAt(frame, toUnit);
50
+ const animations: Animation[] = [];
51
+ let page: HTMLElement | null = null;
52
+ for (const [selector, start] of Object.entries(from)) {
53
+ const end = to[selector];
54
+ const changed = Object.keys(start).filter((property) => start[property] !== end[property]);
55
+ const element = changed.length ? document.querySelector<HTMLElement>(selector) : null;
56
+ if (!element) continue;
57
+ if (selector === pageSelector) page = element;
58
+ const pick = (keyframe: Keyframe) =>
59
+ Object.fromEntries(changed.map((property) => [property, keyframe[property]]));
60
+ animations.push(
61
+ element.animate([pick(start), pick(end)], {
62
+ duration,
63
+ easing: CsrSlide.easing,
64
+ fill: keep ? "forwards" : "none",
65
+ }),
66
+ );
67
+ }
68
+ return animations.length ? new CsrSlide(animations, page) : null;
69
+ }
70
+
71
+ /** What each part's style is with the page `unit` px from its place: 0 shown, the frame's width gone right. */
72
+ static partsAt({ width, height, pageState, prevPageState }: CsrSlideFrame, unit: number): Record<string, Keyframe> {
73
+ const progress = width > 0 ? (width - unit) / width : 1;
74
+ const between = (prev: number, next: number) => px(lerp(prev, next, progress));
75
+ const prevOffset = px((unit - width) / 5);
76
+ const bottomInsetTop = (state: FramePageState) => height - state.bottomInset - state.bottomSafeArea;
77
+ return {
78
+ [pageSelector]: { transform: `translateX(${px(unit)})` },
79
+ [under("#pageContainers")]: { transform: `translateX(${prevOffset})` },
80
+ [`${under("#pageContainers")} .akan-page-content`]: { opacity: `${1 - progress / 2}` },
81
+ [shown("#topInsetContainer")]: {
82
+ opacity: `${progress}`,
83
+ transform: `translateX(${px((width / 5) * (1 - progress))})`,
84
+ },
85
+ [under("#topInsetContainer")]: {
86
+ opacity: `${1 - progress}`,
87
+ transform: `translateX(${px((-width / 5) * progress)})`,
88
+ },
89
+ [shown("#topLeftActionContainer")]: { opacity: `${progress}` },
90
+ [under("#topLeftActionContainer")]: { opacity: `${1 - progress}` },
91
+ [shown("#bottomInsetContainer")]: { opacity: `${progress}`, transform: `translateX(${px(unit)})` },
92
+ [under("#bottomInsetContainer")]: { opacity: `${1 - progress}`, transform: `translateX(${prevOffset})` },
93
+ "#topInsetContainer": {
94
+ top: between(prevPageState.topSafeArea, pageState.topSafeArea),
95
+ height: between(prevPageState.topInset, pageState.topInset),
96
+ },
97
+ "#topLeftActionContainer": {
98
+ top: between(prevPageState.topSafeArea, pageState.topSafeArea),
99
+ height: between(prevPageState.topInset, pageState.topInset),
100
+ minWidth: between(prevPageState.topInset, pageState.topInset),
101
+ },
102
+ "#bottomInsetContainer": {
103
+ top: between(bottomInsetTop(prevPageState), bottomInsetTop(pageState)),
104
+ height: between(prevPageState.bottomInset, pageState.bottomInset),
105
+ },
106
+ "#topSafeArea": { height: between(prevPageState.topSafeArea, pageState.topSafeArea) },
107
+ "#bottomSafeArea": {
108
+ top: between(height - prevPageState.bottomSafeArea, height - pageState.bottomSafeArea),
109
+ height: between(prevPageState.bottomSafeArea, pageState.bottomSafeArea),
110
+ },
111
+ };
112
+ }
113
+
114
+ /** Where the page is right now, as the spring's unit, or null when this slide does not move it. */
115
+ unitNow(): number | null {
116
+ if (!this.#page || typeof DOMMatrixReadOnly === "undefined") return null;
117
+ const { transform } = getComputedStyle(this.#page);
118
+ return !transform || transform === "none" ? 0 : new DOMMatrixReadOnly(transform).m41;
119
+ }
120
+
121
+ get finished() {
122
+ return Promise.all(this.#animations.map((animation) => animation.finished.catch(() => undefined))).then(
123
+ () => undefined,
124
+ );
125
+ }
126
+
127
+ cancel() {
128
+ for (const animation of this.#animations) animation.cancel();
129
+ }
130
+ }
@@ -0,0 +1,30 @@
1
+ "use client";
2
+ import { debugFrame, type Location } from "akanjs/client";
3
+ import { isNativeShell } from "akanjs/client/native";
4
+ import { splashScreen } from "akanjs/client/native/splash-screen";
5
+ import { CsrPageReadiness } from "./csrPageReadiness";
6
+
7
+ export class NativeSplash {
8
+ static readonly drawTimeoutMs = 4_000;
9
+ static #hidden = false;
10
+
11
+ static hideOnceDrawn(location: Location) {
12
+ if (NativeSplash.#hidden || !isNativeShell() || !splashScreen.isSupported("hide")) return () => undefined;
13
+ const startedAt = Date.now();
14
+ let frame = 0;
15
+ const isDrawn = () =>
16
+ !CsrPageReadiness.drawsLater(location) ||
17
+ CsrPageReadiness.isDrawn(location.entryId) ||
18
+ Date.now() - startedAt >= NativeSplash.drawTimeoutMs;
19
+ const hide = () => {
20
+ NativeSplash.#hidden = true;
21
+ void splashScreen.hide().catch((error: unknown) => debugFrame("splash.hide.failed", { error: String(error) }));
22
+ };
23
+
24
+ const check = () => {
25
+ frame = requestAnimationFrame(isDrawn() ? hide : check);
26
+ };
27
+ frame = requestAnimationFrame(check);
28
+ return () => cancelAnimationFrame(frame);
29
+ }
30
+ }
@@ -136,12 +136,16 @@ const commitWhenFrameReady = ({
136
136
  const waitUntilReady = () => {
137
137
  if (pendingNavigation.current !== intent) return;
138
138
  const elapsedMs = Date.now() - intent.createdAt;
139
- const ready = isPendingFrameReady({
139
+ const isFrameReady = isPendingFrameReady({
140
140
  path: toLocation.pathRoute.path,
141
141
  pendingFrameSlots: latestPendingFrameSlots.current,
142
142
  elapsedMs,
143
143
  });
144
- if (ready) {
144
+ const isDrawn =
145
+ !CsrPageReadiness.drawsLater(toLocation) ||
146
+ CsrPageReadiness.isDrawn(toLocation.entryId) ||
147
+ elapsedMs >= PAGE_DRAWN_TIMEOUT_MS;
148
+ if (isFrameReady && isDrawn) {
145
149
  void commitNavigation(elapsedMs >= PENDING_FRAME_READY_TIMEOUT_MS);
146
150
  return;
147
151
  }
@@ -11,10 +11,14 @@ import {
11
11
  type UseCsrTransition,
12
12
  } from "akanjs/client";
13
13
  import { clamp } from "akanjs/common";
14
- import { useCallback, useEffect, useMemo, useRef } from "react";
14
+ import { type RefObject, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
15
+ import { CsrSlide, type CsrSlideFrame } from "./csrSlide";
16
+ import { STACK_TRANSITION_DURATION_MS } from "./useFrameRuntime";
15
17
 
16
18
  const linearEasing = (t: number) => t;
17
19
 
20
+ const stackEasing = (t: number) => 1 - (1 - t) ** 4;
21
+
18
22
  const getBottomInsetTop = (clientHeight: number, pageState: PathRoute["pageState"]) =>
19
23
  clientHeight - pageState.bottomInset - pageState.bottomSafeArea;
20
24
 
@@ -154,7 +158,8 @@ const usePlayOnForward = (
154
158
  { history, location }: RouteState,
155
159
  config: { duration: number; easing?: (t: number) => number },
156
160
  ) => {
157
- useEffect(() => {
161
+
162
+ useLayoutEffect(() => {
158
163
  if (history.current.type === "forward") {
159
164
  void transUnit.start(transUnitRange[0], { immediate: true });
160
165
  void transUnit.start(transUnitRange[1], { config });
@@ -353,8 +358,77 @@ const useScaleOutTrans = (routeState: RouteState): UseCsrTransition => {
353
358
  };
354
359
  };
355
360
 
361
+ const useStackSlide = (
362
+ { transUnit, transUnitRange }: Pick<UseCsrTransition, "transUnit" | "transUnitRange">,
363
+ { history, location, onBack }: RouteState,
364
+ frame: CsrSlideFrame,
365
+ backConfigRef: RefObject<{ duration: number } | null>,
366
+ ) => {
367
+ const slideRef = useRef<CsrSlide | null>(null);
368
+ const frameRef = useRef(frame);
369
+ frameRef.current = frame;
370
+ const cancelSlide = useCallback(() => {
371
+ slideRef.current?.cancel();
372
+ slideRef.current = null;
373
+ }, []);
374
+
375
+ const holdSlide = useCallback(() => {
376
+ const unit = slideRef.current?.unitNow() ?? null;
377
+ cancelSlide();
378
+ if (unit === null) return transUnit.get();
379
+ void transUnit.start(unit, { immediate: true });
380
+ return unit;
381
+ }, []);
382
+ useEffect(() => {
383
+ onBack.current.stack = async () => {
384
+ const config = backConfigRef.current;
385
+ backConfigRef.current = null;
386
+ const fromUnit = holdSlide();
387
+ const share = Math.abs(transUnitRange[0] - fromUnit) / Math.max(1, transUnitRange[0]);
388
+ const slide = CsrSlide.isCalm
389
+ ? null
390
+ : CsrSlide.play(frameRef.current, {
391
+ fromUnit,
392
+ toUnit: transUnitRange[0],
393
+ duration: config?.duration ?? Math.max(120, STACK_TRANSITION_DURATION_MS * share),
394
+ keep: true,
395
+ });
396
+ if (!slide) {
397
+ await transUnit.start(transUnitRange[0], config ? { config } : undefined);
398
+ return;
399
+ }
400
+
401
+ slideRef.current = slide;
402
+ await slide.finished;
403
+ await transUnit.start(transUnitRange[0], { immediate: true });
404
+ };
405
+ }, [transUnitRange]);
406
+ useLayoutEffect(() => {
407
+ cancelSlide();
408
+ if (history.current.type !== "forward" || CsrSlide.isCalm) {
409
+ void transUnit.start(transUnitRange[1], { immediate: true });
410
+ return;
411
+ }
412
+ const slide = CsrSlide.play(frameRef.current, {
413
+ fromUnit: transUnitRange[0],
414
+ toUnit: transUnitRange[1],
415
+ duration: STACK_TRANSITION_DURATION_MS,
416
+ });
417
+ if (slide) {
418
+ slideRef.current = slide;
419
+ void transUnit.start(transUnitRange[1], { immediate: true });
420
+ return;
421
+ }
422
+ void transUnit.start(transUnitRange[0], { immediate: true });
423
+ void transUnit.start(transUnitRange[1], {
424
+ config: { duration: STACK_TRANSITION_DURATION_MS, easing: stackEasing },
425
+ });
426
+ }, [location.entryId ?? location.pathname]);
427
+ return holdSlide;
428
+ };
429
+
356
430
  const useStackTrans = (routeState: RouteState): UseCsrTransition => {
357
- const { clientWidth, clientHeight, location, prevLocation, onBack, pageContentRef } = routeState;
431
+ const { clientWidth, clientHeight, location, prevLocation, pageContentRef } = routeState;
358
432
  const spring = useSlideSpring(clientWidth);
359
433
  const { transUnit, transUnitRange, transProgress } = spring;
360
434
  const initThreshold = useMemo(() => Math.floor(clientWidth), [clientWidth]);
@@ -382,15 +456,14 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
382
456
  pageContent.style.touchAction = scrollLock.touchAction;
383
457
  scrollLockRef.current = null;
384
458
  }, [pageContentRef]);
385
- useEffect(() => {
386
- onBack.current.stack = async () => {
387
- const config = stackBackConfigRef.current;
388
- stackBackConfigRef.current = null;
389
- await transUnit.start(transUnitRange[0], config ? { config } : undefined);
390
- };
391
- }, [transUnitRange]);
459
+ const holdSlide = useStackSlide(
460
+ spring,
461
+ routeState,
462
+ { width: clientWidth, height: clientHeight, pageState, prevPageState },
463
+ stackBackConfigRef,
464
+ );
392
465
  useEffect(() => unlockPageScroll, [unlockPageScroll]);
393
- usePlayOnForward(spring, routeState, { duration: 150 });
466
+ const dragOriginRef = useRef(0);
394
467
 
395
468
  const pageBind = useDrag(
396
469
  ({ first, last, movement: [mx, my], velocity: [vx], direction: [dx], initial: [ix], cancel }) => {
@@ -415,6 +488,7 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
415
488
  }
416
489
  if (absX < GESTURE_INTENT_THRESHOLD || absX <= absY * GESTURE_AXIS_LOCK_RATIO) return;
417
490
  gestureIntent.current = "gesture";
491
+ dragOriginRef.current = holdSlide();
418
492
  lockPageScroll();
419
493
  void Device.getDevice().hideKeyboard();
420
494
  }
@@ -422,13 +496,14 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
422
496
  cancel();
423
497
  return;
424
498
  }
425
- if (mx < transUnitRange[1]) void transUnit.start(transUnitRange[1], { immediate: true });
426
- else if (mx > transUnitRange[0]) void transUnit.start(transUnitRange[0], { immediate: true });
427
- else if (!last) void transUnit.start(mx, { immediate: true });
499
+ const unit = dragOriginRef.current + mx;
500
+ if (unit < transUnitRange[1]) void transUnit.start(transUnitRange[1], { immediate: true });
501
+ else if (unit > transUnitRange[0]) void transUnit.start(transUnitRange[0], { immediate: true });
502
+ else if (!last) void transUnit.start(unit, { immediate: true });
428
503
  if (last) {
429
- const shouldComplete = mx > threshold || (dx > 0 && vx > STACK_VELOCITY_DISMISS_THRESHOLD);
504
+ const shouldComplete = unit > threshold || (dx > 0 && vx > STACK_VELOCITY_DISMISS_THRESHOLD);
430
505
  const target = shouldComplete ? transUnitRange[0] : transUnitRange[1];
431
- const duration = getVelocityAwareDuration(target - mx, vx, shouldComplete ? 150 : 180);
506
+ const duration = getVelocityAwareDuration(target - unit, vx, shouldComplete ? 150 : 180);
432
507
  if (shouldComplete) stackBackConfigRef.current = { duration };
433
508
  else void transUnit.start(transUnitRange[1], { config: { duration } });
434
509
  unlockPageScroll();
@@ -437,6 +512,7 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
437
512
  clientRouter.back();
438
513
  void clientRouter.navigation().catch(() => {
439
514
  stackBackConfigRef.current = null;
515
+ holdSlide();
440
516
  void transUnit.start(transUnitRange[1], { config: { duration } });
441
517
  });
442
518
  }
@@ -28,6 +28,7 @@ export type FrameSlotBucket = "active" | "pending";
28
28
  export type FrameSlotMapByBucket = Record<FrameSlotBucket, FrameSlotMap>;
29
29
  export const PENDING_FRAME_READY_TIMEOUT_MS = 80;
30
30
  export const PENDING_FRAME_READY_MAX_TIMEOUT_MS = 120;
31
+ export const STACK_TRANSITION_DURATION_MS = 350;
31
32
  export const KEYBOARD_SHOW_ANIMATION_DURATION_MS = 420;
32
33
  export const KEYBOARD_HIDE_ANIMATION_DURATION_MS = 90;
33
34
  export const KEYBOARD_SHOW_ANIMATION_EASING = "cubic-bezier(0.16, 1, 0.3, 1)";
@@ -561,7 +562,8 @@ export function createFrameSnapshot({
561
562
  const getTransitionDuration = (type: TransitionType) => {
562
563
  if (typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches) return 1;
563
564
  if (type === "bottomUp" || type === "scaleOut") return 220;
564
- if (type === "fade" || type === "stack") return 150;
565
+ if (type === "stack") return STACK_TRANSITION_DURATION_MS;
566
+ if (type === "fade") return 150;
565
567
  return 0;
566
568
  };
567
569