Skip to content

Commit fca7410

Browse files
committed
refactor: Update Theme component
1 parent 5460a2c commit fca7410

File tree

7 files changed

+183
-266
lines changed

7 files changed

+183
-266
lines changed

docs/src/components/Wrapper/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default class Wrapper extends React.Component<WrapperProps, WrapperState>
158158
</IconButton>,
159159
title: "Toggle Fluent Design",
160160
onClick: () => {
161-
theme.saveTheme(getTheme({
161+
theme.updateTheme(getTheme({
162162
themeName: theme.themeName,
163163
accent: theme.accent,
164164
useFluentDesign: !theme.useFluentDesign,
@@ -174,7 +174,7 @@ export default class Wrapper extends React.Component<WrapperProps, WrapperState>
174174
</IconButton>,
175175
title: "Toggle Theme",
176176
onClick: () => {
177-
theme.saveTheme(getTheme({
177+
theme.updateTheme(getTheme({
178178
themeName: theme.isDarkTheme ? "light" : "dark",
179179
accent: theme.accent,
180180
useFluentDesign: theme.useFluentDesign,

docs/src/routes/Home/components/CustomTheme.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class CustomTheme extends React.Component<CustomThemeProps> {
5656
background={theme.useFluentDesign ? theme.acrylicTexture40.background : theme.chromeLow}
5757
defaultValue={theme.isDarkTheme ? "Dark" : "Light"}
5858
onChangeValue={value => {
59-
theme.saveTheme(getTheme({
59+
theme.updateTheme(getTheme({
6060
themeName: value.toLowerCase() as any,
6161
accent: theme.accent,
6262
useFluentDesign: theme.useFluentDesign,
@@ -69,7 +69,7 @@ export default class CustomTheme extends React.Component<CustomThemeProps> {
6969
defaultChecked={theme.useFluentDesign}
7070
label="Use New Fluent Design"
7171
onCheck={useFluentDesign => {
72-
theme.saveTheme(getTheme({
72+
theme.updateTheme(getTheme({
7373
themeName: theme.themeName,
7474
accent: theme.accent,
7575
useFluentDesign,
@@ -84,7 +84,7 @@ export default class CustomTheme extends React.Component<CustomThemeProps> {
8484
onChangeValue={desktopBackgroundImage => {
8585
const image = new Image();
8686
image.addEventListener("load", function(e) {
87-
theme.saveTheme(getTheme({
87+
theme.updateTheme(getTheme({
8888
themeName: theme.themeName,
8989
accent: theme.accent,
9090
useFluentDesign: theme.useFluentDesign,
@@ -121,7 +121,7 @@ export default class CustomTheme extends React.Component<CustomThemeProps> {
121121
const file = e.currentTarget.files[0];
122122
const reader = new FileReader();
123123
reader.addEventListener("load", () => {
124-
theme.saveTheme(getTheme({
124+
theme.updateTheme(getTheme({
125125
themeName: theme.themeName,
126126
accent: theme.accent,
127127
useFluentDesign: theme.useFluentDesign,
@@ -141,7 +141,7 @@ export default class CustomTheme extends React.Component<CustomThemeProps> {
141141
style={{ margin: "10px 0" }}
142142
defaultColor={theme.accent}
143143
onChangedColor={accent => {
144-
theme.saveTheme(getTheme({
144+
theme.updateTheme(getTheme({
145145
themeName: theme.themeName,
146146
accent,
147147
useFluentDesign: theme.useFluentDesign,

docs/src/routes/Styles/CustomTheme/SimpleExample.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class CustomTheme extends React.Component<any> {
3434
background={theme.useFluentDesign ? theme.acrylicTexture80.background : theme.chromeLow}
3535
defaultValue={theme.isDarkTheme ? "Dark" : "Light"}
3636
onChangeValue={value => {
37-
theme.saveTheme(getTheme({
37+
theme.updateTheme(getTheme({
3838
themeName: value.toLowerCase() as any,
3939
accent: theme.accent,
4040
useFluentDesign: theme.useFluentDesign,
@@ -47,7 +47,7 @@ export default class CustomTheme extends React.Component<any> {
4747
defaultChecked={theme.useFluentDesign}
4848
label="Use New Fluent Design"
4949
onCheck={useFluentDesign => {
50-
theme.saveTheme(getTheme({
50+
theme.updateTheme(getTheme({
5151
themeName: theme.themeName,
5252
accent: theme.accent,
5353
useFluentDesign,
@@ -62,7 +62,7 @@ export default class CustomTheme extends React.Component<any> {
6262
onChangeValue={desktopBackgroundImage => {
6363
const image = new Image();
6464
image.addEventListener("load", function(e) {
65-
theme.saveTheme(getTheme({
65+
theme.updateTheme(getTheme({
6666
themeName: theme.themeName,
6767
accent: theme.accent,
6868
useFluentDesign: theme.useFluentDesign,
@@ -99,7 +99,7 @@ export default class CustomTheme extends React.Component<any> {
9999
const file = e.currentTarget.files[0];
100100
const reader = new FileReader();
101101
reader.addEventListener("load", () => {
102-
theme.saveTheme(getTheme({
102+
theme.updateTheme(getTheme({
103103
themeName: theme.themeName,
104104
accent: theme.accent,
105105
useFluentDesign: theme.useFluentDesign,
@@ -117,7 +117,7 @@ export default class CustomTheme extends React.Component<any> {
117117
style={{ margin: "10px 0" }}
118118
defaultColor={theme.accent}
119119
onChangedColor={accent => {
120-
theme.saveTheme(getTheme({
120+
theme.updateTheme(getTheme({
121121
themeName: theme.themeName,
122122
accent,
123123
useFluentDesign: theme.useFluentDesign,

docs/src/routes/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export class ThemeWrapper extends React.Component {
2222
return (
2323
<Theme
2424
theme={theme}
25-
autoSaveTheme
2625
needGenerateAcrylic={false}
2726
themeWillUpdate={setStaticAcrylicTexture}
2827
>

src/Theme/handleScrollReveal.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export function handleScrollReveal(theme: ReactUWP.ThemeType) {
2+
for (const scrollReveal of theme.scrollReveals) {
3+
const {
4+
rootElm,
5+
animated,
6+
setEnterStyle,
7+
setLeaveStyle,
8+
props: {
9+
topOffset,
10+
bottomOffset
11+
}
12+
} = scrollReveal;
13+
if (!rootElm) return;
14+
const { top, height } = rootElm.getBoundingClientRect();
15+
const { innerHeight } = window;
16+
17+
let isIn = false;
18+
if (height > innerHeight) {
19+
isIn = top < innerHeight - height * height && top > - height * 0.5;
20+
} else {
21+
isIn = top > 0 + topOffset && top + height + bottomOffset < innerHeight;
22+
}
23+
if (isIn) {
24+
if (!animated) {
25+
setEnterStyle();
26+
scrollReveal.animated = true;
27+
}
28+
} else {
29+
if (animated) {
30+
setLeaveStyle();
31+
scrollReveal.animated = false;
32+
}
33+
}
34+
}
35+
}

src/Theme/index.tsx

Lines changed: 15 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from "react";
22
import * as PropTypes from "prop-types";
33

4+
import { handleScrollReveal } from "./handleScrollReveal";
45
import StyleManager from "../styles/StyleManager";
56
import darkTheme from "../styles/darkTheme";
67
import getTheme, { Theme as ThemeType } from "../styles/getTheme";
@@ -16,10 +17,6 @@ export interface DataProps {
1617
* Set theme object. [ThemeType](https://github.com/myxvisual/react-uwp/blob/master/src/index.d.ts#L43), Usually use [getTheme](https://github.com/myxvisual/react-uwp/blob/master/src/styles/getTheme.ts#L28) function to get it.
1718
*/
1819
theme?: ThemeType;
19-
/**
20-
* For simple development, autoSaveTheme can read and save theme to `localStorage`. use global context `theme.saveTheme` method to save.
21-
*/
22-
autoSaveTheme?: boolean;
2320
/**
2421
* set theme will update callback.
2522
*/
@@ -44,7 +41,6 @@ export interface ThemeState {
4441
currTheme?: ThemeType;
4542
}
4643

47-
const customLocalStorageName = "__REACT_UWP__";
4844
const baseClassName = "react-uwp-theme";
4945
const themeCallback: (theme?: ThemeType) => void = () => {};
5046

@@ -67,94 +63,42 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
6763
backgroundEl: RenderToBody;
6864

6965
getDefaultTheme: () => ThemeType = () => {
70-
let { theme, autoSaveTheme } = this.props;
71-
72-
if (!IS_NODE_ENV && autoSaveTheme && !theme) {
73-
theme = this.getLocalStorageTheme();
74-
} else {
75-
theme = theme || darkTheme;
76-
}
77-
78-
return theme;
79-
}
80-
81-
getLocalStorageTheme: () => ThemeType = () => {
82-
let themeConfig: any = {};
8366
let { theme } = this.props;
84-
85-
if (theme) {
86-
Object.assign(themeConfig, {
87-
themeName: theme.themeName,
88-
accent: theme.accent,
89-
useFluentDesign: theme.useFluentDesign,
90-
desktopBackgroundImage: theme.desktopBackgroundImage
91-
});
92-
}
93-
94-
const storageString = localStorage.getItem(customLocalStorageName);
95-
if (storageString) {
96-
let data: any = {};
97-
try {
98-
data = JSON.parse(storageString);
99-
const { themeName, accent, useFluentDesign, desktopBackgroundImage } = data;
100-
theme = getTheme({
101-
themeName: themeName === void 0 ? themeConfig.themeName : themeName,
102-
accent: accent === void 0 ? themeConfig.accent : accent,
103-
useFluentDesign: useFluentDesign === void 0 ? themeConfig.useFluentDesign : useFluentDesign,
104-
desktopBackgroundImage: desktopBackgroundImage === void 0 ? themeConfig.desktopBackgroundImage : desktopBackgroundImage
105-
});
106-
} catch (error) {
107-
theme = this.props.theme || darkTheme;
108-
}
109-
} else {
110-
theme = this.props.theme || darkTheme;
111-
}
67+
theme = theme || darkTheme;
11268

11369
return theme;
11470
}
11571

72+
state: ThemeState = {
73+
currTheme: this.getDefaultTheme()
74+
};
75+
11676
bindNewThemeMethods: (theme: ThemeType) => void = (theme: ThemeType) => {
11777
const { scrollBarStyleSelector } = this.props;
118-
const styleManager: ReactUWP.StyleManager = new StyleManager({ theme }) as any;
119-
styleManager.addCSSTextWithUpdate(getBaseCSSText(theme as any, "uwp-base", scrollBarStyleSelector));
78+
const styleManager: ReactUWP.StyleManager = new StyleManager({ theme });
79+
styleManager.addCSSTextWithUpdate(getBaseCSSText(theme, "uwp-base", scrollBarStyleSelector));
12080
Object.assign(theme, {
12181
desktopBackground: `url(${theme.desktopBackgroundImage}) no-repeat fixed top left / cover`,
12282
updateTheme: this.updateTheme,
123-
saveTheme: this.saveTheme,
12483
addToast: this.addToast,
12584
updateToast: this.updateToast,
12685
deleteToast: this.deleteToast,
12786
scrollRevealListener: this.handleScrollReveal,
12887
forceUpdateTheme: this.forceUpdateTheme,
12988
styleManager
130-
} as any);
89+
});
13190
}
13291

13392
handleNewTheme: (theme: ThemeType) => void = (theme: ThemeType) => {
13493
this.props.themeWillUpdate(theme);
13594
}
13695

137-
state: ThemeState = {
138-
currTheme: (() => {
139-
const theme = this.getDefaultTheme();
140-
this.handleNewTheme(theme);
141-
return theme;
142-
})()
143-
};
144-
14596
getChildContext: () => { theme: ThemeType } = () => {
14697
return { theme: this.state.currTheme };
14798
}
14899

149100
componentDidMount() {
150101
const { currTheme } = this.state;
151-
152-
if (IS_NODE_ENV && this.props.autoSaveTheme) {
153-
const currTheme = this.getLocalStorageTheme();
154-
this.props.themeWillUpdate(currTheme);
155-
this.setState({ currTheme });
156-
}
157-
158102
if (IS_NODE_ENV) setSegoeMDL2AssetsFonts();
159103

160104
if (currTheme.useFluentDesign && currTheme.desktopBackgroundImage && this.props.needGenerateAcrylic) {
@@ -170,7 +114,7 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
170114
const { currTheme } = this.state;
171115
const needGenerateAcrylic = this.sureNeedGenerateAcrylic(theme);
172116

173-
if (nextProps && nextProps.theme && !this.props.autoSaveTheme) {
117+
if (nextProps && nextProps.theme) {
174118
if (
175119
theme.accent !== currTheme.accent ||
176120
theme.themeName !== currTheme.themeName ||
@@ -191,7 +135,7 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
191135
}
192136

193137
componentWillUpdate(nextProps: ThemeProps, nextState: ThemeState) {
194-
this.prevStyleManager = this.state.currTheme.styleManager as any;
138+
this.prevStyleManager = this.state.currTheme.styleManager;
195139
}
196140

197141
componentDidUpdate() {
@@ -231,28 +175,6 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
231175

232176
forceUpdateTheme: (currTheme: ThemeType) => void = (currTheme: ThemeType) => this.setState({ currTheme });
233177

234-
saveTheme: (currTheme: ThemeType) => void = (newTheme?: ThemeType, callback = themeCallback) => {
235-
localStorage.setItem(customLocalStorageName, JSON.stringify({
236-
themeName: newTheme.themeName,
237-
accent: newTheme.accent,
238-
useFluentDesign: newTheme.useFluentDesign,
239-
desktopBackgroundImage: newTheme.desktopBackgroundImage
240-
}));
241-
242-
const needGenerateAcrylic = this.sureNeedGenerateAcrylic(newTheme);
243-
244-
this.handleNewTheme(newTheme);
245-
this.setState({
246-
currTheme: newTheme
247-
}, () => {
248-
callback(newTheme);
249-
if (needGenerateAcrylic) {
250-
this.handleNewTheme(newTheme);
251-
newTheme.generateAcrylicTextures(newTheme, currTheme => this.setState({ currTheme }));
252-
}
253-
});
254-
}
255-
256178
sureNeedGenerateAcrylic: (newTheme: ThemeType) => boolean = (newTheme: ThemeType): boolean => {
257179
const { currTheme } = this.state;
258180
let needGenerateAcrylic = newTheme.desktopBackgroundImage && this.props.needGenerateAcrylic;
@@ -292,7 +214,7 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
292214

293215
findToastNodeTimers: any[] = [];
294216
toastId = -1;
295-
addToast = (toast: React.ReactElement<any>, callback?: (toastId?: number) => void, increaseId = true, currToastId?: number) => {
217+
addToast(toast: React.ReactElement<any>, callback?: (toastId?: number) => void, increaseId = true, currToastId?: number) {
296218
let toastId = currToastId !== void 0 ? currToastId : this.toastId;
297219
if (increaseId) {
298220
toastId += 1;
@@ -310,58 +232,20 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
310232
}
311233
}
312234

313-
updateToast = (toastId: number, toast: React.ReactElement<any>) => {
235+
updateToast(toastId: number, toast: React.ReactElement<any>) {
314236
if (this.toastWrapper) this.toastWrapper.updateToast(toastId, toast);
315237
}
316238

317-
deleteToast = (toastId: number) => {
239+
deleteToast(toastId: number) {
318240
this.state.currTheme.toasts[toastId] = void 0;
319241
}
320242

321243
handleScrollReveal = (e?: Event) => {
322-
const { currTheme } = this.state;
323-
for (const scrollReveal of currTheme.scrollReveals) {
324-
const {
325-
rootElm,
326-
animated,
327-
setEnterStyle,
328-
setLeaveStyle,
329-
props: {
330-
topOffset,
331-
bottomOffset
332-
}
333-
} = scrollReveal;
334-
if (!rootElm) return;
335-
const { top, height } = rootElm.getBoundingClientRect();
336-
const { innerHeight } = window;
337-
338-
let isIn = false;
339-
if (height > innerHeight) {
340-
isIn = top < innerHeight - height * height && top > - height * 0.5;
341-
} else {
342-
isIn = top > 0 + topOffset && top + height + bottomOffset < innerHeight;
343-
}
344-
if (isIn) {
345-
if (!animated) {
346-
setEnterStyle();
347-
scrollReveal.animated = true;
348-
}
349-
} else {
350-
if (animated) {
351-
setLeaveStyle();
352-
scrollReveal.animated = false;
353-
}
354-
}
355-
}
356-
}
357-
358-
cleanLocalStorage = () => {
359-
localStorage.setItem(customLocalStorageName, "");
244+
handleScrollReveal(this.state.currTheme);
360245
}
361246

362247
render() {
363248
const {
364-
autoSaveTheme,
365249
theme,
366250
onGeneratedAcrylic,
367251
children,

0 commit comments

Comments
 (0)