@dloizides/marketing-astro-kit 1.9.3 → 1.10.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 +10 -0
- package/README.md +4 -0
- package/package.json +4 -3
- package/src/HeroBackdrop.astro +19 -57
- package/src/motionController/createMotionController.ts +72 -0
- package/src/motionController/index.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.10.0
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- `./motionController`: `createMotionController(options)` runs a frame loop only while the page is visible and the target is in view, and holds a still under `prefers-reduced-motion` (MOONCUBE-SITE-3 "Stunning background, DevOps experience and ISO 27001 controls"). Its source is self-contained, so an `is:inline` script can carry it.
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- `HeroBackdrop` uses `createMotionController`; behaviour unchanged.
|
|
12
|
+
|
|
3
13
|
## 1.9.1
|
|
4
14
|
|
|
5
15
|
### Fixed
|
package/README.md
CHANGED
|
@@ -298,3 +298,7 @@ For in-editor types, mirror the alias in `tsconfig.json`:
|
|
|
298
298
|
|
|
299
299
|
The Astro build (Rollup) only needs the Vite alias — types are not required for
|
|
300
300
|
`astro build` to succeed.
|
|
301
|
+
|
|
302
|
+
## Motion controller
|
|
303
|
+
|
|
304
|
+
`@dloizides/marketing-astro-kit/motionController` exports `createMotionController(options): { getState, sync }`. It runs `onFrame(deltaMs)` only while the page is visible and `target` is in view, and calls `onStill()` under `prefers-reduced-motion`. `forceStill` and `canRun` add host conditions; call `sync()` when they change. `watchReducedMotion: false` reads the preference once. The function references no module scope, so `HeroBackdrop` carries its source in an `is:inline` script.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dloizides/marketing-astro-kit",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "Reusable, themeable Astro marketing sections for product sites — a Free-vs-Pro pricing comparison table, a trust / credibility section (security cards, customer-logo + testimonial placeholders, discreet built-by), a client-side \"try it\" demo shell, a meeting request form posting to a contact endpoint, and an SEO guides/content-hub card + frontmatter schema. Data-driven via a shared TypeScript schema; themed entirely through the host site's CSS custom properties.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro",
|
|
@@ -60,11 +60,12 @@
|
|
|
60
60
|
"./MeetingRequestForm.astro": "./src/MeetingRequestForm.astro",
|
|
61
61
|
"./meetingRequest": "./src/meetingRequest/index.ts",
|
|
62
62
|
"./DateField.astro": "./src/DateField.astro",
|
|
63
|
-
"./dateField": "./src/dateField/index.ts"
|
|
63
|
+
"./dateField": "./src/dateField/index.ts",
|
|
64
|
+
"./motionController": "./src/motionController/index.ts"
|
|
64
65
|
},
|
|
65
66
|
"scripts": {
|
|
66
67
|
"test": "vitest run",
|
|
67
|
-
"lint": "eslint src/bookingEmbed src/dateField src/meetingRequest --ext .ts",
|
|
68
|
+
"lint": "eslint src/bookingEmbed src/dateField src/meetingRequest src/motionController --ext .ts",
|
|
68
69
|
"typecheck": "tsc --noEmit -p ."
|
|
69
70
|
},
|
|
70
71
|
"dependencies": {
|
package/src/HeroBackdrop.astro
CHANGED
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
// "clean this up" into a module import without changing how tenants are served.
|
|
31
31
|
// ============================================================================
|
|
32
32
|
import type { HeroBackdropPreset } from './heroBackdrop/types';
|
|
33
|
+
import { createMotionController } from './motionController';
|
|
33
34
|
|
|
34
35
|
interface Props {
|
|
35
36
|
/** A catalog entry — see `@dloizides/marketing-astro-kit/heroBackdrop`. */
|
|
@@ -59,6 +60,7 @@ const { preset, class: className = '', mountSelector = '' } = Astro.props;
|
|
|
59
60
|
// left exactly as it was.
|
|
60
61
|
const isEmberCanvas = preset.engine === 'canvas' && preset.variant === 'ember';
|
|
61
62
|
const cfg = isEmberCanvas ? { palette: preset.palette, ember: preset.ember } : null;
|
|
63
|
+
const motionControllerScript = `window.dlzCreateMotionController = ${createMotionController.toString()};`;
|
|
62
64
|
---
|
|
63
65
|
|
|
64
66
|
{
|
|
@@ -71,6 +73,8 @@ const cfg = isEmberCanvas ? { palette: preset.palette, ember: preset.ember } : n
|
|
|
71
73
|
)
|
|
72
74
|
}
|
|
73
75
|
|
|
76
|
+
{isEmberCanvas && <script is:inline set:html={motionControllerScript} />}
|
|
77
|
+
|
|
74
78
|
{
|
|
75
79
|
isEmberCanvas && (
|
|
76
80
|
<script is:inline define:vars={{ heroBackdropCfg: cfg, heroBackdropMount: mountSelector }}>
|
|
@@ -92,7 +96,6 @@ const cfg = isEmberCanvas ? { palette: preset.palette, ember: preset.ember } : n
|
|
|
92
96
|
|
|
93
97
|
var EP = cfg.ember;
|
|
94
98
|
var ctx = cv.getContext('2d');
|
|
95
|
-
var reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
96
99
|
|
|
97
100
|
/* ---- palette: six hex stops -> rgb triples + soft radial sprites ---- */
|
|
98
101
|
var STOPS = cfg.palette.map(function (h) {
|
|
@@ -257,71 +260,30 @@ const cfg = isEmberCanvas ? { palette: preset.palette, ember: preset.ember } : n
|
|
|
257
260
|
draw();
|
|
258
261
|
}
|
|
259
262
|
|
|
260
|
-
/* ---- the loop, and the two conditions that stop it ---- */
|
|
261
|
-
var rafId = 0;
|
|
262
|
-
var running = false;
|
|
263
|
-
var lastT = 0;
|
|
264
|
-
var visible = !document.hidden;
|
|
265
|
-
var onScreen = true;
|
|
266
|
-
function loop(now) {
|
|
267
|
-
if (!running) {
|
|
268
|
-
rafId = 0;
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
var dt = lastT ? now - lastT : 16;
|
|
272
|
-
lastT = now;
|
|
273
|
-
if (dt > 100) dt = 100;
|
|
274
|
-
ensure();
|
|
275
|
-
step(dt);
|
|
276
|
-
draw();
|
|
277
|
-
rafId = window.requestAnimationFrame(loop);
|
|
278
|
-
}
|
|
279
|
-
function stopLoop() {
|
|
280
|
-
running = false;
|
|
281
|
-
if (rafId) {
|
|
282
|
-
window.cancelAnimationFrame(rafId);
|
|
283
|
-
rafId = 0;
|
|
284
|
-
}
|
|
285
|
-
lastT = 0;
|
|
286
|
-
}
|
|
287
|
-
function startLoop() {
|
|
288
|
-
if (reduced || running || !visible || !onScreen) return;
|
|
289
|
-
running = true;
|
|
290
|
-
lastT = 0;
|
|
291
|
-
rafId = window.requestAnimationFrame(loop);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
263
|
resize();
|
|
295
264
|
ensure();
|
|
296
265
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
266
|
+
var motion = window.dlzCreateMotionController({
|
|
267
|
+
target: cv,
|
|
268
|
+
threshold: 0.02,
|
|
269
|
+
watchReducedMotion: false,
|
|
270
|
+
onFrame: function (dt) {
|
|
271
|
+
ensure();
|
|
272
|
+
step(dt);
|
|
273
|
+
draw();
|
|
274
|
+
},
|
|
275
|
+
onStill: function () {
|
|
276
|
+
for (var k = 0; k < 70; k++) step(16);
|
|
277
|
+
draw();
|
|
278
|
+
},
|
|
308
279
|
});
|
|
309
|
-
if ('
|
|
310
|
-
|
|
311
|
-
function (entries) {
|
|
312
|
-
for (var i = 0; i < entries.length; i++) onScreen = entries[i].isIntersecting;
|
|
313
|
-
if (onScreen) startLoop();
|
|
314
|
-
else stopLoop();
|
|
315
|
-
},
|
|
316
|
-
{ threshold: 0.02 }
|
|
317
|
-
).observe(cv);
|
|
318
|
-
}
|
|
280
|
+
if (motion.getState() === 'still') return;
|
|
281
|
+
|
|
319
282
|
var rz = 0;
|
|
320
283
|
window.addEventListener('resize', function () {
|
|
321
284
|
window.clearTimeout(rz);
|
|
322
285
|
rz = window.setTimeout(resize, 150);
|
|
323
286
|
});
|
|
324
|
-
startLoop();
|
|
325
287
|
})();
|
|
326
288
|
</script>
|
|
327
289
|
)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export const enum MotionState {
|
|
2
|
+
Still = 'still',
|
|
3
|
+
Running = 'running',
|
|
4
|
+
Paused = 'paused',
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface MotionControllerOptions {
|
|
8
|
+
target: Element;
|
|
9
|
+
onFrame: (deltaMs: number) => void;
|
|
10
|
+
onStill?: () => void;
|
|
11
|
+
onState?: (state: MotionState) => void;
|
|
12
|
+
threshold?: number;
|
|
13
|
+
watchReducedMotion?: boolean;
|
|
14
|
+
forceStill?: () => boolean;
|
|
15
|
+
canRun?: () => boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MotionController {
|
|
19
|
+
getState: () => MotionState;
|
|
20
|
+
sync: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Runs a frame loop only while the page is visible and the target in view, or holds a still under reduced motion. */
|
|
24
|
+
export function createMotionController(options: MotionControllerOptions): MotionController {
|
|
25
|
+
const STILL = 'still' as string as MotionState;
|
|
26
|
+
const RUNNING = 'running' as string as MotionState;
|
|
27
|
+
const PAUSED = 'paused' as string as MotionState;
|
|
28
|
+
const FIRST_FRAME_MS = 16;
|
|
29
|
+
const MAX_FRAME_MS = 100;
|
|
30
|
+
const { target, onFrame, onStill, onState, threshold = 0, watchReducedMotion = true, forceStill, canRun } = options;
|
|
31
|
+
const reducedQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
|
|
32
|
+
const reducedAtStart = reducedQuery.matches;
|
|
33
|
+
let inView = true;
|
|
34
|
+
let running = false;
|
|
35
|
+
let frame = 0;
|
|
36
|
+
let last = 0;
|
|
37
|
+
const isStill = (): boolean => (watchReducedMotion ? reducedQuery.matches : reducedAtStart) || forceStill?.() === true;
|
|
38
|
+
const getState = (): MotionState => {
|
|
39
|
+
if (isStill()) return STILL;
|
|
40
|
+
return document.hidden || !inView ? PAUSED : RUNNING;
|
|
41
|
+
};
|
|
42
|
+
const tick = (now: number): void => {
|
|
43
|
+
const deltaMs = last === 0 ? FIRST_FRAME_MS : Math.min(now - last, MAX_FRAME_MS);
|
|
44
|
+
last = now;
|
|
45
|
+
onFrame(deltaMs);
|
|
46
|
+
if (running) frame = window.requestAnimationFrame(tick);
|
|
47
|
+
};
|
|
48
|
+
const stop = (): void => {
|
|
49
|
+
running = false;
|
|
50
|
+
window.cancelAnimationFrame(frame);
|
|
51
|
+
last = 0;
|
|
52
|
+
};
|
|
53
|
+
const sync = (): void => {
|
|
54
|
+
const state = getState();
|
|
55
|
+
onState?.(state);
|
|
56
|
+
if (state === RUNNING && canRun?.() !== false) {
|
|
57
|
+
if (!running) { running = true; frame = window.requestAnimationFrame(tick); }
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
stop();
|
|
61
|
+
if (state === STILL) onStill?.();
|
|
62
|
+
};
|
|
63
|
+
if (watchReducedMotion) reducedQuery.addEventListener('change', sync);
|
|
64
|
+
if (watchReducedMotion || !reducedAtStart) {
|
|
65
|
+
document.addEventListener('visibilitychange', sync);
|
|
66
|
+
if ('IntersectionObserver' in window) {
|
|
67
|
+
new IntersectionObserver((entries) => { entries.forEach((entry) => { inView = entry.isIntersecting; }); sync(); }, { threshold }).observe(target);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
sync();
|
|
71
|
+
return { getState, sync };
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createMotionController';
|