akanjs 3.3.0-rc.42 → 3.3.0-rc.44

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.42",
3
+ "version": "3.3.0-rc.44",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -0,0 +1,9 @@
1
+ import { type RefObject } from "react";
2
+ /**
3
+ * Keeps an overlay drawn after `open` turns false for as long as the animation its `data-leaving` styles start on
4
+ * `ref`'s element plays, and unmounts it at once when there is none.
5
+ */
6
+ export declare const useLeave: (open: boolean, ref: RefObject<Element | null>) => {
7
+ drawn: boolean;
8
+ leaving: boolean;
9
+ };
@@ -1,4 +1,4 @@
1
- import type { PathRoute } from "akanjs/client";
1
+ import type { Location, PathRoute } from "akanjs/client";
2
2
  type FramePageState = PathRoute["pageState"];
3
3
  type Keyframe = Record<string, string>;
4
4
  export interface CsrSlideFrame {
@@ -19,6 +19,8 @@ export declare class CsrSlide {
19
19
  private constructor();
20
20
  static get supported(): boolean;
21
21
  static get isCalm(): boolean;
22
+ static holdStill(kind: "push" | "replace", from: Location, to: Location): boolean;
23
+ static isStill(entryId: string | undefined): boolean;
22
24
  static play(frame: CsrSlideFrame, { fromUnit, toUnit, duration, keep }: CsrSlidePlay): CsrSlide | null;
23
25
  /** What each part's style is with the page `unit` px from its place: 0 shown, the frame's width gone right. */
24
26
  static partsAt({ width, height, pageState, prevPageState }: CsrSlideFrame, unit: number): Record<string, Keyframe>;
@@ -19,7 +19,7 @@ export declare const useCsrNavigation: (input: CsrNavigationInput) => {
19
19
  settle: (prev: LocationState["prevLocation"]) => void;
20
20
  cancelPendingNavigation: () => NavigationIntent | null;
21
21
  broadcastSyncNavigation: (kind: "replace" | "pop" | "push" | "back", href: string) => void;
22
- runForwardNavigation: (kind: "push" | "replace", href: string, { scrollToTop }?: RouteOptions) => void;
22
+ runForwardNavigation: (requested: "push" | "replace", href: string, { scrollToTop }?: RouteOptions) => void;
23
23
  };
24
24
  export type CsrNavigation = ReturnType<typeof useCsrNavigation>;
25
25
  export {};
@@ -14,12 +14,32 @@ import { buttonRecipe } from "../Button";
14
14
  import { OverlayPortalProvider, overlayZ, useOverlayLayerProps, useOverlayPortal } from "../overlayLayer";
15
15
  import { SheetHandle } from "../SheetHandle";
16
16
  import { useDismissDrag } from "../useDismissDrag";
17
+ import { useLeave } from "../useLeave";
17
18
 
18
19
  import { isSheetNow, type ModalSurfaceProps, modalSurfaceOf } from "./modalSurface";
19
20
  import { DialogContext } from "./Provider";
20
21
 
21
22
  const DISMISS_DRAG_PX = 120;
22
23
 
24
+ interface CloseSlotProps {
25
+ className: string;
26
+ closeButton?: ReactNode | false;
27
+ onClose: () => void;
28
+ }
29
+
30
+ const CloseSlot = ({ className, closeButton, onClose }: CloseSlotProps) => {
31
+ if (closeButton === false) return null;
32
+ return (
33
+ <div className={cn("absolute right-3 z-10", className)} onClick={onClose}>
34
+ {closeButton ?? (
35
+ <button aria-label="Close" className={buttonRecipe({ variant: "ghost", size: "icon" }, "size-8")} type="button">
36
+ <BiX className="text-xl" />
37
+ </button>
38
+ )}
39
+ </div>
40
+ );
41
+ };
42
+
23
43
  export interface ModalProps extends ModalSurfaceProps {
24
44
  className?: string;
25
45
  bodyClassName?: string;
@@ -52,6 +72,7 @@ export const Modal = ({
52
72
  const isActive = open && isCurrentPage;
53
73
  const [root, setRoot] = useState<HTMLDivElement | null>(null);
54
74
  const [dragY, setDragY] = useState(0);
75
+ const { drawn, leaving } = useLeave(open, ref);
55
76
  const surface = modalSurfaceOf(presentation, placement);
56
77
 
57
78
  const { keyboardHeight } = useVisualViewportFrame();
@@ -63,9 +84,10 @@ export const Modal = ({
63
84
  : null;
64
85
 
65
86
  const requestClose = useCallback(() => {
66
- if (confirmClose && !window.confirm(l("base.confirmClose"))) return;
87
+ if (confirmClose && !window.confirm(l("base.confirmClose"))) return false;
67
88
  setOpen(false);
68
89
  onCancel?.();
90
+ return true;
69
91
  }, [confirmClose, l, onCancel, setOpen]);
70
92
 
71
93
  const dismissDrag = useDismissDrag({
@@ -73,9 +95,9 @@ export const Modal = ({
73
95
  flickSpeed: 0.5,
74
96
  enabled: () => isSheetNow(presentation, placement),
75
97
  onDrag: (y) => setDragY(Math.max(0, y)),
98
+
76
99
  onRelease: ({ dismiss }) => {
77
- setDragY(0);
78
- if (dismiss) requestClose();
100
+ if (!dismiss || !requestClose()) setDragY(0);
79
101
  },
80
102
  gesture: { axis: "y", filterTaps: true },
81
103
  });
@@ -105,13 +127,21 @@ export const Modal = ({
105
127
  };
106
128
  }, [registerDismiss]);
107
129
 
108
- if (!open || !portalElement) return null;
130
+ useEffect(() => {
131
+ if (!drawn) setDragY(0);
132
+ }, [drawn]);
133
+
134
+ if (!drawn || !portalElement) return null;
109
135
 
110
136
  return createPortal(
111
137
  <div
112
138
  {...overlayLayerProps}
113
139
  ref={setRoot}
114
- className={cn("fixed inset-0 flex bg-black/40", surface.root)}
140
+ data-leaving={leaving ? "" : undefined}
141
+ className={cn(
142
+ "fixed inset-0 flex bg-black/40 transition-[background-color] duration-200 data-leaving:pointer-events-none data-leaving:bg-transparent",
143
+ surface.root,
144
+ )}
115
145
  style={{ zIndex: overlayZ.modal, visibility: isCurrentPage ? undefined : "hidden", ...keyboardInset }}
116
146
  onClick={(event) => {
117
147
  if (event.target !== event.currentTarget) return;
@@ -122,6 +152,7 @@ export const Modal = ({
122
152
  <OverlayPortalProvider value={root}>
123
153
  <div
124
154
  ref={ref}
155
+ data-leaving={leaving ? "" : undefined}
125
156
 
126
157
  className={cn(
127
158
  "relative flex w-full flex-col overflow-hidden bg-card text-card-foreground shadow-lg outline-none",
@@ -136,21 +167,7 @@ export const Modal = ({
136
167
  aria-describedby={contentId}
137
168
  tabIndex={-1}
138
169
  >
139
- {/* The dismissal is on the slot, not on the button inside it, so a replacement needs no wiring — a
140
- click on whatever the caller put here bubbles to exactly one handler. */}
141
- {closeButton === false ? null : (
142
- <div className={cn("absolute right-3 z-10", surface.close)} onClick={() => requestClose()}>
143
- {closeButton ?? (
144
- <button
145
- aria-label="Close"
146
- className={buttonRecipe({ variant: "ghost", size: "icon" }, "size-8")}
147
- type="button"
148
- >
149
- <BiX className="text-xl" />
150
- </button>
151
- )}
152
- </div>
153
- )}
170
+ <CloseSlot className={surface.close} closeButton={closeButton} onClose={() => requestClose()} />
154
171
  {presentation === "dialog" ? null : (
155
172
  <SheetHandle {...dragProps} className={cn("touch-none pt-2", surface.handle)} />
156
173
  )}
package/ui/useLeave.ts ADDED
@@ -0,0 +1,42 @@
1
+ "use client";
2
+ import { type RefObject, useLayoutEffect, useState } from "react";
3
+
4
+ const LEAVE_CAP_MS = 600;
5
+
6
+ const leavingAnimationsOf = (element: Element | null) =>
7
+ (element?.getAnimations?.() ?? []).filter((animation) => {
8
+ if (animation.playState === "finished") return false;
9
+ const endTime = animation.effect?.getComputedTiming().endTime;
10
+ return typeof endTime === "number" && Number.isFinite(endTime);
11
+ });
12
+
13
+ /**
14
+ * Keeps an overlay drawn after `open` turns false for as long as the animation its `data-leaving` styles start on
15
+ * `ref`'s element plays, and unmounts it at once when there is none.
16
+ */
17
+ export const useLeave = (open: boolean, ref: RefObject<Element | null>) => {
18
+ const [drawn, setDrawn] = useState(open);
19
+ if (open && !drawn) setDrawn(true);
20
+ const leaving = drawn && !open;
21
+ useLayoutEffect(() => {
22
+ if (!leaving) return;
23
+ const animations = leavingAnimationsOf(ref.current);
24
+ if (!animations.length) {
25
+ setDrawn(false);
26
+ return;
27
+ }
28
+ let settled = false;
29
+ const settle = () => {
30
+ if (settled) return;
31
+ settled = true;
32
+ setDrawn(false);
33
+ };
34
+ const cap = window.setTimeout(settle, LEAVE_CAP_MS);
35
+ void Promise.allSettled(animations.map((animation) => animation.finished)).then(settle);
36
+ return () => {
37
+ settled = true;
38
+ window.clearTimeout(cap);
39
+ };
40
+ }, [leaving]);
41
+ return { drawn, leaving };
42
+ };
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import type { PathRoute } from "akanjs/client";
2
+ import type { Location, PathRoute } from "akanjs/client";
3
3
 
4
4
  type FramePageState = PathRoute["pageState"];
5
5
  type Keyframe = Record<string, string>;
@@ -43,6 +43,20 @@ export class CsrSlide {
43
43
  return typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
44
44
  }
45
45
 
46
+ static #stillEntryId: string | null = null;
47
+
48
+ static holdStill(kind: "push" | "replace", from: Location, to: Location) {
49
+ const isStill =
50
+ kind === "replace" &&
51
+ from.pathRoute.pageState.transition === "none" &&
52
+ to.pathRoute.pageState.transition !== "none";
53
+ CsrSlide.#stillEntryId = isStill ? (to.entryId ?? null) : null;
54
+ return isStill;
55
+ }
56
+ static isStill(entryId: string | undefined) {
57
+ return !!entryId && entryId === CsrSlide.#stillEntryId;
58
+ }
59
+
46
60
  static play(frame: CsrSlideFrame, { fromUnit, toUnit, duration, keep = false }: CsrSlidePlay): CsrSlide | null {
47
61
  if (!CsrSlide.supported || duration <= 0) return null;
48
62
  const from = CsrSlide.partsAt(frame, fromUnit);
@@ -11,6 +11,7 @@ import {
11
11
  import { useCallback } from "react";
12
12
  import { CsrStack } from "./CsrStack";
13
13
  import { CsrPageReadiness } from "./csrPageReadiness";
14
+ import { CsrSlide } from "./csrSlide";
14
15
  import type { CsrKeyboard } from "./useCsrKeyboard";
15
16
  import type { CsrLocationState } from "./useCsrLocationState";
16
17
  import { type CsrRefs, currentRouter } from "./useCsrRefs";
@@ -238,9 +239,11 @@ export const useCsrNavigation = (input: CsrNavigationInput) => {
238
239
  }, [clearPendingSlots]);
239
240
  const broadcastSyncNavigation = useBroadcastSyncNavigation(refs);
240
241
  const runForwardNavigation = useCallback(
241
- (kind: "push" | "replace", href: string, { scrollToTop }: RouteOptions = {}) => {
242
+ (requested: "push" | "replace", href: string, { scrollToTop }: RouteOptions = {}) => {
242
243
  const fromLocation = getCurrentLocation();
243
244
  const target = getLocation(href);
245
+
246
+ const kind = requested === "push" && target.pathname === fromLocation.pathname ? "replace" : requested;
244
247
  const cached = target.pathRoute.pageState.cache
245
248
  ? nav.history.current.cachedLocationMap.get(target.pathRoute.path)
246
249
  : undefined;
@@ -255,7 +258,9 @@ export const useCsrNavigation = (input: CsrNavigationInput) => {
255
258
  ? cached
256
259
  : target;
257
260
  const scrollTop = pageContentRef.current?.scrollTop ?? 0;
261
+ const isStill = CsrSlide.holdStill(kind, fromLocation, toLocation);
258
262
  const slides =
263
+ !isStill &&
259
264
  CsrStack.keyOf(toLocation) !== CsrStack.keyOf(fromLocation) &&
260
265
  shouldPrepareFrameTransition(href) &&
261
266
  toLocation.pathRoute.pageState.transition !== "none";
@@ -162,7 +162,7 @@ const usePlayOnForward = (
162
162
  ) => {
163
163
 
164
164
  useLayoutEffect(() => {
165
- if (history.current.type === "forward") {
165
+ if (history.current.type === "forward" && !CsrSlide.isStill(location.entryId)) {
166
166
  void transUnit.start(transUnitRange[0], { immediate: true });
167
167
  void transUnit.start(transUnitRange[1], { config });
168
168
  } else void transUnit.start(transUnitRange[1], { immediate: true });
@@ -409,7 +409,7 @@ const useStackSlide = (
409
409
  cancelSlide();
410
410
 
411
411
  const isStack = frameRef.current.pageState.transition === "stack";
412
- if (history.current.type !== "forward" || CsrSlide.isCalm || !isStack) {
412
+ if (history.current.type !== "forward" || CsrSlide.isCalm || !isStack || CsrSlide.isStill(location.entryId)) {
413
413
  void transUnit.start(transUnitRange[1], { immediate: true });
414
414
  return;
415
415
  }
@@ -592,7 +592,9 @@ const useBottomUpTrans = (routeState: RouteState): UseCsrTransition => {
592
592
  usePlayOnForward(spring, routeState, { duration: 220, easing: linearEasing });
593
593
 
594
594
  const pageBind = useDrag(
595
- ({ first, last, movement: [, my], initial: [, iy], cancel, event, target }) => {
595
+ ({ first, last, tap, movement: [, my], initial: [, iy], cancel, event, target }) => {
596
+
597
+ if (tap) return;
596
598
  if (iy > initThreshold || (first && scrollerTakesDrag(target, pageContentRef.current, "y", my))) {
597
599
  cancel();
598
600
  return;