Skip to content
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

feat(v2): add support another Prism theme for dark mode #1984

Merged
merged 5 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import defaultTheme from 'prism-react-renderer/themes/palenight';
import Clipboard from 'clipboard';
import rangeParser from 'parse-numeric-range';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useThemeContext from '@theme/hooks/useThemeContext';

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

const highlightLinesRangeRegex = /{([\d,-]+)}/;
Expand All @@ -26,6 +28,9 @@ export default ({children, className: languageClassName, metastring}) => {
const target = useRef(null);
const button = useRef(null);
let highlightLines = [];
const {theme} = useThemeContext();
const prismTheme =
theme === 'dark' ? prism.darkTheme : prism.theme || defaultTheme;

if (metastring && highlightLinesRangeRegex.test(metastring)) {
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
Expand Down Expand Up @@ -65,7 +70,7 @@ export default ({children, className: languageClassName, metastring}) => {
return (
<Highlight
{...defaultProps}
theme={prism.theme || defaultTheme}
theme={prismTheme}
code={children.trim()}
language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
Expand Down
7 changes: 5 additions & 2 deletions packages/docusaurus-theme-classic/src/theme/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import React from 'react';
import Head from '@docusaurus/Head';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useBaseUrl from '@docusaurus/useBaseUrl';

import ThemeProvider from '@theme/ThemeProvider';
import Navbar from '@theme/Navbar';
import Footer from '@theme/Footer';

Expand Down Expand Up @@ -37,8 +39,9 @@ function Layout(props) {
const metaImage = image || defaultImage;
const metaImageUrl = siteUrl + useBaseUrl(metaImage);
const faviconUrl = useBaseUrl(favicon);

return (
<>
<ThemeProvider>
<Head>
{/* TODO: Do not assume that it is in english language */}
<html lang="en" />
Expand Down Expand Up @@ -66,7 +69,7 @@ function Layout(props) {
<Navbar />
<div className="main-wrapper">{children}</div>
{!noFooter && <Footer />}
</>
</ThemeProvider>
);
}

Expand Down
10 changes: 7 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Toggle from '@theme/Toggle';

import classnames from 'classnames';

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

import styles from './styles.module.css';
Expand Down Expand Up @@ -47,10 +47,11 @@ function Navbar() {
const {baseUrl, themeConfig = {}} = siteConfig;
const {navbar = {}, disableDarkMode = false} = themeConfig;
const {title, logo = {}, links = [], hideOnScroll = false} = navbar;

const [sidebarShown, setSidebarShown] = useState(false);
const [isSearchBarExpanded, setIsSearchBarExpanded] = useState(false);
const [theme, setTheme] = useTheme();

const {theme, setTheme} = useThemeContext();
const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll);

const showSidebar = useCallback(() => {
Expand All @@ -63,7 +64,10 @@ function Navbar() {
}, [setSidebarShown]);

const onToggleChange = useCallback(
e => setTheme(e.target.checked ? 'dark' : ''),
e => {
const newTheme = e.target.checked ? 'dark' : '';
setTheme(newTheme);
},
[setTheme],
);

Expand Down
15 changes: 15 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/ThemeContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 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 React from 'react';

const ThemeContext = React.createContext({
theme: null,
setTheme: () => {},
});

export default ThemeContext;
23 changes: 23 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/ThemeProvider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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 React from 'react';

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

function ThemeProvider(props) {
const [theme, setTheme] = useTheme();

return (
<ThemeContext.Provider value={{theme, setTheme}}>
{props.children}
</ThemeContext.Provider>
);
}

export default ThemeProvider;
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 * as React from 'react';
import React from 'react';

const useTheme = () => {
const [theme, setTheme] = React.useState(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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 {useContext} from 'react';

import ThemeContext from '@theme/ThemeContext';

function useThemeContext() {
return useContext(ThemeContext);
}

export default useThemeContext;
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import defaultTheme from 'prism-react-renderer/themes/palenight';
import Clipboard from 'clipboard';
import rangeParser from 'parse-numeric-range';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import useThemeContext from '@theme/hooks/useThemeContext';
import Playground from '@theme/Playground';

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

const highlightLinesRangeRegex = /{([\d,-]+)}/;
Expand All @@ -33,6 +35,9 @@ export default ({
const target = useRef(null);
const button = useRef(null);
let highlightLines = [];
const {theme} = useThemeContext();
const prismTheme =
theme === 'dark' ? prism.darkTheme : prism.theme || defaultTheme;

if (metastring && highlightLinesRangeRegex.test(metastring)) {
const highlightLinesRange = metastring.match(highlightLinesRangeRegex)[1];
Expand Down Expand Up @@ -60,7 +65,7 @@ export default ({
<Playground
scope={{...React}}
code={children.trim()}
theme={prism.theme || defaultTheme}
endiliey marked this conversation as resolved.
Show resolved Hide resolved
theme={prismTheme}
{...props}
/>
);
Expand All @@ -83,7 +88,7 @@ export default ({
return (
<Highlight
{...defaultProps}
theme={prism.theme || defaultTheme}
theme={prismTheme}
code={children.trim()}
language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
Expand Down
4 changes: 4 additions & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ module.exports = {
],
],
themeConfig: {
prism: {
theme: require('prism-react-renderer/themes/github'),
darkTheme: require('prism-react-renderer/themes/dracula'),
},
image: 'img/docusaurus.png',
gtag: {
trackingID: 'UA-141789564-1',
Expand Down