Skip to content

Commit

Permalink
feat: Add theme.styleManager support typescript generics
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Feb 5, 2018
1 parent a52f038 commit a5053ee
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 23 deletions.
46 changes: 24 additions & 22 deletions src/styles/StyleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ export interface StyleClasses {
className?: string;
}

export interface Sheet {
CSSText?: string;
className?: string;
classNameWithHash?: string;
id?: number;
}

export const extendsStyleKeys: any = {
"&:hover": true,
"&:active": true,
"&:focus": true,
"&:disabled": true
};

export class StyleManager {
let StyleManager: ReactUWP.StyleManager;
StyleManager = class {
globalClassName: string;
theme: ReactUWP.ThemeType;
themeId = 0;
themeId: number = 0;
styleElement: HTMLStyleElement = null;
sheets: {
[key: string]: {
CSSText?: string;
className?: string;
classNameWithHash?: string;
id?: number;
}
[key: string]: Sheet
} = {};
styleDidUpdate: () => void;
CSSText = "";
CSSText: string = "";
addedCSSText: {
[key: string]: boolean;
} = {};
Expand All @@ -57,12 +59,12 @@ export class StyleManager {
this.setupStyleElement();
}

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

setupStyleElement = () => {
setupStyleElement = (): void => {
if (IS_NODE_ENV) return;
if (!this.styleElement) {
const name = `data-uwp-jss-${this.themeId}`;
Expand All @@ -72,21 +74,21 @@ export class StyleManager {
}
}

cleanStyleSheet = () => {
cleanStyleSheet = (): void => {
if (this.styleElement) document.head.removeChild(this.styleElement);
this.theme = null;
this.sheets = {};
this.CSSText = "";
this.styleElement = null;
}

style2CSSText = (style: React.CSSProperties) => style ? Object.keys(style).map(key => (
style2CSSText = (style: React.CSSProperties): string => style ? Object.keys(style).map(key => (
` ${replace2Dashes(key)}: ${getStyleValue(key, style[key])};`
)).join("\n") : void 0

sheetsToString = () => `\n${Object.keys(this.sheets).map(id => this.sheets[id].CSSText).join("")}`;
sheetsToString = (): string => `\n${Object.keys(this.sheets).map(id => this.sheets[id].CSSText).join("")}`;

addStyle = (style: CustomCSSProperties, className = "", callback = () => {}) => {
addStyle = (style: CustomCSSProperties, className = "", callback = () => {}): Sheet => {
const id = createHash(`${this.themeId}: ${JSON.stringify(style)}`);
if (this.sheets[id]) return this.sheets[id];

Expand Down Expand Up @@ -117,11 +119,11 @@ export class StyleManager {
return this.sheets[id];
}

addStyleWithUpdate = (style: CustomCSSProperties, className = "") => {
addStyleWithUpdate = (style: CustomCSSProperties, className = ""): Sheet => {
return this.addStyle(style, className, this.renderSheets);
}

addCSSText = (CSSText: string, callback: (shouldUpdate?: boolean) => void = () => {}) => {
addCSSText = (CSSText: string, callback: (shouldUpdate?: boolean) => void = () => {}): void => {
const hash = createHash(CSSText);
const shouldUpdate = !this.addedCSSText[hash];
if (shouldUpdate) {
Expand All @@ -131,7 +133,7 @@ export class StyleManager {
callback(shouldUpdate);
}

addCSSTextWithUpdate = (CSSText: string) => {
addCSSTextWithUpdate = (CSSText: string): void => {
this.addCSSText(CSSText, shouldUpdate => {
if (this.styleElement && shouldUpdate) {
this.updateStyleElement(this.styleElement.textContent += CSSText);
Expand Down Expand Up @@ -202,19 +204,19 @@ export class StyleManager {
return newStyles;
}

renderSheets = () => {
renderSheets = (): void => {
let textContent = this.sheetsToString();
textContent += this.CSSText;
this.updateStyleElement(textContent);
}

updateStyleElement = (textContent: string) => {
updateStyleElement = (textContent: string): void => {
const name = `data-uwp-jss-${this.themeId}`;
if (this.styleElement) {
this.styleElement.textContent = textContent;
this.styleDidUpdate();
}
}
}
} as any;

export default StyleManager;
62 changes: 61 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,73 @@ export interface AcrylicTexture {
background?: string;
}

export interface Sheet {
CSSText?: string;
className?: string;
classNameWithHash?: string;
id?: number;
}

export class StyleManager {
globalClassName?: string;
theme?: ReactUWP.ThemeType;
themeId?: number;
styleElement?: HTMLStyleElement;
sheets?: {
[key: string]: Sheet
};
styleDidUpdate?: () => void;
CSSText?: string;
addedCSSText?: {
[key: string]: boolean;
};

constructor(config: {
theme?: ReactUWP.ThemeType;
globalClassName?: string;
styleDidUpdate?: () => void;
})

setupTheme(theme?: ThemeType): void;

setupStyleElement(): void;

cleanStyleSheet(): void;

style2CSSText(style: React.CSSProperties): string;

sheetsToString(): string;

addStyle(style: CustomCSSProperties, className?: string, callback?: () => void): Sheet;

addStyleWithUpdate(style: CustomCSSProperties, className?: string): Sheet;

addCSSText(CSSText: string, callback?: (shouldUpdate?: boolean) => void ): void;

addCSSTextWithUpdate(CSSText: string): void;

setStyleToManager(config?: {
style?: CustomCSSProperties;
className?: string;
}, callback?: (theme?: ReactUWP.ThemeType) => StyleClasses): StyleClasses;

setStylesToManager<T>(config?: {
styles: T;
className?: string;
}, callback?: (theme?: ReactUWP.ThemeType) => { [P in keyof T]: StyleClasses }): { [P in keyof T]: StyleClasses };

renderSheets(): void;

updateStyleElement(textContent: string): void;
}

export interface ThemeType {
themeName?: "dark" | "light";
fonts?: {
sansSerifFonts?: string;
segoeMDL2Assets?: string;
};
styleManager?: any;
styleManager?: StyleManager;
scrollReveals?: ScrollRevealType[];
scrollRevealListener?: (e?: Event) => void;

Expand Down

0 comments on commit a5053ee

Please sign in to comment.