Skip to content

Commit

Permalink
feat: StyleManager supprot render CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Aug 8, 2017
1 parent 40d3a45 commit 70c0380
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/styles/StyleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const getStyleValue = (key: string, value: string) => ((typeof value === "number
export class StyleManager {
theme: ReactUWP.ThemeType;
themeId: number;
sheets: any[] = [];
styleElement: HTMLStyleElement = null;
sheets: any = {};

constructor(theme: ReactUWP.ThemeType) {
this.setupTheme(theme);
this.addSheet({}, "");
this.addSheet({}, "text");
this.renderToDOM();
}

setupTheme = (theme: ReactUWP.ThemeType) => {
Expand All @@ -32,15 +34,29 @@ export class StyleManager {
lineHeight: 2
};
let CSSText = Object.keys(style).map(key => ` ${replace2Dashes(key)}: ${getStyleValue(key, style[key])};`).join("\n");
const id = createHash(CSSText);
const id = createHash(`.${className} {\n${CSSText}\n}`);
CSSText = `.${className}-${id} {\n${CSSText}\n}`;
this.sheets[id] = { CSSText };
}

updateSheetByID = () => {};

updateAllSheets = () => {};

removeSheetByID = () => {};

renderToDOM = () => {
this.styleElement = document.querySelector("[data-uwp-jss]") as HTMLStyleElement;
const textContent = `\n${Object.keys(this.sheets).map(id => this.sheets[id].CSSText).join("\n")}\n`;
if (!this.styleElement) {
this.styleElement = document.createElement("style");
this.styleElement.setAttribute("data-uwp-jss", "");
this.styleElement.textContent = textContent;
document.head.appendChild(this.styleElement);
} else {
this.styleElement.textContent = textContent;
}
}
}

export default StyleManager;

0 comments on commit 70c0380

Please sign in to comment.