chore: manage files
This commit is contained in:
@@ -14,19 +14,27 @@ const Home: NextPage = () => {
|
|||||||
<div>
|
<div>
|
||||||
<Button>A button use the theme styles</Button>
|
<Button>A button use the theme styles</Button>
|
||||||
<simple-counter name="A counter created by web component" />
|
<simple-counter name="A counter created by web component" />
|
||||||
|
<p>current mode {mode}</p>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
changeMode(mode === 'dark' ? 'light' : 'dark');
|
changeMode('light');
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
current theme mode :{mode}(click to change)
|
light
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
changeMode('dark');
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
dark
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
changeMode('auto');
|
changeMode('auto');
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
click to set "auto" mode
|
auto
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
ThemeProviderValue,
|
ThemeProviderValue,
|
||||||
} from './types';
|
} from './types';
|
||||||
import { lightTheme, darkTheme, globalThemeConstant } from './theme';
|
import { lightTheme, darkTheme, globalThemeConstant } from './theme';
|
||||||
import { SystemTheme, localStorageThemeMode } from '@/styles/utils';
|
import { SystemThemeHelper, localStorageThemeHelper } from './utils';
|
||||||
|
|
||||||
export const ThemeContext = createContext<ThemeProviderValue>({
|
export const ThemeContext = createContext<ThemeProviderValue>({
|
||||||
mode: 'light',
|
mode: 'light',
|
||||||
@@ -31,30 +31,34 @@ export const ThemeProvider = ({
|
|||||||
const changeMode = (themeMode: ThemeMode) => {
|
const changeMode = (themeMode: ThemeMode) => {
|
||||||
themeMode !== mode && setMode(themeMode);
|
themeMode !== mode && setMode(themeMode);
|
||||||
// Remember the theme mode which user selected for next time
|
// Remember the theme mode which user selected for next time
|
||||||
localStorageThemeMode.set(themeMode);
|
localStorageThemeHelper.set(themeMode);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const systemTheme = new SystemTheme();
|
setMode(localStorageThemeHelper.get() || 'auto');
|
||||||
const selectedThemeMode = localStorageThemeMode.get();
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const systemThemeHelper = new SystemThemeHelper();
|
||||||
|
const selectedThemeMode = localStorageThemeHelper.get();
|
||||||
|
|
||||||
const themeMode = selectedThemeMode || mode;
|
const themeMode = selectedThemeMode || mode;
|
||||||
if (themeMode === 'auto') {
|
if (themeMode === 'auto') {
|
||||||
setTheme(systemTheme.get());
|
setTheme(systemThemeHelper.get());
|
||||||
} else {
|
} else {
|
||||||
setTheme(themeMode);
|
setTheme(themeMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When system theme changed, change the theme mode
|
// When system theme changed, change the theme mode
|
||||||
systemTheme.onChange(() => {
|
systemThemeHelper.onChange(() => {
|
||||||
// TODO: There may be should be provided a way to let user choose whether to
|
// TODO: There may be should be provided a way to let user choose whether to
|
||||||
if (mode === 'auto') {
|
if (mode === 'auto') {
|
||||||
setTheme(systemTheme.get());
|
setTheme(systemThemeHelper.get());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
systemTheme.dispose();
|
systemThemeHelper.dispose();
|
||||||
};
|
};
|
||||||
}, [mode]);
|
}, [mode]);
|
||||||
|
|
||||||
|
|||||||
2
src/styles/utils/index.ts
Normal file
2
src/styles/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './systemThemeHelper';
|
||||||
|
export * from './localStorageThemeHelper';
|
||||||
13
src/styles/utils/localStorageThemeHelper.ts
Normal file
13
src/styles/utils/localStorageThemeHelper.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ThemeMode } from '../types';
|
||||||
|
|
||||||
|
export class LocalStorageThemeHelper {
|
||||||
|
name = 'Affine-theme-mode';
|
||||||
|
get = (): ThemeMode | null => {
|
||||||
|
return localStorage.getItem(this.name) as ThemeMode | null;
|
||||||
|
};
|
||||||
|
set = (mode: ThemeMode) => {
|
||||||
|
localStorage.setItem(this.name, mode);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const localStorageThemeHelper = new LocalStorageThemeHelper();
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Theme, ThemeMode } from '@/styles/types';
|
import { Theme } from '../types';
|
||||||
|
|
||||||
export class SystemTheme {
|
export class SystemThemeHelper {
|
||||||
media: MediaQueryList = window.matchMedia('(prefers-color-scheme: light)');
|
media: MediaQueryList = window.matchMedia('(prefers-color-scheme: light)');
|
||||||
eventList: Array<(e: Event) => void> = [];
|
eventList: Array<(e: Event) => void> = [];
|
||||||
eventHandler = (e: Event) => {
|
eventHandler = (e: Event) => {
|
||||||
@@ -27,15 +27,3 @@ export class SystemTheme {
|
|||||||
this.media.removeEventListener('change', this.eventHandler);
|
this.media.removeEventListener('change', this.eventHandler);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LocalStorageThemeMode {
|
|
||||||
name = 'Affine-theme-mode';
|
|
||||||
get = (): ThemeMode | null => {
|
|
||||||
return localStorage.getItem(this.name) as ThemeMode | null;
|
|
||||||
};
|
|
||||||
set = (mode: ThemeMode) => {
|
|
||||||
localStorage.setItem(this.name, mode);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export const localStorageThemeMode = new LocalStorageThemeMode();
|
|
||||||
Reference in New Issue
Block a user