@handysolver/festive 0.0.0-stage → 0.1.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 +149 -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/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/styles.d.ts +1 -0
- package/dist/styles.js +29 -0
- package/package.json +43 -4
package/README.md
CHANGED
|
@@ -1,3 +1,150 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @handysolver/festive
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One React component that adds festival touches to any app: a short falling-emoji
|
|
4
|
+
animation, a dismissible greeting card and an accent colour. Which festival is active
|
|
5
|
+
comes from the Festival API. If the API is down, slow or wrong, nothing shows and your
|
|
6
|
+
app is unaffected.
|
|
7
|
+
|
|
8
|
+
React 18+, TypeScript types included, ESM only, no runtime dependencies, no CSS to import.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @handysolver/festive
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Vite
|
|
17
|
+
|
|
18
|
+
```tsx
|
|
19
|
+
// src/main.tsx
|
|
20
|
+
import { FestiveLayer } from "@handysolver/festive";
|
|
21
|
+
|
|
22
|
+
createRoot(document.getElementById("root")!).render(
|
|
23
|
+
<StrictMode>
|
|
24
|
+
<App />
|
|
25
|
+
<FestiveLayer app="meetmind" apiUrl={import.meta.env.VITE_FESTIVAL_API_URL} />
|
|
26
|
+
</StrictMode>,
|
|
27
|
+
);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# .env
|
|
32
|
+
VITE_FESTIVAL_API_URL=https://your-festival-api.vercel.app
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Vite 8 may warn that the `"use client"` directive "may not be preserved" during a build. It is
|
|
36
|
+
harmless: the directive only matters to Next.js.
|
|
37
|
+
|
|
38
|
+
## Next.js (App Router)
|
|
39
|
+
|
|
40
|
+
The package is already marked `"use client"`, so use it straight from a server layout:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
// app/layout.tsx
|
|
44
|
+
import { FestiveLayer } from "@handysolver/festive";
|
|
45
|
+
|
|
46
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
47
|
+
return (
|
|
48
|
+
<html lang="en" suppressHydrationWarning>
|
|
49
|
+
<body>
|
|
50
|
+
{children}
|
|
51
|
+
<FestiveLayer app="my-next-app" apiUrl={process.env.NEXT_PUBLIC_FESTIVAL_API_URL} />
|
|
52
|
+
</body>
|
|
53
|
+
</html>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Pages Router: put it in `pages/_app.tsx` next to `<Component />`.
|
|
59
|
+
|
|
60
|
+
Mount it once, near the root. An ancestor with a CSS `transform` or `filter` would trap
|
|
61
|
+
its fixed positioning.
|
|
62
|
+
|
|
63
|
+
## Props
|
|
64
|
+
|
|
65
|
+
| Prop | Type | Default | Notes |
|
|
66
|
+
| ---------- | ----------------------------- | ---------- | ----- |
|
|
67
|
+
| `app` | `string` | required | Your app id, as used in the API's `apps` lists |
|
|
68
|
+
| `apiUrl` | `string \| null \| undefined` | – | API origin. Missing or empty renders nothing |
|
|
69
|
+
| `theme` | `"auto" \| "light" \| "dark"` | `"auto"` | See below |
|
|
70
|
+
| `position` | `"top" \| "bottom"` | `"bottom"` | Where the card sits |
|
|
71
|
+
| `disabled` | `boolean` | `false` | Turns everything off |
|
|
72
|
+
|
|
73
|
+
`theme="auto"` reads, in order: `data-theme="dark|light"` on `<html>`, a `.dark` / `.light`
|
|
74
|
+
class on `<html>`, a fixed CSS `color-scheme` on `<html>`, then the system preference. It
|
|
75
|
+
reacts to changes. If your app marks only dark mode (a `.dark` class and nothing else) and
|
|
76
|
+
starts in light mode on a system set to dark, there is nothing to detect: pass `theme` yourself
|
|
77
|
+
or set `color-scheme: light`.
|
|
78
|
+
|
|
79
|
+
## Using the accent in your own CSS
|
|
80
|
+
|
|
81
|
+
While a festival is active, `<html>` gets `data-festival="<id>"` and `--festive-accent`:
|
|
82
|
+
|
|
83
|
+
```css
|
|
84
|
+
.logo { color: var(--festive-accent, #2563eb); }
|
|
85
|
+
[data-festival^="diwali"] .hero { /* ... */ }
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Accents are not checked for contrast; avoid them for body text.
|
|
89
|
+
|
|
90
|
+
## Behaviour
|
|
91
|
+
|
|
92
|
+
- Emoji fall for about 9 seconds, once per tab session per festival, never blocking clicks.
|
|
93
|
+
Skipped for users who prefer reduced motion.
|
|
94
|
+
- Dismissing the card is remembered per festival in `localStorage`.
|
|
95
|
+
- If storage is blocked, both are remembered only until the page reloads.
|
|
96
|
+
- A tab left open rechecks when the user's local date changes.
|
|
97
|
+
|
|
98
|
+
With a Content-Security-Policy, allow the API origin in `connect-src` and inline styles in
|
|
99
|
+
`style-src`.
|
|
100
|
+
|
|
101
|
+
## Preview a festival
|
|
102
|
+
|
|
103
|
+
Open your app with `?festival=<id>`, e.g. `https://your-app.com/?festival=holi-2027`.
|
|
104
|
+
Preview ignores dates and always shows the card and the animation, and it records nothing.
|
|
105
|
+
|
|
106
|
+
## Develop
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
npm install
|
|
110
|
+
npm run check # typecheck, build, tests
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Publish
|
|
114
|
+
|
|
115
|
+
Publishing is public and a version number can never be reused.
|
|
116
|
+
|
|
117
|
+
1. Make sure you own the npm scope. `@handysolver` must be your npm username or an
|
|
118
|
+
organisation you belong to (create one at npmjs.com/org/create). Otherwise, rename first.
|
|
119
|
+
2. Log in, check the contents, publish:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm login
|
|
123
|
+
npm pack --dry-run
|
|
124
|
+
npm publish --access public
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`npm publish` runs `npm run check` first and stops if anything fails.
|
|
128
|
+
|
|
129
|
+
### Rename
|
|
130
|
+
|
|
131
|
+
The name is set only in `package.json` (and copied to the lockfile):
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
npm pkg set name=@your-scope/festive
|
|
135
|
+
npm install --package-lock-only
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Then replace `@handysolver/festive` in this README.
|
|
139
|
+
|
|
140
|
+
### Update
|
|
141
|
+
|
|
142
|
+
Commit your changes, then:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
npm version patch # or minor / major
|
|
146
|
+
npm publish --access public
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
In each app: `npm install @handysolver/festive@latest`. New festivals need no release;
|
|
150
|
+
add them in the Festival API.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
export interface FestiveLayerProps {
|
|
3
|
+
/** Your app id, as used in the Festival API's `apps` lists. */
|
|
4
|
+
app: string;
|
|
5
|
+
/** Origin of the Festival API. Nothing renders while this is missing. */
|
|
6
|
+
apiUrl?: string | null;
|
|
7
|
+
/** "auto" (default) follows the page. */
|
|
8
|
+
theme?: "auto" | "light" | "dark";
|
|
9
|
+
/** Where the greeting card sits. Default "bottom". */
|
|
10
|
+
position?: "top" | "bottom";
|
|
11
|
+
/** Turns everything off and cleans up. */
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function FestiveLayer(props: FestiveLayerProps): ReactElement | null;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { baseUrl, hasFlag, localDate, media, pageTheme, parseFestival, previewId, setFlag } from "./core.js";
|
|
5
|
+
import { css } from "./styles.js";
|
|
6
|
+
const FALL_MS = 9000;
|
|
7
|
+
const FETCH_TIMEOUT_MS = 15000;
|
|
8
|
+
const DATE_CHECK_MS = 60000;
|
|
9
|
+
const REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
|
|
10
|
+
export function FestiveLayer(props) {
|
|
11
|
+
const { app, apiUrl, theme = "auto", position = "bottom", disabled = false } = props;
|
|
12
|
+
const active = useFestival(app, disabled ? "" : baseUrl(apiUrl));
|
|
13
|
+
const dark = useDark(theme);
|
|
14
|
+
useAccent(active?.festival);
|
|
15
|
+
const pieces = useFalling(app, active);
|
|
16
|
+
const [card, dismiss] = useCard(app, active);
|
|
17
|
+
if (!active || (pieces.length === 0 && card === "hidden"))
|
|
18
|
+
return null;
|
|
19
|
+
const { festival } = active;
|
|
20
|
+
const text = festival.banner ?? `Happy ${festival.name}!`;
|
|
21
|
+
// Banners often carry their own emoji; do not show a second one next to it.
|
|
22
|
+
const icon = festival.emoji.some((e) => text.includes(e)) ? undefined : festival.emoji[0];
|
|
23
|
+
return (_jsxs(_Fragment, { children: [_jsx("style", { children: css }), pieces.length > 0 && (_jsx("div", { className: "hsf-fall", "aria-hidden": "true", style: { pointerEvents: "none" }, children: pieces.map((p, i) => (_jsx("span", { className: "hsf-piece", style: p.style, children: p.emoji }, i))) })), card !== "hidden" && (_jsxs("div", { className: "hsf-card", "data-theme": dark ? "dark" : "light", "data-position": position === "top" ? "top" : "bottom", style: { "--hsf-accent": festival.accent }, children: [icon && (_jsx("span", { className: "hsf-icon", "aria-hidden": "true", children: icon })), _jsx("p", { className: "hsf-text", role: "status", children: card === "open" ? text : "" }), _jsx("button", { type: "button", className: "hsf-close", "aria-label": `Dismiss ${festival.name} greeting`, onClick: dismiss, children: _jsx("span", { "aria-hidden": "true", children: "\u00D7" }) })] }))] }));
|
|
24
|
+
}
|
|
25
|
+
/** Fetches the festival, and again whenever the local date changes or a failed attempt can be retried. */
|
|
26
|
+
function useFestival(app, base) {
|
|
27
|
+
const [active, setActive] = useState(null);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!base)
|
|
30
|
+
return;
|
|
31
|
+
let current;
|
|
32
|
+
let timeout;
|
|
33
|
+
let loadedFor = "";
|
|
34
|
+
let failed = false;
|
|
35
|
+
const load = async () => {
|
|
36
|
+
let mine;
|
|
37
|
+
try {
|
|
38
|
+
loadedFor = localDate();
|
|
39
|
+
failed = false;
|
|
40
|
+
const previous = current;
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
mine = current = controller;
|
|
43
|
+
previous?.abort();
|
|
44
|
+
clearTimeout(timeout);
|
|
45
|
+
timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
46
|
+
const preview = previewId();
|
|
47
|
+
const query = new URLSearchParams({ app, date: loadedFor });
|
|
48
|
+
if (preview)
|
|
49
|
+
query.set("preview", preview);
|
|
50
|
+
const res = await fetch(`${base}/api/festival?${query}`, { signal: controller.signal, credentials: "omit" });
|
|
51
|
+
if (!res.ok)
|
|
52
|
+
throw new Error(`HTTP ${res.status}`);
|
|
53
|
+
const festival = parseFestival(await res.json());
|
|
54
|
+
if (current !== mine)
|
|
55
|
+
return;
|
|
56
|
+
clearTimeout(timeout);
|
|
57
|
+
// The API ignores unknown preview ids, so only an exact match counts as a preview.
|
|
58
|
+
setActive(festival && { festival, preview: festival.id === preview });
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
if (current !== mine)
|
|
62
|
+
return; // superseded or unmounted
|
|
63
|
+
clearTimeout(timeout);
|
|
64
|
+
failed = true;
|
|
65
|
+
setActive(null);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const recheck = () => {
|
|
69
|
+
if (localDate() !== loadedFor)
|
|
70
|
+
void load();
|
|
71
|
+
};
|
|
72
|
+
const wake = () => {
|
|
73
|
+
if (document.visibilityState === "hidden")
|
|
74
|
+
return;
|
|
75
|
+
if (failed)
|
|
76
|
+
void load();
|
|
77
|
+
else
|
|
78
|
+
recheck();
|
|
79
|
+
};
|
|
80
|
+
void load();
|
|
81
|
+
const interval = setInterval(recheck, DATE_CHECK_MS);
|
|
82
|
+
document.addEventListener("visibilitychange", wake);
|
|
83
|
+
window.addEventListener("online", wake);
|
|
84
|
+
return () => {
|
|
85
|
+
const last = current;
|
|
86
|
+
current = undefined;
|
|
87
|
+
last?.abort();
|
|
88
|
+
clearTimeout(timeout);
|
|
89
|
+
clearInterval(interval);
|
|
90
|
+
document.removeEventListener("visibilitychange", wake);
|
|
91
|
+
window.removeEventListener("online", wake);
|
|
92
|
+
setActive(null);
|
|
93
|
+
};
|
|
94
|
+
}, [app, base]);
|
|
95
|
+
return active;
|
|
96
|
+
}
|
|
97
|
+
/** Exposes the festival to the host app's CSS as html[data-festival] and --festive-accent. */
|
|
98
|
+
function useAccent(festival) {
|
|
99
|
+
const id = festival?.id;
|
|
100
|
+
const accent = festival?.accent;
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (!id || !accent)
|
|
103
|
+
return;
|
|
104
|
+
const html = document.documentElement;
|
|
105
|
+
html.setAttribute("data-festival", id);
|
|
106
|
+
html.style.setProperty("--festive-accent", accent);
|
|
107
|
+
return () => {
|
|
108
|
+
html.removeAttribute("data-festival");
|
|
109
|
+
html.style.removeProperty("--festive-accent");
|
|
110
|
+
};
|
|
111
|
+
}, [id, accent]);
|
|
112
|
+
}
|
|
113
|
+
function useFalling(app, active) {
|
|
114
|
+
const [pieces, setPieces] = useState([]);
|
|
115
|
+
const id = active?.festival.id;
|
|
116
|
+
const preview = active?.preview ?? false;
|
|
117
|
+
const emoji = active?.festival.emoji.join("\n") ?? "";
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
if (!id || !emoji || media(REDUCED_MOTION)?.matches)
|
|
120
|
+
return;
|
|
121
|
+
const key = `festive:played:${app}:${id}`;
|
|
122
|
+
if (!preview && hasFlag("sessionStorage", key))
|
|
123
|
+
return;
|
|
124
|
+
let timer;
|
|
125
|
+
const start = () => {
|
|
126
|
+
if (document.visibilityState === "hidden")
|
|
127
|
+
return; // wait until someone can see it
|
|
128
|
+
document.removeEventListener("visibilitychange", schedule);
|
|
129
|
+
if (!preview)
|
|
130
|
+
setFlag("sessionStorage", key);
|
|
131
|
+
setPieces(makePieces(emoji.split("\n"), window.innerWidth < 640 ? 16 : 28));
|
|
132
|
+
timer = setTimeout(() => setPieces([]), FALL_MS);
|
|
133
|
+
};
|
|
134
|
+
// Deferred, so StrictMode's throwaway first effect is cancelled before it records anything.
|
|
135
|
+
const schedule = () => {
|
|
136
|
+
clearTimeout(timer);
|
|
137
|
+
timer = setTimeout(start, 0);
|
|
138
|
+
};
|
|
139
|
+
document.addEventListener("visibilitychange", schedule);
|
|
140
|
+
schedule();
|
|
141
|
+
return () => {
|
|
142
|
+
clearTimeout(timer);
|
|
143
|
+
document.removeEventListener("visibilitychange", schedule);
|
|
144
|
+
setPieces([]);
|
|
145
|
+
};
|
|
146
|
+
}, [app, id, preview, emoji]);
|
|
147
|
+
return pieces;
|
|
148
|
+
}
|
|
149
|
+
function makePieces(emoji, count) {
|
|
150
|
+
const rand = (min, max) => min + Math.random() * (max - min);
|
|
151
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
152
|
+
emoji: emoji[i % emoji.length],
|
|
153
|
+
style: {
|
|
154
|
+
left: `${((i + Math.random()) / count) * 96}%`,
|
|
155
|
+
fontSize: `${rand(18, 32).toFixed(0)}px`,
|
|
156
|
+
// delay + duration stays under FALL_MS
|
|
157
|
+
animationDelay: `${rand(0, 3.8).toFixed(2)}s`,
|
|
158
|
+
animationDuration: `${rand(3.8, 5).toFixed(2)}s`,
|
|
159
|
+
"--hsf-drift": `${rand(-60, 60).toFixed(0)}px`,
|
|
160
|
+
"--hsf-spin": `${rand(-240, 240).toFixed(0)}deg`,
|
|
161
|
+
},
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
function useCard(app, active) {
|
|
165
|
+
const [card, setCard] = useState("hidden");
|
|
166
|
+
const id = active?.festival.id;
|
|
167
|
+
const preview = active?.preview ?? false;
|
|
168
|
+
const key = `festive:dismissed:${app}:${id}`;
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (!id || (!preview && hasFlag("localStorage", key)))
|
|
171
|
+
return;
|
|
172
|
+
setCard("pending");
|
|
173
|
+
return () => setCard("hidden");
|
|
174
|
+
}, [id, preview, key]);
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (card === "pending")
|
|
177
|
+
setCard("open");
|
|
178
|
+
}, [card]);
|
|
179
|
+
const dismiss = () => {
|
|
180
|
+
if (!preview)
|
|
181
|
+
setFlag("localStorage", key);
|
|
182
|
+
setCard("hidden");
|
|
183
|
+
};
|
|
184
|
+
return [card, dismiss];
|
|
185
|
+
}
|
|
186
|
+
function useDark(theme) {
|
|
187
|
+
const [dark, setDark] = useState(false);
|
|
188
|
+
useEffect(() => {
|
|
189
|
+
if (theme === "dark" || theme === "light") {
|
|
190
|
+
setDark(theme === "dark");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const html = document.documentElement;
|
|
194
|
+
const system = media("(prefers-color-scheme: dark)");
|
|
195
|
+
// Apps that toggle only `.dark` leave no mark in light mode. Once the page has
|
|
196
|
+
// been seen dark, a missing signal means light, not "ask the system".
|
|
197
|
+
let seenDark = false;
|
|
198
|
+
const update = () => {
|
|
199
|
+
const page = pageTheme(html);
|
|
200
|
+
if (page)
|
|
201
|
+
seenDark = true;
|
|
202
|
+
setDark(page ?? (seenDark ? false : (system?.matches ?? false)));
|
|
203
|
+
};
|
|
204
|
+
update();
|
|
205
|
+
const observer = typeof MutationObserver === "undefined" ? null : new MutationObserver(update);
|
|
206
|
+
observer?.observe(html, { attributes: true, attributeFilter: ["class", "data-theme", "style"] });
|
|
207
|
+
system?.addEventListener?.("change", update);
|
|
208
|
+
return () => {
|
|
209
|
+
observer?.disconnect();
|
|
210
|
+
system?.removeEventListener?.("change", update);
|
|
211
|
+
};
|
|
212
|
+
}, [theme]);
|
|
213
|
+
return dark;
|
|
214
|
+
}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Framework-free helpers. Nothing here runs at import time, so it is safe on the server. */
|
|
2
|
+
export interface Festival {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
emoji: string[];
|
|
6
|
+
accent: string;
|
|
7
|
+
banner?: string;
|
|
8
|
+
}
|
|
9
|
+
/** Returns the festival from an API response body, or null for "none" and for anything malformed. */
|
|
10
|
+
export declare function parseFestival(body: unknown): Festival | null;
|
|
11
|
+
/** The user's local calendar date as YYYY-MM-DD. */
|
|
12
|
+
export declare function localDate(d?: Date): string;
|
|
13
|
+
/** `?festival=<id>` on the current page. */
|
|
14
|
+
export declare function previewId(): string;
|
|
15
|
+
export declare function baseUrl(apiUrl: unknown): string;
|
|
16
|
+
type StorageName = "localStorage" | "sessionStorage";
|
|
17
|
+
export declare function hasFlag(storage: StorageName, key: string): boolean;
|
|
18
|
+
export declare function setFlag(storage: StorageName, key: string): void;
|
|
19
|
+
export declare function media(query: string): MediaQueryList | null;
|
|
20
|
+
/**
|
|
21
|
+
* What the host page says about its theme: true = dark, false = light, null = no signal.
|
|
22
|
+
* Signals on <html>, strongest first: data-theme, .dark/.light class, a single-valued color-scheme.
|
|
23
|
+
*/
|
|
24
|
+
export declare function pageTheme(html: HTMLElement): boolean | null;
|
|
25
|
+
export {};
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** Framework-free helpers. Nothing here runs at import time, so it is safe on the server. */
|
|
2
|
+
const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
3
|
+
const isText = (v) => typeof v === "string" && v.trim() !== "";
|
|
4
|
+
/** Returns the festival from an API response body, or null for "none" and for anything malformed. */
|
|
5
|
+
export function parseFestival(body) {
|
|
6
|
+
const f = body?.festival;
|
|
7
|
+
if (!f || typeof f !== "object")
|
|
8
|
+
return null;
|
|
9
|
+
if (!isText(f.id) || !isText(f.name) || !isText(f.start) || !isText(f.end))
|
|
10
|
+
return null;
|
|
11
|
+
if (!isText(f.accent) || !HEX.test(f.accent))
|
|
12
|
+
return null;
|
|
13
|
+
if (!Array.isArray(f.emoji) || !Array.isArray(f.apps))
|
|
14
|
+
return null;
|
|
15
|
+
return {
|
|
16
|
+
id: f.id,
|
|
17
|
+
name: f.name,
|
|
18
|
+
accent: f.accent,
|
|
19
|
+
// A family emoji is 11 UTF-16 units, so 16 fits real emoji but not stray sentences.
|
|
20
|
+
emoji: f.emoji.filter((e) => isText(e) && e.length <= 16).slice(0, 12),
|
|
21
|
+
banner: isText(f.banner) ? f.banner : undefined,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** The user's local calendar date as YYYY-MM-DD. */
|
|
25
|
+
export function localDate(d = new Date()) {
|
|
26
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
27
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
28
|
+
}
|
|
29
|
+
/** `?festival=<id>` on the current page. */
|
|
30
|
+
export function previewId() {
|
|
31
|
+
return new URLSearchParams(window.location.search).get("festival")?.trim() ?? "";
|
|
32
|
+
}
|
|
33
|
+
export function baseUrl(apiUrl) {
|
|
34
|
+
return typeof apiUrl === "string" ? apiUrl.trim().replace(/\/+$/, "") : "";
|
|
35
|
+
}
|
|
36
|
+
// Storage can throw on access, on read or on write (private mode, blocked cookies, full quota).
|
|
37
|
+
// When it does, remember for the life of the page instead.
|
|
38
|
+
const memory = new Set();
|
|
39
|
+
export function hasFlag(storage, key) {
|
|
40
|
+
if (memory.has(key))
|
|
41
|
+
return true;
|
|
42
|
+
try {
|
|
43
|
+
return window[storage].getItem(key) !== null;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function setFlag(storage, key) {
|
|
50
|
+
try {
|
|
51
|
+
window[storage].setItem(key, "1");
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
memory.add(key);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export function media(query) {
|
|
58
|
+
try {
|
|
59
|
+
return window.matchMedia(query);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* What the host page says about its theme: true = dark, false = light, null = no signal.
|
|
67
|
+
* Signals on <html>, strongest first: data-theme, .dark/.light class, a single-valued color-scheme.
|
|
68
|
+
*/
|
|
69
|
+
export function pageTheme(html) {
|
|
70
|
+
const attr = html.getAttribute("data-theme")?.toLowerCase();
|
|
71
|
+
if (attr === "dark" || attr === "light")
|
|
72
|
+
return attr === "dark";
|
|
73
|
+
if (html.classList.contains("dark"))
|
|
74
|
+
return true;
|
|
75
|
+
if (html.classList.contains("light"))
|
|
76
|
+
return false;
|
|
77
|
+
const scheme = getComputedStyle(html).getPropertyValue("color-scheme").replace("only", "").trim();
|
|
78
|
+
return scheme === "dark" ? true : scheme === "light" ? false : null;
|
|
79
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const css = "\n.hsf-fall{position:fixed;inset:0;overflow:hidden;pointer-events:none;z-index:2147483000;contain:strict}\n.hsf-piece{position:absolute;top:0;line-height:1;user-select:none;opacity:0;will-change:transform;animation:hsf-fall linear both}\n@keyframes hsf-fall{\n0%{transform:translate3d(0,-12vh,0) rotate(0);opacity:0}\n8%{opacity:.95}\n85%{opacity:.95}\n100%{transform:translate3d(var(--hsf-drift),108vh,0) rotate(var(--hsf-spin));opacity:0}}\n.hsf-card{--hsf-bg:#fff;--hsf-fg:#1f2328;--hsf-line:rgba(0,0,0,.12);--hsf-hover:rgba(0,0,0,.07);\nposition:fixed;left:0;right:0;margin:0 auto;box-sizing:border-box;width:fit-content;\nmax-width:min(420px,calc(100vw - 24px - env(safe-area-inset-left,0px) - env(safe-area-inset-right,0px)));\nz-index:2147483001;display:flex;align-items:center;gap:10px;padding:8px 8px 8px 14px;\nbackground:var(--hsf-bg);color:var(--hsf-fg);border:1px solid var(--hsf-line);border-left:4px solid var(--hsf-accent);\nborder-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,.18);\nfont:500 14px/1.4 system-ui,-apple-system,\"Segoe UI\",Roboto,sans-serif;text-align:left;\nanimation:hsf-in .35s ease-out}\n.hsf-card[data-position=bottom]{bottom:calc(16px + env(safe-area-inset-bottom,0px))}\n.hsf-card[data-position=top]{top:calc(16px + env(safe-area-inset-top,0px))}\n.hsf-card[data-theme=dark]{--hsf-bg:#1f2328;--hsf-fg:#f3f4f6;--hsf-line:rgba(255,255,255,.16);--hsf-hover:rgba(255,255,255,.12)}\n.hsf-icon{flex:none;font-size:20px;line-height:1}\n.hsf-text{flex:1;min-width:0;margin:0;overflow-wrap:anywhere;font:inherit;color:inherit}\n.hsf-close{flex:none;box-sizing:border-box;width:36px;height:36px;margin:0;padding:0;border:0;border-radius:8px;\nbackground:transparent;color:inherit;font:400 20px/1 system-ui,sans-serif;cursor:pointer}\n.hsf-close:hover{background:var(--hsf-hover)}\n.hsf-close:focus-visible{outline:2px solid currentColor;outline-offset:-2px}\n@keyframes hsf-in{from{opacity:0;transform:translateY(8px)}}\n@media (prefers-reduced-motion:reduce){.hsf-fall{display:none}.hsf-card{animation:none}}\n@media print{.hsf-fall,.hsf-card{display:none}}\n";
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const css = `
|
|
2
|
+
.hsf-fall{position:fixed;inset:0;overflow:hidden;pointer-events:none;z-index:2147483000;contain:strict}
|
|
3
|
+
.hsf-piece{position:absolute;top:0;line-height:1;user-select:none;opacity:0;will-change:transform;animation:hsf-fall linear both}
|
|
4
|
+
@keyframes hsf-fall{
|
|
5
|
+
0%{transform:translate3d(0,-12vh,0) rotate(0);opacity:0}
|
|
6
|
+
8%{opacity:.95}
|
|
7
|
+
85%{opacity:.95}
|
|
8
|
+
100%{transform:translate3d(var(--hsf-drift),108vh,0) rotate(var(--hsf-spin));opacity:0}}
|
|
9
|
+
.hsf-card{--hsf-bg:#fff;--hsf-fg:#1f2328;--hsf-line:rgba(0,0,0,.12);--hsf-hover:rgba(0,0,0,.07);
|
|
10
|
+
position:fixed;left:0;right:0;margin:0 auto;box-sizing:border-box;width:fit-content;
|
|
11
|
+
max-width:min(420px,calc(100vw - 24px - env(safe-area-inset-left,0px) - env(safe-area-inset-right,0px)));
|
|
12
|
+
z-index:2147483001;display:flex;align-items:center;gap:10px;padding:8px 8px 8px 14px;
|
|
13
|
+
background:var(--hsf-bg);color:var(--hsf-fg);border:1px solid var(--hsf-line);border-left:4px solid var(--hsf-accent);
|
|
14
|
+
border-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,.18);
|
|
15
|
+
font:500 14px/1.4 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;text-align:left;
|
|
16
|
+
animation:hsf-in .35s ease-out}
|
|
17
|
+
.hsf-card[data-position=bottom]{bottom:calc(16px + env(safe-area-inset-bottom,0px))}
|
|
18
|
+
.hsf-card[data-position=top]{top:calc(16px + env(safe-area-inset-top,0px))}
|
|
19
|
+
.hsf-card[data-theme=dark]{--hsf-bg:#1f2328;--hsf-fg:#f3f4f6;--hsf-line:rgba(255,255,255,.16);--hsf-hover:rgba(255,255,255,.12)}
|
|
20
|
+
.hsf-icon{flex:none;font-size:20px;line-height:1}
|
|
21
|
+
.hsf-text{flex:1;min-width:0;margin:0;overflow-wrap:anywhere;font:inherit;color:inherit}
|
|
22
|
+
.hsf-close{flex:none;box-sizing:border-box;width:36px;height:36px;margin:0;padding:0;border:0;border-radius:8px;
|
|
23
|
+
background:transparent;color:inherit;font:400 20px/1 system-ui,sans-serif;cursor:pointer}
|
|
24
|
+
.hsf-close:hover{background:var(--hsf-hover)}
|
|
25
|
+
.hsf-close:focus-visible{outline:2px solid currentColor;outline-offset:-2px}
|
|
26
|
+
@keyframes hsf-in{from{opacity:0;transform:translateY(8px)}}
|
|
27
|
+
@media (prefers-reduced-motion:reduce){.hsf-fall{display:none}.hsf-card{animation:none}}
|
|
28
|
+
@media print{.hsf-fall,.hsf-card{display:none}}
|
|
29
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@handysolver/festive",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Drop-in React festival layer: falling emoji, greeting card and accent colour.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"check": "npm run typecheck && npm run build && npm test",
|
|
28
|
+
"prepublishOnly": "npm run check"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": ">=18"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@testing-library/dom": "^10.4.2",
|
|
35
|
+
"@testing-library/react": "^16.3.3",
|
|
36
|
+
"@types/node": "^26.6.11",
|
|
37
|
+
"@types/react": "^19.3.3",
|
|
38
|
+
"@types/react-dom": "^19.3.0",
|
|
39
|
+
"jsdom": "^29.1.1",
|
|
40
|
+
"react": "^19.3.1",
|
|
41
|
+
"react-dom": "^19.3.1",
|
|
42
|
+
"typescript": "5.9",
|
|
43
|
+
"vitest": "^5.1.2"
|
|
44
|
+
}
|
|
45
|
+
}
|