refactor(v2): toggle data-theme with vanilla js instead of react helmet (#2127)

* refactor(v2): toggle data-theme with vanilla js instead of react helmet

* use document documentElement
This commit is contained in:
Endi 2019-12-16 07:57:24 +07:00 committed by Yangshun Tay
parent ee00ecf569
commit 33622c5347
4 changed files with 114 additions and 116 deletions

View file

@ -9,9 +9,14 @@ import * as React from 'react';
const useTheme = () => {
const [theme, setTheme] = React.useState(
typeof document !== 'undefined'
? document.querySelector('html').getAttribute('data-theme')
? document.documentElement.getAttribute('data-theme')
: '',
);
React.useEffect(() => {
document.documentElement.setAttribute('data-theme', theme);
}, [theme]);
React.useEffect(() => {
try {
const localStorageTheme = localStorage.getItem('theme');