@crossed/styled
Version:
A universal & performant styling library for React Native, Next.js & React
178 lines (177 loc) • 5.27 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var RegistryBridge_exports = {};
__export(RegistryBridge_exports, {
RegistryBridge: () => RegistryBridge,
parse: () => parse
});
module.exports = __toCommonJS(RegistryBridge_exports);
var import_setTheme = require("../setTheme");
var import_utils = require("../plugins/utils");
var import_isWeb = require("../isWeb");
const parse = (t, parentName, isWeb) => {
return !isWeb ? { theme: t, values: t } : Object.entries(t).reduce(
(acc, [key, value]) => {
if (Array.isArray(value)) {
} else if (["number", "string"].includes(typeof value)) {
const name = (0, import_utils.convertKeyToCss)(
`${parentName ? `${parentName}-` : ""}${key}`
);
acc.theme[key] = `var(--${name})`;
acc.values[`--${name}`] = (0, import_utils.normalizeUnitPixel)(
"marginTop",
value,
isWeb
);
} else if (typeof value === "object") {
const toto = parse(
value,
(0, import_utils.convertKeyToCss)(`${parentName ? `${parentName}-` : ""}${key}`),
isWeb
);
acc.theme = {
...acc.theme,
...Object.entries(toto.theme).reduce(
(acc2, [key2, value2]) => {
acc2[key] = { ...acc2[key], [key2]: value2 };
return acc2;
},
{}
)
};
acc.values = {
...acc.values,
...toto.values
};
}
return acc;
},
{ theme: {}, values: {} }
);
};
class RegistryBridge {
constructor() {
this.plugins = [];
this._debug = false;
this._listen = /* @__PURE__ */ new Set();
}
setThemes(themes) {
this.themes = themes;
return this;
}
get themeName() {
return this._themeName;
}
setInitialThemeName(themeName) {
if (!this._themeName) {
(0, import_setTheme.setTheme)(this._themeName, themeName);
this._themeName = themeName;
}
return this;
}
setThemeName(themeName) {
(0, import_setTheme.setTheme)(this._themeName, themeName);
this._themeName = themeName;
this._listen.forEach((cb) => cb(themeName));
return this;
}
getTheme(web) {
if (!this.themes) {
console.warn("Themes are not set");
return {};
}
return parse(this.themes[this.themeName] || {}, void 0, web ?? import_isWeb.isWeb).theme;
}
getThemes() {
return this.themes;
}
subscribe(cb) {
this._listen.add(cb);
return () => {
this._listen.delete(cb);
};
}
setDebug(d) {
this._debug = d;
return this;
}
addPlugin(plugin) {
this.plugins.push(plugin);
return this;
}
getPlugins() {
return this.plugins;
}
log(e) {
if (this._debug) {
console.log(`[@crossed/styled] ${e}`);
}
}
apply(params, options) {
this.log(`Registry apply`);
const par = params();
Object.keys(par).forEach((key) => {
const styles = Object.entries(par[key]).reduce(
(acc, [key2, value]) => {
if (key2 === "paddingHorizontal") {
acc.paddingLeft = value;
acc.paddingRight = value;
} else if (key2 === "paddingVertical") {
acc.paddingTop = value;
acc.paddingBottom = value;
} else if (key2 === "padding") {
acc.paddingTop = value;
acc.paddingBottom = value;
acc.paddingLeft = value;
acc.paddingRight = value;
} else if (key2 === "marginHorizontal") {
acc.marginLeft = value;
acc.marginRight = value;
} else if (key2 === "marginVertical") {
acc.marginTop = value;
acc.marginBottom = value;
} else if (key2 === "margin") {
acc.marginTop = value;
acc.marginBottom = value;
acc.marginLeft = value;
acc.marginRight = value;
} else {
acc[key2] = value;
}
return acc;
},
{}
);
this.plugins.forEach(({ test, apply, name }) => {
if (typeof key === "string") {
const keyFind = test.includes(key);
if (keyFind) {
this.log(`[${name}] Find "${key}" for "${name}" plugin`);
apply == null ? void 0 : apply({ ...options, key, styles });
}
}
});
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RegistryBridge,
parse
});
//# sourceMappingURL=RegistryBridge.js.map