@zag-js/anatomy
Version:
29 lines (27 loc) • 1.08 kB
JavaScript
// src/create-anatomy.ts
var createAnatomy = (name, parts = []) => ({
parts: (...values) => {
if (isEmpty(parts)) {
return createAnatomy(name, values);
}
throw new Error("createAnatomy().parts(...) should only be called once. Did you mean to use .extendWith(...) ?");
},
extendWith: (...values) => createAnatomy(name, [...parts, ...values]),
rename: (newName) => createAnatomy(newName, parts),
keys: () => parts,
build: () => [...new Set(parts)].reduce(
(prev, part) => Object.assign(prev, {
[part]: {
selector: [
`&[data-scope="${toKebabCase(name)}"][data-part="${toKebabCase(part)}"]`,
`& [data-scope="${toKebabCase(name)}"][data-part="${toKebabCase(part)}"]`
].join(", "),
attrs: { "data-scope": toKebabCase(name), "data-part": toKebabCase(part) }
}
}),
{}
)
});
var toKebabCase = (value) => value.replace(/([A-Z])([A-Z])/g, "$1-$2").replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").toLowerCase();
var isEmpty = (v) => v.length === 0;
export { createAnatomy };