neu-ts-css-theming
Version:
Lightweight CSS-variables theming with TS helpers for Angular or any web app.
750 lines (748 loc) • 23.7 kB
JavaScript
// src/style.data-types.ts
var AppThemeNamesList = [
"primary",
"secondary",
"tertiary",
"accent",
"success",
"info",
"warning",
"danger",
"light",
"dark",
"accent",
"custom"
];
var DisplayDataTypeList = [
"block",
"grid",
"flex",
"-webkit-box",
"contents"
];
var OverflowDataTypeList = [
"visible",
"hidden",
"clip",
"scroll",
"auto",
"initial",
"inherit"
];
var AlignContentDataTypeList = [
"flex-start",
"flex-end",
"center",
"space-between",
"space-around",
"space-evenly",
"start",
"end",
"baseline",
"normal",
"stretch",
"inherit",
"initial",
"unset"
];
var PositionDataTypeList = [
"static",
"relative",
"fixed",
"absolute",
"sticky"
];
var FlexDirectionDataTypeList = [
"column",
"row"
];
var FlexWrapDataTypeList = [
"nowrap",
"wrap",
"wrap-reverse",
"initial",
"inherit"
];
var JustifyContentDataTypeList = [
"flex-start",
"flex-end",
"center",
"space-between",
"space-around",
"space-evenly",
"initial",
"inherit"
];
var JustifyItemsDataTypeList = [
"legacy",
"normal",
"stretch",
"start",
"left",
"center",
"end",
"right",
"safe",
"unsafe",
"baseline",
"initial",
"inherit"
];
var FlexAlignItemsDataTypeList = [
"normal",
"stretch",
"center",
"flex-start",
"flex-end",
"baseline",
"initial",
"inherit"
];
var GridAlignItemsDataTypeList = [
"normal",
"stretch",
"center",
"start",
"end",
"baseline",
"initial",
"inherit"
];
var TextAlignDataTypeList = [
"left",
"center",
"right"
];
var TextWeightDataTypeList = [
"light",
"lighter",
"normal",
"bold",
"bolder"
];
var TextWhiteSpaceDataTypeList = [
"normal",
"nowrap",
"break-spaces",
"pre",
"pre-line",
"pre-wrap"
];
var TextOverflowDataTypeList = [
"ellipsis",
"clip",
"visible",
"hidden"
];
var TextBoxOrientDataTypeList = [
"horizontal",
"vertical",
"inline-axis",
"block-axis"
];
var TextDecorationDataTypeList = [
"none",
"underline",
"overline",
"line-through",
"blink",
// Deprecated, may not be supported
"inherit",
"initial",
"unset"
];
var BorderStyleDataTypeList = [
"solid",
"dashed",
"dotted",
"double",
"groove",
"ridge",
"inset",
"outset",
"none",
"hidden"
];
var IconTypeDataTypeList = [
"mat",
"span"
];
var OutlineStyleDataTypeList = [
"none",
"hidden",
"dotted",
"dashed",
"solid",
"double",
"groove",
"ridge",
"inset",
"outset",
"initial",
"inherit"
];
var VisibilityDataTypeList = [
"visible",
"hidden",
"collapse",
"initial",
"inherit"
];
var CursorDataTypeList = [
"alias",
"all-scroll",
"auto",
"cell",
"col-resize",
"context-menu",
"copy",
"crosshair",
"default",
"e-resize",
"ew-resize",
"grab",
"grabbing",
"help",
"move",
"n-resize",
"ne-resize",
"nesw-resize",
"ns-resize",
"nw-resize",
"nwse-resize",
"no-drop",
"none",
"not-allowed",
"pointer",
"progress",
"row-resize",
"s-resize",
"se-resize",
"sw-resize",
"text",
// 'url()',
"vertical-text",
"w-resize",
"wait",
"zoom-in",
"zoom-out",
"initial",
"inherit"
];
// src/style.models.ts
var AppThemeModel = class _AppThemeModel {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.themeName !== void 0 || populateDefaults === true) {
this.themeName = config !== void 0 && config !== void 0 && (config?.themeName === void 0 || config?.themeName === void 0) ? "custom" : config?.themeName || "light";
}
if (config?.colors !== void 0 || populateDefaults === true) {
this.colors = new AppThemeColorsModel(config?.colors);
}
}
// FUNCTION: ANGULAR > configAppTheme
static configAppTheme(config) {
const themeConfig = !config || typeof config === "string" ? void 0 : config || void 0;
const themeName = themeConfig !== void 0 ? themeConfig?.themeName || "custom" : config || "light";
let returnValue = new _AppThemeModel(themeConfig);
returnValue.themeName = themeName;
if (themeName !== "custom") {
returnValue = new _AppThemeModel({
themeName,
colors: new AppThemeColorsModel({
backgroundColor: `rgba(var(--app-theme-${themeName}-background-rgb), 1)`,
textColor: `rgba(var(--app-theme-${themeName}-text-rgb), 1)`,
iconColor: `rgba(var(--app-theme-${themeName}-text-rgb), 1)`,
borderColor: `rgba(var(--app-theme-${themeName}-border-rgb), 0.25)`,
rippleColor: `rgba(var(--app-theme-${themeName}-ripple-rgb), 0.2)`,
boxShadowColor: `rgba(var(--app-theme-${themeName}-shadow-rgb), 0.2`
})
});
}
return returnValue;
}
};
var AppThemeColorsModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.backgroundColor !== void 0 || populateDefaults === true) {
this.backgroundColor = config?.backgroundColor || "rgba(var(--app-theme-light-background-rgb), 1)";
}
if (config?.textColor !== void 0 || populateDefaults === true) {
this.textColor = config?.textColor || "rgba(var(--app-theme-light-text-rgb), 1)";
}
if (config?.iconColor !== void 0 || populateDefaults === true) {
this.iconColor = config?.iconColor || this.textColor;
}
if (config?.borderColor !== void 0 || populateDefaults === true) {
this.borderColor = config?.borderColor || "rgba(var(--app-theme-light-border-rgb), 1)";
}
if (config?.rippleColor !== void 0 || populateDefaults === true) {
this.rippleColor = config?.rippleColor || "rgba(var(--app-theme-light-ripple-rgb), 1)";
}
if (config?.boxShadowColor !== void 0 || populateDefaults === true) {
this.boxShadowColor = "rgba(var(--app-theme-light-shadow-rgb), 1)";
}
}
};
var AppStyleModel = class {
// Angular constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.reveal !== void 0 || populateDefaults === true) {
this.reveal = config?.reveal ?? true;
}
if (config?.order !== void 0 || populateDefaults === true) {
this.order = config?.order || "0";
}
if (config?.cursor !== void 0 || populateDefaults === true) {
this.cursor = config?.cursor || "auto";
}
if (config?.visibility !== void 0 || populateDefaults === true) {
this.visibility = config?.visibility || "visible";
}
if (config?.transition !== void 0 || populateDefaults === true) {
this.transition = config?.transition || "none";
}
if (config?.transform !== void 0 || populateDefaults === true) {
this.transform = config?.transform || "none";
}
if (config?.display !== void 0 || populateDefaults === true) {
this.display = config?.display || "block";
}
if (config?.overflow !== void 0 || populateDefaults === true) {
this.overflow = config?.overflow || "visible";
}
if (config?.overflowX !== void 0 || populateDefaults === true) {
this.overflowX = config?.overflowX || "visible";
}
if (config?.overflowY !== void 0 || populateDefaults === true) {
this.overflowY = config?.overflowY || "visible";
}
if (config?.opacity !== void 0 || populateDefaults === true) {
this.opacity = config?.opacity || "1";
}
if (config?.zIndex !== void 0 || populateDefaults === true) {
this.zIndex = config?.zIndex || "var(--app-z-index)";
}
if (config?.position !== void 0 || populateDefaults === true) {
this.position = config?.position || "static";
}
if (config?.top !== void 0 || populateDefaults === true) {
this.top = config?.top || "0px";
}
if (config?.right !== void 0 || populateDefaults === true) {
this.right = config?.right || "0px";
}
if (config?.bottom !== void 0 || populateDefaults === true) {
this.bottom = config?.bottom || "0px";
}
if (config?.left !== void 0 || populateDefaults === true) {
this.left = config?.left || "0px";
}
if (config?.gap !== void 0 || populateDefaults === true) {
this.gap = config?.gap || "0px";
}
if (config?.margin !== void 0 || populateDefaults === true) {
this.margin = new MarginStyleModel(config?.margin);
}
if (config?.padding !== void 0 || populateDefaults === true) {
this.padding = new PaddingStyleModel(config?.padding);
}
if (config?.backgroundColor !== void 0 || populateDefaults === true) {
this.backgroundColor = config?.backgroundColor || theme?.colors?.backgroundColor;
}
if (config?.rippleColor !== void 0 || populateDefaults === true) {
this.rippleColor = config?.rippleColor || theme?.colors?.rippleColor;
}
if (config?.alignContent !== void 0 || populateDefaults === true) {
this.alignContent = config?.alignContent || "normal";
}
if (config?.flex !== void 0 || populateDefaults === true) {
this.flex = new FlexStyleModel(config?.flex);
}
if (config?.grid !== void 0 || populateDefaults === true) {
this.grid = new GridStyleModel(config?.grid);
}
if (config?.border !== void 0 || populateDefaults === true) {
this.border = new BorderStyleModel({ ...config?.border, theme });
}
if (config?.boxShadow !== void 0 || populateDefaults === true) {
this.boxShadow = {
general: config?.boxShadow?.general || new BoxShadowStyleModel({ ...config?.boxShadow?.general, theme }),
hover: config?.boxShadow?.hover || new BoxShadowStyleModel({ ...config?.boxShadow?.hover, theme }),
active: config?.boxShadow?.active || new BoxShadowStyleModel({ ...config?.boxShadow?.active, theme })
};
}
if (config?.text !== void 0 || populateDefaults === true) {
this.text = new TextStyleModel({ ...config?.text, theme });
}
if (config?.size !== void 0 || populateDefaults === true) {
this.size = new SizeStyleModel(config?.size);
}
if (config?.outline !== void 0 || populateDefaults === true) {
this.outline = new OutlineStyleModel({ ...config?.outline, theme });
}
}
};
var MarginStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.top !== void 0 || populateDefaults === true) {
this.top = config?.top || "0px";
}
if (config?.right !== void 0 || populateDefaults === true) {
this.right = config?.right || "0px";
}
if (config?.bottom !== void 0 || populateDefaults === true) {
this.bottom = config?.bottom || "0px";
}
if (config?.left !== void 0 || populateDefaults === true) {
this.left = config?.left || "0px";
}
}
};
var PaddingStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.top !== void 0 || populateDefaults === true) {
this.top = config?.top || "0px";
}
if (config?.right !== void 0 || populateDefaults === true) {
this.right = config?.right || "0px";
}
if (config?.bottom !== void 0 || populateDefaults === true) {
this.bottom = config?.bottom || "0px";
}
if (config?.left !== void 0 || populateDefaults === true) {
this.left = config?.left || "0px";
}
}
};
var FlexStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.flexDirection !== void 0 || populateDefaults === true) {
this.flexDirection = config?.flexDirection || void 0;
}
if (config?.flexWrap !== void 0 || populateDefaults === true) {
this.flexWrap = config?.flexWrap || void 0;
}
if (config?.justifyContent !== void 0 || populateDefaults === true) {
this.justifyContent = config?.justifyContent || void 0;
}
if (config?.alignItems !== void 0 || populateDefaults === true) {
this.alignItems = config?.alignItems || void 0;
}
}
};
var GridStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.gridTemplateColumns !== void 0 || populateDefaults === true) {
this.gridTemplateColumns = config?.gridTemplateColumns || void 0;
}
if (config?.gridTemplateRows !== void 0 || populateDefaults === true) {
this.gridTemplateRows = config?.gridTemplateRows || void 0;
}
if (config?.gridArea !== void 0 || populateDefaults === true) {
this.gridArea = config?.gridArea || void 0;
}
if (config?.gridColumn !== void 0 || populateDefaults === true) {
this.gridColumn = config?.gridColumn || void 0;
}
if (config?.gridRow !== void 0 || populateDefaults === true) {
this.gridRow = config?.gridRow || void 0;
}
if (config?.justifyItems !== void 0 || populateDefaults === true) {
this.justifyItems = config?.justifyItems || void 0;
}
if (config?.alignItems !== void 0 || populateDefaults === true) {
this.alignItems = config?.alignItems || void 0;
}
}
};
var BorderStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.color !== void 0 || populateDefaults === true) {
this.color = config?.color || theme?.colors?.borderColor;
}
if (config?.style !== void 0 || populateDefaults === true) {
this.style = config?.style || "solid";
}
if (config?.width !== void 0 || populateDefaults === true) {
this.width = config?.width || "1px";
}
if (config?.radius !== void 0 || populateDefaults === true) {
this.radius = config?.radius || "0px";
}
}
};
var BoxShadowsStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.general !== void 0 || populateDefaults === true) {
this.general = config?.general || new BoxShadowStyleModel({
...config?.general,
color: config?.general?.color || theme?.colors?.boxShadowColor
});
}
if (config?.hover !== void 0 || populateDefaults === true) {
this.hover = config?.hover || new BoxShadowStyleModel({
...config?.hover,
color: config?.hover?.color || theme?.colors?.boxShadowColor
});
}
if (config?.active !== void 0 || populateDefaults === true) {
this.active = config?.active || new BoxShadowStyleModel({
...config?.active,
color: config?.active?.color || theme?.colors?.boxShadowColor
});
}
}
};
var BoxShadowStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.hOffset !== void 0 || populateDefaults === true) {
this.hOffset = config?.hOffset || "0";
}
if (config?.vOffset !== void 0 || populateDefaults === true) {
this.vOffset = config?.vOffset || "0";
}
if (config?.blur !== void 0 || populateDefaults === true) {
this.blur = config?.blur || "0";
}
if (config?.spread !== void 0 || populateDefaults === true) {
this.spread = config?.spread || "0";
}
if (config?.color !== void 0 || populateDefaults === true) {
this.color = config?.color || theme?.colors?.boxShadowColor;
}
}
};
var TextStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.reveal !== void 0 || populateDefaults === true) {
this.reveal = config?.reveal ?? false;
}
if (config?.caption !== void 0 || populateDefaults === true) {
this.caption = config?.caption || "";
}
if (config?.align !== void 0 || populateDefaults === true) {
this.align = config?.align || "left";
}
if (config?.color !== void 0 || populateDefaults === true) {
this.color = config?.color || theme?.colors?.textColor;
}
if (config?.whiteSpace !== void 0 || populateDefaults === true) {
this.whiteSpace = config?.whiteSpace || "normal";
}
if (config?.overflow !== void 0 || populateDefaults === true) {
this.overflow = config?.overflow || "visible";
}
if (config?.lineClamp !== void 0 || populateDefaults === true) {
this.lineClamp = config?.lineClamp || "none";
}
if (config?.boxOrient !== void 0 || populateDefaults === true) {
this.boxOrient = config?.boxOrient || "horizontal";
}
if (config?.textDecoration !== void 0 || populateDefaults === true) {
this.textDecoration = config?.textDecoration || "none";
}
if (config?.letterSpacing !== void 0 || populateDefaults === true) {
this.letterSpacing = config?.letterSpacing || void 0;
}
if (config?.lineHeight !== void 0 || populateDefaults === true) {
this.lineHeight = config?.lineHeight || void 0;
}
if (config?.font !== void 0 || populateDefaults === true) {
this.font = new FontStyleModel(config?.font);
}
}
};
var IconStyleModel = class extends TextStyleModel {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
super(config);
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.type !== void 0 || populateDefaults === true) {
this.type = config?.type || "mat";
}
if (config?.name !== void 0 || populateDefaults === true) {
this.name = {
text: config?.name?.text || "help",
reveal: config?.name?.reveal || false
};
if (this.type === "span") {
const dynamicSize = configDynamicSize(config?.font?.size);
this.width = config?.width || dynamicSize?.width;
this.height = config?.height || dynamicSize?.height;
}
}
}
};
var SizeStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.minWidth !== void 0 || populateDefaults === true) {
this.minWidth = config?.minWidth || "0px";
}
if (config?.width !== void 0 || populateDefaults === true) {
this.width = config?.width || "auto";
}
if (config?.maxWidth !== void 0 || populateDefaults === true) {
this.maxWidth = config?.maxWidth || "100%";
}
if (config?.minHeight !== void 0 || populateDefaults === true) {
this.minHeight = config?.minHeight || "0px";
}
if (config?.height !== void 0 || populateDefaults === true) {
this.height = config?.height || "auto";
}
if (config?.maxHeight !== void 0 || populateDefaults === true) {
this.maxHeight = config?.maxHeight || "100%";
}
}
};
var configDynamicSize = (value) => {
if (value === void 0 || value === void 0) return void 0;
const [width, height] = value.includes(" ") ? value.split(" ") : [value, value];
return { width, height };
};
var FontStyleModel = class {
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
if (config?.family !== void 0 || populateDefaults === true) {
this.family = config?.family || "var(--app-theme-font-family)";
}
if (config?.size !== void 0 || populateDefaults === true) {
this.size = config?.size || "var(--app-theme-font-size)";
}
if (config?.weight !== void 0 || populateDefaults === true) {
this.weight = config?.weight || "normal";
}
if (config?.lineHeight !== void 0 || populateDefaults === true) {
this.lineHeight = config?.lineHeight || `calc(${this.size} * 1.5)`;
}
}
};
var OutlineStyleModel = class {
// FUNCTION: ANGULAR > constructor
constructor(config, populateDefaultVariablesForAllFields) {
const populateDefaults = populateDefaultVariablesForAllFields ?? true;
const theme = AppThemeModel.configAppTheme(config?.theme);
if (config?.theme !== void 0 || populateDefaults === true) {
this.theme = theme;
}
if (config?.color !== void 0 || populateDefaults === true) {
this.color = config?.color || theme?.colors?.borderColor;
}
if (config?.style !== void 0 || populateDefaults === true) {
this.style = config?.style || "solid";
}
if (config?.width !== void 0 || populateDefaults === true) {
this.width = config?.width || "0px";
}
}
};
// src/index.ts
var DEFAULT_CSS_VAR_PREFIX = "--app-theme-";
var THEME_ATTR = "data-theme";
var docEl = typeof document !== "undefined" ? document.documentElement : void 0;
function applyCssVars(vars, opts = {}) {
const target = opts.target ?? docEl;
if (!target) return;
for (const [k, v] of Object.entries(vars)) target.style.setProperty(k, v);
}
function setTheme(theme, opts = {}) {
const target = opts.target ?? docEl;
if (!target) return;
const attr = opts.attr ?? THEME_ATTR;
target.setAttribute(attr, String(theme));
}
function getTheme(opts = {}) {
const target = opts.target ?? docEl;
if (!target) return void 0;
const attr = opts.attr ?? THEME_ATTR;
return target.getAttribute(attr);
}
export {
AlignContentDataTypeList,
AppStyleModel,
AppThemeColorsModel,
AppThemeModel,
AppThemeNamesList,
BorderStyleDataTypeList,
BorderStyleModel,
BoxShadowStyleModel,
BoxShadowsStyleModel,
CursorDataTypeList,
DEFAULT_CSS_VAR_PREFIX,
DisplayDataTypeList,
FlexAlignItemsDataTypeList,
FlexDirectionDataTypeList,
FlexStyleModel,
FlexWrapDataTypeList,
FontStyleModel,
GridAlignItemsDataTypeList,
GridStyleModel,
IconStyleModel,
IconTypeDataTypeList,
JustifyContentDataTypeList,
JustifyItemsDataTypeList,
MarginStyleModel,
OutlineStyleDataTypeList,
OutlineStyleModel,
OverflowDataTypeList,
PaddingStyleModel,
PositionDataTypeList,
SizeStyleModel,
THEME_ATTR,
TextAlignDataTypeList,
TextBoxOrientDataTypeList,
TextDecorationDataTypeList,
TextOverflowDataTypeList,
TextStyleModel,
TextWeightDataTypeList,
TextWhiteSpaceDataTypeList,
VisibilityDataTypeList,
applyCssVars,
configDynamicSize,
getTheme,
setTheme
};
//# sourceMappingURL=index.js.map