-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(v2): refactor dark toggle into a hook #1899
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import * as React from 'react'; | ||
|
||
const useTheme = defaultTheme => { | ||
const [theme, setTheme] = React.useState(defaultTheme); | ||
React.useEffect(() => { | ||
try { | ||
setTheme(localStorage.getItem('theme')); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, [setTheme]); | ||
|
||
const setThemeSyncWithLocalStorage = React.useCallback( | ||
nextTheme => { | ||
try { | ||
localStorage.setItem('theme', nextTheme); | ||
setTheme(nextTheme); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
}, | ||
[setTheme], | ||
); | ||
|
||
return [theme, setThemeSyncWithLocalStorage]; | ||
}; | ||
|
||
export default useTheme; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,7 +5,7 @@ | |||||
* LICENSE file in the root directory of this source tree. | ||||||
*/ | ||||||
|
||||||
import React, {useCallback, useState, useEffect} from 'react'; | ||||||
import React, {useCallback, useState} from 'react'; | ||||||
import Toggle from 'react-toggle'; | ||||||
|
||||||
import Link from '@docusaurus/Link'; | ||||||
|
@@ -17,6 +17,8 @@ import SearchBar from '@theme/SearchBar'; | |||||
|
||||||
import classnames from 'classnames'; | ||||||
|
||||||
import useTheme from '../../hooks/theme'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be @theme/hooks/theme There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this means I should put hooks under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
iirc this works as well. Yeah it should be under theme dir There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved hooks into |
||||||
|
||||||
import styles from './styles.module.css'; | ||||||
|
||||||
function NavLink(props) { | ||||||
|
@@ -47,11 +49,11 @@ function Navbar() { | |||||
const context = useDocusaurusContext(); | ||||||
const [sidebarShown, setSidebarShown] = useState(false); | ||||||
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false); | ||||||
const currentTheme = | ||||||
const [theme, setTheme] = useTheme( | ||||||
typeof document !== 'undefined' | ||||||
? document.querySelector('html').getAttribute('data-theme') | ||||||
: ''; | ||||||
const [theme, setTheme] = useState(currentTheme); | ||||||
: '', | ||||||
); | ||||||
const {siteConfig = {}} = context; | ||||||
const {baseUrl, themeConfig = {}} = siteConfig; | ||||||
const {algolia, navbar = {}} = themeConfig; | ||||||
|
@@ -64,24 +66,10 @@ function Navbar() { | |||||
setSidebarShown(false); | ||||||
}, [setSidebarShown]); | ||||||
|
||||||
useEffect(() => { | ||||||
try { | ||||||
const localStorageTheme = localStorage.getItem('theme'); | ||||||
setTheme(localStorageTheme); | ||||||
} catch (err) { | ||||||
console.error(err); | ||||||
} | ||||||
}, []); | ||||||
|
||||||
const onToggleChange = e => { | ||||||
const nextTheme = e.target.checked ? 'dark' : ''; | ||||||
setTheme(nextTheme); | ||||||
try { | ||||||
localStorage.setItem('theme', nextTheme); | ||||||
} catch (err) { | ||||||
console.error(err); | ||||||
} | ||||||
}; | ||||||
const onToggleChange = useCallback( | ||||||
e => setTheme(e.target.checked ? 'dark' : ''), | ||||||
[setTheme], | ||||||
); | ||||||
|
||||||
const logoUrl = useBaseUrl(logo.src); | ||||||
return ( | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use
if its undefined ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to let the hook computes default by itself.