@theweekendprojects/emvb-daisyui 0.0.0-stage → 0.2.1

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 (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +106 -2
  3. package/licenses/daisyui-MIT.txt +3 -0
  4. package/package.json +55 -4
  5. package/src/catalog/basic.ts +407 -0
  6. package/src/catalog/class-names.ts +623 -0
  7. package/src/catalog/countdown.ts +392 -0
  8. package/src/catalog/data-display.ts +563 -0
  9. package/src/catalog/forms.ts +433 -0
  10. package/src/catalog/index.ts +77 -0
  11. package/src/catalog/interactive.ts +567 -0
  12. package/src/catalog/layout-marketing.ts +641 -0
  13. package/src/catalog/missing-complex.ts +430 -0
  14. package/src/catalog/missing-primitives.ts +409 -0
  15. package/src/catalog/navigation.ts +438 -0
  16. package/src/catalog/value-class.ts +20 -0
  17. package/src/compatibility/index.ts +104 -0
  18. package/src/compound/editor.ts +299 -0
  19. package/src/compound/index.ts +157 -0
  20. package/src/conformance/cli.ts +9 -0
  21. package/src/conformance/compile.ts +77 -0
  22. package/src/conformance/index.ts +4 -0
  23. package/src/conformance/manifest.ts +39 -0
  24. package/src/conformance/official-inventory.ts +135 -0
  25. package/src/conformance/report-cli.ts +5 -0
  26. package/src/conformance/report.ts +59 -0
  27. package/src/conformance/verify-css.ts +22 -0
  28. package/src/extension/class-token.ts +11 -0
  29. package/src/extension/classes.ts +636 -0
  30. package/src/extension/collect-classes.ts +73 -0
  31. package/src/extension/content.ts +93 -0
  32. package/src/extension/daisy.css +12441 -0
  33. package/src/extension/daisy.js +1 -0
  34. package/src/extension/elements.ts +262 -0
  35. package/src/extension/index.ts +39 -0
  36. package/src/extension/to-output.ts +113 -0
  37. package/src/generator/artifacts.ts +81 -0
  38. package/src/generator/class-names.ts +35 -0
  39. package/src/generator/cli.ts +21 -0
  40. package/src/generator/generate.ts +122 -0
  41. package/src/generator/index.ts +2 -0
  42. package/src/generator/write-class-names.ts +10 -0
  43. package/src/index.ts +12 -0
  44. package/src/render/classes.ts +48 -0
  45. package/src/render/href.ts +19 -0
  46. package/src/render/index.ts +3 -0
  47. package/src/render/provider.ts +37 -0
  48. package/src/renderer.ts +6 -0
  49. package/src/runtime/browser.ts +11 -0
  50. package/src/runtime/index.ts +41 -0
  51. package/src/test-support.ts +13 -0
  52. package/src/themes/index.ts +72 -0
  53. package/src/types.ts +116 -0
  54. package/src/validation/index.ts +153 -0
  55. package/src/verified/primitives.ts +62 -0
@@ -0,0 +1,392 @@
1
+ import type { DaisyComponentDefinition, DaisyComponentNode, DaisyVNode } from "../types.ts";
2
+ import { DIGITS_2_CLASS, valueClass } from "./value-class.ts";
3
+
4
+ type Unit = "d" | "h" | "m" | "s";
5
+ const ALL_UNITS: readonly Unit[] = ["d", "h", "m", "s"];
6
+ const DEFAULT_LABELS = ["days", "hours", "min", "sec"] as const;
7
+ const DEFAULT_UNITS: readonly Unit[] = ["d", "h", "m", "s"];
8
+ const DEFAULT_SIZE = "text-3xl";
9
+ const DEFAULT_ALIGN: readonly [string, string] = ["justify-center", "text-center"];
10
+ const UNIT_SETS: Readonly<Record<string, readonly Unit[]>> = {
11
+ dhms: DEFAULT_UNITS,
12
+ dhm: ["d", "h", "m"],
13
+ hms: ["h", "m", "s"],
14
+ d: ["d"],
15
+ };
16
+ const SIZES: Readonly<Record<string, string>> = {
17
+ sm: "text-xl",
18
+ md: DEFAULT_SIZE,
19
+ lg: "text-5xl",
20
+ xl: "text-7xl",
21
+ };
22
+ // [grid/flex content alignment, text alignment] for each Alignment option.
23
+ const ALIGN: Readonly<Record<string, readonly [string, string]>> = {
24
+ start: ["justify-start", "text-start"],
25
+ center: DEFAULT_ALIGN,
26
+ end: ["justify-end", "text-end"],
27
+ };
28
+
29
+ /** Time zones in the picker. `viewer` is each visitor's own clock; the rest are fixed instants. */
30
+ export const COUNTDOWN_ZONES: ReadonlyArray<readonly [string, string]> = [
31
+ ["viewer", "Visitor's own time zone"],
32
+ ["UTC", "UTC"],
33
+ ["Europe/London", "London"],
34
+ ["Europe/Berlin", "Berlin, Paris, Rome"],
35
+ ["America/New_York", "New York"],
36
+ ["America/Chicago", "Chicago"],
37
+ ["America/Los_Angeles", "Los Angeles"],
38
+ ["Asia/Dubai", "Dubai"],
39
+ ["Asia/Dhaka", "Dhaka"],
40
+ ["Asia/Kolkata", "India"],
41
+ ["Asia/Singapore", "Singapore"],
42
+ ["Asia/Tokyo", "Tokyo"],
43
+ ["Australia/Sydney", "Sydney"],
44
+ ];
45
+
46
+ const pad = (n: number) => String(n).padStart(2, "0");
47
+
48
+ /** `YYYY-MM-DDTHH:mm` for a date, read in UTC or in the machine's own zone. */
49
+ export function pickerTime(date: Date, utc = false): string {
50
+ const parts = utc
51
+ ? {
52
+ y: date.getUTCFullYear(),
53
+ mo: date.getUTCMonth(),
54
+ d: date.getUTCDate(),
55
+ h: date.getUTCHours(),
56
+ mi: date.getUTCMinutes(),
57
+ }
58
+ : {
59
+ y: date.getFullYear(),
60
+ mo: date.getMonth(),
61
+ d: date.getDate(),
62
+ h: date.getHours(),
63
+ mi: date.getMinutes(),
64
+ };
65
+ return `${parts.y}-${pad(parts.mo + 1)}-${pad(parts.d)}T${pad(parts.h)}:${pad(parts.mi)}`;
66
+ }
67
+
68
+ /** A target `days` from now at `hour:00`, in the form the date picker stores. */
69
+ export function targetIn(days: number, hour: number, utc = false): string {
70
+ const at = new Date();
71
+ if (utc) at.setUTCDate(at.getUTCDate() + days);
72
+ else at.setDate(at.getDate() + days);
73
+ return pickerTime(
74
+ utc ? new Date(at.setUTCHours(hour, 0, 0, 0)) : new Date(at.setHours(hour, 0, 0, 0)),
75
+ utc,
76
+ );
77
+ }
78
+
79
+ /** How far a zone's clock is ahead of UTC at an instant, in ms. Throws RangeError for a bad zone. */
80
+ function offsetMs(instant: number, zone: string): number {
81
+ const parts = Object.fromEntries(
82
+ new Intl.DateTimeFormat("en-US", {
83
+ timeZone: zone,
84
+ hourCycle: "h23",
85
+ year: "numeric",
86
+ month: "2-digit",
87
+ day: "2-digit",
88
+ hour: "2-digit",
89
+ minute: "2-digit",
90
+ second: "2-digit",
91
+ })
92
+ .formatToParts(new Date(instant))
93
+ .map((part) => [part.type, part.value]),
94
+ );
95
+ const shown = Date.UTC(
96
+ Number(parts.year),
97
+ Number(parts.month) - 1,
98
+ Number(parts.day),
99
+ Number(parts.hour),
100
+ Number(parts.minute),
101
+ Number(parts.second),
102
+ );
103
+ return shown - Math.floor(instant / 1000) * 1000;
104
+ }
105
+
106
+ /** The instant a wall-clock reading (`YYYY-MM-DDTHH:mm:ss`) happens in a zone, or NaN. */
107
+ export function wallToInstant(wall: string, zone: string): number {
108
+ const [y, mo, d, h, mi, s] = wall.split(/[-T:]/).map(Number) as [
109
+ number,
110
+ number,
111
+ number,
112
+ number,
113
+ number,
114
+ number,
115
+ ];
116
+ const asUtc = Date.UTC(y, mo - 1, d, h, mi, s);
117
+ if (zone === "UTC") return asUtc;
118
+ try {
119
+ // Two passes: the offset at the guess can differ from the offset at the answer around a DST change.
120
+ const first = asUtc - offsetMs(asUtc, zone);
121
+ return asUtc - offsetMs(first, zone);
122
+ } catch {
123
+ return Number.NaN;
124
+ }
125
+ }
126
+
127
+ const INSTANT = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?(?:Z|[+-]\d{2}:?\d{2})$/;
128
+ const WALL = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?$/;
129
+
130
+ export type CountdownTarget = { kind: "instant"; ms: number } | { kind: "local"; wall: string };
131
+
132
+ /**
133
+ * What the Ends-at field means. A reading with its own `Z` or offset is that exact instant. A bare
134
+ * reading is read in the chosen zone, or on each visitor's clock for `viewer`. Anything else is
135
+ * not a date, so the component falls back to its fixed number.
136
+ */
137
+ export function parseTarget(raw: unknown, zone: unknown): CountdownTarget | undefined {
138
+ const text = String(raw ?? "").trim();
139
+ if (INSTANT.test(text)) {
140
+ const ms = Date.parse(text.replace(" ", "T").replace(/([+-]\d{2})(\d{2})$/, "$1:$2"));
141
+ return Number.isNaN(ms) ? undefined : { kind: "instant", ms };
142
+ }
143
+ const m = WALL.exec(text);
144
+ if (!m) return undefined;
145
+ const part = (i: number) => (m[i] === undefined ? 0 : Number(m[i]));
146
+ const [y, mo, d, h, mi, s] = [part(1), part(2), part(3), part(4), part(5), part(6)] as [
147
+ number,
148
+ number,
149
+ number,
150
+ number,
151
+ number,
152
+ number,
153
+ ];
154
+ const probe = new Date(Date.UTC(y, mo - 1, d));
155
+ const real =
156
+ probe.getUTCFullYear() === y && probe.getUTCMonth() === mo - 1 && probe.getUTCDate() === d;
157
+ if (!real || h > 23 || mi > 59 || s > 59) return undefined;
158
+ const wall = `${m[1]}-${m[2]}-${m[3]}T${pad(h)}:${pad(mi)}:${pad(s)}`;
159
+ const where = String(zone ?? "viewer");
160
+ if (where === "viewer") return { kind: "local", wall };
161
+ const ms = wallToInstant(wall, where);
162
+ return Number.isNaN(ms) ? undefined : { kind: "instant", ms };
163
+ }
164
+
165
+ /** Each unit's number for `left` seconds. Without days, hours count every hour left. Daisy stops at 999. */
166
+ const cap = (n: number) => Math.min(n, 999);
167
+ export function remaining(left: number, units: readonly Unit[]): Record<Unit, number> {
168
+ const days = units.includes("d");
169
+ return {
170
+ d: cap(Math.floor(left / 86400)),
171
+ h: cap(days ? Math.floor((left % 86400) / 3600) : Math.floor(left / 3600)),
172
+ m: cap(Math.floor((left % 3600) / 60)),
173
+ s: left % 60,
174
+ };
175
+ }
176
+
177
+ const unitLabels = (raw: unknown): string[] => {
178
+ const given = String(raw ?? "").split("|");
179
+ return DEFAULT_LABELS.map((fallback, i) => given[i]?.trim().slice(0, 24) || fallback);
180
+ };
181
+
182
+ const clampNumber = (value: unknown, min: number, max: number) => {
183
+ const n = Number(value);
184
+ return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : min;
185
+ };
186
+
187
+ function fixedNumber(node: DaisyComponentNode, classes: string[]): DaisyVNode {
188
+ const max = clampNumber(node.props.max, 1, 999);
189
+ const value = clampNumber(node.props.value, 0, max);
190
+ const suffix = String(node.props.suffix ?? "").trim();
191
+ return {
192
+ tag: "span",
193
+ attrs: {
194
+ class: classes.join(" "),
195
+ role: "timer",
196
+ "aria-label": `${node.props.label}: ${value}${suffix ? ` ${suffix}` : ""}`,
197
+ },
198
+ children: [
199
+ {
200
+ tag: "span",
201
+ // The class is the first paint (up to 100); the runtime sets the variable for any value.
202
+ attrs: { class: valueClass(value), "data-daisy-value": String(value) },
203
+ children: [String(value)],
204
+ },
205
+ // Its own span with a margin: a space at the start of text inside a flex row is dropped.
206
+ ...(suffix
207
+ ? [{ tag: "span", attrs: { class: "ms-2" }, children: [suffix] } satisfies DaisyVNode]
208
+ : []),
209
+ ],
210
+ };
211
+ }
212
+
213
+ function dateCountdown(
214
+ node: DaisyComponentNode,
215
+ classes: string[],
216
+ target: CountdownTarget,
217
+ ): DaisyVNode {
218
+ const p = node.props;
219
+ const units = UNIT_SETS[String(p.units)] ?? DEFAULT_UNITS;
220
+ const layout = p.layout === "plain" || p.layout === "clock" ? p.layout : "boxes";
221
+ const [justify, textAlign] = ALIGN[String(p.align)] ?? DEFAULT_ALIGN;
222
+ const message = String(p.expired ?? "")
223
+ .trim()
224
+ .slice(0, 200);
225
+ // A visitor-clock reading cannot be placed on the server, so the first paint reads it as UTC.
226
+ const ms = target.kind === "instant" ? target.ms : Date.parse(`${target.wall}Z`);
227
+ const now = Date.now();
228
+ const left = Math.max(0, Math.floor((ms - now) / 1000));
229
+ // Over means the deadline has passed, not "under a second left": the clock reads 0 until then.
230
+ const done = message !== "" && ms <= now && target.kind === "instant";
231
+ const values = remaining(left, units);
232
+ const labels = unitLabels(p.labels);
233
+
234
+ const number = (unit: Unit): DaisyVNode => ({
235
+ tag: "span",
236
+ attrs: {
237
+ class:
238
+ unit === "d" ? valueClass(values[unit]) : `${valueClass(values[unit])} ${DIGITS_2_CLASS}`,
239
+ "data-daisy-unit": unit,
240
+ },
241
+ children: [String(values[unit])],
242
+ });
243
+
244
+ const box = (unit: Unit): DaisyVNode => ({
245
+ tag: "div",
246
+ attrs: {
247
+ class:
248
+ layout === "boxes"
249
+ ? "flex flex-col items-center gap-1 p-3 bg-neutral rounded-box text-neutral-content"
250
+ : "flex flex-col items-center gap-1",
251
+ },
252
+ children: [
253
+ { tag: "span", attrs: { class: classes.join(" ") }, children: [number(unit)] },
254
+ {
255
+ tag: "span",
256
+ attrs: { class: "text-xs opacity-80" },
257
+ children: [labels[ALL_UNITS.indexOf(unit)] ?? ""],
258
+ },
259
+ ],
260
+ });
261
+
262
+ const face: DaisyVNode =
263
+ layout === "clock"
264
+ ? {
265
+ tag: "div",
266
+ attrs: { class: `flex ${justify}` },
267
+ children: [
268
+ {
269
+ tag: "span",
270
+ attrs: { class: classes.join(" ") },
271
+ children: units.flatMap((unit, i) =>
272
+ i === 0 ? [number(unit)] : [":", number(unit)],
273
+ ),
274
+ },
275
+ ],
276
+ }
277
+ : {
278
+ tag: "div",
279
+ // A wrapping row: four boxes side by side overflowed a phone screen, so they wrap instead.
280
+ attrs: { class: `flex flex-wrap items-start gap-4 text-center ${justify}` },
281
+ children: units.map(box),
282
+ };
283
+
284
+ const size = SIZES[String(p.size)] ?? DEFAULT_SIZE;
285
+ return {
286
+ tag: "div",
287
+ attrs: {
288
+ class: "w-full",
289
+ role: done ? "status" : "timer",
290
+ "aria-label": String(p.label),
291
+ ...(target.kind === "instant"
292
+ ? { "data-daisy-countdown": new Date(target.ms).toISOString() }
293
+ : { "data-daisy-countdown-local": target.wall }),
294
+ },
295
+ children: [
296
+ {
297
+ tag: "div",
298
+ attrs: { "data-daisy-live": "", ...(done ? { class: "hidden" } : {}) },
299
+ children: [face],
300
+ },
301
+ ...(message
302
+ ? [
303
+ {
304
+ tag: "div",
305
+ attrs: {
306
+ "data-daisy-done": "",
307
+ class: `${size} font-semibold py-3 ${textAlign}${done ? "" : " hidden"}`,
308
+ },
309
+ children: [message],
310
+ } satisfies DaisyVNode,
311
+ ]
312
+ : []),
313
+ ],
314
+ };
315
+ }
316
+
317
+ export const COUNTDOWN: DaisyComponentDefinition = {
318
+ id: "countdown",
319
+ label: "Countdown",
320
+ category: "Data display",
321
+ tag: "span",
322
+ rootClass: "countdown font-mono",
323
+ fields: [
324
+ { key: "label", label: "Accessible label", kind: "text", default: "Time remaining" },
325
+ { key: "target", label: "Ends at", kind: "datetime", default: "" },
326
+ {
327
+ key: "zone",
328
+ label: "Time zone",
329
+ kind: "select",
330
+ default: "viewer",
331
+ options: COUNTDOWN_ZONES.map(([id]) => id),
332
+ optionLabels: Object.fromEntries(COUNTDOWN_ZONES),
333
+ },
334
+ {
335
+ key: "units",
336
+ label: "Show",
337
+ kind: "select",
338
+ default: "dhms",
339
+ options: ["dhms", "dhm", "hms", "d"],
340
+ optionLabels: {
341
+ dhms: "Days, hours, minutes, seconds",
342
+ dhm: "Days, hours, minutes",
343
+ hms: "Hours, minutes, seconds",
344
+ d: "Days only",
345
+ },
346
+ },
347
+ {
348
+ key: "layout",
349
+ label: "Layout",
350
+ kind: "select",
351
+ default: "boxes",
352
+ options: ["boxes", "plain", "clock"],
353
+ optionLabels: { boxes: "Boxes", plain: "Plain", clock: "Clock (12:34:56)" },
354
+ },
355
+ {
356
+ key: "align",
357
+ label: "Alignment",
358
+ kind: "select",
359
+ default: "center",
360
+ options: ["start", "center", "end"],
361
+ },
362
+ {
363
+ key: "size",
364
+ label: "Size",
365
+ kind: "select",
366
+ default: "md",
367
+ options: ["sm", "md", "lg", "xl"],
368
+ classMap: SIZES,
369
+ },
370
+ {
371
+ key: "labels",
372
+ label: "Unit labels, separated by |",
373
+ kind: "text",
374
+ default: "days|hours|min|sec",
375
+ },
376
+ {
377
+ key: "expired",
378
+ label: "Message when time is up (empty keeps zeros)",
379
+ kind: "text",
380
+ default: "",
381
+ },
382
+ { key: "value", label: "Fixed number (only when no date is set)", kind: "number", default: 42 },
383
+ { key: "max", label: "Fixed number maximum", kind: "number", default: 99 },
384
+ { key: "suffix", label: "Fixed number suffix", kind: "text", default: " seconds" },
385
+ ],
386
+ // A countdown that is added already counts down: a week from now at 09:00 on the visitor's clock.
387
+ initial: () => ({ target: targetIn(7, 9) }),
388
+ build(node, classes) {
389
+ const target = parseTarget(node.props.target, node.props.zone);
390
+ return target ? dateCountdown(node, classes, target) : fixedNumber(node, classes);
391
+ },
392
+ };