mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-25 06:56:56 +02:00
refactor(v2): avoid to use raw theme values (#2254)
This commit is contained in:
parent
d1219b1233
commit
ab25ee3e0f
6 changed files with 47 additions and 34 deletions
|
@ -42,10 +42,10 @@ export default ({children, className: languageClassName, metastring}) => {
|
||||||
const button = useRef(null);
|
const button = useRef(null);
|
||||||
let highlightLines = [];
|
let highlightLines = [];
|
||||||
|
|
||||||
const {theme} = useThemeContext();
|
const {isDarkTheme} = useThemeContext();
|
||||||
const lightModeTheme = prism.theme || defaultTheme;
|
const lightModeTheme = prism.theme || defaultTheme;
|
||||||
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
||||||
const prismTheme = theme === 'dark' ? darkModeTheme : lightModeTheme;
|
const prismTheme = isDarkTheme ? darkModeTheme : lightModeTheme;
|
||||||
|
|
||||||
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
||||||
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
|
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
|
||||||
|
|
|
@ -52,7 +52,7 @@ function Navbar() {
|
||||||
const [sidebarShown, setSidebarShown] = useState(false);
|
const [sidebarShown, setSidebarShown] = useState(false);
|
||||||
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);
|
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);
|
||||||
|
|
||||||
const {theme, setTheme} = useThemeContext();
|
const {isDarkTheme, setLightTheme, setDarkTheme} = useThemeContext();
|
||||||
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);
|
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);
|
||||||
|
|
||||||
useLockBodyScroll(sidebarShown);
|
useLockBodyScroll(sidebarShown);
|
||||||
|
@ -65,11 +65,8 @@ function Navbar() {
|
||||||
}, [setSidebarShown]);
|
}, [setSidebarShown]);
|
||||||
|
|
||||||
const onToggleChange = useCallback(
|
const onToggleChange = useCallback(
|
||||||
e => {
|
e => (e.target.checked ? setDarkTheme() : setLightTheme()),
|
||||||
const newTheme = e.target.checked ? 'dark' : '';
|
[setLightTheme, setDarkTheme],
|
||||||
setTheme(newTheme);
|
|
||||||
},
|
|
||||||
[setTheme],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const logoUrl = useBaseUrl(logo.src);
|
const logoUrl = useBaseUrl(logo.src);
|
||||||
|
@ -134,7 +131,7 @@ function Navbar() {
|
||||||
<Toggle
|
<Toggle
|
||||||
className={styles.displayOnlyInLargeViewport}
|
className={styles.displayOnlyInLargeViewport}
|
||||||
aria-label="Dark mode toggle"
|
aria-label="Dark mode toggle"
|
||||||
checked={theme === 'dark'}
|
checked={isDarkTheme}
|
||||||
onChange={onToggleChange}
|
onChange={onToggleChange}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
@ -160,7 +157,7 @@ function Navbar() {
|
||||||
{!disableDarkMode && sidebarShown && (
|
{!disableDarkMode && sidebarShown && (
|
||||||
<Toggle
|
<Toggle
|
||||||
aria-label="Dark mode toggle in sidebar"
|
aria-label="Dark mode toggle in sidebar"
|
||||||
checked={theme === 'dark'}
|
checked={isDarkTheme}
|
||||||
onChange={onToggleChange}
|
onChange={onToggleChange}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -8,8 +8,9 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const ThemeContext = React.createContext({
|
const ThemeContext = React.createContext({
|
||||||
theme: null,
|
isDarkTheme: false,
|
||||||
setTheme: () => {},
|
setLightTheme: () => {},
|
||||||
|
setDarkTheme: () => {},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default ThemeContext;
|
export default ThemeContext;
|
||||||
|
|
|
@ -11,10 +11,10 @@ import useTheme from '@theme/hooks/useTheme';
|
||||||
import ThemeContext from '@theme/ThemeContext';
|
import ThemeContext from '@theme/ThemeContext';
|
||||||
|
|
||||||
function ThemeProvider(props) {
|
function ThemeProvider(props) {
|
||||||
const [theme, setTheme] = useTheme();
|
const {isDarkTheme, setLightTheme, setDarkTheme} = useTheme();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeContext.Provider value={{theme, setTheme}}>
|
<ThemeContext.Provider value={{isDarkTheme, setLightTheme, setDarkTheme}}>
|
||||||
{props.children}
|
{props.children}
|
||||||
</ThemeContext.Provider>
|
</ThemeContext.Provider>
|
||||||
);
|
);
|
||||||
|
|
|
@ -5,25 +5,48 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import {useState, useCallback, useEffect} from 'react';
|
||||||
|
|
||||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||||
|
|
||||||
|
const themes = {
|
||||||
|
light: '',
|
||||||
|
dark: 'dark',
|
||||||
|
};
|
||||||
|
|
||||||
const useTheme = () => {
|
const useTheme = () => {
|
||||||
const {
|
const {
|
||||||
siteConfig: {themeConfig: {disableDarkMode}} = {},
|
siteConfig: {themeConfig: {disableDarkMode}} = {},
|
||||||
} = useDocusaurusContext();
|
} = useDocusaurusContext();
|
||||||
const [theme, setTheme] = React.useState(
|
const [theme, setTheme] = useState(
|
||||||
typeof document !== 'undefined'
|
typeof document !== 'undefined'
|
||||||
? document.documentElement.getAttribute('data-theme')
|
? document.documentElement.getAttribute('data-theme')
|
||||||
: '',
|
: themes.light,
|
||||||
);
|
);
|
||||||
|
const setThemeSyncWithLocalStorage = useCallback(
|
||||||
|
newTheme => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem('theme', newTheme);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[setTheme],
|
||||||
|
);
|
||||||
|
const setLightTheme = useCallback(() => {
|
||||||
|
setTheme(themes.light);
|
||||||
|
setThemeSyncWithLocalStorage(themes.light);
|
||||||
|
}, []);
|
||||||
|
const setDarkTheme = useCallback(() => {
|
||||||
|
setTheme(themes.dark);
|
||||||
|
setThemeSyncWithLocalStorage(themes.dark);
|
||||||
|
}, []);
|
||||||
|
|
||||||
React.useEffect(() => {
|
useEffect(() => {
|
||||||
document.documentElement.setAttribute('data-theme', theme);
|
document.documentElement.setAttribute('data-theme', theme);
|
||||||
}, [theme]);
|
}, [theme]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
useEffect(() => {
|
||||||
if (disableDarkMode) {
|
if (disableDarkMode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -38,19 +61,11 @@ const useTheme = () => {
|
||||||
}
|
}
|
||||||
}, [setTheme]);
|
}, [setTheme]);
|
||||||
|
|
||||||
const setThemeSyncWithLocalStorage = React.useCallback(
|
return {
|
||||||
nextTheme => {
|
isDarkTheme: theme === themes.dark,
|
||||||
try {
|
setLightTheme,
|
||||||
localStorage.setItem('theme', nextTheme);
|
setDarkTheme,
|
||||||
setTheme(nextTheme);
|
};
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[setTheme],
|
|
||||||
);
|
|
||||||
|
|
||||||
return [theme, setThemeSyncWithLocalStorage];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default useTheme;
|
export default useTheme;
|
||||||
|
|
|
@ -49,10 +49,10 @@ export default ({
|
||||||
const button = useRef(null);
|
const button = useRef(null);
|
||||||
let highlightLines = [];
|
let highlightLines = [];
|
||||||
|
|
||||||
const {theme} = useThemeContext();
|
const {isDarkTheme} = useThemeContext();
|
||||||
const lightModeTheme = prism.theme || defaultTheme;
|
const lightModeTheme = prism.theme || defaultTheme;
|
||||||
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
const darkModeTheme = prism.darkTheme || lightModeTheme;
|
||||||
const prismTheme = theme === 'dark' ? darkModeTheme : lightModeTheme;
|
const prismTheme = isDarkTheme ? darkModeTheme : lightModeTheme;
|
||||||
|
|
||||||
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
if (metastring && highlightLinesRangeRegex.test(metastring)) {
|
||||||
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
|
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue