Skip to content

Commit

Permalink
perf(theme): now addStyle works with CLASSES_MAP
Browse files Browse the repository at this point in the history
  • Loading branch information
alyleui committed Aug 16, 2018
1 parent b1f62e6 commit a77a7e2
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/lib/src/theme/theme2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface StylesElementMap {
interface StyleMap03 {
[id: string]: { // example: lyTabs
styles: StylesFn2<any> | Styles2,
media?: string,
themes: { // example: minima-dark
/** Css */
default?: string,
Expand Down Expand Up @@ -94,7 +95,7 @@ export class LyTheme2 {
* @param instance The instance of this, this replaces the existing style with a new one when it changes
*/
addStyle<T>(id: string, style: Style<T>, el?: any, instance?: string) {
const newClass = this.setUpStyle<T>(id, style);
const newClass = this.addCss(id, style as any);
if (instance) {
el.classList.remove(instance);
}
Expand All @@ -121,14 +122,26 @@ export class LyTheme2 {
// });
for (const key in STYLE_MAP_03) {
if (STYLE_MAP_03.hasOwnProperty(key)) {
const { styles } = STYLE_MAP_03[key];
this._createStyleContent2(styles, key, true);
const { styles, media } = STYLE_MAP_03[key];
this._createStyleContent2(styles, key, true, media);
}
}
this._styleMap.forEach((dataStyle) => {
dataStyle.styleElement.innerText = this.core._createStyleContent(this.config, dataStyle.style, dataStyle.id);
});
}

/**
* add style, similar to setUpStyle but this only accept string
* @param id id of style
* @param css style in string
*/
private addCss(id: string, css: ((t) => string) | string, media?: string): string {
const newId = `~>${id}`;
this._createStyleContent2(css as any, newId, false, media);
return CLASSES_MAP[newId];
}

/**
* Add new add a new style sheet
* @param styles styles
Expand All @@ -141,21 +154,21 @@ export class LyTheme2 {
return CLASSES_MAP[newId];
}

_createStyleContent2<T>(styles: StylesFn2<T> | Styles2, id: string, forChangeTheme?: boolean) {
_createStyleContent2<T>(styles: StylesFn2<T> | Styles2, id: string, forChangeTheme?: boolean, media?: string) {
const styleMap = id in STYLE_MAP_03
? STYLE_MAP_03[id]
: STYLE_MAP_03[id] = {
styles,
media,
themes: {} as any
};

if (!(styleMap.themes.default || this.config.name in styleMap.themes)) {
let css;
if (typeof styles === 'function') {
css = groupStyleToString(styles(this.config), id);
css = groupStyleToString(styles(this.config), id, media);
styleMap.themes[this.config.name] = css;
} else {
css = groupStyleToString(styles, id);
css = groupStyleToString(styles, id, media);
styleMap.themes.default = css;
}

Expand Down Expand Up @@ -189,9 +202,15 @@ export interface Styles2 {
}
export type StylesFn2<T> = (T) => Styles2;

function groupStyleToString(styles: Styles2, id: string) {
function groupStyleToString(styles: Styles2, id: string, media?: string) {
let content = '';
// let newKey = '';
// const string
if (typeof styles === 'string') {
CLASSES_MAP[id] = `e${nextId++}`;
const css = `.${CLASSES_MAP[id]}{${styles}}`;
return media ? toMedia(css, media) : css;
}
const classesMap = id in CLASSES_MAP
? CLASSES_MAP[id]
: CLASSES_MAP[id] = {};
Expand Down Expand Up @@ -282,3 +301,7 @@ export function capitalizeFirstLetter(str: string) {
return str[0].toUpperCase() + str.slice(1);
}

function toMedia(css: string, media: string) {
return `@media ${media}{${css}}`;
}

0 comments on commit a77a7e2

Please sign in to comment.