Skip to content

Commit

Permalink
feat(): add dot for dynamic classname
Browse files Browse the repository at this point in the history
  • Loading branch information
Enlcxx committed Apr 24, 2022
1 parent aee484c commit d89140c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/lib/src/theme/core-theme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class CoreTheme {
private _themeMap = new Map<string, ThemeVariables>();
private _styleMap = new Map<string, Map<string, DataStyle>>();
private _document: Document;
static classNamePrefix?: string;
constructor(
private rendererFactory: RendererFactory2,
@Inject(DOCUMENT) _document: any,
Expand All @@ -28,7 +29,7 @@ export class CoreTheme {
this._document = _document;

if (!isDevMode() && _classNamePrefix) {
this.classNamePrefix = _classNamePrefix;
CoreTheme.classNamePrefix = this.classNamePrefix = _classNamePrefix;
}

if (platform.isBrowser) {
Expand Down
23 changes: 22 additions & 1 deletion src/lib/src/theme/theme2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { StyleMap5,
import { Subject } from 'rxjs';
import { StyleTemplate, StringIdGenerator } from '../parse';
import { Platform } from '@angular/cdk/platform';
import { memoize } from '../minimal/memoize';

const REF_REG_EXP = /\{([\w-]+)\}/g;

Expand Down Expand Up @@ -547,11 +548,15 @@ function groupStyleToString(
// set new id if not exist
if (!(key in classesMap)) {
classesMap[key] = isDevMode()
? `${toValidClassName(name + key)}-${createNextClassId(classNamePrefix)}`
? (key === 'root'
? `${toValidClassName(name)}${createNextClassId(classNamePrefix)}`
: `${toValidClassName(name + key)}-${createNextClassId(classNamePrefix)}`)
: createNextClassId(classNamePrefix);
}

} else if (typeof value === 'object' || value === null) {
// TODO: @deprecated
// remove this in the future
// set new id if not exist
if (!(key in classesMap)) {
classesMap[key] = isDevMode()
Expand Down Expand Up @@ -813,6 +818,22 @@ function createNextClassId(classNamePrefix?: string) {
? `${classNamePrefix}${yClassID.next()}`
: yClassID.next();
}

/**
* Create next class id from key
* @private
*/
export const ck = memoize((_key: string) => {
return createNextClassId(CoreTheme.classNamePrefix);
});

/**
* Create next selector id from key
*/
export const dot = memoize((key: string) => {
return `.${ck(key)}`;
});

function createNextKeyframeId() {
return `k${(nextKeyFrameId++).toString(36)}`;
}
Expand Down

0 comments on commit d89140c

Please sign in to comment.