@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
package/README.md
CHANGED
|
@@ -1,3 +1,213 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @handysolver/festive
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Festival celebrations for any React or Next.js app. Which festival is on comes from the
|
|
4
|
+
[Festival API](https://github.com/ashwik-handysolver/festival-api). If it cannot be reached, nothing
|
|
5
|
+
shows and your app is unaffected.
|
|
6
|
+
|
|
7
|
+
React 18+, TypeScript types included, ESM only, no runtime dependencies, no CSS to import, no Tailwind needed.
|
|
8
|
+
|
|
9
|
+
| | Full design (new in 0.2) | Simple layer |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| Use | `<FestiveProvider>` and its parts | `<FestiveLayer>` |
|
|
12
|
+
| Looks like | Confetti, garland, badge with a party popper, glow and stars | Falling emoji and a greeting card |
|
|
13
|
+
| Setup | About 5 lines | 1 line |
|
|
14
|
+
|
|
15
|
+
Use one or the other in an app, not both.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @handysolver/festive
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
# Full design
|
|
22
|
+
|
|
23
|
+
Designed for Diwali, Dussehra, Christmas, New Year, Republic Day, Independence Day and Holi.
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { FestiveProvider, FestiveSurface, FestiveBadge, FestiveGarland } from "@handysolver/festive";
|
|
27
|
+
|
|
28
|
+
<FestiveProvider app="my-app" apiUrl="https://festival-api-coral.vercel.app">
|
|
29
|
+
<header style={{ position: "relative", isolation: "isolate" }}>
|
|
30
|
+
<FestiveSurface /> {/* confetti behind the header's content */}
|
|
31
|
+
...your header...
|
|
32
|
+
<FestiveBadge /> {/* greeting, Celebrate and close */}
|
|
33
|
+
<FestiveGarland /> {/* hangs from the header's lower edge */}
|
|
34
|
+
</header>
|
|
35
|
+
<main style={{ position: "relative" }}>...</main>
|
|
36
|
+
</FestiveProvider>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
With only `<FestiveProvider>` around the app you get the opening burst, the glow and the stars.
|
|
40
|
+
In Next.js, put it in `app/layout.tsx` (around `{children}`) or `pages/_app.tsx`.
|
|
41
|
+
|
|
42
|
+
**What your layout needs**
|
|
43
|
+
|
|
44
|
+
- **Content:** `position: relative` on the main content, and solid backgrounds on cards and tables, so
|
|
45
|
+
the celebration stays behind them. Don't add a `z-index`: it can trap pop-ups under the header.
|
|
46
|
+
- **Background:** on `<html>` or `<body>`, or on an element that is not positioned. A positioned element
|
|
47
|
+
with a background covers the glow and confetti.
|
|
48
|
+
- **Header:** positioned, with `isolation: isolate` and no `overflow: hidden`, which would cut the garland off.
|
|
49
|
+
- **No `transform` or `filter`** on elements around the provider.
|
|
50
|
+
|
|
51
|
+
**Optional parts**
|
|
52
|
+
|
|
53
|
+
- **Sidebar:** put `<FestiveSurface />` in an empty stretch of it, inside
|
|
54
|
+
`<div className="festive-fade" style={{ position: "relative", isolation: "isolate", flex: 1 }}>`.
|
|
55
|
+
- **Sticky strip around the header** with its own background: make it a positioned stacking context and
|
|
56
|
+
put `<FestiveHeaderBackdrop />` first in it.
|
|
57
|
+
- **Cards:** `className="festive-card"` gives a festival border and glow on hover in dark mode.
|
|
58
|
+
- **On/off switch or preview picker:** `useFestive()` returns `effectsOn`, `toggleEffects`,
|
|
59
|
+
`previewKind`, `setPreviewKind`, `triggerBurst`, `dismiss` and `activeFestival`.
|
|
60
|
+
- **Your own CSS:** `<html>` has `data-festival="diwali"`, `data-festive-theme="light|dark"` and
|
|
61
|
+
`--festive-accent` while a festival is on.
|
|
62
|
+
|
|
63
|
+
**Provider props**
|
|
64
|
+
|
|
65
|
+
| Prop | Default | |
|
|
66
|
+
| --- | --- | --- |
|
|
67
|
+
| `app` | required | Your app's name, different for each app |
|
|
68
|
+
| `apiUrl` | - | Festival API address. Without it only previews show |
|
|
69
|
+
| `theme` | `"auto"` | `"light"`, `"dark"`, or follow the page: `data-theme`, a `.dark`/`.light` class, a fixed `color-scheme`, then the background of `<body>` or `<html>` |
|
|
70
|
+
| `layout` | `header`, `aside`, `main` | CSS selectors the confetti is aimed around: `{ header, sidebar, content }`. Missing ones are skipped |
|
|
71
|
+
| `festival` | - | Show one whatever the date, for testing, such as `"holi"` |
|
|
72
|
+
| `disabled` | `false` | Turns everything off |
|
|
73
|
+
|
|
74
|
+
**Preview:** add `?festival=diwali` to the address, before or after a `#`.
|
|
75
|
+
**Reduced motion** is respected. A new kind of festival in the API shows nothing until it is designed here.
|
|
76
|
+
|
|
77
|
+
# Simple layer
|
|
78
|
+
|
|
79
|
+
## Vite
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
// src/main.tsx
|
|
83
|
+
import { FestiveLayer } from "@handysolver/festive";
|
|
84
|
+
|
|
85
|
+
createRoot(document.getElementById("root")!).render(
|
|
86
|
+
<StrictMode>
|
|
87
|
+
<App />
|
|
88
|
+
<FestiveLayer app="meetmind" apiUrl={import.meta.env.VITE_FESTIVAL_API_URL} />
|
|
89
|
+
</StrictMode>,
|
|
90
|
+
);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# .env
|
|
95
|
+
VITE_FESTIVAL_API_URL=https://your-festival-api.vercel.app
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Vite 8 may warn that the `"use client"` directive "may not be preserved" during a build. It is
|
|
99
|
+
harmless: the directive only matters to Next.js.
|
|
100
|
+
|
|
101
|
+
## Next.js (App Router)
|
|
102
|
+
|
|
103
|
+
The package is already marked `"use client"`, so use it straight from a server layout:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
// app/layout.tsx
|
|
107
|
+
import { FestiveLayer } from "@handysolver/festive";
|
|
108
|
+
|
|
109
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
110
|
+
return (
|
|
111
|
+
<html lang="en" suppressHydrationWarning>
|
|
112
|
+
<body>
|
|
113
|
+
{children}
|
|
114
|
+
<FestiveLayer app="my-next-app" apiUrl={process.env.NEXT_PUBLIC_FESTIVAL_API_URL} />
|
|
115
|
+
</body>
|
|
116
|
+
</html>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Pages Router: put it in `pages/_app.tsx` next to `<Component />`.
|
|
122
|
+
|
|
123
|
+
Mount it once, near the root. An ancestor with a CSS `transform` or `filter` would trap
|
|
124
|
+
its fixed positioning.
|
|
125
|
+
|
|
126
|
+
## Props
|
|
127
|
+
|
|
128
|
+
| Prop | Type | Default | Notes |
|
|
129
|
+
| ---------- | ----------------------------- | ---------- | ----- |
|
|
130
|
+
| `app` | `string` | required | Your app id, as used in the API's `apps` lists |
|
|
131
|
+
| `apiUrl` | `string \| null \| undefined` | – | API origin. Missing or empty renders nothing |
|
|
132
|
+
| `theme` | `"auto" \| "light" \| "dark"` | `"auto"` | See below |
|
|
133
|
+
| `position` | `"top" \| "bottom"` | `"bottom"` | Where the card sits |
|
|
134
|
+
| `disabled` | `boolean` | `false` | Turns everything off |
|
|
135
|
+
|
|
136
|
+
`theme="auto"` reads, in order: `data-theme="dark|light"` on `<html>`, a `.dark` / `.light`
|
|
137
|
+
class on `<html>`, a fixed CSS `color-scheme` on `<html>`, then the system preference. It
|
|
138
|
+
reacts to changes. If your app marks only dark mode (a `.dark` class and nothing else) and
|
|
139
|
+
starts in light mode on a system set to dark, there is nothing to detect: pass `theme` yourself
|
|
140
|
+
or set `color-scheme: light`.
|
|
141
|
+
|
|
142
|
+
## Using the accent in your own CSS
|
|
143
|
+
|
|
144
|
+
While a festival is active, `<html>` gets `data-festival="<id>"` and `--festive-accent`:
|
|
145
|
+
|
|
146
|
+
```css
|
|
147
|
+
.logo { color: var(--festive-accent, #2563eb); }
|
|
148
|
+
[data-festival^="diwali"] .hero { /* ... */ }
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Accents are not checked for contrast; avoid them for body text.
|
|
152
|
+
|
|
153
|
+
## Behaviour
|
|
154
|
+
|
|
155
|
+
- Emoji fall for about 9 seconds, once per tab session per festival, never blocking clicks.
|
|
156
|
+
Skipped for users who prefer reduced motion.
|
|
157
|
+
- Dismissing the card is remembered per festival in `localStorage`.
|
|
158
|
+
- If storage is blocked, both are remembered only until the page reloads.
|
|
159
|
+
- A tab left open rechecks when the user's local date changes.
|
|
160
|
+
|
|
161
|
+
With a Content-Security-Policy, allow the API origin in `connect-src` and inline styles in
|
|
162
|
+
`style-src`.
|
|
163
|
+
|
|
164
|
+
## Preview a festival
|
|
165
|
+
|
|
166
|
+
Open your app with `?festival=<id>`, e.g. `https://your-app.com/?festival=holi-2027`.
|
|
167
|
+
Preview ignores dates and always shows the card and the animation, and it records nothing.
|
|
168
|
+
|
|
169
|
+
## Develop
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
npm install
|
|
173
|
+
npm run check # typecheck, build, tests
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Publish
|
|
177
|
+
|
|
178
|
+
Publishing is public and a version number can never be reused.
|
|
179
|
+
|
|
180
|
+
1. Make sure you own the npm scope. `@handysolver` must be your npm username or an
|
|
181
|
+
organisation you belong to (create one at npmjs.com/org/create). Otherwise, rename first.
|
|
182
|
+
2. Log in, check the contents, publish:
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
npm login
|
|
186
|
+
npm pack --dry-run
|
|
187
|
+
npm publish --access public
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`npm publish` runs `npm run check` first and stops if anything fails.
|
|
191
|
+
|
|
192
|
+
### Rename
|
|
193
|
+
|
|
194
|
+
The name is set only in `package.json` (and copied to the lockfile):
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
npm pkg set name=@your-scope/festive
|
|
198
|
+
npm install --package-lock-only
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Then replace `@handysolver/festive` in this README.
|
|
202
|
+
|
|
203
|
+
### Update
|
|
204
|
+
|
|
205
|
+
Commit your changes, then:
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
npm version patch # or minor / major
|
|
209
|
+
npm publish --access public
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
In each app: `npm install @handysolver/festive@latest`. New festivals need no release;
|
|
213
|
+
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
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
import { useFestive } from "./FestiveProvider.js";
|
|
5
|
+
/** The greeting for your header. "Celebrate" is a party popper: it fires from where it was pressed. */
|
|
6
|
+
export function FestiveBadge() {
|
|
7
|
+
const { activeFestival, triggerBurst, dismiss } = useFestive();
|
|
8
|
+
const buttonRef = useRef(null);
|
|
9
|
+
const celebrateRef = useRef(null);
|
|
10
|
+
if (!activeFestival)
|
|
11
|
+
return null;
|
|
12
|
+
const { kind, accent, motif, badge, pill } = activeFestival;
|
|
13
|
+
const celebrate = () => {
|
|
14
|
+
// The word "Celebrate" is left out on smaller screens; the whole button stands in for it
|
|
15
|
+
let box = celebrateRef.current?.getBoundingClientRect();
|
|
16
|
+
if (!box?.width)
|
|
17
|
+
box = buttonRef.current?.getBoundingClientRect();
|
|
18
|
+
if (box)
|
|
19
|
+
triggerBurst({ x: box.left + box.width / 2, y: box.top + box.height / 2 });
|
|
20
|
+
};
|
|
21
|
+
return (_jsxs("div", { "data-festival": kind, className: "hsx-badge", style: { "--festive-accent": accent, "--hsx-border": pill.border, "--hsx-shadow": pill.shadow }, children: [_jsx("span", { "aria-hidden": "true", className: "hsx-shimmer" }), _jsxs("button", { ref: buttonRef, type: "button", onClick: celebrate, "aria-label": `${badge}. Celebrate`, title: "Celebrate", className: "hsx-celebrate", children: [motif ? (_jsx("span", { "aria-hidden": "true", className: "hsx-motif", children: motif })) : (_jsx("span", { "aria-hidden": "true", className: "hsx-flag" })), _jsx("span", { className: "hsx-greeting", children: badge }), _jsx("span", { ref: celebrateRef, "aria-hidden": "true", className: "hsx-chip", children: "\uD83C\uDF89 Celebrate" })] }), _jsx("button", { type: "button", onClick: dismiss, "aria-label": `Dismiss ${badge}`, className: "hsx-dismiss", children: _jsxs("svg", { width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: [_jsx("path", { d: "M18 6 6 18" }), _jsx("path", { d: "m6 6 12 12" })] }) })] }));
|
|
22
|
+
}
|