Skip to content

Commit

Permalink
feat: Update StyleManager support add CSSText
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Aug 8, 2017
1 parent 040c2a9 commit 8149025
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/styles/StyleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export class StyleManager {
styleNode: HTMLStyleElement;
sheetsDidUpdate: () => void;
CSSText = "";
addedCSSText: {
[key: string]: boolean;
} = {};

constructor(config: {
theme: ReactUWP.ThemeType;
Expand All @@ -37,13 +40,23 @@ export class StyleManager {
this.sheetsDidUpdate = sheetsDidUpdate || (() => {});
this.globalClassName = globalClassName ? `${globalClassName}-` : "";
this.setupTheme(theme);
this.initStyleElement();
}

setupTheme = (theme: ReactUWP.ThemeType) => {
this.theme = theme;
this.themeId = createHash([theme.accent, theme.themeName, theme.useFluentDesign].join(", "));
}

initStyleElement = () => {
const name = `data-uwp-jss-${this.themeId}`;
if (!this.styleElement) {
this.styleElement = document.createElement("style");
this.styleElement.setAttribute(name, "");
document.head.appendChild(this.styleElement);
}
}

cleanStyleSheet = () => {
if (this.styleElement) document.head.removeChild(this.styleElement);
this.theme = null;
Expand Down Expand Up @@ -93,13 +106,21 @@ export class StyleManager {
return this.addSheet(style, className, this.updateSheetsToDOM);
}

addCSSText = (CSSText: string) => this.CSSText += CSSText;
addCSSText = (CSSText: string, callback = () => {}) => {
const hash = createHash(CSSText);
if (!this.addedCSSText[hash]) {
this.addedCSSText[hash] = true;
this.CSSText += CSSText;
callback();
}
}

addCSSTextWithUpdate = (CSSText: string) => {
this.addCSSText(CSSText);
if (this.styleElement) {
this.updateStyleTextContent(this.styleElement.textContent += CSSText);
}
this.addCSSText(CSSText, () => {
if (this.styleElement) {
this.updateStyleTextContent(this.styleElement.textContent += CSSText);
}
});
}

removeSheetByID = () => {};
Expand All @@ -112,15 +133,10 @@ export class StyleManager {

updateStyleTextContent = (textContent: string) => {
const name = `data-uwp-jss-${this.themeId}`;
if (!this.styleElement) {
this.styleElement = document.createElement("style");
this.styleElement.setAttribute(name, "");
this.styleElement.textContent = textContent;
document.head.appendChild(this.styleElement);
} else {
if (this.styleElement) {
this.styleElement.textContent = textContent;
this.sheetsDidUpdate();
}
this.sheetsDidUpdate();
}
}

Expand Down

0 comments on commit 8149025

Please sign in to comment.