@handysolver/festive 0.0.0-stage → 0.2.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/README.md +212 -2
- package/dist/FestiveLayer.d.ts +14 -0
- package/dist/FestiveLayer.js +214 -0
- package/dist/core.d.ts +25 -0
- package/dist/core.js +79 -0
- package/dist/design/FestiveBadge.d.ts +3 -0
- package/dist/design/FestiveBadge.js +22 -0
- package/dist/design/FestiveGarland.d.ts +2 -0
- package/dist/design/FestiveGarland.js +122 -0
- package/dist/design/FestiveProvider.d.ts +50 -0
- package/dist/design/FestiveProvider.js +272 -0
- package/dist/design/celebration.d.ts +28 -0
- package/dist/design/celebration.js +515 -0
- package/dist/design/festivals.d.ts +30 -0
- package/dist/design/festivals.js +112 -0
- package/dist/design/styles.d.ts +1 -0
- package/dist/design/styles.js +289 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6 -0
- package/dist/styles.d.ts +1 -0
- package/dist/styles.js +29 -0
- package/package.json +43 -4
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { prefersReducedMotion } from './celebration.js';
|
|
5
|
+
import { useFestive } from './FestiveProvider.js';
|
|
6
|
+
// What hangs along the bottom of the top bar. Nothing here takes clicks: the strand follows the
|
|
7
|
+
// pointer by listening to the window, so the page under it stays usable.
|
|
8
|
+
const SPACING = 32;
|
|
9
|
+
const SPAN = 5; // gaps between the points where the wire is held up
|
|
10
|
+
const DROOP = 6;
|
|
11
|
+
// Pointer reach, and the spring the hanging pieces swing on
|
|
12
|
+
const REACH = 90;
|
|
13
|
+
const SWAY = 2;
|
|
14
|
+
const STIFFNESS = 170;
|
|
15
|
+
const DAMPING = 9;
|
|
16
|
+
const BULB_COLORS = ['#ef4444', '#10b981', '#f59e0b', '#e0f2fe'];
|
|
17
|
+
const useWidth = (ref) => {
|
|
18
|
+
const [width, setWidth] = useState(0);
|
|
19
|
+
useLayoutEffect(() => {
|
|
20
|
+
const el = ref.current;
|
|
21
|
+
if (!el)
|
|
22
|
+
return;
|
|
23
|
+
setWidth(el.clientWidth);
|
|
24
|
+
if (typeof ResizeObserver === 'undefined')
|
|
25
|
+
return;
|
|
26
|
+
const observer = new ResizeObserver(() => setWidth(el.clientWidth));
|
|
27
|
+
observer.observe(el);
|
|
28
|
+
return () => observer.disconnect();
|
|
29
|
+
}, [ref]);
|
|
30
|
+
return width;
|
|
31
|
+
};
|
|
32
|
+
// Pieces near the pointer brighten and swing. Runs only while something is moving.
|
|
33
|
+
function useSwing(ref, items) {
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const strand = ref.current;
|
|
36
|
+
if (!strand || prefersReducedMotion())
|
|
37
|
+
return;
|
|
38
|
+
const els = [...strand.querySelectorAll('[data-hang]')];
|
|
39
|
+
const state = els.map(() => ({ offset: 0, speed: 0, lit: 0 }));
|
|
40
|
+
let pointer = null;
|
|
41
|
+
let frame = 0;
|
|
42
|
+
let last = 0;
|
|
43
|
+
const tick = (now) => {
|
|
44
|
+
const dt = last ? Math.min((now - last) / 1000, 0.032) : 0.016;
|
|
45
|
+
last = now;
|
|
46
|
+
let moving = false;
|
|
47
|
+
els.forEach((el, i) => {
|
|
48
|
+
const s = state[i];
|
|
49
|
+
const away = pointer === null ? Infinity : items[i].x - pointer;
|
|
50
|
+
const near = Math.max(0, 1 - Math.abs(away) / REACH);
|
|
51
|
+
const target = near * SWAY * Math.sign(away);
|
|
52
|
+
s.speed += ((target - s.offset) * STIFFNESS - s.speed * DAMPING) * dt;
|
|
53
|
+
s.offset += s.speed * dt;
|
|
54
|
+
s.lit += (near - s.lit) * Math.min(1, dt * 12);
|
|
55
|
+
if (Math.abs(s.speed) > 0.05 || Math.abs(target - s.offset) > 0.02 || Math.abs(near - s.lit) > 0.01)
|
|
56
|
+
moving = true;
|
|
57
|
+
el.style.transform = `translateX(${s.offset.toFixed(2)}px) rotate(${(-s.offset * 5).toFixed(1)}deg)`;
|
|
58
|
+
el.style.setProperty('--lit', s.lit.toFixed(2));
|
|
59
|
+
});
|
|
60
|
+
frame = moving ? requestAnimationFrame(tick) : 0;
|
|
61
|
+
};
|
|
62
|
+
const onMove = (e) => {
|
|
63
|
+
// The strand is at the top of the page; anything further down needs no measuring
|
|
64
|
+
if (e.clientY > 140 && pointer === null)
|
|
65
|
+
return;
|
|
66
|
+
const box = strand.getBoundingClientRect();
|
|
67
|
+
const inside = e.clientY > box.top - 12 && e.clientY < box.top + 30 && e.clientX > box.left - REACH && e.clientX < box.right + REACH;
|
|
68
|
+
const next = inside ? e.clientX - box.left : null;
|
|
69
|
+
if (next === pointer)
|
|
70
|
+
return;
|
|
71
|
+
pointer = next;
|
|
72
|
+
if (!frame) {
|
|
73
|
+
last = 0;
|
|
74
|
+
frame = requestAnimationFrame(tick);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
window.addEventListener('pointermove', onMove, { passive: true });
|
|
78
|
+
return () => {
|
|
79
|
+
window.removeEventListener('pointermove', onMove);
|
|
80
|
+
cancelAnimationFrame(frame);
|
|
81
|
+
};
|
|
82
|
+
}, [ref, items]);
|
|
83
|
+
}
|
|
84
|
+
// Lights or flowers on a drooping wire
|
|
85
|
+
const Strand = ({ kind }) => {
|
|
86
|
+
const ref = useRef(null);
|
|
87
|
+
const width = useWidth(ref);
|
|
88
|
+
const { items, wire } = useMemo(() => {
|
|
89
|
+
const gaps = Math.max(0, Math.floor(width / SPACING));
|
|
90
|
+
const start = (width - gaps * SPACING) / 2;
|
|
91
|
+
const points = Array.from({ length: width ? gaps + 1 : 0 }, (_, i) => {
|
|
92
|
+
// A parabola between supports; the last stretch may be shorter than the others
|
|
93
|
+
const from = Math.floor(i / SPAN) * SPAN;
|
|
94
|
+
const length = Math.min(SPAN, gaps - from) || 1;
|
|
95
|
+
const f = (i - from) / length;
|
|
96
|
+
return { x: start + i * SPACING, y: 4 * DROOP * f * (1 - f) };
|
|
97
|
+
});
|
|
98
|
+
let path = points.length ? `M${points[0].x} 0` : '';
|
|
99
|
+
for (let from = 0; from < gaps; from += SPAN) {
|
|
100
|
+
const to = Math.min(gaps, from + SPAN);
|
|
101
|
+
path += ` Q${(points[from].x + points[to].x) / 2} ${DROOP * 2} ${points[to].x} 0`;
|
|
102
|
+
}
|
|
103
|
+
return { items: points, wire: path };
|
|
104
|
+
}, [width]);
|
|
105
|
+
useSwing(ref, items);
|
|
106
|
+
return (_jsxs("div", { ref: ref, className: "hsx-strand", children: [_jsx("svg", { width: width, height: DROOP + 2, className: "hsx-wire", children: _jsx("path", { d: wire, fill: "none", strokeWidth: "1.25", className: `hsx-wire-${kind}` }) }), items.map(({ x, y }, i) => (_jsx("span", { "data-hang": true, className: "hsx-hang", style: { left: x, top: y }, children: _jsx("span", { className: `hsx-${kind}`, style: {
|
|
107
|
+
color: kind === 'bulb' ? BULB_COLORS[i % BULB_COLORS.length] : undefined,
|
|
108
|
+
animationDelay: `${(i * 0.15).toFixed(2)}s`,
|
|
109
|
+
} }) }, i)))] }));
|
|
110
|
+
};
|
|
111
|
+
const Diya = ({ index }) => (_jsxs("svg", { viewBox: "0 0 24 24", className: "hsx-diya", children: [_jsx("path", { className: "hsx-diya-flame", style: { animationDelay: `${index * -0.37}s` }, d: "M12 1 C15.4 6 16 9 14.6 11.6 C13.6 13.6 10.4 13.6 9.4 11.6 C8 9 8.6 6 12 1 Z", fill: "#f59e0b" }), _jsx("path", { className: "hsx-diya-flame", style: { animationDelay: `${index * -0.37}s` }, d: "M12 6 C13.6 8.6 13.6 12 12 12.6 C10.4 12 10.4 8.6 12 6 Z", fill: "#fef9c3" }), _jsx("path", { d: "M2 13 C5 14.6 19 14.6 22 13 C21.4 18.6 17 22 12 22 C7 22 2.6 18.6 2 13 Z", fill: "#b45309" }), _jsx("path", { d: "M2 13 C5 14.6 19 14.6 22 13 C19 16.4 5 16.4 2 13 Z", fill: "#f59e0b" })] }));
|
|
112
|
+
const DIYAS = 7;
|
|
113
|
+
export const FestiveGarland = () => {
|
|
114
|
+
const { activeFestival } = useFestive();
|
|
115
|
+
if (!activeFestival)
|
|
116
|
+
return null;
|
|
117
|
+
const { garland, accent, line } = activeFestival;
|
|
118
|
+
const isStrand = garland === 'lights' || garland === 'marigolds';
|
|
119
|
+
return (_jsxs("div", { "aria-hidden": "true", "data-garland": garland, className: "hsx-garland", style: { '--festive-accent': accent }, children: [garland === 'silk' ? (_jsx("div", { className: "hsx-silk", children: _jsx("div", { className: "hsx-silk-wave", children: _jsx("div", { className: "hsx-silk-cloth", style: { background: line } }) }) })) : (!isStrand && _jsx("div", { className: "hsx-runner", style: { background: line || undefined }, "data-garland": garland })), isStrand && _jsx(Strand, { kind: garland === 'lights' ? 'bulb' : 'marigold' }), garland === 'diyas' && (_jsx("div", { className: "hsx-diyas", children: Array.from({ length: DIYAS }, (_, i) => (
|
|
120
|
+
// Five on small screens
|
|
121
|
+
_jsx("span", { className: i === 1 || i === DIYAS - 2 ? 'hsx-wide-only' : 'hsx-block', children: _jsx(Diya, { index: i }) }, i))) }))] }));
|
|
122
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
import { type Layout, type Point } from "./celebration.js";
|
|
3
|
+
import { type ActiveFestival, type FestivalKind } from "./festivals.js";
|
|
4
|
+
/** Full intensity on arrival, then the quiet look for the rest of the day. */
|
|
5
|
+
type EntranceStage = "entrance" | "settled";
|
|
6
|
+
export interface FestiveValue {
|
|
7
|
+
/** The festival being shown, or null. */
|
|
8
|
+
activeFestival: ActiveFestival | null;
|
|
9
|
+
entranceStage: EntranceStage;
|
|
10
|
+
isDark: boolean;
|
|
11
|
+
/** Fires the party popper from a point, or replays the opening scene. */
|
|
12
|
+
triggerBurst: (from?: Point) => void;
|
|
13
|
+
/** Closes the greeting for this festival. */
|
|
14
|
+
dismiss: () => void;
|
|
15
|
+
/** For an on/off switch. Switching on also brings back a closed greeting. */
|
|
16
|
+
effectsOn: boolean;
|
|
17
|
+
toggleEffects: () => void;
|
|
18
|
+
/** For a preview picker. */
|
|
19
|
+
previewKind: FestivalKind | "";
|
|
20
|
+
setPreviewKind: (kind: FestivalKind | "") => void;
|
|
21
|
+
}
|
|
22
|
+
export declare const useFestive: () => FestiveValue;
|
|
23
|
+
export interface FestiveProviderProps {
|
|
24
|
+
/** Your app's name, as used in the Festival API's `apps` lists. */
|
|
25
|
+
app: string;
|
|
26
|
+
/** Origin of the Festival API. Without it only previews show. */
|
|
27
|
+
apiUrl?: string | null;
|
|
28
|
+
/** "auto" (default) follows the page. */
|
|
29
|
+
theme?: "auto" | "light" | "dark";
|
|
30
|
+
/** Show this festival whatever the date, for testing. */
|
|
31
|
+
festival?: FestivalKind;
|
|
32
|
+
/** CSS selectors for your layout. Default: header, aside, main. */
|
|
33
|
+
layout?: Layout;
|
|
34
|
+
/** Turns everything off and cleans up. */
|
|
35
|
+
disabled?: boolean;
|
|
36
|
+
children?: ReactNode;
|
|
37
|
+
}
|
|
38
|
+
/** A burst on arrival, then a soft light and a few stars behind the page. Wrap your app in it. */
|
|
39
|
+
export declare function FestiveProvider(props: FestiveProviderProps): ReactElement;
|
|
40
|
+
/**
|
|
41
|
+
* A window onto the confetti. Place it in a container that is positioned and has `isolation: isolate`:
|
|
42
|
+
* it paints above that container's background and below everything in it.
|
|
43
|
+
*/
|
|
44
|
+
export declare function FestiveSurface(): ReactElement | null;
|
|
45
|
+
/**
|
|
46
|
+
* Continues the light and confetti behind a sticky strip around the header, which would otherwise cut
|
|
47
|
+
* them off in a straight line. Place it first in that strip.
|
|
48
|
+
*/
|
|
49
|
+
export declare function FestiveHeaderBackdrop(): ReactElement | null;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
import { baseUrl, localDate, media, pageTheme } from "../core.js";
|
|
5
|
+
import { Celebration, CELEBRATION_MS, prefersReducedMotion } from "./celebration.js";
|
|
6
|
+
import { activeFromReply, dismissKey, festivalFromId, previewFromAddress } from "./festivals.js";
|
|
7
|
+
import { css } from "./styles.js";
|
|
8
|
+
const DATE_CHECK_MS = 60000;
|
|
9
|
+
const FETCH_TIMEOUT_MS = 15000;
|
|
10
|
+
const nothing = () => { };
|
|
11
|
+
const FestiveContext = createContext({
|
|
12
|
+
activeFestival: null,
|
|
13
|
+
entranceStage: "settled",
|
|
14
|
+
isDark: false,
|
|
15
|
+
triggerBurst: nothing,
|
|
16
|
+
dismiss: nothing,
|
|
17
|
+
effectsOn: true,
|
|
18
|
+
toggleEffects: nothing,
|
|
19
|
+
previewKind: "",
|
|
20
|
+
setPreviewKind: nothing,
|
|
21
|
+
});
|
|
22
|
+
const CelebrationContext = createContext(null);
|
|
23
|
+
export const useFestive = () => useContext(FestiveContext);
|
|
24
|
+
// Today's festival, looked up again when the local date changes. No answer means no festival.
|
|
25
|
+
function useTodaysFestival(app, base) {
|
|
26
|
+
const [festival, setFestival] = useState(null);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!base)
|
|
29
|
+
return;
|
|
30
|
+
let loadedFor = "";
|
|
31
|
+
let request;
|
|
32
|
+
const check = async () => {
|
|
33
|
+
const date = localDate();
|
|
34
|
+
if (date === loadedFor)
|
|
35
|
+
return;
|
|
36
|
+
loadedFor = date;
|
|
37
|
+
request?.abort();
|
|
38
|
+
const mine = (request = new AbortController());
|
|
39
|
+
const timeout = setTimeout(() => mine.abort(), FETCH_TIMEOUT_MS);
|
|
40
|
+
setFestival(null);
|
|
41
|
+
try {
|
|
42
|
+
const query = new URLSearchParams({ app, date });
|
|
43
|
+
const res = await fetch(`${base}/api/festival?${query}`, { signal: mine.signal, credentials: "omit" });
|
|
44
|
+
const reply = res.ok ? await res.json() : null;
|
|
45
|
+
if (request === mine)
|
|
46
|
+
setFestival(activeFromReply(reply, date));
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
if (request === mine)
|
|
50
|
+
loadedFor = ""; // offline or too slow: try again at the next check
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
clearTimeout(timeout);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
void check();
|
|
57
|
+
const timer = setInterval(() => void check(), DATE_CHECK_MS);
|
|
58
|
+
return () => {
|
|
59
|
+
clearInterval(timer);
|
|
60
|
+
const last = request;
|
|
61
|
+
request = undefined;
|
|
62
|
+
last?.abort();
|
|
63
|
+
setFestival(null);
|
|
64
|
+
};
|
|
65
|
+
}, [app, base]);
|
|
66
|
+
return base ? festival : null;
|
|
67
|
+
}
|
|
68
|
+
// For apps that do not mark their theme: what the page actually looks like. The system setting alone
|
|
69
|
+
// would put dark pieces on a page that is always white.
|
|
70
|
+
function looksDark(systemDark) {
|
|
71
|
+
for (const el of [document.body, document.documentElement]) {
|
|
72
|
+
const color = getComputedStyle(el).backgroundColor;
|
|
73
|
+
const n = (color.match(/-?[\d.]+(?:e-?\d+)?/g) ?? []).map(Number);
|
|
74
|
+
if (color === "transparent" || n.length < 3 || (n.length > 3 && n[3] < 0.5))
|
|
75
|
+
continue;
|
|
76
|
+
if (/^rgb/.test(color))
|
|
77
|
+
return (0.2126 * n[0] + 0.7152 * n[1] + 0.0722 * n[2]) / 255 < 0.5;
|
|
78
|
+
if (/^ok(lab|lch)/.test(color))
|
|
79
|
+
return n[0] < 0.6;
|
|
80
|
+
if (/^(lab|lch)/.test(color))
|
|
81
|
+
return n[0] < 50;
|
|
82
|
+
}
|
|
83
|
+
// No background of its own: white, unless the page lets the browser darken it
|
|
84
|
+
const scheme = getComputedStyle(document.documentElement).colorScheme ?? "";
|
|
85
|
+
return scheme.includes("dark") && systemDark;
|
|
86
|
+
}
|
|
87
|
+
function useIsDark(theme) {
|
|
88
|
+
const [dark, setDark] = useState(theme === "dark");
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
if (theme !== "auto") {
|
|
91
|
+
setDark(theme === "dark");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const system = media("(prefers-color-scheme: dark)");
|
|
95
|
+
const update = () => setDark(pageTheme(document.documentElement) ?? looksDark(system?.matches ?? false));
|
|
96
|
+
update();
|
|
97
|
+
const observer = typeof MutationObserver === "undefined" ? null : new MutationObserver(update);
|
|
98
|
+
observer?.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "data-theme", "style"] });
|
|
99
|
+
if (document.body)
|
|
100
|
+
observer?.observe(document.body, { attributes: true, attributeFilter: ["class", "data-theme", "style"] });
|
|
101
|
+
system?.addEventListener?.("change", update);
|
|
102
|
+
return () => {
|
|
103
|
+
observer?.disconnect();
|
|
104
|
+
system?.removeEventListener?.("change", update);
|
|
105
|
+
};
|
|
106
|
+
}, [theme]);
|
|
107
|
+
return dark;
|
|
108
|
+
}
|
|
109
|
+
const read = (key) => {
|
|
110
|
+
try {
|
|
111
|
+
return localStorage.getItem(key);
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const write = (key, value) => {
|
|
118
|
+
try {
|
|
119
|
+
if (value === null)
|
|
120
|
+
localStorage.removeItem(key);
|
|
121
|
+
else
|
|
122
|
+
localStorage.setItem(key, value);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// remembered until the page reloads
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
/** A burst on arrival, then a soft light and a few stars behind the page. Wrap your app in it. */
|
|
129
|
+
export function FestiveProvider(props) {
|
|
130
|
+
const { app, apiUrl, theme = "auto", festival, layout, disabled = false, children } = props;
|
|
131
|
+
const effectsKey = `festive_effects_${app}`;
|
|
132
|
+
const [previewKind, setPreviewKind] = useState("");
|
|
133
|
+
// Nothing is known on the server: storage and the address are read once the page is running
|
|
134
|
+
const [mounted, setMounted] = useState(false);
|
|
135
|
+
const [enabled, setEnabled] = useState(true);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
setEnabled(read(effectsKey) !== "off");
|
|
138
|
+
setMounted(true);
|
|
139
|
+
}, [effectsKey]);
|
|
140
|
+
const on = mounted && enabled && !disabled;
|
|
141
|
+
const preview = useMemo(() => {
|
|
142
|
+
if (!mounted)
|
|
143
|
+
return null;
|
|
144
|
+
const kind = festival || previewKind;
|
|
145
|
+
return kind ? festivalFromId(kind, true) : previewFromAddress(window.location);
|
|
146
|
+
}, [mounted, festival, previewKind]);
|
|
147
|
+
const todays = useTodaysFestival(app, on && !preview ? baseUrl(apiUrl) : "");
|
|
148
|
+
const current = preview || todays;
|
|
149
|
+
const key = current ? dismissKey(app, current.id) : "";
|
|
150
|
+
// What was closed in this visit, and what was brought back; null leaves it to what is stored
|
|
151
|
+
const [closed, setClosed] = useState(null);
|
|
152
|
+
// A preview always shows, and closing it leaves the real day alone
|
|
153
|
+
const isDismissed = closed?.key === key ? closed.value : Boolean(current && !current.isPreview && read(key) !== null);
|
|
154
|
+
const effectsOn = enabled && !isDismissed;
|
|
155
|
+
const active = on && effectsOn ? current : null;
|
|
156
|
+
const isDark = useIsDark(theme);
|
|
157
|
+
const [celebration] = useState(() => new Celebration());
|
|
158
|
+
celebration.dark = isDark;
|
|
159
|
+
celebration.layout = layout ?? {};
|
|
160
|
+
// The entrance
|
|
161
|
+
const [settledId, setSettledId] = useState("");
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
if (!active)
|
|
164
|
+
return;
|
|
165
|
+
// After the first paint, so the top bar and sidebar have their places
|
|
166
|
+
const frame = requestAnimationFrame(() => celebration.burst(active));
|
|
167
|
+
const timer = setTimeout(() => setSettledId(active.id), prefersReducedMotion() ? 0 : CELEBRATION_MS);
|
|
168
|
+
return () => {
|
|
169
|
+
cancelAnimationFrame(frame);
|
|
170
|
+
clearTimeout(timer);
|
|
171
|
+
celebration.clear();
|
|
172
|
+
};
|
|
173
|
+
}, [active, celebration]);
|
|
174
|
+
// Lets your own CSS pick up the season
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (!active)
|
|
177
|
+
return;
|
|
178
|
+
const root = document.documentElement;
|
|
179
|
+
root.dataset.festival = active.kind;
|
|
180
|
+
root.dataset.festiveTheme = isDark ? "dark" : "light";
|
|
181
|
+
root.style.setProperty("--festive-accent", active.accent);
|
|
182
|
+
return () => {
|
|
183
|
+
delete root.dataset.festival;
|
|
184
|
+
delete root.dataset.festiveTheme;
|
|
185
|
+
root.style.removeProperty("--festive-accent");
|
|
186
|
+
};
|
|
187
|
+
}, [active, isDark]);
|
|
188
|
+
const entranceStage = active && settledId !== active.id ? "entrance" : "settled";
|
|
189
|
+
const value = useMemo(() => ({
|
|
190
|
+
activeFestival: active,
|
|
191
|
+
entranceStage,
|
|
192
|
+
isDark,
|
|
193
|
+
triggerBurst: (from) => {
|
|
194
|
+
if (active)
|
|
195
|
+
celebration.burst(active, from);
|
|
196
|
+
},
|
|
197
|
+
dismiss: () => {
|
|
198
|
+
if (!current)
|
|
199
|
+
return;
|
|
200
|
+
setClosed({ key, value: true });
|
|
201
|
+
if (!current.isPreview)
|
|
202
|
+
write(key, "1");
|
|
203
|
+
},
|
|
204
|
+
effectsOn,
|
|
205
|
+
toggleEffects: () => {
|
|
206
|
+
setEnabled(!effectsOn);
|
|
207
|
+
write(effectsKey, effectsOn ? "off" : null);
|
|
208
|
+
if (effectsOn || !current)
|
|
209
|
+
return;
|
|
210
|
+
setClosed({ key, value: false });
|
|
211
|
+
if (!current.isPreview)
|
|
212
|
+
write(key, null);
|
|
213
|
+
},
|
|
214
|
+
previewKind,
|
|
215
|
+
setPreviewKind: (kind) => {
|
|
216
|
+
setPreviewKind(kind);
|
|
217
|
+
setClosed(null);
|
|
218
|
+
if (kind)
|
|
219
|
+
setEnabled(true);
|
|
220
|
+
},
|
|
221
|
+
}), [active, current, key, entranceStage, isDark, celebration, effectsOn, effectsKey, previewKind]);
|
|
222
|
+
return (_jsx(FestiveContext.Provider, { value: value, children: _jsxs(CelebrationContext.Provider, { value: active ? celebration : null, children: [active && _jsx("style", { children: css }), active && _jsx(Backdrop, { festival: active }, active.id), children] }) }));
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* A window onto the confetti. Place it in a container that is positioned and has `isolation: isolate`:
|
|
226
|
+
* it paints above that container's background and below everything in it.
|
|
227
|
+
*/
|
|
228
|
+
export function FestiveSurface() {
|
|
229
|
+
return _jsx(Surface, { className: "hsx-behind" });
|
|
230
|
+
}
|
|
231
|
+
function Surface({ className }) {
|
|
232
|
+
const celebration = useContext(CelebrationContext);
|
|
233
|
+
const ref = useRef(null);
|
|
234
|
+
useEffect(() => (celebration && ref.current ? celebration.addSurface(ref.current) : undefined), [celebration]);
|
|
235
|
+
return celebration ? _jsx("canvas", { ref: ref, "aria-hidden": "true", className: `hsx-surface ${className}` }) : null;
|
|
236
|
+
}
|
|
237
|
+
// Light at the top of the page and, faintly, around its edges
|
|
238
|
+
function Bloom({ festival: { palette, motion, accent } }) {
|
|
239
|
+
const { entranceStage, isDark } = useFestive();
|
|
240
|
+
const opacity = entranceStage === "entrance" ? (isDark ? 0.3 : 0.4) : isDark ? 0.2 : 0.26;
|
|
241
|
+
const colors = palette.map((c, i) => `radial-gradient(ellipse 42% 100% at ${(i * 100) / (palette.length - 1)}% 0%, ${c}, transparent)`);
|
|
242
|
+
return (_jsxs("div", { className: "hsx-bloom", style: { opacity }, children: [_jsx("div", { className: `hsx-bloom-light ${motion === "clouds" ? "hsx-aurora" : ""}`, style: { background: colors.join(", ") } }), _jsx("div", { className: "hsx-fill", style: { boxShadow: `inset 0 0 110px -10px ${accent}` } })] }));
|
|
243
|
+
}
|
|
244
|
+
// A few stars that stay on in the margins. Plain CSS, so nothing is drawn by script all day.
|
|
245
|
+
const AMBIENT_STARS = [
|
|
246
|
+
{ right: "1.2%", top: "16%", size: 12 },
|
|
247
|
+
{ right: "3.6%", top: "34%", size: 9 },
|
|
248
|
+
{ right: "1.6%", top: "52%", size: 11 },
|
|
249
|
+
{ right: "4.4%", top: "71%", size: 10 },
|
|
250
|
+
{ right: "14%", bottom: "4%", size: 12 },
|
|
251
|
+
{ left: "44%", bottom: "3%", size: 9 },
|
|
252
|
+
{ left: "24%", bottom: "5%", size: 11 },
|
|
253
|
+
];
|
|
254
|
+
function Backdrop({ festival }) {
|
|
255
|
+
const { entranceStage } = useFestive();
|
|
256
|
+
return (_jsxs("div", { "aria-hidden": "true", "data-festive": "backdrop", className: "hsx-backdrop", children: [_jsx(Bloom, { festival: festival }), entranceStage === "settled" &&
|
|
257
|
+
AMBIENT_STARS.map(({ size, ...place }, i) => (_jsx("span", { className: "hsx-star", style: {
|
|
258
|
+
...place,
|
|
259
|
+
width: size,
|
|
260
|
+
height: size,
|
|
261
|
+
background: festival.confetti[i % festival.confetti.length],
|
|
262
|
+
animationDelay: `${i * -0.9}s`,
|
|
263
|
+
} }, i))), _jsx(Surface, { className: "" })] }));
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Continues the light and confetti behind a sticky strip around the header, which would otherwise cut
|
|
267
|
+
* them off in a straight line. Place it first in that strip.
|
|
268
|
+
*/
|
|
269
|
+
export function FestiveHeaderBackdrop() {
|
|
270
|
+
const { activeFestival } = useFestive();
|
|
271
|
+
return activeFestival ? (_jsxs("div", { "aria-hidden": "true", className: "hsx-header-backdrop", children: [_jsx(Bloom, { festival: activeFestival }, activeFestival.id), _jsx(Surface, { className: "" })] })) : null;
|
|
272
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ActiveFestival } from './festivals.js';
|
|
2
|
+
export declare const CELEBRATION_MS = 3500;
|
|
3
|
+
/** CSS selectors for the parts of the page the confetti is aimed around. */
|
|
4
|
+
export interface Layout {
|
|
5
|
+
header?: string;
|
|
6
|
+
sidebar?: string;
|
|
7
|
+
content?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Point {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const prefersReducedMotion: () => boolean;
|
|
14
|
+
export declare class Celebration {
|
|
15
|
+
dark: boolean;
|
|
16
|
+
layout: Layout;
|
|
17
|
+
private pieces;
|
|
18
|
+
private surfaces;
|
|
19
|
+
private painted;
|
|
20
|
+
private frame;
|
|
21
|
+
private last;
|
|
22
|
+
private lastBurst;
|
|
23
|
+
addSurface(canvas: HTMLCanvasElement): () => void;
|
|
24
|
+
burst(festival: ActiveFestival, from?: Point): void;
|
|
25
|
+
clear(): void;
|
|
26
|
+
private tick;
|
|
27
|
+
private paint;
|
|
28
|
+
}
|