@dloizides/marketing-astro-kit 1.11.0 → 1.13.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +173 -154
  2. package/README.md +72 -26
  3. package/package.json +16 -7
  4. package/src/ContactCard.astro +107 -0
  5. package/src/contactCard/CardActions.astro +20 -0
  6. package/src/contactCard/CardBrand.astro +25 -0
  7. package/src/contactCard/CardContacts.astro +22 -0
  8. package/src/contactCard/CardContactsVariant.ts +5 -0
  9. package/src/contactCard/CardDialog.astro +16 -0
  10. package/src/contactCard/CardIcon.astro +12 -0
  11. package/src/contactCard/CardIconName.ts +6 -0
  12. package/src/contactCard/CardIdentity.astro +14 -0
  13. package/src/contactCard/CardLinkKind.ts +6 -0
  14. package/src/contactCard/CardPortrait.astro +14 -0
  15. package/src/contactCard/CardQr.astro +24 -0
  16. package/src/contactCard/CardSave.astro +43 -0
  17. package/src/contactCard/CardValues.astro +12 -0
  18. package/src/contactCard/ContactCardLayout.ts +7 -0
  19. package/src/contactCard/ContactPhotoType.ts +5 -0
  20. package/src/contactCard/InstallRoute.ts +6 -0
  21. package/src/contactCard/cardIcons.ts +11 -0
  22. package/src/contactCard/cardLinks.ts +43 -0
  23. package/src/contactCard/checks.ts +28 -0
  24. package/src/contactCard/chooseInstall.ts +21 -0
  25. package/src/contactCard/contactCard.css +69 -0
  26. package/src/contactCard/index.ts +16 -0
  27. package/src/contactCard/layouts.css +106 -0
  28. package/src/contactCard/manifest.ts +37 -0
  29. package/src/contactCard/motion/OpenMotion.ts +7 -0
  30. package/src/contactCard/motion/QrMotion.ts +7 -0
  31. package/src/contactCard/motion/SaveMotion.ts +7 -0
  32. package/src/contactCard/motion/TiltMotion.ts +7 -0
  33. package/src/contactCard/motion/beatTable.ts +144 -0
  34. package/src/contactCard/motion/beats.ts +43 -0
  35. package/src/contactCard/motion/cardMotion.ts +49 -0
  36. package/src/contactCard/motion/easings.ts +9 -0
  37. package/src/contactCard/motion/holdDuringOpen.ts +22 -0
  38. package/src/contactCard/motion/mountMotion.ts +26 -0
  39. package/src/contactCard/motion/mountOpenMark.ts +20 -0
  40. package/src/contactCard/motion/mountQrTurn.ts +86 -0
  41. package/src/contactCard/motion/mountSave.ts +26 -0
  42. package/src/contactCard/motion/mountTilt.ts +92 -0
  43. package/src/contactCard/motion/openCss.ts +18 -0
  44. package/src/contactCard/motion/parts.ts +25 -0
  45. package/src/contactCard/motion/playBeats.ts +21 -0
  46. package/src/contactCard/motion/sides.ts +33 -0
  47. package/src/contactCard/motion/tiltPose.ts +38 -0
  48. package/src/contactCard/motion.css +39 -0
  49. package/src/contactCard/mount.ts +76 -0
  50. package/src/contactCard/phone.ts +11 -0
  51. package/src/contactCard/qrSvg.ts +54 -0
  52. package/src/contactCard/types.ts +47 -0
  53. package/src/contactCard/vCard.ts +48 -0
  54. package/src/contactCard/vCardLines.ts +33 -0
  55. package/src/logoIntro/LogoIntroState.ts +8 -0
  56. package/src/logoIntro/geometry.ts +53 -0
  57. package/src/logoIntro/index.ts +6 -0
  58. package/src/logoIntro/runLogoIntro.ts +75 -0
  59. package/src/logoIntro/settleReveal.ts +146 -0
  60. package/src/logoIntro/strokes.ts +61 -0
  61. package/src/logoIntro/timeline.ts +30 -0
  62. package/src/motionController/createMotionController.ts +11 -9
@@ -0,0 +1,75 @@
1
+ import { DEFAULT_CENTRE_SIZE, pickVisible, placeMark, type CentreSize } from './geometry';
2
+ import { LOGO_INTRO_DATASET_KEY, LogoIntroState } from './LogoIntroState';
3
+ import { glideEndMs, glideKeyframes, type LogoIntroTiming } from './timeline';
4
+
5
+ export interface LogoIntroOptions {
6
+ stage: HTMLElement;
7
+ targets: readonly HTMLElement[];
8
+ timing: LogoIntroTiming;
9
+ reducedMotion: boolean;
10
+ size?: CentreSize;
11
+ stateHost?: HTMLElement;
12
+ logoPhase?: (mark: HTMLElement) => readonly Animation[];
13
+ reveal?: (stage: HTMLElement, glideStartMs: number) => readonly Animation[];
14
+ }
15
+
16
+ export interface LogoIntroController {
17
+ readonly state: LogoIntroState;
18
+ readonly done: Promise<void>;
19
+ finish(): void;
20
+ }
21
+
22
+ function setState(host: HTMLElement, state: LogoIntroState): void {
23
+ host.dataset[LOGO_INTRO_DATASET_KEY] = state;
24
+ }
25
+
26
+ function still(host: HTMLElement): LogoIntroController {
27
+ setState(host, LogoIntroState.Still);
28
+ return { state: LogoIntroState.Still, done: Promise.resolve(), finish: (): void => undefined };
29
+ }
30
+
31
+ function glide(mark: HTMLElement, options: LogoIntroOptions): Animation {
32
+ const endOpacity = getComputedStyle(mark).opacity;
33
+ const viewport = { width: window.innerWidth, height: window.innerHeight };
34
+ const placement = placeMark(mark.getBoundingClientRect(), viewport, options.size ?? DEFAULT_CENTRE_SIZE);
35
+ return mark.animate(glideKeyframes(placement, options.timing, endOpacity), {
36
+ duration: glideEndMs(options.timing),
37
+ fill: 'backwards',
38
+ });
39
+ }
40
+
41
+ function startAll(mark: HTMLElement, options: LogoIntroOptions): Animation[] {
42
+ return [
43
+ glide(mark, options),
44
+ ...(options.logoPhase?.(mark) ?? []),
45
+ ...(options.reveal?.(options.stage, options.timing.holdUntilMs) ?? []),
46
+ ];
47
+ }
48
+
49
+ /** Plays the mark centred, glides it to its slot and runs the reveal; still under reduced motion. */
50
+ export function runLogoIntro(options: LogoIntroOptions): LogoIntroController {
51
+ const host = options.stateHost ?? document.documentElement;
52
+ const mark = pickVisible(options.targets);
53
+ if (options.reducedMotion || mark === undefined) return still(host);
54
+ setState(host, LogoIntroState.Playing);
55
+ const animations = startAll(mark, options);
56
+ let state = LogoIntroState.Playing;
57
+ const finish = (): void => animations.forEach((animation) => animation.finish());
58
+ const startWidth = window.innerWidth;
59
+ const onResize = (): void => {
60
+ if (window.innerWidth !== startWidth) finish();
61
+ };
62
+ window.addEventListener('resize', onResize);
63
+ const done = Promise.allSettled(animations.map((animation) => animation.finished)).then(() => {
64
+ window.removeEventListener('resize', onResize);
65
+ state = LogoIntroState.Done;
66
+ setState(host, state);
67
+ });
68
+ return {
69
+ get state(): LogoIntroState {
70
+ return state;
71
+ },
72
+ done,
73
+ finish,
74
+ };
75
+ }
@@ -0,0 +1,146 @@
1
+ import { staggerMs } from './timeline';
2
+
3
+ export interface SettleTiming {
4
+ lineStartMs: number;
5
+ lineStepMs: number;
6
+ ruleDrawMs: number;
7
+ settleAfterMs: number;
8
+ settleMs: number;
9
+ fadeAfterMs: number;
10
+ fadeMs: number;
11
+ frameStepMs: number;
12
+ frameDrawMs: number;
13
+ fillAfterMs: number;
14
+ fillMs: number;
15
+ drawEase: string;
16
+ settleEase: string;
17
+ }
18
+
19
+ export interface LineSchedule {
20
+ ruleAt: number;
21
+ settleAt: number;
22
+ fadeAt: number;
23
+ endAt: number;
24
+ }
25
+
26
+ export interface SettleRevealOptions {
27
+ stage: HTMLElement;
28
+ lines: readonly HTMLElement[];
29
+ frames: readonly HTMLElement[];
30
+ startMs: number;
31
+ accent: string;
32
+ timing: SettleTiming;
33
+ }
34
+
35
+ interface Rect {
36
+ left: number;
37
+ top: number;
38
+ width: number;
39
+ height: number;
40
+ }
41
+
42
+ const RULE_GAP_PX = 4;
43
+ const RULE_PX = 2;
44
+ const FRAME_BORDER = '1.5px';
45
+ const SETTLE_RISE = '0 6px';
46
+ const OVERLAY_LAYER = '3';
47
+
48
+ /** When a line's rule draws, the line settles, the rule fades and all of it ends. */
49
+ export function lineSchedule(index: number, startMs: number, timing: SettleTiming): LineSchedule {
50
+ const ruleAt = staggerMs(index, startMs + timing.lineStartMs, timing.lineStepMs);
51
+ const settleAt = ruleAt + timing.settleAfterMs;
52
+ const fadeAt = settleAt + timing.fadeAfterMs;
53
+ return { ruleAt, settleAt, fadeAt, endAt: fadeAt + timing.fadeMs };
54
+ }
55
+
56
+ /** When the outline of the frame at `index` starts to draw. */
57
+ export function frameDrawAt(index: number, startMs: number, timing: SettleTiming): number {
58
+ return staggerMs(index, startMs + timing.lineStartMs, timing.frameStepMs);
59
+ }
60
+
61
+ function hasSize(element: HTMLElement): boolean {
62
+ const box = element.getBoundingClientRect();
63
+ return box.width > 0 && box.height > 0;
64
+ }
65
+
66
+ function overlay(stage: HTMLElement, box: Rect): HTMLDivElement {
67
+ const origin = stage.getBoundingClientRect();
68
+ const div = document.createElement('div');
69
+ div.setAttribute('aria-hidden', 'true');
70
+ Object.assign(div.style, {
71
+ position: 'absolute',
72
+ left: `${box.left - origin.left}px`,
73
+ top: `${box.top - origin.top}px`,
74
+ width: `${box.width}px`,
75
+ height: `${box.height}px`,
76
+ pointerEvents: 'none',
77
+ zIndex: OVERLAY_LAYER,
78
+ boxSizing: 'border-box',
79
+ });
80
+ stage.appendChild(div);
81
+ return div;
82
+ }
83
+
84
+ function removeOn(animation: Animation, element: HTMLElement): Animation {
85
+ const remove = (): void => element.remove();
86
+ animation.finished.then(remove, remove);
87
+ return animation;
88
+ }
89
+
90
+ function ruleKeyframes(at: LineSchedule, timing: SettleTiming): Keyframe[] {
91
+ const span = at.endAt - at.ruleAt;
92
+ const fadeShare = (at.fadeAt - at.ruleAt) / span;
93
+ const drawnShare = Math.min(timing.ruleDrawMs / span, fadeShare);
94
+ return [
95
+ { scale: '0 1', opacity: 1, offset: 0, easing: timing.drawEase },
96
+ { scale: '1 1', opacity: 1, offset: drawnShare },
97
+ { scale: '1 1', opacity: 1, offset: fadeShare },
98
+ { scale: '1 1', opacity: 0, offset: 1 },
99
+ ];
100
+ }
101
+
102
+ function revealLine(options: SettleRevealOptions, line: HTMLElement, index: number): Animation[] {
103
+ const { timing } = options;
104
+ const at = lineSchedule(index, options.startMs, timing);
105
+ const box = line.getBoundingClientRect();
106
+ const rule = overlay(options.stage, { left: box.left, top: box.bottom + RULE_GAP_PX, width: box.width, height: RULE_PX });
107
+ Object.assign(rule.style, { background: options.accent, transformOrigin: 'left center' });
108
+ const ruleRun = rule.animate(ruleKeyframes(at, timing), { delay: at.ruleAt, duration: at.endAt - at.ruleAt, fill: 'both' });
109
+ const settle = line.animate(
110
+ [
111
+ { opacity: 0, translate: SETTLE_RISE },
112
+ { opacity: 1, translate: '0 0' },
113
+ ],
114
+ { delay: at.settleAt, duration: timing.settleMs, easing: timing.settleEase, fill: 'backwards' },
115
+ );
116
+ return [removeOn(ruleRun, rule), settle];
117
+ }
118
+
119
+ function revealFrame(options: SettleRevealOptions, element: HTMLElement, index: number): Animation[] {
120
+ const { timing } = options;
121
+ const drawAt = frameDrawAt(index, options.startMs, timing);
122
+ const outline = overlay(options.stage, element.getBoundingClientRect());
123
+ Object.assign(outline.style, { border: `${FRAME_BORDER} solid ${options.accent}`, borderRadius: getComputedStyle(element).borderRadius });
124
+ const span = timing.frameDrawMs + timing.fadeMs;
125
+ const draw = outline.animate(
126
+ [
127
+ { clipPath: 'inset(0 100% 0 0)', opacity: 1, offset: 0, easing: timing.drawEase },
128
+ { clipPath: 'inset(0 0 0 0)', opacity: 1, offset: timing.frameDrawMs / span },
129
+ { clipPath: 'inset(0 0 0 0)', opacity: 0, offset: 1 },
130
+ ],
131
+ { delay: drawAt, duration: span, fill: 'both' },
132
+ );
133
+ const fill = element.animate([{ opacity: 0 }, { opacity: 1 }], {
134
+ delay: drawAt + timing.fillAfterMs,
135
+ duration: timing.fillMs,
136
+ fill: 'backwards',
137
+ });
138
+ return [removeOn(draw, outline), fill];
139
+ }
140
+
141
+ /** Entrance where accent rules and outlines draw, the copy settles in, then the overlays fade. */
142
+ export function settleReveal(options: SettleRevealOptions): Animation[] {
143
+ const lines = options.lines.filter(hasSize).flatMap((line, index) => revealLine(options, line, index));
144
+ const frames = options.frames.filter(hasSize).flatMap((frame, index) => revealFrame(options, frame, index));
145
+ return [...lines, ...frames];
146
+ }
@@ -0,0 +1,61 @@
1
+ import { staggerMs } from './timeline';
2
+
3
+ export interface StrokeTiming {
4
+ delayMs: number;
5
+ durationMs: number;
6
+ stepMs: number;
7
+ easing: string;
8
+ }
9
+
10
+ export interface SweepStyle {
11
+ colour: string;
12
+ dashShare: number;
13
+ width: number;
14
+ }
15
+
16
+ const LIGHT_IN = 0.2;
17
+ const LIGHT_OUT = 0.8;
18
+
19
+ /** Draws each stroke from nothing to whole, one after another. */
20
+ export function drawStrokes(strokes: readonly SVGGeometryElement[], timing: StrokeTiming): Animation[] {
21
+ return strokes.map((stroke, index) => {
22
+ stroke.setAttribute('pathLength', '1');
23
+ return stroke.animate(
24
+ [
25
+ { strokeDasharray: '1 1', strokeDashoffset: 1 },
26
+ { strokeDasharray: '1 1', strokeDashoffset: 0 },
27
+ ],
28
+ { delay: staggerMs(index, timing.delayMs, timing.stepMs), duration: timing.durationMs, easing: timing.easing, fill: 'backwards' },
29
+ );
30
+ });
31
+ }
32
+
33
+ function lightFor(stroke: SVGGeometryElement, style: SweepStyle): SVGGeometryElement {
34
+ const light = stroke.cloneNode(false) as SVGGeometryElement;
35
+ light.setAttribute('pathLength', '1');
36
+ light.setAttribute('stroke-width', String(style.width));
37
+ light.setAttribute('stroke-linecap', 'round');
38
+ light.setAttribute('stroke-dasharray', `${style.dashShare} ${1 - style.dashShare}`);
39
+ light.style.stroke = style.colour;
40
+ stroke.after(light);
41
+ return light;
42
+ }
43
+
44
+ /** Runs a short light once round each stroke, then removes it. */
45
+ export function sweepStrokes(strokes: readonly SVGGeometryElement[], timing: StrokeTiming, style: SweepStyle): Animation[] {
46
+ return strokes.map((stroke, index) => {
47
+ const light = lightFor(stroke, style);
48
+ const animation = light.animate(
49
+ [
50
+ { strokeDashoffset: 0, opacity: 0 },
51
+ { opacity: 1, offset: LIGHT_IN },
52
+ { opacity: 1, offset: LIGHT_OUT },
53
+ { strokeDashoffset: -1, opacity: 0 },
54
+ ],
55
+ { delay: staggerMs(index, timing.delayMs, timing.stepMs), duration: timing.durationMs, easing: timing.easing, fill: 'both' },
56
+ );
57
+ const remove = (): void => light.remove();
58
+ animation.finished.then(remove, remove);
59
+ return animation;
60
+ });
61
+ }
@@ -0,0 +1,30 @@
1
+ import type { MarkPlacement } from './geometry';
2
+
3
+ export interface LogoIntroTiming {
4
+ fadeInMs: number;
5
+ holdUntilMs: number;
6
+ glideMs: number;
7
+ glideEase: string;
8
+ }
9
+
10
+ /** Time at which the mark is back in its slot. */
11
+ export function glideEndMs(timing: LogoIntroTiming): number {
12
+ return timing.holdUntilMs + timing.glideMs;
13
+ }
14
+
15
+ /** Start time of the item at `index` in a staggered run. */
16
+ export function staggerMs(index: number, startMs: number, stepMs: number): number {
17
+ return startMs + index * stepMs;
18
+ }
19
+
20
+ /** Keyframes of the whole mark run: fade in centred, hold, glide home to its resting opacity. */
21
+ export function glideKeyframes(placement: MarkPlacement, timing: LogoIntroTiming, endOpacity: string): Keyframe[] {
22
+ const total = glideEndMs(timing);
23
+ const centred = { translate: `${placement.dx}px ${placement.dy}px`, scale: String(placement.scale) };
24
+ return [
25
+ { ...centred, opacity: 0, offset: 0 },
26
+ { ...centred, opacity: 1, offset: timing.fadeInMs / total },
27
+ { ...centred, opacity: 1, offset: timing.holdUntilMs / total, easing: timing.glideEase },
28
+ { translate: '0px 0px', scale: '1', opacity: endOpacity, offset: 1 },
29
+ ];
30
+ }
@@ -13,6 +13,7 @@ export interface MotionControllerOptions {
13
13
  watchReducedMotion?: boolean;
14
14
  forceStill?: () => boolean;
15
15
  canRun?: () => boolean;
16
+ signal?: AbortSignal;
16
17
  }
17
18
 
18
19
  export interface MotionController {
@@ -27,7 +28,7 @@ export function createMotionController(options: MotionControllerOptions): Motion
27
28
  const PAUSED = 'paused' as string as MotionState;
28
29
  const FIRST_FRAME_MS = 16;
29
30
  const MAX_FRAME_MS = 100;
30
- const { target, onFrame, onStill, onState, threshold = 0, watchReducedMotion = true, forceStill, canRun } = options;
31
+ const { target, onFrame, onStill, onState, threshold = 0, watchReducedMotion = true, forceStill, canRun, signal } = options;
31
32
  const reducedQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
32
33
  const reducedAtStart = reducedQuery.matches;
33
34
  let inView = true;
@@ -53,20 +54,21 @@ export function createMotionController(options: MotionControllerOptions): Motion
53
54
  const sync = (): void => {
54
55
  const state = getState();
55
56
  onState?.(state);
56
- if (state === RUNNING && canRun?.() !== false) {
57
+ if (state === RUNNING && canRun?.() !== false && signal?.aborted !== true) {
57
58
  if (!running) { running = true; frame = window.requestAnimationFrame(tick); }
58
59
  return;
59
60
  }
60
61
  stop();
61
62
  if (state === STILL) onStill?.();
62
63
  };
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
- }
64
+ if (watchReducedMotion) reducedQuery.addEventListener('change', sync, { signal });
65
+ const watchesPage = watchReducedMotion || !reducedAtStart;
66
+ if (watchesPage) document.addEventListener('visibilitychange', sync, { signal });
67
+ const observer = watchesPage && 'IntersectionObserver' in window
68
+ ? new IntersectionObserver((entries) => { entries.forEach((entry) => { inView = entry.isIntersecting; }); sync(); }, { threshold })
69
+ : undefined;
70
+ observer?.observe(target);
71
+ signal?.addEventListener('abort', () => { observer?.disconnect(); sync(); }, { once: true });
70
72
  sync();
71
73
  return { getState, sync };
72
74
  }