@thedatagrid/docs-kit 0.2.2 → 0.4.0
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/CHANGELOG.md +30 -0
- package/README.md +206 -4
- package/dist/code/demoFiles.d.ts.map +1 -1
- package/dist/code/demoFiles.js +2 -2
- package/dist/code/demoFiles.js.map +1 -1
- package/dist/code/index.d.ts +1 -0
- package/dist/code/index.d.ts.map +1 -1
- package/dist/code/index.js +1 -0
- package/dist/code/index.js.map +1 -1
- package/dist/code/regions.d.ts +2 -1
- package/dist/code/regions.d.ts.map +1 -1
- package/dist/code/regions.js +12 -8
- package/dist/code/regions.js.map +1 -1
- package/dist/code/vueBlocks.d.ts +3 -0
- package/dist/code/vueBlocks.d.ts.map +1 -0
- package/dist/code/vueBlocks.js +29 -0
- package/dist/code/vueBlocks.js.map +1 -0
- package/dist/demos/build.d.ts +5 -2
- package/dist/demos/build.d.ts.map +1 -1
- package/dist/demos/build.js +244 -85
- package/dist/demos/build.js.map +1 -1
- package/dist/demos/compile/core.d.ts +9 -1
- package/dist/demos/compile/core.d.ts.map +1 -1
- package/dist/demos/compile/core.js +19 -8
- package/dist/demos/compile/core.js.map +1 -1
- package/dist/demos/compile/vue.d.ts +107 -0
- package/dist/demos/compile/vue.d.ts.map +1 -0
- package/dist/demos/compile/vue.js +187 -0
- package/dist/demos/compile/vue.js.map +1 -0
- package/dist/demos/config.d.ts +27 -7
- package/dist/demos/config.d.ts.map +1 -1
- package/dist/demos/config.js.map +1 -1
- package/dist/demos/index.d.ts +3 -2
- package/dist/demos/index.d.ts.map +1 -1
- package/dist/demos/index.js +1 -0
- package/dist/demos/index.js.map +1 -1
- package/dist/demos/pages.d.ts +1 -1
- package/dist/demos/pages.d.ts.map +1 -1
- package/dist/demos/pages.js +4 -1
- package/dist/demos/pages.js.map +1 -1
- package/dist/demos/runtime/vue.d.ts +3 -0
- package/dist/demos/runtime/vue.d.ts.map +1 -0
- package/dist/demos/runtime/vue.js +15 -0
- package/dist/demos/runtime/vue.js.map +1 -0
- package/dist/demos/worker/compile.worker.js +3 -22
- package/dist/demos/worker/compile.worker.js.map +1 -1
- package/dist/demos/worker/compileVue.worker.d.ts +2 -0
- package/dist/demos/worker/compileVue.worker.d.ts.map +1 -0
- package/dist/demos/worker/compileVue.worker.js +11 -0
- package/dist/demos/worker/compileVue.worker.js.map +1 -0
- package/dist/demos/worker/start.d.ts +7 -0
- package/dist/demos/worker/start.d.ts.map +1 -0
- package/dist/demos/worker/start.js +27 -0
- package/dist/demos/worker/start.js.map +1 -0
- package/dist/fork/codesandbox.d.ts +15 -1
- package/dist/fork/codesandbox.d.ts.map +1 -1
- package/dist/fork/codesandbox.js +30 -19
- package/dist/fork/codesandbox.js.map +1 -1
- package/dist/fork/index.d.ts +18 -0
- package/dist/fork/index.d.ts.map +1 -1
- package/dist/fork/index.js +18 -0
- package/dist/fork/index.js.map +1 -1
- package/dist/fork/stackblitz.d.ts +18 -0
- package/dist/fork/stackblitz.d.ts.map +1 -0
- package/dist/fork/stackblitz.js +98 -0
- package/dist/fork/stackblitz.js.map +1 -0
- package/dist/frameworks/index.d.ts +4 -0
- package/dist/frameworks/index.d.ts.map +1 -0
- package/dist/frameworks/index.js +4 -0
- package/dist/frameworks/index.js.map +1 -0
- package/dist/frameworks/preference.d.ts +10 -0
- package/dist/frameworks/preference.d.ts.map +1 -0
- package/dist/frameworks/preference.js +34 -0
- package/dist/frameworks/preference.js.map +1 -0
- package/dist/frameworks/react/FrameworkBanner.d.ts +34 -0
- package/dist/frameworks/react/FrameworkBanner.d.ts.map +1 -0
- package/dist/frameworks/react/FrameworkBanner.js +50 -0
- package/dist/frameworks/react/FrameworkBanner.js.map +1 -0
- package/dist/frameworks/react/FrameworkContent.d.ts +21 -0
- package/dist/frameworks/react/FrameworkContent.d.ts.map +1 -0
- package/dist/frameworks/react/FrameworkContent.js +25 -0
- package/dist/frameworks/react/FrameworkContent.js.map +1 -0
- package/dist/frameworks/react/FrameworkLinks.d.ts +16 -0
- package/dist/frameworks/react/FrameworkLinks.d.ts.map +1 -0
- package/dist/frameworks/react/FrameworkLinks.js +50 -0
- package/dist/frameworks/react/FrameworkLinks.js.map +1 -0
- package/dist/frameworks/react/FrameworkSwitcher.d.ts +27 -0
- package/dist/frameworks/react/FrameworkSwitcher.d.ts.map +1 -0
- package/dist/frameworks/react/FrameworkSwitcher.js +42 -0
- package/dist/frameworks/react/FrameworkSwitcher.js.map +1 -0
- package/dist/frameworks/react/index.d.ts +6 -0
- package/dist/frameworks/react/index.d.ts.map +1 -0
- package/dist/frameworks/react/index.js +6 -0
- package/dist/frameworks/react/index.js.map +1 -0
- package/dist/frameworks/react/useFrameworkPreference.d.ts +32 -0
- package/dist/frameworks/react/useFrameworkPreference.d.ts.map +1 -0
- package/dist/frameworks/react/useFrameworkPreference.js +45 -0
- package/dist/frameworks/react/useFrameworkPreference.js.map +1 -0
- package/dist/frameworks/routeTree.d.ts +24 -0
- package/dist/frameworks/routeTree.d.ts.map +1 -0
- package/dist/frameworks/routeTree.js +31 -0
- package/dist/frameworks/routeTree.js.map +1 -0
- package/dist/frameworks/routing.d.ts +105 -0
- package/dist/frameworks/routing.d.ts.map +1 -0
- package/dist/frameworks/routing.js +142 -0
- package/dist/frameworks/routing.js.map +1 -0
- package/dist/frameworks/styles.css +104 -0
- package/dist/react/Demo.d.ts +12 -0
- package/dist/react/Demo.d.ts.map +1 -1
- package/dist/react/Demo.js +10 -4
- package/dist/react/Demo.js.map +1 -1
- package/dist/react/DemoShell.d.ts +5 -1
- package/dist/react/DemoShell.d.ts.map +1 -1
- package/dist/react/DemoShell.js +9 -6
- package/dist/react/DemoShell.js.map +1 -1
- package/dist/react/editor.d.ts.map +1 -1
- package/dist/react/editor.js +3 -0
- package/dist/react/editor.js.map +1 -1
- package/dist/react/fork.d.ts +4 -3
- package/dist/react/fork.d.ts.map +1 -1
- package/dist/react/fork.js +6 -5
- package/dist/react/fork.js.map +1 -1
- package/dist/react/styles.css +10 -0
- package/dist/remark/index.d.ts +2 -0
- package/dist/remark/index.d.ts.map +1 -1
- package/dist/remark/index.js +2 -0
- package/dist/remark/index.js.map +1 -1
- package/dist/remark/remarkDemoFences.d.ts +13 -0
- package/dist/remark/remarkDemoFences.d.ts.map +1 -1
- package/dist/remark/remarkDemoFences.js +48 -4
- package/dist/remark/remarkDemoFences.js.map +1 -1
- package/dist/remark/remarkFrameworkLinks.d.ts +36 -0
- package/dist/remark/remarkFrameworkLinks.d.ts.map +1 -0
- package/dist/remark/remarkFrameworkLinks.js +77 -0
- package/dist/remark/remarkFrameworkLinks.js.map +1 -0
- package/dist/remark/remarkFrameworkText.d.ts +16 -0
- package/dist/remark/remarkFrameworkText.d.ts.map +1 -0
- package/dist/remark/remarkFrameworkText.js +39 -0
- package/dist/remark/remarkFrameworkText.js.map +1 -0
- package/dist/showcase/AutomatedDemo.d.ts +46 -0
- package/dist/showcase/AutomatedDemo.d.ts.map +1 -0
- package/dist/showcase/AutomatedDemo.js +169 -0
- package/dist/showcase/AutomatedDemo.js.map +1 -0
- package/dist/showcase/LazyShowcase.d.ts +31 -0
- package/dist/showcase/LazyShowcase.d.ts.map +1 -0
- package/dist/showcase/LazyShowcase.js +51 -0
- package/dist/showcase/LazyShowcase.js.map +1 -0
- package/dist/showcase/hooks.d.ts +19 -0
- package/dist/showcase/hooks.d.ts.map +1 -0
- package/dist/showcase/hooks.js +63 -0
- package/dist/showcase/hooks.js.map +1 -0
- package/dist/showcase/index.d.ts +17 -0
- package/dist/showcase/index.d.ts.map +1 -0
- package/dist/showcase/index.js +15 -0
- package/dist/showcase/index.js.map +1 -0
- package/dist/showcase/scripted.d.ts +100 -0
- package/dist/showcase/scripted.d.ts.map +1 -0
- package/dist/showcase/scripted.js +381 -0
- package/dist/showcase/scripted.js.map +1 -0
- package/dist/showcase/styles.css +174 -0
- package/package.json +34 -4
- package/src/code/demoFiles.ts +2 -2
- package/src/code/index.ts +1 -0
- package/src/code/regions.ts +14 -8
- package/src/code/vueBlocks.ts +25 -0
- package/src/demos/build.ts +293 -99
- package/src/demos/compile/core.ts +27 -7
- package/src/demos/compile/vue.ts +259 -0
- package/src/demos/config.ts +44 -7
- package/src/demos/index.ts +13 -0
- package/src/demos/pages.ts +4 -2
- package/src/demos/runtime/vue.ts +17 -0
- package/src/demos/worker/compile.worker.ts +3 -30
- package/src/demos/worker/compileVue.worker.ts +13 -0
- package/src/demos/worker/start.ts +35 -0
- package/src/fork/codesandbox.ts +43 -21
- package/src/fork/index.ts +25 -0
- package/src/fork/stackblitz.ts +107 -0
- package/src/frameworks/index.ts +21 -0
- package/src/frameworks/preference.ts +35 -0
- package/src/frameworks/react/FrameworkBanner.tsx +112 -0
- package/src/frameworks/react/FrameworkContent.tsx +33 -0
- package/src/frameworks/react/FrameworkLinks.tsx +52 -0
- package/src/frameworks/react/FrameworkSwitcher.tsx +78 -0
- package/src/frameworks/react/index.ts +10 -0
- package/src/frameworks/react/useFrameworkPreference.ts +91 -0
- package/src/frameworks/routeTree.ts +54 -0
- package/src/frameworks/routing.ts +194 -0
- package/src/frameworks/styles.css +104 -0
- package/src/react/Demo.tsx +21 -2
- package/src/react/DemoShell.tsx +15 -7
- package/src/react/editor.ts +2 -0
- package/src/react/fork.ts +12 -5
- package/src/react/styles.css +10 -0
- package/src/remark/index.ts +6 -0
- package/src/remark/remarkDemoFences.ts +58 -3
- package/src/remark/remarkFrameworkLinks.ts +96 -0
- package/src/remark/remarkFrameworkText.ts +44 -0
- package/src/showcase/AutomatedDemo.tsx +266 -0
- package/src/showcase/LazyShowcase.tsx +111 -0
- package/src/showcase/hooks.ts +84 -0
- package/src/showcase/index.ts +23 -0
- package/src/showcase/scripted.ts +533 -0
- package/src/showcase/styles.css +174 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, type RefObject } from 'react';
|
|
4
|
+
|
|
5
|
+
/** Subscribes to a media query; `false` during SSR. */
|
|
6
|
+
export function useMediaQuery(query: string) {
|
|
7
|
+
const [matches, setMatches] = useState(false);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const mql = window.matchMedia(query);
|
|
10
|
+
const update = () => setMatches(mql.matches);
|
|
11
|
+
update();
|
|
12
|
+
mql.addEventListener('change', update);
|
|
13
|
+
return () => mql.removeEventListener('change', update);
|
|
14
|
+
}, [query]);
|
|
15
|
+
return matches;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function usePrefersReducedMotion() {
|
|
19
|
+
return useMediaQuery('(prefers-reduced-motion: reduce)');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** `true` while the document is visible (tab focused / not minimized). */
|
|
23
|
+
export function usePageVisible() {
|
|
24
|
+
const [visible, setVisible] = useState(true);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const update = () => setVisible(document.visibilityState !== 'hidden');
|
|
27
|
+
update();
|
|
28
|
+
document.addEventListener('visibilitychange', update);
|
|
29
|
+
return () => document.removeEventListener('visibilitychange', update);
|
|
30
|
+
}, []);
|
|
31
|
+
return visible;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `true` while any part of the element (by `threshold`) is in the viewport. */
|
|
35
|
+
export function useInView(
|
|
36
|
+
ref: RefObject<Element | null>,
|
|
37
|
+
{
|
|
38
|
+
threshold = 0,
|
|
39
|
+
rootMargin = '0px',
|
|
40
|
+
}: { threshold?: number; rootMargin?: string } = {},
|
|
41
|
+
) {
|
|
42
|
+
const [inView, setInView] = useState(false);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const node = ref.current;
|
|
45
|
+
if (!node) return;
|
|
46
|
+
const observer = new IntersectionObserver(
|
|
47
|
+
(entries) => setInView(entries.some((entry) => entry.isIntersecting)),
|
|
48
|
+
{ threshold, rootMargin },
|
|
49
|
+
);
|
|
50
|
+
observer.observe(node);
|
|
51
|
+
return () => observer.disconnect();
|
|
52
|
+
}, [ref, threshold, rootMargin]);
|
|
53
|
+
return inView;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Should a scripted show run on this device at all? Not on touch-only
|
|
58
|
+
* devices, small viewports, or with reduced motion.
|
|
59
|
+
*/
|
|
60
|
+
export function canRunScriptedShow(minViewportWidth = 768) {
|
|
61
|
+
if (typeof window === 'undefined') return false;
|
|
62
|
+
return (
|
|
63
|
+
window.matchMedia('(pointer: fine)').matches &&
|
|
64
|
+
!window.matchMedia('(prefers-reduced-motion: reduce)').matches &&
|
|
65
|
+
window.innerWidth >= minViewportWidth
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** requestIdleCallback with a timeout fallback (Safari has no rIC). */
|
|
70
|
+
export function whenIdle(callback: () => void, timeout = 1500) {
|
|
71
|
+
const w = window as Window & {
|
|
72
|
+
requestIdleCallback?: (
|
|
73
|
+
cb: () => void,
|
|
74
|
+
opts?: { timeout: number },
|
|
75
|
+
) => number;
|
|
76
|
+
cancelIdleCallback?: (id: number) => void;
|
|
77
|
+
};
|
|
78
|
+
if (w.requestIdleCallback) {
|
|
79
|
+
const id = w.requestIdleCallback(callback, { timeout });
|
|
80
|
+
return () => w.cancelIdleCallback?.(id);
|
|
81
|
+
}
|
|
82
|
+
const id = window.setTimeout(callback, Math.min(timeout, 300));
|
|
83
|
+
return () => window.clearTimeout(id);
|
|
84
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Showcase kit: product-agnostic building blocks for live, scripted
|
|
3
|
+
* marketing demos — a fake cursor toolkit, the automated "take over" shell,
|
|
4
|
+
* and a poster-first lazy loader.
|
|
5
|
+
*
|
|
6
|
+
* BOUNDARY RULE: nothing in this folder may import product code or the rest
|
|
7
|
+
* of the kit — only React and sibling files (test/showcase.test.ts checks it).
|
|
8
|
+
*
|
|
9
|
+
* Styles: import `@thedatagrid/docs-kit/showcase/styles.css` once.
|
|
10
|
+
*/
|
|
11
|
+
export * from './scripted.ts';
|
|
12
|
+
export * from './hooks.ts';
|
|
13
|
+
export { AutomatedDemo } from './AutomatedDemo.tsx';
|
|
14
|
+
export type {
|
|
15
|
+
AutomatedDemoProps,
|
|
16
|
+
AutomatedDemoStatus,
|
|
17
|
+
ShowcaseScriptContext,
|
|
18
|
+
} from './AutomatedDemo.tsx';
|
|
19
|
+
export { LazyShowcase } from './LazyShowcase.tsx';
|
|
20
|
+
export type {
|
|
21
|
+
LazyShowcaseProps,
|
|
22
|
+
LazyShowcaseLoadedProps,
|
|
23
|
+
} from './LazyShowcase.tsx';
|
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny automation toolkit for a "scripted demo you can take over": a fake SVG
|
|
3
|
+
* cursor tweened over a LIVE component, with real state changes applied at
|
|
4
|
+
* each "click". Everything is cancellable through one AbortSignal, so a single
|
|
5
|
+
* real user interaction stops the show instantly and leaves the component
|
|
6
|
+
* exactly where the script brought it.
|
|
7
|
+
*
|
|
8
|
+
* Product-agnostic on purpose: no imports from any grid or app code.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Thrown by every helper when the script is aborted; callers swallow it. */
|
|
12
|
+
export class ScriptCancelled extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super('script cancelled');
|
|
15
|
+
this.name = 'ScriptCancelled';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function assertActive(signal: AbortSignal) {
|
|
20
|
+
if (signal.aborted) throw new ScriptCancelled();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function sleep(ms: number, signal: AbortSignal): Promise<void> {
|
|
24
|
+
assertActive(signal);
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const id = window.setTimeout(() => {
|
|
27
|
+
signal.removeEventListener('abort', onAbort);
|
|
28
|
+
resolve();
|
|
29
|
+
}, ms);
|
|
30
|
+
const onAbort = () => {
|
|
31
|
+
window.clearTimeout(id);
|
|
32
|
+
reject(new ScriptCancelled());
|
|
33
|
+
};
|
|
34
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Poll until `get` returns a truthy value; null when it times out. */
|
|
39
|
+
export async function waitFor<T>(
|
|
40
|
+
get: () => T | null | undefined | false,
|
|
41
|
+
signal: AbortSignal,
|
|
42
|
+
{
|
|
43
|
+
timeout = 12000,
|
|
44
|
+
interval = 120,
|
|
45
|
+
}: { timeout?: number; interval?: number } = {},
|
|
46
|
+
): Promise<T | null> {
|
|
47
|
+
const deadline = Date.now() + timeout;
|
|
48
|
+
for (;;) {
|
|
49
|
+
assertActive(signal);
|
|
50
|
+
const value = get();
|
|
51
|
+
if (value) return value;
|
|
52
|
+
if (Date.now() > deadline) return null;
|
|
53
|
+
await sleep(interval, signal);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type Point = { x: number; y: number };
|
|
58
|
+
|
|
59
|
+
export const easeInOutCubic = (t: number) =>
|
|
60
|
+
t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
61
|
+
|
|
62
|
+
/** Run `onFrame(easedT)` on every animation frame for `duration` ms. */
|
|
63
|
+
export function tween(
|
|
64
|
+
onFrame: (t: number) => void,
|
|
65
|
+
duration: number,
|
|
66
|
+
signal: AbortSignal,
|
|
67
|
+
ease: (t: number) => number = easeInOutCubic,
|
|
68
|
+
): Promise<void> {
|
|
69
|
+
assertActive(signal);
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const start = performance.now();
|
|
72
|
+
let raf = 0;
|
|
73
|
+
const onAbort = () => {
|
|
74
|
+
cancelAnimationFrame(raf);
|
|
75
|
+
reject(new ScriptCancelled());
|
|
76
|
+
};
|
|
77
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
78
|
+
const frame = (now: number) => {
|
|
79
|
+
const t = Math.min(1, (now - start) / duration);
|
|
80
|
+
try {
|
|
81
|
+
onFrame(ease(t));
|
|
82
|
+
} catch (error) {
|
|
83
|
+
signal.removeEventListener('abort', onAbort);
|
|
84
|
+
reject(error);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (t >= 1) {
|
|
88
|
+
signal.removeEventListener('abort', onAbort);
|
|
89
|
+
resolve();
|
|
90
|
+
} else {
|
|
91
|
+
raf = requestAnimationFrame(frame);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
raf = requestAnimationFrame(frame);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Inertial "fling": starts at `velocity` (px per ms) and decays exponentially,
|
|
100
|
+
* like a trackpad swipe released at speed. `apply` receives the delta for the
|
|
101
|
+
* frame and returns false to stop early (for example when an edge is hit).
|
|
102
|
+
* Resolves with the total distance travelled.
|
|
103
|
+
*/
|
|
104
|
+
export function fling(
|
|
105
|
+
apply: (delta: number) => boolean | void,
|
|
106
|
+
{
|
|
107
|
+
velocity,
|
|
108
|
+
/** Fraction of velocity kept per 16.7ms frame. 0.95 ≈ a long glide. */
|
|
109
|
+
friction = 0.955,
|
|
110
|
+
minVelocity = 0.02,
|
|
111
|
+
maxDuration = 4000,
|
|
112
|
+
}: {
|
|
113
|
+
velocity: number;
|
|
114
|
+
friction?: number;
|
|
115
|
+
minVelocity?: number;
|
|
116
|
+
maxDuration?: number;
|
|
117
|
+
},
|
|
118
|
+
signal: AbortSignal,
|
|
119
|
+
): Promise<number> {
|
|
120
|
+
assertActive(signal);
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
let v = velocity;
|
|
123
|
+
let last = performance.now();
|
|
124
|
+
const start = last;
|
|
125
|
+
let travelled = 0;
|
|
126
|
+
let raf = 0;
|
|
127
|
+
const onAbort = () => {
|
|
128
|
+
cancelAnimationFrame(raf);
|
|
129
|
+
reject(new ScriptCancelled());
|
|
130
|
+
};
|
|
131
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
132
|
+
const done = () => {
|
|
133
|
+
signal.removeEventListener('abort', onAbort);
|
|
134
|
+
resolve(travelled);
|
|
135
|
+
};
|
|
136
|
+
const frame = (now: number) => {
|
|
137
|
+
const dt = Math.min(48, now - last);
|
|
138
|
+
last = now;
|
|
139
|
+
const delta = v * dt;
|
|
140
|
+
travelled += delta;
|
|
141
|
+
let keepGoing: boolean | void = true;
|
|
142
|
+
try {
|
|
143
|
+
keepGoing = apply(delta);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
signal.removeEventListener('abort', onAbort);
|
|
146
|
+
reject(error);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
v *= Math.pow(friction, dt / 16.67);
|
|
150
|
+
if (
|
|
151
|
+
keepGoing === false ||
|
|
152
|
+
Math.abs(v) < minVelocity ||
|
|
153
|
+
now - start > maxDuration
|
|
154
|
+
) {
|
|
155
|
+
done();
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
raf = requestAnimationFrame(frame);
|
|
159
|
+
};
|
|
160
|
+
raf = requestAnimationFrame(frame);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const SYNTHETIC_POINTER_ID = 999;
|
|
165
|
+
|
|
166
|
+
function safeDispatch(el: EventTarget, event: Event) {
|
|
167
|
+
try {
|
|
168
|
+
el.dispatchEvent(event);
|
|
169
|
+
} catch {
|
|
170
|
+
// a hostile handler must not kill the script
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function mouseInit(
|
|
175
|
+
clientX: number,
|
|
176
|
+
clientY: number,
|
|
177
|
+
extra: MouseEventInit = {},
|
|
178
|
+
) {
|
|
179
|
+
return {
|
|
180
|
+
bubbles: true,
|
|
181
|
+
cancelable: true,
|
|
182
|
+
composed: true,
|
|
183
|
+
view: window,
|
|
184
|
+
clientX,
|
|
185
|
+
clientY,
|
|
186
|
+
...extra,
|
|
187
|
+
} satisfies MouseEventInit;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function pointerEvent(
|
|
191
|
+
type: string,
|
|
192
|
+
clientX: number,
|
|
193
|
+
clientY: number,
|
|
194
|
+
extra: PointerEventInit = {},
|
|
195
|
+
) {
|
|
196
|
+
return new PointerEvent(type, {
|
|
197
|
+
...mouseInit(clientX, clientY),
|
|
198
|
+
pointerId: SYNTHETIC_POINTER_ID,
|
|
199
|
+
pointerType: 'mouse',
|
|
200
|
+
isPrimary: true,
|
|
201
|
+
...extra,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Dispatch a full pointer+mouse sequence so whatever the target listens to
|
|
207
|
+
* (pointerdown, mousedown, click…) fires. Coordinates are real client-space
|
|
208
|
+
* coords, so `elementFromPoint`-style logic in handlers also works.
|
|
209
|
+
*
|
|
210
|
+
* Note: synthetic events have `isTrusted === false`; the takeover logic in
|
|
211
|
+
* AutomatedDemo relies on that to tell the script apart from the user.
|
|
212
|
+
*/
|
|
213
|
+
export function dispatchClickSequence(
|
|
214
|
+
el: HTMLElement,
|
|
215
|
+
clientX: number,
|
|
216
|
+
clientY: number,
|
|
217
|
+
) {
|
|
218
|
+
safeDispatch(el, pointerEvent('pointerover', clientX, clientY));
|
|
219
|
+
safeDispatch(
|
|
220
|
+
el,
|
|
221
|
+
pointerEvent('pointerenter', clientX, clientY, { bubbles: false }),
|
|
222
|
+
);
|
|
223
|
+
safeDispatch(el, new MouseEvent('mouseover', mouseInit(clientX, clientY)));
|
|
224
|
+
safeDispatch(
|
|
225
|
+
el,
|
|
226
|
+
pointerEvent('pointerdown', clientX, clientY, { button: 0, buttons: 1 }),
|
|
227
|
+
);
|
|
228
|
+
safeDispatch(
|
|
229
|
+
el,
|
|
230
|
+
new MouseEvent(
|
|
231
|
+
'mousedown',
|
|
232
|
+
mouseInit(clientX, clientY, { button: 0, buttons: 1 }),
|
|
233
|
+
),
|
|
234
|
+
);
|
|
235
|
+
safeDispatch(
|
|
236
|
+
el,
|
|
237
|
+
pointerEvent('pointerup', clientX, clientY, { button: 0, buttons: 0 }),
|
|
238
|
+
);
|
|
239
|
+
safeDispatch(
|
|
240
|
+
el,
|
|
241
|
+
new MouseEvent('mouseup', mouseInit(clientX, clientY, { button: 0 })),
|
|
242
|
+
);
|
|
243
|
+
safeDispatch(
|
|
244
|
+
el,
|
|
245
|
+
new MouseEvent(
|
|
246
|
+
'click',
|
|
247
|
+
mouseInit(clientX, clientY, { button: 0, detail: 1 }),
|
|
248
|
+
),
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Fire hover events (pointerover/enter, mouseover/enter/move) on `el`. */
|
|
253
|
+
export function dispatchHover(
|
|
254
|
+
el: HTMLElement,
|
|
255
|
+
clientX: number,
|
|
256
|
+
clientY: number,
|
|
257
|
+
) {
|
|
258
|
+
safeDispatch(el, pointerEvent('pointerover', clientX, clientY));
|
|
259
|
+
safeDispatch(
|
|
260
|
+
el,
|
|
261
|
+
pointerEvent('pointerenter', clientX, clientY, { bubbles: false }),
|
|
262
|
+
);
|
|
263
|
+
safeDispatch(el, new MouseEvent('mouseover', mouseInit(clientX, clientY)));
|
|
264
|
+
safeDispatch(
|
|
265
|
+
el,
|
|
266
|
+
new MouseEvent(
|
|
267
|
+
'mouseenter',
|
|
268
|
+
mouseInit(clientX, clientY, { bubbles: false }),
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
safeDispatch(el, pointerEvent('pointermove', clientX, clientY));
|
|
272
|
+
safeDispatch(el, new MouseEvent('mousemove', mouseInit(clientX, clientY)));
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Synthetic pointer ids are unknown to the browser, so `setPointerCapture`
|
|
277
|
+
* throws for them. While a scripted drag runs, shadow it on the element
|
|
278
|
+
* instance (and restore it afterwards).
|
|
279
|
+
*/
|
|
280
|
+
function shimPointerCapture(el: HTMLElement) {
|
|
281
|
+
const own = Object.prototype.hasOwnProperty.call(el, 'setPointerCapture');
|
|
282
|
+
const previous = own ? el.setPointerCapture : undefined;
|
|
283
|
+
const previousRelease = Object.prototype.hasOwnProperty.call(
|
|
284
|
+
el,
|
|
285
|
+
'releasePointerCapture',
|
|
286
|
+
)
|
|
287
|
+
? el.releasePointerCapture
|
|
288
|
+
: undefined;
|
|
289
|
+
el.setPointerCapture = () => {};
|
|
290
|
+
el.releasePointerCapture = () => {};
|
|
291
|
+
return () => {
|
|
292
|
+
if (previous) el.setPointerCapture = previous;
|
|
293
|
+
else delete (el as Partial<HTMLElement>).setPointerCapture;
|
|
294
|
+
if (previousRelease) el.releasePointerCapture = previousRelease;
|
|
295
|
+
else delete (el as Partial<HTMLElement>).releasePointerCapture;
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Distance-based duration: constant perceived speed, clamped to sane bounds. */
|
|
300
|
+
function moveDuration(from: Point, to: Point, speed = 1) {
|
|
301
|
+
const distance = Math.hypot(to.x - from.x, to.y - from.y);
|
|
302
|
+
return Math.min(1100, Math.max(260, distance / 0.9)) / speed;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const CURSOR_HTML = `
|
|
306
|
+
<span class="showcase-cursor__ring"></span>
|
|
307
|
+
<svg class="showcase-cursor__arrow" width="26" height="30" viewBox="0 0 26 30" aria-hidden="true">
|
|
308
|
+
<path d="M2 1 L2 24 L8.4 18.6 L12.2 27.6 L16.6 25.7 L12.8 16.9 L20.5 16.6 Z"
|
|
309
|
+
fill="#14161c" stroke="#fff" stroke-width="1.6" stroke-linejoin="round" />
|
|
310
|
+
</svg>
|
|
311
|
+
<span class="showcase-cursor__label"></span>
|
|
312
|
+
`;
|
|
313
|
+
|
|
314
|
+
export type ScriptedCursor = ReturnType<typeof createScriptedCursor>;
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The fake mouse: a cursor element inside a pointer-events-none overlay that
|
|
318
|
+
* covers the demo. Positions are in overlay-local coordinates; the arrow tip
|
|
319
|
+
* is the hotspot.
|
|
320
|
+
*/
|
|
321
|
+
export function createScriptedCursor(overlay: HTMLElement) {
|
|
322
|
+
const el = document.createElement('div');
|
|
323
|
+
el.className = 'showcase-cursor';
|
|
324
|
+
el.innerHTML = CURSOR_HTML;
|
|
325
|
+
overlay.appendChild(el);
|
|
326
|
+
const label = el.querySelector<HTMLElement>('.showcase-cursor__label')!;
|
|
327
|
+
|
|
328
|
+
let pos: Point = {
|
|
329
|
+
x: overlay.clientWidth * 0.5,
|
|
330
|
+
y: overlay.clientHeight * 0.65,
|
|
331
|
+
};
|
|
332
|
+
const render = () => {
|
|
333
|
+
el.style.transform = `translate3d(${pos.x}px, ${pos.y}px, 0)`;
|
|
334
|
+
};
|
|
335
|
+
render();
|
|
336
|
+
|
|
337
|
+
const toLocal = (clientX: number, clientY: number): Point => {
|
|
338
|
+
const rect = overlay.getBoundingClientRect();
|
|
339
|
+
return { x: clientX - rect.left, y: clientY - rect.top };
|
|
340
|
+
};
|
|
341
|
+
const toClient = (p: Point): Point => {
|
|
342
|
+
const rect = overlay.getBoundingClientRect();
|
|
343
|
+
return { x: p.x + rect.left, y: p.y + rect.top };
|
|
344
|
+
};
|
|
345
|
+
const centerOf = (
|
|
346
|
+
target: HTMLElement,
|
|
347
|
+
offset: Partial<Point> = {},
|
|
348
|
+
): Point => {
|
|
349
|
+
const rect = target.getBoundingClientRect();
|
|
350
|
+
return toLocal(
|
|
351
|
+
rect.left + rect.width / 2 + (offset.x ?? 0),
|
|
352
|
+
rect.top + rect.height / 2 + (offset.y ?? 0),
|
|
353
|
+
);
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const cursor = {
|
|
357
|
+
element: el,
|
|
358
|
+
show() {
|
|
359
|
+
el.classList.add('is-visible');
|
|
360
|
+
},
|
|
361
|
+
hide() {
|
|
362
|
+
el.classList.remove('is-visible', 'is-pressed', 'is-clicking');
|
|
363
|
+
cursor.setLabel(null);
|
|
364
|
+
},
|
|
365
|
+
/** A small caption next to the cursor ("⇢ swipe", "1M rows"…); null hides it. */
|
|
366
|
+
setLabel(text: string | null) {
|
|
367
|
+
label.textContent = text ?? '';
|
|
368
|
+
el.classList.toggle('has-label', !!text);
|
|
369
|
+
},
|
|
370
|
+
/** Current hotspot position in client coordinates (for event dispatch). */
|
|
371
|
+
clientPoint(): Point {
|
|
372
|
+
return toClient(pos);
|
|
373
|
+
},
|
|
374
|
+
localPoint(): Point {
|
|
375
|
+
return { ...pos };
|
|
376
|
+
},
|
|
377
|
+
|
|
378
|
+
async moveTo(
|
|
379
|
+
target: Point,
|
|
380
|
+
signal: AbortSignal,
|
|
381
|
+
{ speed = 1 }: { speed?: number } = {},
|
|
382
|
+
) {
|
|
383
|
+
el.classList.add('is-visible');
|
|
384
|
+
const from = { ...pos };
|
|
385
|
+
await tween(
|
|
386
|
+
(t) => {
|
|
387
|
+
pos = {
|
|
388
|
+
x: from.x + (target.x - from.x) * t,
|
|
389
|
+
y: from.y + (target.y - from.y) * t,
|
|
390
|
+
};
|
|
391
|
+
render();
|
|
392
|
+
},
|
|
393
|
+
moveDuration(from, target, speed),
|
|
394
|
+
signal,
|
|
395
|
+
);
|
|
396
|
+
},
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Move to an element's center (plus optional offset from center).
|
|
400
|
+
* Recomputes the rect at call time, so virtualized cells are fine as long
|
|
401
|
+
* as they exist when the move starts.
|
|
402
|
+
*/
|
|
403
|
+
async moveToElement(
|
|
404
|
+
target: HTMLElement,
|
|
405
|
+
signal: AbortSignal,
|
|
406
|
+
offset: Partial<Point> = {},
|
|
407
|
+
options?: { speed?: number },
|
|
408
|
+
) {
|
|
409
|
+
await cursor.moveTo(centerOf(target, offset), signal, options);
|
|
410
|
+
},
|
|
411
|
+
|
|
412
|
+
/** Move onto `target` and fire hover events on it (opens hover menus). */
|
|
413
|
+
async hover(
|
|
414
|
+
target: HTMLElement,
|
|
415
|
+
signal: AbortSignal,
|
|
416
|
+
offset: Partial<Point> = {},
|
|
417
|
+
) {
|
|
418
|
+
await cursor.moveToElement(target, signal, offset);
|
|
419
|
+
const client = toClient(pos);
|
|
420
|
+
dispatchHover(target, client.x, client.y);
|
|
421
|
+
},
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Play the click ripple; optionally dispatch a real event sequence on
|
|
425
|
+
* `target` at the current cursor position. Resolves after the press —
|
|
426
|
+
* the ripple keeps expanding while the script moves on.
|
|
427
|
+
*/
|
|
428
|
+
async click(signal: AbortSignal, target?: HTMLElement | null) {
|
|
429
|
+
el.classList.remove('is-clicking');
|
|
430
|
+
// reflow so re-adding the class restarts the ripple animation
|
|
431
|
+
void el.offsetWidth;
|
|
432
|
+
el.classList.add('is-clicking');
|
|
433
|
+
await sleep(130, signal);
|
|
434
|
+
let restore: (() => void) | undefined;
|
|
435
|
+
if (target) {
|
|
436
|
+
const client = toClient(pos);
|
|
437
|
+
restore = shimPointerCapture(target);
|
|
438
|
+
dispatchClickSequence(target, client.x, client.y);
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
await sleep(200, signal);
|
|
442
|
+
} finally {
|
|
443
|
+
// handlers often finish in a rAF that may still call setPointerCapture
|
|
444
|
+
restore?.();
|
|
445
|
+
el.classList.remove('is-clicking');
|
|
446
|
+
}
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Press on `source`, drag to `to` (overlay-local) dispatching pointermove
|
|
451
|
+
* along the way, release. Events go to `source` (handlers that track a
|
|
452
|
+
* drag usually listen on the pressed element with pointer capture).
|
|
453
|
+
*/
|
|
454
|
+
async drag(
|
|
455
|
+
source: HTMLElement,
|
|
456
|
+
to: Point,
|
|
457
|
+
signal: AbortSignal,
|
|
458
|
+
{
|
|
459
|
+
duration = 900,
|
|
460
|
+
steps = 36,
|
|
461
|
+
}: { duration?: number; steps?: number } = {},
|
|
462
|
+
) {
|
|
463
|
+
await cursor.moveToElement(source, signal);
|
|
464
|
+
const restore = shimPointerCapture(source);
|
|
465
|
+
try {
|
|
466
|
+
const start = toClient(pos);
|
|
467
|
+
el.classList.add('is-pressed');
|
|
468
|
+
safeDispatch(
|
|
469
|
+
source,
|
|
470
|
+
pointerEvent('pointerdown', start.x, start.y, {
|
|
471
|
+
button: 0,
|
|
472
|
+
buttons: 1,
|
|
473
|
+
}),
|
|
474
|
+
);
|
|
475
|
+
safeDispatch(
|
|
476
|
+
source,
|
|
477
|
+
new MouseEvent(
|
|
478
|
+
'mousedown',
|
|
479
|
+
mouseInit(start.x, start.y, { button: 0, buttons: 1 }),
|
|
480
|
+
),
|
|
481
|
+
);
|
|
482
|
+
await sleep(160, signal);
|
|
483
|
+
const from = { ...pos };
|
|
484
|
+
let lastStep = -1;
|
|
485
|
+
await tween(
|
|
486
|
+
(t) => {
|
|
487
|
+
pos = {
|
|
488
|
+
x: from.x + (to.x - from.x) * t,
|
|
489
|
+
y: from.y + (to.y - from.y) * t,
|
|
490
|
+
};
|
|
491
|
+
render();
|
|
492
|
+
const step = Math.floor(t * steps);
|
|
493
|
+
if (step !== lastStep) {
|
|
494
|
+
lastStep = step;
|
|
495
|
+
const client = toClient(pos);
|
|
496
|
+
safeDispatch(
|
|
497
|
+
source,
|
|
498
|
+
pointerEvent('pointermove', client.x, client.y, { buttons: 1 }),
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
},
|
|
502
|
+
duration,
|
|
503
|
+
signal,
|
|
504
|
+
);
|
|
505
|
+
await sleep(140, signal);
|
|
506
|
+
const end = toClient(pos);
|
|
507
|
+
safeDispatch(
|
|
508
|
+
source,
|
|
509
|
+
pointerEvent('pointerup', end.x, end.y, { button: 0, buttons: 0 }),
|
|
510
|
+
);
|
|
511
|
+
safeDispatch(
|
|
512
|
+
source,
|
|
513
|
+
new MouseEvent('mouseup', mouseInit(end.x, end.y, { button: 0 })),
|
|
514
|
+
);
|
|
515
|
+
el.classList.remove('is-pressed');
|
|
516
|
+
// handlers commonly finish the drop in a rAF
|
|
517
|
+
await sleep(120, signal);
|
|
518
|
+
} finally {
|
|
519
|
+
el.classList.remove('is-pressed');
|
|
520
|
+
restore();
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
|
|
524
|
+
/** Convert an element's center to overlay-local coordinates. */
|
|
525
|
+
centerOf,
|
|
526
|
+
|
|
527
|
+
destroy() {
|
|
528
|
+
el.remove();
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
return cursor;
|
|
533
|
+
}
|