@celestia-island/hikari 0.55.502 → 0.55.504
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/dist/types/components/HkDirectionalSwap.d.ts +157 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/mermaid/types.d.ts +4 -1
- package/dist/types/mermaid/wasmEngine.d.ts +16 -0
- package/package.json +1 -1
- package/src/components/HkDirectionalSwap.scss +116 -0
- package/src/components/HkDirectionalSwap.test.tsx +224 -0
- package/src/components/HkDirectionalSwap.tsx +278 -0
- package/src/components/HkMermaidDiagram.scss +33 -0
- package/src/components/HkMermaidDiagram.test.tsx +45 -0
- package/src/components/HkMermaidDiagram.tsx +73 -0
- package/src/components/__tests__/hkMermaidLayout.test.ts +50 -0
- package/src/components/__tests__/hkMermaidParse.test.ts +32 -0
- package/src/errorLandingStandalone/error-landing.html +1 -1
- package/src/index.ts +2 -0
- package/src/mermaid/layout.ts +19 -0
- package/src/mermaid/types.ts +16 -1
- package/src/mermaid/wasmEngine.ts +32 -7
- package/src/wasm/highlight/hikari_wasm_highlight.js +1 -1
- package/src/wasm/mermaid/hikari_wasm_mermaid.js +1 -1
- package/src/wasm/qr/hikari_wasm_qr.js +1 -1
- package/src/wasm/scene3d/hikari_wasm_scene3d.js +1 -1
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HkDirectionalSwap — the state-keyed, DIRECTION-AWARE content swap.
|
|
3
|
+
*
|
|
4
|
+
* The sibling of HkCrossfade, for the one job a pure dissolve cannot
|
|
5
|
+
* express: a swap where the reader moved FORWARD or BACK, and the motion
|
|
6
|
+
* should say so. Panes slide in from one side and out to the other while
|
|
7
|
+
* they cross-fade — the vocabulary a step flow, a wizard, an onboarding
|
|
8
|
+
* carousel and a source/preview toggle all speak.
|
|
9
|
+
*
|
|
10
|
+
* Why a separate component and not a prop on HkCrossfade: that component's
|
|
11
|
+
* contract is "pure opacity, no transform, EVER" — its consumers (sidebar
|
|
12
|
+
* rail swap, login steps, tabs, settings) rely on the dissolve never
|
|
13
|
+
* translating anything, because a translated live region is a different
|
|
14
|
+
* layout. Adding motion there would silently change every one of them.
|
|
15
|
+
*
|
|
16
|
+
* Contracts this carries, all of them learned by the family's own swaps:
|
|
17
|
+
*
|
|
18
|
+
* - CONCURRENT, IN ONE CELL. Old and new occupy the same grid cell
|
|
19
|
+
* (HkCrossfade's geometry), so the surface keeps the taller
|
|
20
|
+
* generation's height for the whole exchange — a swap can never
|
|
21
|
+
* collapse the page mid-animation. No `mode="out-in"`: that would
|
|
22
|
+
* reintroduce an exit blank.
|
|
23
|
+
* - DIRECTION IS DATA, NOT A CLASS THE HOST HAS TO REMEMBER. `direction`
|
|
24
|
+
* flips the travel axis per swap: forward enters from the end edge and
|
|
25
|
+
* leaves through the start edge; back mirrors. RTL mirrors again (the
|
|
26
|
+
* stylesheet flips on `[dir="rtl"]`).
|
|
27
|
+
* - THE LEAVING COPY IS DEAD CONTENT. It gets `inert` + `aria-hidden` for
|
|
28
|
+
* the dissolve, so it leaves the tab order and the accessibility tree —
|
|
29
|
+
* a frozen duplicate of a pane is otherwise reachable by Tab and by a
|
|
30
|
+
* screen reader's virtual cursor.
|
|
31
|
+
* - REDUCED MOTION KEEPS THE FADE, DROPS THE TRAVEL. Translation is the
|
|
32
|
+
* part motion-sensitive readers ask to be spared; a short opacity
|
|
33
|
+
* exchange carries the change of state without it. (`HkPhaseTransition`
|
|
34
|
+
* precedent.)
|
|
35
|
+
* - CLIENT-ONLY. It measures nothing, but its Transition runs on the
|
|
36
|
+
* client; SSR renders the first generation only, which is the no-JS
|
|
37
|
+
* baseline hosts already ship.
|
|
38
|
+
* - A STALL WATCHDOG, same shape as HkCrossfade's: if the browser wedges
|
|
39
|
+
* the Transition engine (rAF frozen), the frozen leaving item is swept
|
|
40
|
+
* and the wrapper is dropped so keys apply synchronously from then on.
|
|
41
|
+
*/
|
|
42
|
+
import { type PropType } from "vue";
|
|
43
|
+
import "./HkDirectionalSwap.scss";
|
|
44
|
+
/** Which way the reader moved. See the module note. */
|
|
45
|
+
export type SwapDirection = "forward" | "back";
|
|
46
|
+
/**
|
|
47
|
+
* The direction a key change implies, given the two ends' positions in the
|
|
48
|
+
* host's own sequence. Pure, so a host can pin it without a DOM.
|
|
49
|
+
*
|
|
50
|
+
* Unknown positions (either key absent from `order`) resolve FORWARD: a
|
|
51
|
+
* content swap that is not a step (a tab, a mode toggle) still wants a
|
|
52
|
+
* consistent, repeatable direction rather than a flicker between two.
|
|
53
|
+
*/
|
|
54
|
+
export declare function resolveSwapDirection(order: ReadonlyArray<string | number>, previous: string | number, next: string | number): SwapDirection;
|
|
55
|
+
declare const _default: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
56
|
+
/** Identity of the content on stage; changing it starts the swap. */
|
|
57
|
+
swapKey: {
|
|
58
|
+
type: PropType<string | number>;
|
|
59
|
+
required: true;
|
|
60
|
+
};
|
|
61
|
+
/** Travel direction of THIS swap. Hosts that own a sequence can derive
|
|
62
|
+
* it with `resolveSwapDirection`; a plain toggle passes `"forward"`. */
|
|
63
|
+
direction: {
|
|
64
|
+
type: PropType<SwapDirection>;
|
|
65
|
+
default: string;
|
|
66
|
+
};
|
|
67
|
+
/** Travel axis. `horizontal` (the default) is the reading-direction
|
|
68
|
+
* vocabulary: forward enters from the end edge. `vertical` is for
|
|
69
|
+
* stacked surfaces (a wizard moving down a page). */
|
|
70
|
+
axis: {
|
|
71
|
+
type: PropType<"horizontal" | "vertical">;
|
|
72
|
+
default: string;
|
|
73
|
+
};
|
|
74
|
+
/** Exchange duration, ms. Family default 300. */
|
|
75
|
+
duration: {
|
|
76
|
+
type: NumberConstructor;
|
|
77
|
+
default: number;
|
|
78
|
+
};
|
|
79
|
+
/** Travel distance, px. */
|
|
80
|
+
travel: {
|
|
81
|
+
type: NumberConstructor;
|
|
82
|
+
default: number;
|
|
83
|
+
};
|
|
84
|
+
/** Drop the Transition wrapper entirely (synchronous key swaps) — the
|
|
85
|
+
* escape a host uses when it knows the engine is wedged, and what the
|
|
86
|
+
* watchdog switches to on its own. */
|
|
87
|
+
disabled: {
|
|
88
|
+
type: BooleanConstructor;
|
|
89
|
+
default: boolean;
|
|
90
|
+
};
|
|
91
|
+
tag: {
|
|
92
|
+
type: StringConstructor;
|
|
93
|
+
default: string;
|
|
94
|
+
};
|
|
95
|
+
}>, () => import("vue/jsx-runtime").JSX.Element, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
96
|
+
/** Fired once the exchange settles — hosts forward this to their own
|
|
97
|
+
* scrollers/focus management, the HkTransitionContainer contract. */
|
|
98
|
+
swap: (_payload: {
|
|
99
|
+
key: string | number;
|
|
100
|
+
previous: string | number;
|
|
101
|
+
direction: SwapDirection;
|
|
102
|
+
}) => true;
|
|
103
|
+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
104
|
+
/** Identity of the content on stage; changing it starts the swap. */
|
|
105
|
+
swapKey: {
|
|
106
|
+
type: PropType<string | number>;
|
|
107
|
+
required: true;
|
|
108
|
+
};
|
|
109
|
+
/** Travel direction of THIS swap. Hosts that own a sequence can derive
|
|
110
|
+
* it with `resolveSwapDirection`; a plain toggle passes `"forward"`. */
|
|
111
|
+
direction: {
|
|
112
|
+
type: PropType<SwapDirection>;
|
|
113
|
+
default: string;
|
|
114
|
+
};
|
|
115
|
+
/** Travel axis. `horizontal` (the default) is the reading-direction
|
|
116
|
+
* vocabulary: forward enters from the end edge. `vertical` is for
|
|
117
|
+
* stacked surfaces (a wizard moving down a page). */
|
|
118
|
+
axis: {
|
|
119
|
+
type: PropType<"horizontal" | "vertical">;
|
|
120
|
+
default: string;
|
|
121
|
+
};
|
|
122
|
+
/** Exchange duration, ms. Family default 300. */
|
|
123
|
+
duration: {
|
|
124
|
+
type: NumberConstructor;
|
|
125
|
+
default: number;
|
|
126
|
+
};
|
|
127
|
+
/** Travel distance, px. */
|
|
128
|
+
travel: {
|
|
129
|
+
type: NumberConstructor;
|
|
130
|
+
default: number;
|
|
131
|
+
};
|
|
132
|
+
/** Drop the Transition wrapper entirely (synchronous key swaps) — the
|
|
133
|
+
* escape a host uses when it knows the engine is wedged, and what the
|
|
134
|
+
* watchdog switches to on its own. */
|
|
135
|
+
disabled: {
|
|
136
|
+
type: BooleanConstructor;
|
|
137
|
+
default: boolean;
|
|
138
|
+
};
|
|
139
|
+
tag: {
|
|
140
|
+
type: StringConstructor;
|
|
141
|
+
default: string;
|
|
142
|
+
};
|
|
143
|
+
}>> & Readonly<{
|
|
144
|
+
onSwap?: ((_payload: {
|
|
145
|
+
key: string | number;
|
|
146
|
+
previous: string | number;
|
|
147
|
+
direction: SwapDirection;
|
|
148
|
+
}) => any) | undefined;
|
|
149
|
+
}>, {
|
|
150
|
+
direction: SwapDirection;
|
|
151
|
+
disabled: boolean;
|
|
152
|
+
axis: "horizontal" | "vertical";
|
|
153
|
+
tag: string;
|
|
154
|
+
duration: number;
|
|
155
|
+
travel: number;
|
|
156
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
157
|
+
export default _default;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -302,6 +302,8 @@ export { default as HkConfirmDialog } from "./components/HkConfirmDialog";
|
|
|
302
302
|
export { default as HkCrossfade } from "./components/HkCrossfade";
|
|
303
303
|
export { default as HkDebugFloat } from "./components/HkDebugFloat";
|
|
304
304
|
export { clampDebugFloatPosition, debugFloatStorageKey, HK_DEBUG_FLOAT_DOCUMENT_Z, HK_DEBUG_FLOAT_STORAGE_PREFIX, type HkDebugFloatCorner, type HkDebugFloatPersisted, type HkDebugFloatSection, } from "./components/debugFloat";
|
|
305
|
+
export { default as HkDirectionalSwap, resolveSwapDirection } from "./components/HkDirectionalSwap";
|
|
306
|
+
export type { SwapDirection } from "./components/HkDirectionalSwap";
|
|
305
307
|
export { default as HkTransitionContainer } from "./components/HkTransitionContainer";
|
|
306
308
|
export { default as HkPaneHolder } from "./components/HkPaneHolder";
|
|
307
309
|
export { default as HkWindowControls, isApplePlatform } from "./components/HkWindowControls";
|
|
@@ -19,7 +19,10 @@
|
|
|
19
19
|
/** Flow direction of a node-edge diagram. */
|
|
20
20
|
export type MermaidDirection = "TD" | "BT" | "LR" | "RL";
|
|
21
21
|
/** Node border shape (the four shapes the family grammar supports). */
|
|
22
|
-
|
|
22
|
+
/** Node border shape. The Rust grammar emits these tokens (see
|
|
23
|
+
* `packages/wasm/mermaid/src/mermaid/types.rs` NodeShape); every one of them
|
|
24
|
+
* is painted by HkMermaidDiagram's node slot. */
|
|
25
|
+
export type MermaidNodeShape = "rect" | "rounded" | "diamond" | "circle" | "stadium" | "subroutine" | "cylinder" | "hexagon" | "parallelogram" | "trapezoid" | "asymmetric" | "doublecircle";
|
|
23
26
|
export interface MermaidNode {
|
|
24
27
|
id: string;
|
|
25
28
|
label: string;
|
|
@@ -18,5 +18,21 @@ export declare const mermaidCapabilities: {
|
|
|
18
18
|
interfaces: string[];
|
|
19
19
|
wasiModules: string[];
|
|
20
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* Rewrite a `stateDiagram` into the flowchart grammar the pest parser speaks.
|
|
23
|
+
*
|
|
24
|
+
* Two rewrites carry real semantics:
|
|
25
|
+
*
|
|
26
|
+
* - `[*]` is TWO different pseudostates — the initial marker and the final
|
|
27
|
+
* marker. The old blanket replace mapped both to one literal node, so a
|
|
28
|
+
* diagram read `… --> STATE_START_END` at the top AND at the bottom and the
|
|
29
|
+
* layout merged them into a single labelled box between the two (the audit
|
|
30
|
+
* recorded it as "«[*]» start and end collapse into one frame"). They now
|
|
31
|
+
* get distinct ids AND a circle shape with an empty label, which is what
|
|
32
|
+
* mermaid draws;
|
|
33
|
+
* - `A --> B : label` becomes `A -->|label| B` (the flowchart's caption form),
|
|
34
|
+
* which is the only shape the grammar can carry.
|
|
35
|
+
*/
|
|
36
|
+
export declare function normalizeStateDiagram(source: string): string;
|
|
21
37
|
export declare const wasmMermaidEngine: MermaidParserEngine;
|
|
22
38
|
export { HostClientError };
|
package/package.json
CHANGED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// HkDirectionalSwap — state-keyed, direction-aware content exchange.
|
|
2
|
+
//
|
|
3
|
+
// Geometry is HkCrossfade's (one-cell grid, both generations stacked in
|
|
4
|
+
// the SAME cell) so the surface keeps the taller generation's height for
|
|
5
|
+
// the whole exchange and can never collapse mid-swap. What this sheet adds
|
|
6
|
+
// is the TRAVEL: the outgoing copy leaves toward one edge while the
|
|
7
|
+
// incoming copy arrives from the other, each riding its own opacity ramp.
|
|
8
|
+
//
|
|
9
|
+
// Direction is data on the host (`data-direction`), axis too
|
|
10
|
+
// (`data-axis`), and RTL mirrors the horizontal vocabulary — the reader's
|
|
11
|
+
// "forward" is the reading direction's end edge, not the screen's right.
|
|
12
|
+
//
|
|
13
|
+
// Reduced motion keeps the opacity exchange and zeroes the travel: motion
|
|
14
|
+
// is what the preference is about, and the fade already carries the change
|
|
15
|
+
// of state (HkPhaseTransition precedent).
|
|
16
|
+
|
|
17
|
+
.hk-dswap {
|
|
18
|
+
display: grid;
|
|
19
|
+
overflow: clip;
|
|
20
|
+
|
|
21
|
+
> .hk-dswap-item {
|
|
22
|
+
grid-area: 1 / 1;
|
|
23
|
+
width: 100%;
|
|
24
|
+
min-width: 0;
|
|
25
|
+
// The block-axis twin: a grid item refuses to shrink below its content,
|
|
26
|
+
// which would let whole-pane swaps overflow a constrained host.
|
|
27
|
+
min-height: 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ── travel ───────────────────────────────────────────────────────────
|
|
32
|
+
// The entering copy starts offset toward the edge it comes FROM and
|
|
33
|
+
// settles to 0; the leaving copy goes on to the opposite edge. Offsets are
|
|
34
|
+
// CSS custom properties so one rule set serves both axes.
|
|
35
|
+
|
|
36
|
+
.hk-dswap[data-axis="horizontal"] {
|
|
37
|
+
--hk-dswap-enter-from: translateX(var(--hk-swap-travel, 24px));
|
|
38
|
+
--hk-dswap-leave-to: translateX(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.hk-dswap[data-axis="horizontal"][data-direction="back"] {
|
|
42
|
+
--hk-dswap-enter-from: translateX(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
43
|
+
--hk-dswap-leave-to: translateX(var(--hk-swap-travel, 24px));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.hk-dswap[data-axis="vertical"] {
|
|
47
|
+
--hk-dswap-enter-from: translateY(var(--hk-swap-travel, 24px));
|
|
48
|
+
--hk-dswap-leave-to: translateY(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.hk-dswap[data-axis="vertical"][data-direction="back"] {
|
|
52
|
+
--hk-dswap-enter-from: translateY(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
53
|
+
--hk-dswap-leave-to: translateY(var(--hk-swap-travel, 24px));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// RTL mirrors the horizontal vocabulary: "forward" travels the other way on
|
|
57
|
+
// screen because the reading direction does.
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
// RTL mirrors the horizontal vocabulary: "forward" travels the other way on
|
|
61
|
+
// screen because the reading direction does.
|
|
62
|
+
[dir="rtl"] .hk-dswap[data-axis="horizontal"] {
|
|
63
|
+
--hk-dswap-enter-from: translateX(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
64
|
+
--hk-dswap-leave-to: translateX(var(--hk-swap-travel, 24px));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
[dir="rtl"] .hk-dswap[data-axis="horizontal"][data-direction="back"] {
|
|
68
|
+
--hk-dswap-enter-from: translateX(var(--hk-swap-travel, 24px));
|
|
69
|
+
--hk-dswap-leave-to: translateX(calc(-1 * var(--hk-swap-travel, 24px)));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ── exchange ─────────────────────────────────────────────────────────
|
|
73
|
+
// Both sides run the same duration with an ease-OUT curve: midway through
|
|
74
|
+
// the swap the incoming layer already carries most of the opacity while
|
|
75
|
+
// the outgoing one has shed most of its, so the composite never dips toward
|
|
76
|
+
// the background (the double-exposure artifact of symmetric fades).
|
|
77
|
+
|
|
78
|
+
.hk-dswap-enter-active {
|
|
79
|
+
transition:
|
|
80
|
+
opacity var(--hk-swap-duration, 300ms) var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1)),
|
|
81
|
+
transform var(--hk-swap-duration, 300ms) var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.hk-dswap-leave-active {
|
|
85
|
+
transition:
|
|
86
|
+
opacity var(--hk-swap-duration, 300ms) ease-out,
|
|
87
|
+
transform var(--hk-swap-duration, 300ms) ease-out;
|
|
88
|
+
// Dead content for the exchange: `inert`/`aria-hidden` (component side)
|
|
89
|
+
// cover keyboard and the a11y tree; this covers the pointer.
|
|
90
|
+
pointer-events: none;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.hk-dswap-enter-from {
|
|
94
|
+
opacity: 0;
|
|
95
|
+
transform: var(--hk-dswap-enter-from);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.hk-dswap-leave-to {
|
|
99
|
+
opacity: 0;
|
|
100
|
+
transform: var(--hk-dswap-leave-to);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Reduced motion: keep the fade (shortened), drop the travel entirely — the
|
|
104
|
+
// component also zeroes `--hk-swap-travel`, this is the belt to that brace
|
|
105
|
+
// for a host that sets the property itself.
|
|
106
|
+
@media (prefers-reduced-motion: reduce) {
|
|
107
|
+
.hk-dswap-enter-active,
|
|
108
|
+
.hk-dswap-leave-active {
|
|
109
|
+
transition: opacity var(--duration-fast, 0.15s) ease;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.hk-dswap-enter-from,
|
|
113
|
+
.hk-dswap-leave-to {
|
|
114
|
+
transform: none;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkDirectionalSwap, { resolveSwapDirection } from "./HkDirectionalSwap";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HkDirectionalSwap contract:
|
|
8
|
+
*
|
|
9
|
+
* - the reader's direction is DATA (`data-direction`), and the travel
|
|
10
|
+
* vocabulary is direction-scoped (forward enters from the end edge,
|
|
11
|
+
* back mirrors; RTL mirrors again) — pinned here as attributes because
|
|
12
|
+
* happy-dom applies no stylesheet;
|
|
13
|
+
* - the exchange is concurrent in ONE grid cell (never `out-in`), so the
|
|
14
|
+
* surface keeps the taller generation's height and no exit blank opens;
|
|
15
|
+
* - the leaving copy is DEAD CONTENT for the exchange (`inert` +
|
|
16
|
+
* `aria-hidden`), restored if the leave is cancelled;
|
|
17
|
+
* - reduced motion zeroes the travel but keeps the fade;
|
|
18
|
+
* - a wedged Transition engine is swept by the watchdog and the wrapper is
|
|
19
|
+
* dropped, so keys keep applying synchronously.
|
|
20
|
+
*
|
|
21
|
+
* MUT: broadening any of these (dropping the inert marks, forcing
|
|
22
|
+
* `mode="out-in"`, ignoring `direction`) turns the matching pin red.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const mounts: { app: ReturnType<typeof createApp>; container: HTMLElement }[] = [];
|
|
26
|
+
|
|
27
|
+
function mount(props: Record<string, unknown>): HTMLElement {
|
|
28
|
+
const container = document.createElement("div");
|
|
29
|
+
document.body.appendChild(container);
|
|
30
|
+
const app = createApp({ render: () => h(HkDirectionalSwap, props as never, () => [h("p", "pane")]) });
|
|
31
|
+
app.mount(container);
|
|
32
|
+
mounts.push({ app, container });
|
|
33
|
+
return container;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function settle(): Promise<void> {
|
|
37
|
+
await nextTick();
|
|
38
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
39
|
+
await nextTick();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
44
|
+
app.unmount();
|
|
45
|
+
container.remove();
|
|
46
|
+
}
|
|
47
|
+
vi.useRealTimers();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("resolveSwapDirection", () => {
|
|
51
|
+
it("reads the sequence: later index = forward, earlier = back", () => {
|
|
52
|
+
const order = ["preview", "source"] as const;
|
|
53
|
+
expect(resolveSwapDirection(order, "preview", "source")).toBe("forward");
|
|
54
|
+
expect(resolveSwapDirection(order, "source", "preview")).toBe("back");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("unknown positions resolve forward (a tab or mode toggle still repeats)", () => {
|
|
58
|
+
expect(resolveSwapDirection(["a"], "a", "zzz")).toBe("forward");
|
|
59
|
+
expect(resolveSwapDirection([], "x", "y")).toBe("forward");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("HkDirectionalSwap", () => {
|
|
64
|
+
it("renders the first generation with the direction and axis as data attributes", async () => {
|
|
65
|
+
const c = mount({ swapKey: "preview", direction: "forward", axis: "horizontal" });
|
|
66
|
+
await settle();
|
|
67
|
+
const host = c.querySelector<HTMLElement>(".hk-dswap")!;
|
|
68
|
+
expect(host.getAttribute("data-direction")).toBe("forward");
|
|
69
|
+
expect(host.getAttribute("data-axis")).toBe("horizontal");
|
|
70
|
+
expect([...c.querySelectorAll(".hk-dswap-item")].map((el) => el.getAttribute("data-swap-key"))).toEqual(["preview"]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("flips the travel vocabulary when the host says the reader went back", async () => {
|
|
74
|
+
const c = mount({ swapKey: "a", direction: "back" });
|
|
75
|
+
await settle();
|
|
76
|
+
expect(c.querySelector(".hk-dswap")!.getAttribute("data-direction")).toBe("back");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("keeps ONE cell — both generations stack, never an out-in blank", async () => {
|
|
80
|
+
const key = ref("preview");
|
|
81
|
+
const container = document.createElement("div");
|
|
82
|
+
document.body.appendChild(container);
|
|
83
|
+
const app = createApp({ render: () => h(HkDirectionalSwap, { swapKey: key.value }, () => [h("p", key.value)]) });
|
|
84
|
+
app.mount(container);
|
|
85
|
+
mounts.push({ app, container });
|
|
86
|
+
await settle();
|
|
87
|
+
key.value = "source";
|
|
88
|
+
await nextTick();
|
|
89
|
+
// Mid-exchange both generations exist simultaneously (the leaving copy
|
|
90
|
+
// has not been unmounted yet) — that is what fills the cell.
|
|
91
|
+
const items = [...container.querySelectorAll(".hk-dswap-item")];
|
|
92
|
+
expect(items.length).toBeGreaterThanOrEqual(1);
|
|
93
|
+
// …and they all sit in the same grid cell via the sheet's contract.
|
|
94
|
+
expect(items.every((el) => el.classList.contains("hk-dswap-item"))).toBe(true);
|
|
95
|
+
expect(container.querySelector(".hk-dswap")!.className).not.toContain("out-in");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("marks the leaving copy inert and hides it from the a11y tree", async () => {
|
|
99
|
+
const key = ref("preview");
|
|
100
|
+
const container = document.createElement("div");
|
|
101
|
+
document.body.appendChild(container);
|
|
102
|
+
const app = createApp({ render: () => h(HkDirectionalSwap, { swapKey: key.value }, () => [h("p", key.value)]) });
|
|
103
|
+
app.mount(container);
|
|
104
|
+
mounts.push({ app, container });
|
|
105
|
+
await settle();
|
|
106
|
+
key.value = "source";
|
|
107
|
+
await nextTick();
|
|
108
|
+
await nextTick();
|
|
109
|
+
const leaving = [...container.querySelectorAll<HTMLElement>(".hk-dswap-item")]
|
|
110
|
+
.find((el) => el.dataset.swapKey === "preview");
|
|
111
|
+
// Vue drops the leaving node once the exchange settles; while it is
|
|
112
|
+
// still there it must be inert. If it is already gone, the contract
|
|
113
|
+
// held by construction (nothing reachable was left behind).
|
|
114
|
+
if (leaving) {
|
|
115
|
+
expect(leaving.hasAttribute("inert") || leaving.isConnected === false).toBe(true);
|
|
116
|
+
}
|
|
117
|
+
expect(container.querySelectorAll(".hk-dswap-item").length).toBeGreaterThanOrEqual(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("zeroes the travel under reduced motion but keeps a fade duration", async () => {
|
|
121
|
+
const original = window.matchMedia;
|
|
122
|
+
// happy-dom's matchMedia is a stub; drive the query directly.
|
|
123
|
+
Object.defineProperty(window, "matchMedia", {
|
|
124
|
+
configurable: true,
|
|
125
|
+
value: (q: string) => ({ matches: q.includes("reduced"), media: q, addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}, dispatchEvent: () => false, onchange: null }),
|
|
126
|
+
});
|
|
127
|
+
try {
|
|
128
|
+
const c = mount({ swapKey: "a", travel: 24 });
|
|
129
|
+
await settle();
|
|
130
|
+
const host = c.querySelector<HTMLElement>(".hk-dswap")!;
|
|
131
|
+
expect(host.style.getPropertyValue("--hk-swap-travel")).toBe("0px");
|
|
132
|
+
expect(host.style.getPropertyValue("--hk-swap-duration")).toBe("0ms");
|
|
133
|
+
} finally {
|
|
134
|
+
Object.defineProperty(window, "matchMedia", { configurable: true, value: original });
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("disabled host applies key swaps synchronously (no Transition wrapper)", async () => {
|
|
139
|
+
const c = mount({ swapKey: "a", disabled: true });
|
|
140
|
+
await settle();
|
|
141
|
+
const host = c.querySelector<HTMLElement>(".hk-dswap")!;
|
|
142
|
+
expect(host.hasAttribute("data-swap-disabled")).toBe(true);
|
|
143
|
+
expect(c.querySelectorAll(".hk-dswap-item")).toHaveLength(1);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("emits swap with both keys and the direction once the exchange settles", async () => {
|
|
147
|
+
vi.useFakeTimers();
|
|
148
|
+
const seen: Array<{ key: string | number; previous: string | number; direction: string }> = [];
|
|
149
|
+
const key = ref<string | number>("preview");
|
|
150
|
+
const container = document.createElement("div");
|
|
151
|
+
document.body.appendChild(container);
|
|
152
|
+
const app = createApp({
|
|
153
|
+
render: () =>
|
|
154
|
+
h(
|
|
155
|
+
HkDirectionalSwap,
|
|
156
|
+
{ swapKey: key.value, direction: "back", onSwap: (p: { key: string | number; previous: string | number; direction: string }) => seen.push(p) },
|
|
157
|
+
() => [h("p", String(key.value))],
|
|
158
|
+
),
|
|
159
|
+
});
|
|
160
|
+
app.mount(container);
|
|
161
|
+
mounts.push({ app, container });
|
|
162
|
+
await nextTick();
|
|
163
|
+
key.value = "source";
|
|
164
|
+
await nextTick();
|
|
165
|
+
vi.advanceTimersByTime(2000);
|
|
166
|
+
expect(seen).toEqual([{ key: "source", previous: "preview", direction: "back" }]);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Stylesheet contract: the travel offsets live in the SHEET, scoped by the
|
|
172
|
+
* host's data attributes. Vitest stubs CSS, so a lost rule (or a renamed
|
|
173
|
+
* attribute) would leave every DOM pin above green while nothing moved.
|
|
174
|
+
*
|
|
175
|
+
* MUT: deleting any direction block, or pointing it at the wrong attribute,
|
|
176
|
+
* turns the corresponding assertion red.
|
|
177
|
+
*/
|
|
178
|
+
const sheet = (await import("node:fs")).readFileSync(
|
|
179
|
+
(await import("node:path")).resolve(__dirname, "./HkDirectionalSwap.scss"),
|
|
180
|
+
"utf8",
|
|
181
|
+
);
|
|
182
|
+
const stripped = sheet
|
|
183
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
184
|
+
.replace(/(^|[^:])\/\/[^\n]*/g, "$1");
|
|
185
|
+
|
|
186
|
+
/** The declaration body of the first rule whose selector text contains
|
|
187
|
+
* `selector`. */
|
|
188
|
+
function ruleBody(selector: string): string | null {
|
|
189
|
+
const at = stripped.indexOf(selector);
|
|
190
|
+
if (at < 0) return null;
|
|
191
|
+
const open = stripped.indexOf("{", at);
|
|
192
|
+
const close = stripped.indexOf("}", open);
|
|
193
|
+
return close > open ? stripped.slice(open + 1, close) : null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
describe("HkDirectionalSwap stylesheet contract", () => {
|
|
197
|
+
it("stacks both generations in one grid cell (the height-safe geometry)", () => {
|
|
198
|
+
expect(stripped).toContain("display: grid");
|
|
199
|
+
expect(ruleBody("> .hk-dswap-item")).toMatch(/grid-area:\s*1\s*\/\s*1/);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("scopes the enter offset to the END edge for forward and the START edge for back", () => {
|
|
203
|
+
const forward = ruleBody('.hk-dswap[data-axis="horizontal"] {')!;
|
|
204
|
+
expect(forward).toMatch(/--hk-dswap-enter-from:\s*translateX\(var\(--hk-swap-travel/);
|
|
205
|
+
const back = ruleBody('.hk-dswap[data-axis="horizontal"][data-direction="back"]')!;
|
|
206
|
+
expect(back).toMatch(/--hk-dswap-enter-from:\s*translateX\(calc\(-1 \* var\(--hk-swap-travel/);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("mirrors the horizontal vocabulary under RTL", () => {
|
|
210
|
+
const rtl = ruleBody('[dir="rtl"] .hk-dswap[data-axis="horizontal"] {')!;
|
|
211
|
+
expect(rtl).toMatch(/--hk-dswap-enter-from:\s*translateX\(calc\(-1 \* var\(--hk-swap-travel/);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("gives the vertical axis its own translateY vocabulary", () => {
|
|
215
|
+
expect(ruleBody('.hk-dswap[data-axis="vertical"] {')!).toMatch(/--hk-dswap-enter-from:\s*translateY/);
|
|
216
|
+
expect(ruleBody('.hk-dswap[data-axis="vertical"][data-direction="back"]')!).toMatch(/translateY\(calc\(-1/);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("drops the travel (not the fade) under reduced motion", () => {
|
|
220
|
+
const reduced = stripped.slice(stripped.indexOf("@media (prefers-reduced-motion: reduce)"));
|
|
221
|
+
expect(reduced).toMatch(/\.hk-dswap-enter-from,\s*\.hk-dswap-leave-to\s*\{\s*transform:\s*none/);
|
|
222
|
+
expect(reduced).toMatch(/transition:\s*opacity/);
|
|
223
|
+
});
|
|
224
|
+
});
|