UNPKG

ng-zorro-antd

Version:

An enterprise-class UI components based on Ant Design and Angular

183 lines (182 loc) • 7.32 kB
import * as i0 from "@angular/core"; import { CSP_NONCE, DestroyRef, Injectable, InjectionToken, afterNextRender, assertInInjectionContext, computed, inject, makeEnvironmentProviders, signal } from "@angular/core"; import { SIGNAL } from "@angular/core/primitives/signals"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { Subject } from "rxjs"; import { filter, map } from "rxjs/operators"; import { TinyColor } from "@ctrl/tinycolor"; import { generate } from "ng-zorro-antd/core/color"; import { warn } from "ng-zorro-antd/core/logger"; import { canUseDom, updateCSS } from "ng-zorro-antd/core/util"; const NZ_CONFIG = new InjectionToken(typeof ngDevMode !== "undefined" && ngDevMode ? "nz-config" : ""); function provideNzConfig(config) { return makeEnvironmentProviders([{ provide: NZ_CONFIG, useValue: config }]); } const dynamicStyleMark = `-ant-${Date.now()}-${Math.random()}`; function getStyle(globalPrefixCls, theme) { const variables = {}; const formatColor = (color, updater) => { let clone = color.clone(); clone = updater?.(clone) || clone; return clone.toRgbString(); }; const fillColor = (colorVal, type) => { const baseColor = new TinyColor(colorVal); const colorPalettes = generate(baseColor.toRgbString()); variables[`${type}-color`] = formatColor(baseColor); variables[`${type}-color-disabled`] = colorPalettes[1]; variables[`${type}-color-hover`] = colorPalettes[4]; variables[`${type}-color-active`] = colorPalettes[7]; variables[`${type}-color-outline`] = baseColor.clone().setAlpha(.2).toRgbString(); variables[`${type}-color-deprecated-bg`] = colorPalettes[1]; variables[`${type}-color-deprecated-border`] = colorPalettes[3]; }; if (theme.primaryColor) { fillColor(theme.primaryColor, "primary"); const primaryColor = new TinyColor(theme.primaryColor); const primaryColors = generate(primaryColor.toRgbString()); primaryColors.forEach((color, index) => { variables[`primary-${index + 1}`] = color; }); variables["primary-color-deprecated-l-35"] = formatColor(primaryColor, (c) => c.lighten(35)); variables["primary-color-deprecated-l-20"] = formatColor(primaryColor, (c) => c.lighten(20)); variables["primary-color-deprecated-t-20"] = formatColor(primaryColor, (c) => c.tint(20)); variables["primary-color-deprecated-t-50"] = formatColor(primaryColor, (c) => c.tint(50)); variables["primary-color-deprecated-f-12"] = formatColor(primaryColor, (c) => c.setAlpha(c.getAlpha() * .12)); const primaryActiveColor = new TinyColor(primaryColors[0]); variables["primary-color-active-deprecated-f-30"] = formatColor(primaryActiveColor, (c) => c.setAlpha(c.getAlpha() * .3)); variables["primary-color-active-deprecated-d-02"] = formatColor(primaryActiveColor, (c) => c.darken(2)); } if (theme.successColor) fillColor(theme.successColor, "success"); if (theme.warningColor) fillColor(theme.warningColor, "warning"); if (theme.errorColor) fillColor(theme.errorColor, "error"); if (theme.infoColor) fillColor(theme.infoColor, "info"); return ` :root { ${Object.keys(variables).map((key) => `--${globalPrefixCls}-${key}: ${variables[key]};`).join("\n")} } `.trim(); } function registerTheme(globalPrefixCls, theme, cspNonce) { const style = getStyle(globalPrefixCls, theme); if (canUseDom()) updateCSS(style, `${dynamicStyleMark}-dynamic-theme`, { cspNonce }); else warn(`NzConfigService: SSR do not support dynamic theme with css variables.`); } function isDefined(value) { return value !== void 0; } const defaultPrefixCls = "ant"; var NzConfigService = class NzConfigService { configUpdated$ = new Subject(); _configMap = /* @__PURE__ */ new Map(); config = inject(NZ_CONFIG, { optional: true }) || {}; cspNonce = inject(CSP_NONCE, { optional: true }); constructor() { if (this.config.theme) registerTheme(this.getConfig().prefixCls?.prefixCls || defaultPrefixCls, this.config.theme, this.cspNonce); } _getConfigValue(componentName) { let configValue = this._configMap.get(componentName); if (configValue) return configValue; configValue = signal(this.config[componentName]); this._configMap.set(componentName, configValue); return configValue; } getConfig() { return this.config; } getConfigForComponent(componentName) { return this.config[componentName]; } getConfigChangeEventForComponent(componentName) { return this.configUpdated$.pipe(filter((n) => n === componentName), map(() => void 0)); } set(componentName, value) { this.config[componentName] = { ...this.config[componentName], ...value }; this._configMap.get(componentName)?.set(this.config[componentName]); if (componentName === "theme" && this.config.theme) registerTheme(this.getConfig().prefixCls?.prefixCls || defaultPrefixCls, this.config.theme, this.cspNonce); this.configUpdated$.next(componentName); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzConfigService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzConfigService, providedIn: "root" }); }; i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.2", ngImport: i0, type: NzConfigService, decorators: [{ type: Injectable, args: [{ providedIn: "root" }] }], ctorParameters: () => [] }); function onConfigChangeEventForComponent(componentName, callback) { if (typeof ngDevMode !== "undefined" && ngDevMode) assertInInjectionContext(onConfigChangeEventForComponent); const destroyRef = inject(DestroyRef); const nzConfigService = inject(NzConfigService); let subscription = null; const ref = afterNextRender(() => { subscription = nzConfigService.getConfigChangeEventForComponent(componentName).pipe(takeUntilDestroyed(destroyRef)).subscribe(callback); }); return () => { ref.destroy(); subscription?.unsubscribe(); }; } function WithConfig() { return function(_value, context) { context.addInitializer(function() { const nzConfigService = inject(NzConfigService); const originalValue = this[context.name]; let value; let assignedByUser = false; Object.defineProperty(this, context.name, { get: () => { const configValue = nzConfigService.getConfigForComponent(this["_nzModuleName"])?.[context.name]; if (assignedByUser) return value; if (isDefined(configValue)) return configValue; return originalValue; }, set: (newValue) => { assignedByUser = isDefined(newValue); value = newValue; }, enumerable: true, configurable: true }); }); }; } function withConfigFactory(componentName) { return (name, inputSignal, defaultValue) => { const configValueSignal = inject(NzConfigService)["_getConfigValue"](componentName); return computed(() => { const configValue = configValueSignal()?.[name]; const inputValue = inputSignal(); if (inputSignal[SIGNAL].version > 0 && isDefined(inputValue)) return inputValue; if (isDefined(configValue)) return configValue; return defaultValue; }); }; } export { NZ_CONFIG, NzConfigService, WithConfig, getStyle, onConfigChangeEventForComponent, provideNzConfig, registerTheme, withConfigFactory }; //# sourceMappingURL=ng-zorro-antd-core-config.mjs.map