@poupe/theme-builder
Version:
Design token management and theme generation system for Poupe UI framework
139 lines (134 loc) • 4.12 kB
JavaScript
import { generateCSSRulesArray } from '@poupe/css';
import { M as hexString, q as colord } from './shared/theme-builder.BWnUhZKi.mjs';
export { h as hct } from './shared/theme-builder.BWnUhZKi.mjs';
import '@poupe/material-color-utilities';
import { random } from 'colord';
import { e as standardDynamicSchemes } from './shared/theme-builder.Dj2iZHrI.mjs';
import 'colord/plugins/mix';
const reHexValue = /^#?(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
const isHexValue = (s) => reHexValue.test(s);
const isThemeSchemeKey = (s) => !!s && s in standardDynamicSchemes;
const asThemeSchemeKey = (key) => {
if (isThemeSchemeKey(key)) {
return key;
}
return void 0;
};
const getParam = (param) => {
if (param === void 0) {
return void 0;
} else if (!Array.isArray(param)) {
return param;
} else if (param.length > 0) {
return param[0];
} else {
return void 0;
}
};
const getRandomColor = () => {
const c = random();
if (!c.isValid()) {
throw new Error("Failed to generate random color");
}
return c.toHex();
};
const colorToURL = (c) => {
const s = c ? hexString(c) : getRandomColor();
return s.slice(1);
};
function stringifyCSSRulesArray(rules = [], options = {}) {
const {
newLine = "\n",
...formatOptions
} = options;
const lines = [...generateCSSRulesArray(rules, formatOptions)];
return lines.join(newLine);
}
async function* stringifyCSSRulesArrayStream(rules = [], options = {}) {
const {
newLine = "\n",
...formatOptions
} = options;
for (const line of generateCSSRulesArray(rules, formatOptions)) {
yield line + newLine;
}
}
function stringifyCSSRulesArrayAsStream(rules = [], options = {}) {
const {
newLine = "\n",
...formatOptions
} = options;
return new ReadableStream({
start(controller) {
const generator = generateCSSRulesArray(rules, formatOptions);
const encoder = new TextEncoder();
function pump() {
const { done, value } = generator.next();
if (done) {
controller.close();
return;
}
controller.enqueue(encoder.encode(value + newLine));
pump();
}
pump();
}
});
}
function stringifyCSSRulesArrayAsResponse(rules = [], options = {}) {
const {
headers: extraHeaders,
newLine = "\n",
...formatOptions
} = options;
const content = stringifyCSSRulesArray(rules, { newLine, ...formatOptions });
const finalContent = content ? content + newLine : content;
const headers = new Headers(extraHeaders);
if (!headers.has("Content-Type")) {
headers.set("Content-Type", "text/css; charset=utf-8");
}
return new Response(finalContent, {
status: 200,
headers
});
}
function stringifyCSSRulesArrayAsStreamingResponse(rules = [], options = {}) {
const {
headers: extraHeaders,
...streamOptions
} = options;
const headers = new Headers(extraHeaders);
if (!headers.has("Content-Type")) {
headers.set("Content-Type", "text/css; charset=utf-8");
}
const stream = stringifyCSSRulesArrayAsStream(rules, streamOptions);
return new Response(stream, {
status: 200,
headers
});
}
const getColorParam = (param, filter) => {
const s = filter ? filter(getParam(param)) : getParam(param);
if (s === void 0 || s === "") {
return { param: s };
}
if (isHexValue(s)) {
const hex = (s.startsWith("#") ? s : `#${s}`).toLowerCase();
return { param: s, color: hex };
}
const c = colord(s);
if (c.isValid()) {
const hex = c.toHex();
return { param: s, color: hex };
}
return { param: s };
};
const getThemeSchemeParam = (param, filter) => {
const key = filter ? filter(getParam(param)) : getParam(param);
return {
param: key,
scheme: asThemeSchemeKey(key)
};
};
export { asThemeSchemeKey, colorToURL, colord, getColorParam, getParam, getRandomColor, getThemeSchemeParam, hexString, isHexValue, isThemeSchemeKey, stringifyCSSRulesArray, stringifyCSSRulesArrayAsResponse, stringifyCSSRulesArrayAsStream, stringifyCSSRulesArrayAsStreamingResponse, stringifyCSSRulesArrayStream };
//# sourceMappingURL=server.mjs.map