Skip to content

Commit

Permalink
refactor(v2): refactor dark toggle into a hook (#1899)
Browse files Browse the repository at this point in the history
* change(v2): refactor dark toggle into a theme

* follow how themes resolve files

* let theme hook to pick up default theme by itself
  • Loading branch information
wgao19 authored and yangshun committed Oct 27, 2019
1 parent 84462e2 commit 01f4d9a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
14 changes: 8 additions & 6 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
- Slightly adjust search icon position to be more aligned on small width device.
- Convert sitemap plugin to TypeScript.
- Significantly reduce main bundle size and initial HTML payload on production build. Generated JS files from webpack is also shorter in name.
- Refactor dark toggle into a hook.

## 2.0.0-alpha.31

- Footer is now sticky/ pinned to the bottom of the viewport in desktop browsers.
- Footer is now sticky/ pinned to the bottom of the viewport in desktop browsers.
- Footer is now also displayed in docs page for consistency.
- Remove empty doc sidebar container if sidebar for a particular doc page does not exist. Otherwise, it will cause an additional empty space.
- Default PostCSS loader now only polyfills stage 3+ features (previously it was stage 2) like Create React App. Stage 2 CSS is considered relatively unstable and subject to change while Stage 3 features will likely become a standard.
- Default PostCSS loader now only polyfills stage 3+ features (previously it was stage 2) like Create React App. Stage 2 CSS is considered relatively unstable and subject to change while Stage 3 features will likely become a standard.
- Fix search bar focus bug. When you put the focus on search input, previously the focus will remain although we have clicked to other area outside of the search input.
- New themeConfig option `sidebarCollapsible`. It is on by default. If explicitly set to `false`, all doc items in sidebar is expanded. Otherwise, it will still be a collapsible sidebar.
- Disable adding hashes to the generated class names of CSS modules in dev mode. Generating unique identifiers takes some time, which can be saved since including paths to files in class names is enough to avoid collisions.
Expand All @@ -27,11 +28,12 @@

- Fix babel transpilation include/exclude logic to be more efficient. This also fix a very weird bug `TypeError: Cannot assign to read only property 'exports' of object '#<Object>'`.([#1868](https://github.com/facebook/docusaurus/pull/1868))

If you are still encountering the error. Please check whether you use `module.exports` for your `.js` file instead of doing `export` (mixing CJS and ES). See https://github.com/webpack/webpack/issues/4039#issuecomment-477779322 and https://github.com/webpack/webpack/issues/4039#issuecomment-273804003 for more context.
If you are still encountering the error. Please check whether you use `module.exports` for your `.js` file instead of doing `export` (mixing CJS and ES). See https://github.com/webpack/webpack/issues/4039#issuecomment-477779322 and https://github.com/webpack/webpack/issues/4039#issuecomment-273804003 for more context.

## 2.0.0-alpha.29

**HOTFIX for 2.0.0-alpha.28**.
**HOTFIX for 2.0.0-alpha.28**.

- Fix missing `core-js` dependencies on `@docusaurus/core`.
- Fix wrong `@babel/env` preset configuration that causes build compilation error.
- New UI for webpack compilation progress bar.
Expand All @@ -46,7 +48,7 @@ If you are still encountering the error. Please check whether you use `module.ex
- Fix logo URL in footer to be appended with baseUrl automatically.
- Add the option `--no-open` for `start` command.
- Set `@babel/env` useBuiltins to `usage`. This will automatically use browserlist and import polyfills required.
- Modified TerserWebpackPlugin `terserOptions` for better cross-browser compatibility.
- Modified TerserWebpackPlugin `terserOptions` for better cross-browser compatibility.
- **BREAKING** `withBaseUrl` is renamed to `useBaseUrl` because its a React Hooks. Make sure you import/rename it correctly. Eg: `import useBaseUrl from '@docusaurus/useBaseUrl`;
- Fix potential security vulnerability because we're exposing the directory structure of the host machine.
- Upgrade dependencies.
Expand All @@ -56,7 +58,7 @@ If you are still encountering the error. Please check whether you use `module.ex
- Add `@theme/Tabs` which can be used to implement multi-language code tabs.
- Implement `custom_edit_url` and `hide_title` markdown header for docusaurus v1 feature parity.
- Reduce memory usage and slightly faster production build.
- Misc dependency upgrades.
- Misc dependency upgrades.

## 2.0.0-alpha.26

Expand Down
32 changes: 8 additions & 24 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -17,6 +17,8 @@ import SearchBar from '@theme/SearchBar';

import classnames from 'classnames';

import useTheme from '@theme/hooks/theme';

import styles from './styles.module.css';

function NavLink(props) {
Expand Down Expand Up @@ -47,11 +49,7 @@ function Navbar() {
const context = useDocusaurusContext();
const [sidebarShown, setSidebarShown] = useState(false);
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);
const currentTheme =
typeof document !== 'undefined'
? document.querySelector('html').getAttribute('data-theme')
: '';
const [theme, setTheme] = useState(currentTheme);
const [theme, setTheme] = useTheme();
const {siteConfig = {}} = context;
const {baseUrl, themeConfig = {}} = siteConfig;
const {algolia, navbar = {}} = themeConfig;
Expand All @@ -64,24 +62,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 (
Expand Down
38 changes: 38 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/hooks/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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 = () => {
const [theme, setTheme] = React.useState(
typeof document !== 'undefined'
? document.querySelector('html').getAttribute('data-theme')
: '',
);
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;

0 comments on commit 01f4d9a

Please sign in to comment.