akanjs 3.3.0-rc.18 → 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 +1 -1
- package/types/webkit/csrSlide.d.ts +30 -0
- package/ui/System/CSR.tsx +2 -0
- package/webkit/csrSlide.ts +130 -0
- package/webkit/useCsrTransitions.ts +87 -15
package/package.json
CHANGED
|
@@ -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 {};
|
package/ui/System/CSR.tsx
CHANGED
|
@@ -349,6 +349,7 @@ const CSRFrameSlotTargets = ({ slot }: { slot: FrameSlotTarget }) => {
|
|
|
349
349
|
<animated.div
|
|
350
350
|
key={id}
|
|
351
351
|
id={id}
|
|
352
|
+
data-page-type={pageType}
|
|
352
353
|
inert={hidden}
|
|
353
354
|
aria-hidden={hidden ? true : undefined}
|
|
354
355
|
className={cn(
|
|
@@ -470,6 +471,7 @@ const CSRPageContainer = ({ entry, prefix, layoutStyle }: CSRPageContainerProps)
|
|
|
470
471
|
<animated.div
|
|
471
472
|
id={`pageContainer-${key}`}
|
|
472
473
|
data-path={pathRoute.path}
|
|
474
|
+
data-page-type={pageType}
|
|
473
475
|
inert={pageType !== "current"}
|
|
474
476
|
aria-hidden={pageType === "current" ? undefined : true}
|
|
475
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
|
+
}
|
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
type UseCsrTransition,
|
|
12
12
|
} from "akanjs/client";
|
|
13
13
|
import { clamp } from "akanjs/common";
|
|
14
|
-
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
|
14
|
+
import { type RefObject, useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
|
15
|
+
import { CsrSlide, type CsrSlideFrame } from "./csrSlide";
|
|
15
16
|
import { STACK_TRANSITION_DURATION_MS } from "./useFrameRuntime";
|
|
16
17
|
|
|
17
18
|
const linearEasing = (t: number) => t;
|
|
@@ -357,8 +358,77 @@ const useScaleOutTrans = (routeState: RouteState): UseCsrTransition => {
|
|
|
357
358
|
};
|
|
358
359
|
};
|
|
359
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
|
+
|
|
360
430
|
const useStackTrans = (routeState: RouteState): UseCsrTransition => {
|
|
361
|
-
const { clientWidth, clientHeight, location, prevLocation,
|
|
431
|
+
const { clientWidth, clientHeight, location, prevLocation, pageContentRef } = routeState;
|
|
362
432
|
const spring = useSlideSpring(clientWidth);
|
|
363
433
|
const { transUnit, transUnitRange, transProgress } = spring;
|
|
364
434
|
const initThreshold = useMemo(() => Math.floor(clientWidth), [clientWidth]);
|
|
@@ -386,15 +456,14 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
|
|
|
386
456
|
pageContent.style.touchAction = scrollLock.touchAction;
|
|
387
457
|
scrollLockRef.current = null;
|
|
388
458
|
}, [pageContentRef]);
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}, [transUnitRange]);
|
|
459
|
+
const holdSlide = useStackSlide(
|
|
460
|
+
spring,
|
|
461
|
+
routeState,
|
|
462
|
+
{ width: clientWidth, height: clientHeight, pageState, prevPageState },
|
|
463
|
+
stackBackConfigRef,
|
|
464
|
+
);
|
|
396
465
|
useEffect(() => unlockPageScroll, [unlockPageScroll]);
|
|
397
|
-
|
|
466
|
+
const dragOriginRef = useRef(0);
|
|
398
467
|
|
|
399
468
|
const pageBind = useDrag(
|
|
400
469
|
({ first, last, movement: [mx, my], velocity: [vx], direction: [dx], initial: [ix], cancel }) => {
|
|
@@ -419,6 +488,7 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
|
|
|
419
488
|
}
|
|
420
489
|
if (absX < GESTURE_INTENT_THRESHOLD || absX <= absY * GESTURE_AXIS_LOCK_RATIO) return;
|
|
421
490
|
gestureIntent.current = "gesture";
|
|
491
|
+
dragOriginRef.current = holdSlide();
|
|
422
492
|
lockPageScroll();
|
|
423
493
|
void Device.getDevice().hideKeyboard();
|
|
424
494
|
}
|
|
@@ -426,13 +496,14 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
|
|
|
426
496
|
cancel();
|
|
427
497
|
return;
|
|
428
498
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
else if (
|
|
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 });
|
|
432
503
|
if (last) {
|
|
433
|
-
const shouldComplete =
|
|
504
|
+
const shouldComplete = unit > threshold || (dx > 0 && vx > STACK_VELOCITY_DISMISS_THRESHOLD);
|
|
434
505
|
const target = shouldComplete ? transUnitRange[0] : transUnitRange[1];
|
|
435
|
-
const duration = getVelocityAwareDuration(target -
|
|
506
|
+
const duration = getVelocityAwareDuration(target - unit, vx, shouldComplete ? 150 : 180);
|
|
436
507
|
if (shouldComplete) stackBackConfigRef.current = { duration };
|
|
437
508
|
else void transUnit.start(transUnitRange[1], { config: { duration } });
|
|
438
509
|
unlockPageScroll();
|
|
@@ -441,6 +512,7 @@ const useStackTrans = (routeState: RouteState): UseCsrTransition => {
|
|
|
441
512
|
clientRouter.back();
|
|
442
513
|
void clientRouter.navigation().catch(() => {
|
|
443
514
|
stackBackConfigRef.current = null;
|
|
515
|
+
holdSlide();
|
|
444
516
|
void transUnit.start(transUnitRange[1], { config: { duration } });
|
|
445
517
|
});
|
|
446
518
|
}
|