@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.
- package/LICENSE +21 -0
- package/README.md +106 -2
- package/licenses/daisyui-MIT.txt +3 -0
- package/package.json +55 -4
- package/src/catalog/basic.ts +407 -0
- package/src/catalog/class-names.ts +623 -0
- package/src/catalog/countdown.ts +392 -0
- package/src/catalog/data-display.ts +563 -0
- package/src/catalog/forms.ts +433 -0
- package/src/catalog/index.ts +77 -0
- package/src/catalog/interactive.ts +567 -0
- package/src/catalog/layout-marketing.ts +641 -0
- package/src/catalog/missing-complex.ts +430 -0
- package/src/catalog/missing-primitives.ts +409 -0
- package/src/catalog/navigation.ts +438 -0
- package/src/catalog/value-class.ts +20 -0
- package/src/compatibility/index.ts +104 -0
- package/src/compound/editor.ts +299 -0
- package/src/compound/index.ts +157 -0
- package/src/conformance/cli.ts +9 -0
- package/src/conformance/compile.ts +77 -0
- package/src/conformance/index.ts +4 -0
- package/src/conformance/manifest.ts +39 -0
- package/src/conformance/official-inventory.ts +135 -0
- package/src/conformance/report-cli.ts +5 -0
- package/src/conformance/report.ts +59 -0
- package/src/conformance/verify-css.ts +22 -0
- package/src/extension/class-token.ts +11 -0
- package/src/extension/classes.ts +636 -0
- package/src/extension/collect-classes.ts +73 -0
- package/src/extension/content.ts +93 -0
- package/src/extension/daisy.css +12441 -0
- package/src/extension/daisy.js +1 -0
- package/src/extension/elements.ts +262 -0
- package/src/extension/index.ts +39 -0
- package/src/extension/to-output.ts +113 -0
- package/src/generator/artifacts.ts +81 -0
- package/src/generator/class-names.ts +35 -0
- package/src/generator/cli.ts +21 -0
- package/src/generator/generate.ts +122 -0
- package/src/generator/index.ts +2 -0
- package/src/generator/write-class-names.ts +10 -0
- package/src/index.ts +12 -0
- package/src/render/classes.ts +48 -0
- package/src/render/href.ts +19 -0
- package/src/render/index.ts +3 -0
- package/src/render/provider.ts +37 -0
- package/src/renderer.ts +6 -0
- package/src/runtime/browser.ts +11 -0
- package/src/runtime/index.ts +41 -0
- package/src/test-support.ts +13 -0
- package/src/themes/index.ts +72 -0
- package/src/types.ts +116 -0
- package/src/validation/index.ts +153 -0
- package/src/verified/primitives.ts +62 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { componentClasses, mergeClasses } from "../render/classes.ts";
|
|
2
|
+
import {
|
|
3
|
+
officialDaisyComponent,
|
|
4
|
+
OFFICIAL_DAISYUI_COMPONENTS,
|
|
5
|
+
} from "../conformance/official-inventory.ts";
|
|
6
|
+
import { isVerifiedDaisyComponent } from "../conformance/manifest.ts";
|
|
7
|
+
import type {
|
|
8
|
+
DaisyComponentDefinition,
|
|
9
|
+
DaisyComponentNode,
|
|
10
|
+
GeneratedDaisyComponent,
|
|
11
|
+
DaisyVNode,
|
|
12
|
+
} from "../types.ts";
|
|
13
|
+
|
|
14
|
+
function fieldDefaults(
|
|
15
|
+
definition: DaisyComponentDefinition,
|
|
16
|
+
): Record<string, string | number | boolean> {
|
|
17
|
+
return Object.fromEntries(definition.fields.map((field) => [field.key, field.default]));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function fieldAttrs(
|
|
21
|
+
definition: DaisyComponentDefinition,
|
|
22
|
+
node: DaisyComponentNode,
|
|
23
|
+
): Record<string, string> {
|
|
24
|
+
const attrs: Record<string, string> = {};
|
|
25
|
+
for (const field of definition.fields) {
|
|
26
|
+
if (!field.attribute) continue;
|
|
27
|
+
const value = node.props[field.key] ?? field.default;
|
|
28
|
+
if (typeof value === "boolean") {
|
|
29
|
+
if (value)
|
|
30
|
+
attrs[field.attribute] = field.attribute.startsWith("aria-") ? "true" : field.attribute;
|
|
31
|
+
} else if (value !== "") attrs[field.attribute] = String(value);
|
|
32
|
+
}
|
|
33
|
+
if (definition.role) attrs.role = definition.role;
|
|
34
|
+
return attrs;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function textChildren(definition: DaisyComponentDefinition, node: DaisyComponentNode): string[] {
|
|
38
|
+
return definition.fields
|
|
39
|
+
.filter((field) => field.textContent)
|
|
40
|
+
.map((field) => String(node.props[field.key] ?? field.default));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function generateComponent(definition: DaisyComponentDefinition): GeneratedDaisyComponent {
|
|
44
|
+
validateDefinition(definition);
|
|
45
|
+
const official = officialDaisyComponent(definition.id);
|
|
46
|
+
return {
|
|
47
|
+
definition,
|
|
48
|
+
descriptor: {
|
|
49
|
+
type: "provider",
|
|
50
|
+
provider: "daisyui",
|
|
51
|
+
component: definition.id,
|
|
52
|
+
name: definition.label,
|
|
53
|
+
group: "content",
|
|
54
|
+
defaultTab: "content",
|
|
55
|
+
container: definition.container === true,
|
|
56
|
+
// The daisyUI extension offers these components in the Add panel now; no duplicate tiles.
|
|
57
|
+
addPanel: false,
|
|
58
|
+
category: official?.category ?? definition.category,
|
|
59
|
+
support: isVerifiedDaisyComponent(definition.id) ? "verified" : "experimental",
|
|
60
|
+
icon: official?.icon ?? "layout",
|
|
61
|
+
keywords: [
|
|
62
|
+
definition.id,
|
|
63
|
+
definition.label,
|
|
64
|
+
official?.category ?? definition.category,
|
|
65
|
+
"daisyUI",
|
|
66
|
+
],
|
|
67
|
+
order: official
|
|
68
|
+
? OFFICIAL_DAISYUI_COMPONENTS.findIndex((entry) => entry.id === official.id)
|
|
69
|
+
: 999,
|
|
70
|
+
fields: definition.fields.map((field) => ({
|
|
71
|
+
key: field.key,
|
|
72
|
+
kind: field.kind,
|
|
73
|
+
label: field.label,
|
|
74
|
+
options: field.options?.map((value) => ({
|
|
75
|
+
value,
|
|
76
|
+
label: field.optionLabels?.[value] ?? titleCase(value),
|
|
77
|
+
})),
|
|
78
|
+
})),
|
|
79
|
+
},
|
|
80
|
+
defaults: () => ({
|
|
81
|
+
type: "provider",
|
|
82
|
+
provider: "daisyui",
|
|
83
|
+
component: definition.id,
|
|
84
|
+
props: { ...fieldDefaults(definition), ...definition.initial?.() },
|
|
85
|
+
...(definition.container ? { children: [] } : {}),
|
|
86
|
+
}),
|
|
87
|
+
build(node, attrs = {}, children: DaisyVNode[] = [], options) {
|
|
88
|
+
const classes = componentClasses(definition, node.props);
|
|
89
|
+
if (definition.build) return definition.build(node, classes, children, options);
|
|
90
|
+
return {
|
|
91
|
+
tag: definition.tag,
|
|
92
|
+
attrs: {
|
|
93
|
+
...attrs,
|
|
94
|
+
...fieldAttrs(definition, node),
|
|
95
|
+
class: mergeClasses(attrs.class, classes.join(" ")),
|
|
96
|
+
},
|
|
97
|
+
children: [...textChildren(definition, node), ...children],
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function validateDefinition(definition: DaisyComponentDefinition): void {
|
|
104
|
+
if (!/^[a-z][a-z0-9-]*$/.test(definition.id))
|
|
105
|
+
throw new Error(`Invalid component id: ${definition.id}`);
|
|
106
|
+
if (!definition.rootClass.trim()) throw new Error(`${definition.id}: rootClass is required`);
|
|
107
|
+
const keys = new Set<string>();
|
|
108
|
+
for (const field of definition.fields) {
|
|
109
|
+
if (keys.has(field.key)) throw new Error(`${definition.id}: duplicate field ${field.key}`);
|
|
110
|
+
keys.add(field.key);
|
|
111
|
+
if (field.options && !field.options.includes(String(field.default)))
|
|
112
|
+
throw new Error(`${definition.id}.${field.key}: default must be an option`);
|
|
113
|
+
if (field.classMap) {
|
|
114
|
+
for (const option of field.options ?? [])
|
|
115
|
+
if (!(option in field.classMap))
|
|
116
|
+
throw new Error(`${definition.id}.${field.key}: missing class for ${option}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const titleCase = (value: string) =>
|
|
122
|
+
value.replace(/[-_]/g, " ").replace(/\b\w/g, (char) => char.toUpperCase());
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { DAISY_COMPONENT_CATALOG } from "../catalog/index.ts";
|
|
3
|
+
import { catalogClassNames, classNamesModule } from "./class-names.ts";
|
|
4
|
+
|
|
5
|
+
writeFileSync(
|
|
6
|
+
new URL("../catalog/class-names.ts", import.meta.url),
|
|
7
|
+
classNamesModule(catalogClassNames(DAISY_COMPONENT_CATALOG)),
|
|
8
|
+
);
|
|
9
|
+
// oxlint-disable-next-line no-console -- a script reports what it wrote
|
|
10
|
+
console.log("wrote src/catalog/class-names.ts");
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export * from "./types.ts";
|
|
2
|
+
export * from "./render/index.ts";
|
|
3
|
+
export * from "./generator/index.ts";
|
|
4
|
+
export * from "./catalog/index.ts";
|
|
5
|
+
export * from "./themes/index.ts";
|
|
6
|
+
export { daisyRenderer } from "./renderer.ts";
|
|
7
|
+
export * from "./validation/index.ts";
|
|
8
|
+
export * from "./compatibility/index.ts";
|
|
9
|
+
export * from "./conformance/index.ts";
|
|
10
|
+
export * from "./verified/primitives.ts";
|
|
11
|
+
export * from "./compound/index.ts";
|
|
12
|
+
export * from "./runtime/index.ts";
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { DaisyComponentDefinition, DaisyProps } from "../types.ts";
|
|
2
|
+
|
|
3
|
+
// Inline-level roots ignore auto margins, so they shrink to their content and centre as blocks.
|
|
4
|
+
// Everything else is block-level and centres with `mx-auto` alone.
|
|
5
|
+
const INLINE_ROOTS = new Set([
|
|
6
|
+
"badge",
|
|
7
|
+
"btn",
|
|
8
|
+
"kbd",
|
|
9
|
+
"status",
|
|
10
|
+
"toggle",
|
|
11
|
+
"checkbox",
|
|
12
|
+
"radio",
|
|
13
|
+
"swap",
|
|
14
|
+
"rating",
|
|
15
|
+
"radial-progress",
|
|
16
|
+
"indicator",
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
// Button shapes that set their own width or size and so must not get the natural-width override.
|
|
20
|
+
const SHAPED_BUTTONS = ["btn-block", "btn-wide", "btn-square", "btn-circle"];
|
|
21
|
+
|
|
22
|
+
export function componentClasses(
|
|
23
|
+
definition: DaisyComponentDefinition,
|
|
24
|
+
props: DaisyProps,
|
|
25
|
+
): string[] {
|
|
26
|
+
const classes = [definition.rootClass];
|
|
27
|
+
for (const field of definition.fields) {
|
|
28
|
+
if (!field.classMap) continue;
|
|
29
|
+
const value = String(props[field.key] ?? field.default);
|
|
30
|
+
const className = field.classMap[value];
|
|
31
|
+
if (className) classes.push(className);
|
|
32
|
+
}
|
|
33
|
+
// A button keeps its natural width unless its shape asks for full, square or round. A site-wide
|
|
34
|
+
// `.btn { width: 100% }` rule otherwise stretches every button; the important flag wins over it.
|
|
35
|
+
if (definition.rootClass === "btn" && !SHAPED_BUTTONS.some((c) => classes.includes(c)))
|
|
36
|
+
classes.push("w-auto!");
|
|
37
|
+
// Every daisyUI element is centred in its parent, with room above and below so stacked blocks
|
|
38
|
+
// do not run into each other.
|
|
39
|
+
if (INLINE_ROOTS.has(definition.rootClass)) classes.push("w-fit");
|
|
40
|
+
classes.push("mx-auto", "my-4");
|
|
41
|
+
return classes;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function mergeClasses(...values: Array<string | undefined>): string {
|
|
45
|
+
return [...new Set(values.flatMap((value) => value?.split(/\s+/).filter(Boolean) ?? []))].join(
|
|
46
|
+
" ",
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Reject executable and protocol-relative URLs while preserving normal site-relative links. */
|
|
2
|
+
export function safeHref(value: unknown): string {
|
|
3
|
+
const href = String(value ?? "").trim();
|
|
4
|
+
if (!href) return "#";
|
|
5
|
+
if (href.startsWith("/") && !href.startsWith("//")) return href;
|
|
6
|
+
if (
|
|
7
|
+
href.startsWith("#") ||
|
|
8
|
+
href.startsWith("?") ||
|
|
9
|
+
href.startsWith("mailto:") ||
|
|
10
|
+
href.startsWith("tel:")
|
|
11
|
+
)
|
|
12
|
+
return href;
|
|
13
|
+
try {
|
|
14
|
+
const parsed = new URL(href);
|
|
15
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:" ? href : "#";
|
|
16
|
+
} catch {
|
|
17
|
+
return "#";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { generateComponent } from "../generator/generate.ts";
|
|
2
|
+
import type {
|
|
3
|
+
DaisyRenderer,
|
|
4
|
+
DaisyComponentDefinition,
|
|
5
|
+
DaisyComponentNode,
|
|
6
|
+
GeneratedDaisyComponent,
|
|
7
|
+
DaisyVNode,
|
|
8
|
+
} from "../types.ts";
|
|
9
|
+
import { isCompoundPart, renderCompoundNode } from "../compound/index.ts";
|
|
10
|
+
import { nodeForRender, type DaisyRenderContext } from "../runtime/index.ts";
|
|
11
|
+
|
|
12
|
+
/** Builds the daisyUI renderer: its components, node factory and markup render. No registration. */
|
|
13
|
+
export function createDaisyRenderer(definitions: readonly DaisyComponentDefinition[]): DaisyRenderer {
|
|
14
|
+
const components = new Map<string, GeneratedDaisyComponent>(
|
|
15
|
+
definitions.map((definition) => [definition.id, generateComponent(definition)]),
|
|
16
|
+
);
|
|
17
|
+
return {
|
|
18
|
+
components,
|
|
19
|
+
createNode(component, id) {
|
|
20
|
+
const generated = components.get(component);
|
|
21
|
+
if (!generated) throw new Error(`Unknown daisyUI component: ${component}`);
|
|
22
|
+
return { id, ...generated.defaults() };
|
|
23
|
+
},
|
|
24
|
+
render(
|
|
25
|
+
node: DaisyComponentNode,
|
|
26
|
+
attrs: Record<string, string> = {},
|
|
27
|
+
children: DaisyVNode[] = [],
|
|
28
|
+
context: DaisyRenderContext = { mode: "public" },
|
|
29
|
+
) {
|
|
30
|
+
const prepared = nodeForRender(node, context);
|
|
31
|
+
if (isCompoundPart(prepared.component)) return renderCompoundNode(prepared, children);
|
|
32
|
+
const generated = components.get(prepared.component);
|
|
33
|
+
if (!generated) throw new Error(`Unknown daisyUI component: ${node.component}`);
|
|
34
|
+
return generated.build(prepared, attrs, children, { mode: context.mode });
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
package/src/renderer.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DAISY_COMPONENT_CATALOG } from "./catalog/index.ts";
|
|
2
|
+
import { COLUMNS } from "./catalog/layout-marketing.ts";
|
|
3
|
+
import { createDaisyRenderer } from "./render/provider.ts";
|
|
4
|
+
|
|
5
|
+
/** The daisyUI renderer for the catalog. The extension calls it; nothing is registered globally. */
|
|
6
|
+
export const daisyRenderer = createDaisyRenderer([...DAISY_COMPONENT_CATALOG, COLUMNS]);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const INTERACTIVE = `(()=>{const q=(id)=>id&&document.getElementById(id);document.addEventListener('click',e=>{const b=e.target.closest('[data-daisy-command]');if(!b)return;const el=q(b.getAttribute('aria-controls'));if(!el)return;const c=b.dataset.daisyCommand;if(c==='show-modal'&&el.showModal){b.dataset.returnFocus='true';el.showModal();const f=el.querySelector('button,[href],input,select,textarea,[tabindex]:not([tabindex="-1"])');f&&f.focus();}if(c==='close'&&el.close)el.close();});document.addEventListener('close',e=>{const d=e.target;if(!(d instanceof HTMLDialogElement))return;const b=document.querySelector('[data-daisy-command="show-modal"][aria-controls="'+CSS.escape(d.id)+'"]');b&&b.focus();},true);document.addEventListener('keydown',e=>{const tab=e.target.closest('[role="tab"]');if(!tab)return;const list=[...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]')];let i=list.indexOf(tab);if(e.key==='ArrowRight')i=(i+1)%list.length;else if(e.key==='ArrowLeft')i=(i-1+list.length)%list.length;else if(e.key==='Home')i=0;else if(e.key==='End')i=list.length-1;else return;e.preventDefault();list.forEach((x,n)=>{x.tabIndex=n===i?0:-1;x.setAttribute('aria-selected',n===i?'true':'false');const p=q(x.getAttribute('aria-controls'));if(p)p.hidden=n!==i;});list[i].focus();});})();`;
|
|
2
|
+
|
|
3
|
+
// Countdown. Inline `style` is refused in public markup, so the first paint uses a class and this
|
|
4
|
+
// script sets `--value` through the CSS object model, which is how daisyUI documents it. A fixed
|
|
5
|
+
// number (`data-daisy-value`) is applied once. A date ticks every second: an exact instant in
|
|
6
|
+
// `data-daisy-countdown`, or a wall-clock reading on the visitor's own clock in
|
|
7
|
+
// `data-daisy-countdown-local`. At zero it shows the `data-daisy-done` message when there is one.
|
|
8
|
+
// Without days, hours count every hour left, as the server does (countdown.ts `remaining`).
|
|
9
|
+
const COUNTDOWN = `(()=>{const put=(el,n)=>{el.style.setProperty('--value',n);el.textContent=String(n);};const when=(r)=>{const abs=r.dataset.daisyCountdown;if(abs){const t=Date.parse(abs);return t;}const w=(r.dataset.daisyCountdownLocal||'').split(/[-T:]/).map(Number);if(w.length<5||w.some(Number.isNaN))return NaN;return new Date(w[0],w[1]-1,w[2],w[3],w[4],w[5]||0).getTime();};const tick=()=>{const now=Date.now();document.querySelectorAll('[data-daisy-countdown],[data-daisy-countdown-local]').forEach(r=>{const t=when(r);if(Number.isNaN(t))return;const s=Math.max(0,Math.floor((t-now)/1000));const days=!!r.querySelector('[data-daisy-unit="d"]');const v={d:Math.floor(s/86400),h:days?Math.floor(s%86400/3600):Math.floor(s/3600),m:Math.floor(s%3600/60),s:s%60};r.querySelectorAll('[data-daisy-unit]').forEach(el=>{const n=v[el.dataset.daisyUnit];if(n!==undefined)put(el,Math.min(n,999));});const done=r.querySelector('[data-daisy-done]');if(t<=now&&done){const live=r.querySelector('[data-daisy-live]');if(live)live.classList.add('hidden');done.classList.remove('hidden');r.setAttribute('role','status');}});};const start=()=>{document.querySelectorAll('[data-daisy-value]').forEach(el=>{const n=Number(el.dataset.daisyValue);if(Number.isFinite(n))put(el,n);});if(!document.querySelector('[data-daisy-countdown],[data-daisy-countdown-local]'))return;tick();setInterval(()=>{if(!document.hidden)tick();},1000);document.addEventListener('visibilitychange',()=>{if(!document.hidden)tick();});};if(document.readyState==='loading')document.addEventListener('DOMContentLoaded',start);else start();})();`;
|
|
10
|
+
|
|
11
|
+
export const DAISY_INTERACTIVE_RUNTIME = INTERACTIVE + COUNTDOWN;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DaisyComponentNode } from "../types.ts";
|
|
2
|
+
|
|
3
|
+
export interface DaisyRenderContext {
|
|
4
|
+
mode: "editor" | "public";
|
|
5
|
+
previewState?: Readonly<Record<string, unknown>>;
|
|
6
|
+
}
|
|
7
|
+
const TRANSIENT_BY_COMPONENT: Readonly<Record<string, readonly string[]>> = {
|
|
8
|
+
modal: ["open"],
|
|
9
|
+
dropdown: ["open"],
|
|
10
|
+
drawer: ["open", "checked"],
|
|
11
|
+
swap: ["checked"],
|
|
12
|
+
tabs: ["active"],
|
|
13
|
+
carousel: ["current"],
|
|
14
|
+
};
|
|
15
|
+
const transientKeys = (node: DaisyComponentNode) => TRANSIENT_BY_COMPONENT[node.component] ?? [];
|
|
16
|
+
|
|
17
|
+
/** Separates runtime/editor preview state from authored defaults without changing unrelated controls. */
|
|
18
|
+
export function nodeForRender(
|
|
19
|
+
node: DaisyComponentNode,
|
|
20
|
+
context: DaisyRenderContext,
|
|
21
|
+
): DaisyComponentNode {
|
|
22
|
+
const props = { ...node.props };
|
|
23
|
+
if (node.component === "modal" || node.component === "dropdown")
|
|
24
|
+
for (const key of transientKeys(node)) delete props[key];
|
|
25
|
+
// Editors need to see a modal's content while building, so the canvas shows it open. The public
|
|
26
|
+
// page keeps it closed unless the author sets defaultOpen.
|
|
27
|
+
if (node.component === "modal" && context.mode === "editor") props.open = true;
|
|
28
|
+
const preview = context.mode === "editor" ? context.previewState?.[node.id] : undefined;
|
|
29
|
+
if (preview && typeof preview === "object") Object.assign(props, preview);
|
|
30
|
+
if (node.props.defaultOpen === true && context.mode === "public") props.open = true;
|
|
31
|
+
return { ...node, props, children: node.children?.map((child) => nodeForRender(child, context)) };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function serializableNode(node: DaisyComponentNode): DaisyComponentNode {
|
|
35
|
+
const props = { ...node.props };
|
|
36
|
+
for (const key of transientKeys(node)) delete props[key];
|
|
37
|
+
return { ...node, props, children: node.children?.map(serializableNode) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const DAISY_RUNTIME_SCRIPT = `document.addEventListener('click',e=>{const b=e.target.closest('[data-daisy-command]');if(!b)return;const id=b.getAttribute('aria-controls');const el=id&&document.getElementById(id);if(!el)return;const c=b.dataset.daisyCommand;if(c==='show-modal'&&el.showModal)el.showModal();if(c==='close'&&el.close)el.close();});`;
|
|
41
|
+
export * from "./browser.ts";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DaisyVNode } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
/** The value, or a thrown error that names what was missing. For tests and fixtures. */
|
|
4
|
+
export function must<T>(value: T | null | undefined, what = "value"): T {
|
|
5
|
+
if (value === null || value === undefined) throw new Error(`missing ${what}`);
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** A rendered child that must be an element, not text. For tests. */
|
|
10
|
+
export function element(child: DaisyVNode | string | undefined): DaisyVNode {
|
|
11
|
+
if (child === undefined || typeof child === "string") throw new Error("expected an element");
|
|
12
|
+
return child;
|
|
13
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export const BUILTIN_DAISY_THEMES = ["light", "dark", "corporate", "business", "cupcake"] as const;
|
|
2
|
+
export type BuiltinDaisyTheme = (typeof BUILTIN_DAISY_THEMES)[number];
|
|
3
|
+
|
|
4
|
+
export interface DaisyThemeConfig {
|
|
5
|
+
defaultTheme: string;
|
|
6
|
+
darkTheme?: string;
|
|
7
|
+
enabledThemes: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_DAISY_THEME_CONFIG: DaisyThemeConfig = {
|
|
11
|
+
defaultTheme: "light",
|
|
12
|
+
darkTheme: "dark",
|
|
13
|
+
enabledThemes: BUILTIN_DAISY_THEMES,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const THEME_NAME = /^[a-z][a-z0-9-]{0,49}$/;
|
|
17
|
+
|
|
18
|
+
export function validateThemeConfig(config: DaisyThemeConfig): string[] {
|
|
19
|
+
const issues: string[] = [];
|
|
20
|
+
if (!THEME_NAME.test(config.defaultTheme)) issues.push("Invalid default theme name");
|
|
21
|
+
if (config.darkTheme && !THEME_NAME.test(config.darkTheme))
|
|
22
|
+
issues.push("Invalid dark theme name");
|
|
23
|
+
if (!config.enabledThemes.includes(config.defaultTheme))
|
|
24
|
+
issues.push("Default theme must be enabled");
|
|
25
|
+
if (config.darkTheme && !config.enabledThemes.includes(config.darkTheme))
|
|
26
|
+
issues.push("Dark theme must be enabled");
|
|
27
|
+
if (new Set(config.enabledThemes).size !== config.enabledThemes.length)
|
|
28
|
+
issues.push("Theme names must be unique");
|
|
29
|
+
return issues;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function themeAttributes(
|
|
33
|
+
theme: string | undefined,
|
|
34
|
+
config: DaisyThemeConfig = DEFAULT_DAISY_THEME_CONFIG,
|
|
35
|
+
): Record<string, string> {
|
|
36
|
+
const selected = theme || config.defaultTheme;
|
|
37
|
+
if (!config.enabledThemes.includes(selected))
|
|
38
|
+
throw new Error(`Theme is not enabled: ${selected}`);
|
|
39
|
+
return { "data-theme": selected };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function generateDaisyThemeCss(config: DaisyThemeConfig): string {
|
|
43
|
+
const issues = validateThemeConfig(config);
|
|
44
|
+
if (issues.length) throw new Error(issues.join("; "));
|
|
45
|
+
const tokens = config.enabledThemes.map((name) =>
|
|
46
|
+
name === config.defaultTheme
|
|
47
|
+
? `${name} --default`
|
|
48
|
+
: name === config.darkTheme
|
|
49
|
+
? `${name} --prefersdark`
|
|
50
|
+
: name,
|
|
51
|
+
);
|
|
52
|
+
return `@plugin "daisyui" {\n themes: ${tokens.join(", ")};\n}\n`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const color = (value: string): string => {
|
|
56
|
+
if (!/^#[0-9a-f]{6}$/i.test(value)) throw new Error(`Invalid theme color: ${value}`);
|
|
57
|
+
return value;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export function generateBrandTheme(
|
|
61
|
+
name: string,
|
|
62
|
+
colors: {
|
|
63
|
+
primary: string;
|
|
64
|
+
secondary: string;
|
|
65
|
+
accent: string;
|
|
66
|
+
neutral?: string;
|
|
67
|
+
base100?: string;
|
|
68
|
+
},
|
|
69
|
+
): string {
|
|
70
|
+
if (!THEME_NAME.test(name)) throw new Error("Invalid brand theme name");
|
|
71
|
+
return `@plugin "daisyui/theme" {\n name: "${name}";\n --color-primary: ${color(colors.primary)};\n --color-secondary: ${color(colors.secondary)};\n --color-accent: ${color(colors.accent)};\n --color-neutral: ${color(colors.neutral ?? "#1f2937")};\n --color-base-100: ${color(colors.base100 ?? "#ffffff")};\n}\n`;
|
|
72
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** Field kinds the daisyUI catalog declares. Local copies of the shapes EmVB's extension output takes. */
|
|
2
|
+
export type DaisyFieldKind =
|
|
3
|
+
| "text"
|
|
4
|
+
| "textarea"
|
|
5
|
+
| "number"
|
|
6
|
+
| "select"
|
|
7
|
+
| "boolean"
|
|
8
|
+
| "href"
|
|
9
|
+
| "datetime"
|
|
10
|
+
| "media";
|
|
11
|
+
export type DaisyFieldValue = string | number | boolean;
|
|
12
|
+
export type DaisyProps = Record<string, DaisyFieldValue | undefined>;
|
|
13
|
+
/** A markup tree: a tag, its attributes (undefined values are skipped) and its children. */
|
|
14
|
+
export interface DaisyVNode {
|
|
15
|
+
tag: string;
|
|
16
|
+
attrs: Record<string, string | undefined>;
|
|
17
|
+
children: Array<DaisyVNode | string>;
|
|
18
|
+
}
|
|
19
|
+
export interface DaisyComponentNode {
|
|
20
|
+
id: string;
|
|
21
|
+
type: "provider";
|
|
22
|
+
provider: "daisyui";
|
|
23
|
+
component: string;
|
|
24
|
+
props: DaisyProps;
|
|
25
|
+
providerMeta?: Record<string, DaisyFieldValue>;
|
|
26
|
+
/** Name shown in the editor's Layers panel. Compound parts (slides, tabs, rows) get one. */
|
|
27
|
+
editorLabel?: string;
|
|
28
|
+
children?: DaisyComponentNode[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface DaisyFieldDefinition {
|
|
32
|
+
key: string;
|
|
33
|
+
label: string;
|
|
34
|
+
kind: DaisyFieldKind;
|
|
35
|
+
default: DaisyFieldValue;
|
|
36
|
+
options?: readonly string[];
|
|
37
|
+
/** Panel text for an option; unset options show their value in title case. */
|
|
38
|
+
optionLabels?: Readonly<Record<string, string>>;
|
|
39
|
+
classMap?: Readonly<Record<string, string>>;
|
|
40
|
+
attribute?: string;
|
|
41
|
+
textContent?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface DaisyComponentDefinition {
|
|
45
|
+
id: string;
|
|
46
|
+
label: string;
|
|
47
|
+
category:
|
|
48
|
+
| "Actions"
|
|
49
|
+
| "Data display"
|
|
50
|
+
| "Feedback"
|
|
51
|
+
| "Layout"
|
|
52
|
+
| "Marketing"
|
|
53
|
+
| "Navigation"
|
|
54
|
+
| "Interactive";
|
|
55
|
+
tag: string;
|
|
56
|
+
rootClass: string;
|
|
57
|
+
fields: readonly DaisyFieldDefinition[];
|
|
58
|
+
container?: boolean;
|
|
59
|
+
role?: string;
|
|
60
|
+
/** Props computed each time a node is created, on top of the fields' static defaults. */
|
|
61
|
+
initial?: () => DaisyProps;
|
|
62
|
+
build?: (
|
|
63
|
+
node: DaisyComponentNode,
|
|
64
|
+
classes: string[],
|
|
65
|
+
children: DaisyVNode[],
|
|
66
|
+
options?: DaisyRenderOptions,
|
|
67
|
+
) => DaisyVNode;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface GeneratedDaisyComponent {
|
|
71
|
+
definition: DaisyComponentDefinition;
|
|
72
|
+
descriptor: {
|
|
73
|
+
type: "provider";
|
|
74
|
+
provider: "daisyui";
|
|
75
|
+
component: string;
|
|
76
|
+
name: string;
|
|
77
|
+
group: "content";
|
|
78
|
+
defaultTab: "content";
|
|
79
|
+
category?: string;
|
|
80
|
+
/** True when the component holds child elements (see the descriptor of the stored provider node). */
|
|
81
|
+
container?: boolean;
|
|
82
|
+
/** False hides the Add panel tile; existing nodes still render (see the descriptor of the stored provider node). */
|
|
83
|
+
addPanel?: boolean;
|
|
84
|
+
support?: "verified" | "experimental";
|
|
85
|
+
icon?: string;
|
|
86
|
+
keywords?: readonly string[];
|
|
87
|
+
order?: number;
|
|
88
|
+
fields: Array<{
|
|
89
|
+
key: string;
|
|
90
|
+
kind: DaisyFieldKind;
|
|
91
|
+
label: string;
|
|
92
|
+
options?: Array<{ value: string; label: string }>;
|
|
93
|
+
}>;
|
|
94
|
+
};
|
|
95
|
+
defaults: () => Omit<DaisyComponentNode, "id">;
|
|
96
|
+
build: (
|
|
97
|
+
node: DaisyComponentNode,
|
|
98
|
+
attrs?: Record<string, string>,
|
|
99
|
+
children?: DaisyVNode[],
|
|
100
|
+
options?: DaisyRenderOptions,
|
|
101
|
+
) => DaisyVNode;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface DaisyRenderer {
|
|
105
|
+
components: ReadonlyMap<string, GeneratedDaisyComponent>;
|
|
106
|
+
createNode(component: string, id: string): DaisyComponentNode;
|
|
107
|
+
render(
|
|
108
|
+
node: DaisyComponentNode,
|
|
109
|
+
attrs?: Record<string, string>,
|
|
110
|
+
children?: DaisyVNode[],
|
|
111
|
+
context?: { mode: "editor" | "public" },
|
|
112
|
+
): DaisyVNode;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Render options: only the editor canvas differs from the public page. */
|
|
116
|
+
export type DaisyRenderOptions = { mode?: "editor" | "public" };
|