mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
feat(ui): set theme according to system configuration
This commit is contained in:
parent
c760d53e25
commit
a245f5f1e6
2 changed files with 36 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
|||
import React from 'react';
|
||||
import { Route, Switch } from 'react-router-dom';
|
||||
import AppContent from './components/AppContent';
|
||||
import useTheme from './hooks/use-theme';
|
||||
import initI18n from './init/i18n';
|
||||
import Consent from './pages/Consent';
|
||||
import SignIn from './pages/SignIn';
|
||||
|
@ -9,14 +10,18 @@ import './scss/normalized.scss';
|
|||
|
||||
initI18n();
|
||||
|
||||
const App = () => (
|
||||
<AppContent theme="dark">
|
||||
<Switch>
|
||||
<Route exact path="/sign-in" component={SignIn} />
|
||||
<Route exact path="/sign-in/consent" component={Consent} />
|
||||
<Route exact path="/Register" component={Register} />
|
||||
</Switch>
|
||||
</AppContent>
|
||||
);
|
||||
const App = () => {
|
||||
const theme = useTheme();
|
||||
|
||||
return (
|
||||
<AppContent theme={theme}>
|
||||
<Switch>
|
||||
<Route exact path="/sign-in" component={SignIn} />
|
||||
<Route exact path="/sign-in/consent" component={Consent} />
|
||||
<Route exact path="/register" component={Register} />
|
||||
</Switch>
|
||||
</AppContent>
|
||||
);
|
||||
};
|
||||
|
||||
export default App;
|
||||
|
|
22
packages/ui/src/hooks/use-theme.ts
Normal file
22
packages/ui/src/hooks/use-theme.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
import { Theme } from '@/components/AppContent';
|
||||
|
||||
const darkThemeWatchMedia = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
const getThemeBySystemConfiguration = (): Theme => (darkThemeWatchMedia.matches ? 'dark' : 'light');
|
||||
|
||||
export default function useTheme() {
|
||||
const [theme, setTheme] = useState(getThemeBySystemConfiguration());
|
||||
|
||||
useEffect(() => {
|
||||
const changeTheme = () => {
|
||||
setTheme(getThemeBySystemConfiguration());
|
||||
};
|
||||
|
||||
darkThemeWatchMedia.addEventListener('change', changeTheme);
|
||||
return () => {
|
||||
darkThemeWatchMedia.removeEventListener('change', changeTheme);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return theme;
|
||||
}
|
Loading…
Reference in a new issue