From 27220843b5f5d9b7440f458604639ca827c861f6 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 26 Feb 2022 16:21:49 +0800 Subject: [PATCH 1/7] refactor(theme-classic): replace color mode toggle with button; remove switchConfig --- .../src/__tests__/validateThemeConfig.test.ts | 32 ++----- .../src/theme-classic.d.ts | 16 ++++ .../src/theme/IconDarkMode/index.tsx | 20 +++++ .../src/theme/IconLightMode/index.tsx | 20 +++++ .../src/theme/Toggle/index.tsx | 36 +++----- .../src/theme/Toggle/styles.module.css | 84 +++---------------- .../src/validateThemeConfig.ts | 26 ++---- .../src/utils/useThemeConfig.ts | 7 -- website/docs/api/docusaurus.config.js.md | 12 --- .../docs/api/themes/theme-configuration.md | 17 ---- 10 files changed, 88 insertions(+), 182 deletions(-) create mode 100644 packages/docusaurus-theme-classic/src/theme/IconDarkMode/index.tsx create mode 100644 packages/docusaurus-theme-classic/src/theme/IconLightMode/index.tsx diff --git a/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts b/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts index 004594b5e74b..61fb0a05826f 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts +++ b/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts @@ -509,16 +509,17 @@ describe('themeConfig', () => { const withDefaultValues = (colorMode) => _.merge({}, DEFAULT_CONFIG.colorMode, colorMode); - test('minimal config', () => { + test('switch config', () => { const colorMode = { switchConfig: { darkIcon: '🌙', }, }; - expect(testValidateThemeConfig({colorMode})).toEqual({ - ...DEFAULT_CONFIG, - colorMode: withDefaultValues(colorMode), - }); + expect(() => + testValidateThemeConfig({colorMode}), + ).toThrowErrorMatchingInlineSnapshot( + `"colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or Toggle instead."`, + ); }); test('max config', () => { @@ -526,17 +527,6 @@ describe('themeConfig', () => { defaultMode: 'dark', disableSwitch: false, respectPrefersColorScheme: true, - switchConfig: { - darkIcon: '🌙', - darkIconStyle: { - marginTop: '1px', - marginLeft: '2px', - }, - lightIcon: '☀️', - lightIconStyle: { - marginLeft: '1px', - }, - }, }; expect(testValidateThemeConfig({colorMode})).toEqual({ ...DEFAULT_CONFIG, @@ -562,16 +552,6 @@ describe('themeConfig', () => { }, }); }); - - test('empty switch config', () => { - const colorMode = { - switchConfig: {}, - }; - expect(testValidateThemeConfig({colorMode})).toEqual({ - ...DEFAULT_CONFIG, - colorMode: withDefaultValues(colorMode), - }); - }); }); }); diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 518512b11f6f..2a1e3a3aa0c5 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -663,6 +663,14 @@ declare module '@theme/IconArrow' { export default function IconArrow(props: Props): JSX.Element; } +declare module '@theme/IconDarkMode' { + import type {ComponentProps} from 'react'; + + export interface Props extends ComponentProps<'svg'> {} + + export default function IconDarkMode(props: Props): JSX.Element; +} + declare module '@theme/IconEdit' { import type {ComponentProps} from 'react'; @@ -671,6 +679,14 @@ declare module '@theme/IconEdit' { export default function IconEdit(props: Props): JSX.Element; } +declare module '@theme/IconLightMode' { + import type {ComponentProps} from 'react'; + + export interface Props extends ComponentProps<'svg'> {} + + export default function IconLightMode(props: Props): JSX.Element; +} + declare module '@theme/IconMenu' { import type {ComponentProps} from 'react'; diff --git a/packages/docusaurus-theme-classic/src/theme/IconDarkMode/index.tsx b/packages/docusaurus-theme-classic/src/theme/IconDarkMode/index.tsx new file mode 100644 index 000000000000..d319096eeab3 --- /dev/null +++ b/packages/docusaurus-theme-classic/src/theme/IconDarkMode/index.tsx @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 type {Props} from '@theme/IconDarkMode'; + +export default function IconDarkMode(props: Props): JSX.Element { + return ( + + + + ); +} diff --git a/packages/docusaurus-theme-classic/src/theme/IconLightMode/index.tsx b/packages/docusaurus-theme-classic/src/theme/IconLightMode/index.tsx new file mode 100644 index 000000000000..9c080c966791 --- /dev/null +++ b/packages/docusaurus-theme-classic/src/theme/IconLightMode/index.tsx @@ -0,0 +1,20 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * 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 type {Props} from '@theme/IconLightMode'; + +export default function IconLightMode(props: Props): JSX.Element { + return ( + + + + ); +} diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx index 4a554f088bb2..a8de1a49dae6 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx @@ -7,9 +7,10 @@ import React, {useState, useRef, useEffect, memo} from 'react'; import type {Props} from '@theme/Toggle'; -import {useThemeConfig, type ColorModeConfig} from '@docusaurus/theme-common'; import useIsBrowser from '@docusaurus/useIsBrowser'; import {translate} from '@docusaurus/Translate'; +import IconLightMode from '@theme/IconLightMode'; +import IconDarkMode from '@theme/IconDarkMode'; import clsx from 'clsx'; import styles from './styles.module.css'; @@ -18,15 +19,12 @@ import styles from './styles.module.css'; const ToggleComponent = memo( ({ className, - switchConfig, checked: defaultChecked, disabled, onChange, }: Props & { - switchConfig: ColorModeConfig['switchConfig']; disabled: boolean; }): JSX.Element => { - const {darkIcon, darkIconStyle, lightIcon, lightIconStyle} = switchConfig; const [checked, setChecked] = useState(defaultChecked); const [focused, setFocused] = useState(false); const inputRef = useRef(null); @@ -44,21 +42,16 @@ const ToggleComponent = memo( })}> {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
inputRef.current?.click()}> -
- - {darkIcon} - -
-
- - {lightIcon} - -
-
+ +
- ); + return ; } diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css index c293660b85c5..87d53d11045b 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css @@ -6,11 +6,12 @@ */ .toggle { - touch-action: pan-x; position: relative; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; + width: 32px; + height: 32px; } .toggleScreenReader { @@ -27,80 +28,15 @@ cursor: not-allowed; } -.toggleTrack { - width: 50px; - height: 24px; - border-radius: 30px; - background-color: #4d4d4d; - transition: all 0.2s ease; -} - -.toggleTrackCheck { - position: absolute; - width: 14px; - height: 10px; - top: 0; - bottom: 0; - margin: auto 0; - left: 8px; - opacity: 0; - transition: opacity 0.25s ease; -} - -.toggleChecked .toggleTrackCheck, -[data-theme='dark'] .toggle .toggleTrackCheck { - opacity: 1; - transition: opacity 0.25s ease; -} - -.toggleTrackX { - position: absolute; - width: 10px; - height: 10px; - top: 0; - bottom: 0; - margin: auto 0; - right: 10px; - opacity: 1; - transition: opacity 0.25s ease; -} - -.toggleChecked .toggleTrackX, -[data-theme='dark'] .toggle .toggleTrackX { - opacity: 0; -} - -.toggleTrackThumb { - position: absolute; - top: 1px; - left: 1px; - width: 22px; - height: 22px; - border: 1px solid #4d4d4d; - border-radius: 50%; - background-color: #fafafa; - transition: all 0.25s ease; -} - -.toggleFocused .toggleTrackThumb, -.toggle:hover .toggleTrackThumb { - box-shadow: 0 0 2px 3px var(--ifm-color-primary); -} - -/* stylelint-disable-next-line no-descending-specificity */ -.toggleChecked .toggleTrackThumb, -[data-theme='dark'] .toggle .toggleTrackThumb { - left: 27px; -} - -.toggle:active:not(.toggleDisabled) .toggleTrackThumb { - box-shadow: 0 0 5px 5px var(--ifm-color-primary); -} - -.toggleIcon { +.toggleButton { align-items: center; display: flex; - height: 10px; justify-content: center; - width: 10px; + width: 100%; + height: 100%; +} + +[data-theme='light'] .darkToggleIcon, +[data-theme='dark'] .lightToggleIcon { + display: none; } diff --git a/packages/docusaurus-theme-classic/src/validateThemeConfig.ts b/packages/docusaurus-theme-classic/src/validateThemeConfig.ts index 4f7a9e3ded5d..d51800463b23 100644 --- a/packages/docusaurus-theme-classic/src/validateThemeConfig.ts +++ b/packages/docusaurus-theme-classic/src/validateThemeConfig.ts @@ -21,12 +21,6 @@ const DEFAULT_COLOR_MODE_CONFIG = { defaultMode: 'light', disableSwitch: false, respectPrefersColorScheme: false, - switchConfig: { - darkIcon: '🌜', - darkIconStyle: {}, - lightIcon: '🌞', - lightIconStyle: {}, - }, }; export const DEFAULT_CONFIG = { @@ -220,20 +214,12 @@ const ColorModeSchema = Joi.object({ respectPrefersColorScheme: Joi.bool().default( DEFAULT_COLOR_MODE_CONFIG.respectPrefersColorScheme, ), - switchConfig: Joi.object({ - darkIcon: Joi.string().default( - DEFAULT_COLOR_MODE_CONFIG.switchConfig.darkIcon, - ), - darkIconStyle: Joi.object().default( - DEFAULT_COLOR_MODE_CONFIG.switchConfig.darkIconStyle, - ), - lightIcon: Joi.string().default( - DEFAULT_COLOR_MODE_CONFIG.switchConfig.lightIcon, - ), - lightIconStyle: Joi.object().default( - DEFAULT_COLOR_MODE_CONFIG.switchConfig.lightIconStyle, - ), - }).default(DEFAULT_COLOR_MODE_CONFIG.switchConfig), + switchConfig: Joi.any() + .forbidden() + .messages({ + 'any.unknown': + 'colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or Toggle instead.', + }), }).default(DEFAULT_COLOR_MODE_CONFIG); // schema can probably be improved diff --git a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts index 26975d2563ce..762ed3a14943 100644 --- a/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts +++ b/packages/docusaurus-theme-common/src/utils/useThemeConfig.ts @@ -7,7 +7,6 @@ import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import type {PrismTheme} from 'prism-react-renderer'; -import type {CSSProperties} from 'react'; import type {DeepPartial} from 'utility-types'; export type DocsVersionPersistence = 'localStorage' | 'none'; @@ -43,12 +42,6 @@ export type ColorModeConfig = { defaultMode: 'light' | 'dark'; disableSwitch: boolean; respectPrefersColorScheme: boolean; - switchConfig: { - darkIcon: string; - darkIconStyle: CSSProperties; - lightIcon: string; - lightIconStyle: CSSProperties; - }; }; export type AnnouncementBarConfig = { diff --git a/website/docs/api/docusaurus.config.js.md b/website/docs/api/docusaurus.config.js.md index ab50dc53956c..4f064ef619b1 100644 --- a/website/docs/api/docusaurus.config.js.md +++ b/website/docs/api/docusaurus.config.js.md @@ -284,18 +284,6 @@ module.exports = { defaultMode: 'light', disableSwitch: false, respectPrefersColorScheme: true, - switchConfig: { - darkIcon: '🌙', - lightIcon: '\u2600', - // React inline style object - // see https://reactjs.org/docs/dom-elements.html#style - darkIconStyle: { - marginLeft: '2px', - }, - lightIconStyle: { - marginLeft: '1px', - }, - }, }, navbar: { title: 'Site Title', diff --git a/website/docs/api/themes/theme-configuration.md b/website/docs/api/themes/theme-configuration.md index e79cf89614cf..108aa6aa78d3 100644 --- a/website/docs/api/themes/theme-configuration.md +++ b/website/docs/api/themes/theme-configuration.md @@ -28,11 +28,6 @@ Accepted fields: | `defaultMode` | 'light' \| 'dark' | `'light'` | The color mode when user first visits the site. | | `disableSwitch` | `boolean` | `false` | Hides the switch in the navbar. Useful if you want to support a single color mode. | | `respectPrefersColorScheme` | `boolean` | `false` | Whether to use the `prefers-color-scheme` media-query, using user system preferences, instead of the hardcoded `defaultMode`. | -| `switchConfig` | _See below_ | _See below_ | Dark/light switch icon options. | -| `switchConfig.darkIcon` | `string` | `'🌜'` | Icon for the switch while in dark mode. | -| `switchConfig.darkIconStyle` | JSX style object (see [documentation](https://reactjs.org/docs/dom-elements.html#style)) | `{}` | CSS to apply to dark icon. | -| `switchConfig.lightIcon` | `string` | `'🌞'` | Icon for the switch while in light mode. | -| `switchConfig.lightIconStyle` | JSX style object | `{}` | CSS to apply to light icon. | @@ -46,18 +41,6 @@ module.exports = { defaultMode: 'light', disableSwitch: false, respectPrefersColorScheme: false, - switchConfig: { - darkIcon: '🌙', - darkIconStyle: { - marginLeft: '2px', - }, - // Unicode icons such as '\u2600' will work - // Unicode with 5 chars require brackets: '\u{1F602}' - lightIcon: '\u{1F602}', - lightIconStyle: { - marginLeft: '1px', - }, - }, }, // highlight-end }, From 5d22a0c548f134ebf158cdcfb978c90cabb994d0 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 26 Feb 2022 16:28:54 +0800 Subject: [PATCH 2/7] refactor --- .../src/theme/Toggle/index.tsx | 147 ++++++++---------- .../src/validateThemeConfig.ts | 10 +- 2 files changed, 73 insertions(+), 84 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx index a8de1a49dae6..b74c6af439c4 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import React, {useState, useRef, useEffect, memo} from 'react'; +import React, {useState, useRef, useEffect} from 'react'; import type {Props} from '@theme/Toggle'; import useIsBrowser from '@docusaurus/useIsBrowser'; import {translate} from '@docusaurus/Translate'; @@ -15,87 +15,78 @@ import IconDarkMode from '@theme/IconDarkMode'; import clsx from 'clsx'; import styles from './styles.module.css'; -// Based on react-toggle (https://github.com/aaronshaf/react-toggle/). -const ToggleComponent = memo( - ({ - className, - checked: defaultChecked, - disabled, - onChange, - }: Props & { - disabled: boolean; - }): JSX.Element => { - const [checked, setChecked] = useState(defaultChecked); - const [focused, setFocused] = useState(false); - const inputRef = useRef(null); +function Toggle({ + className, + checked: defaultChecked, + onChange, +}: Props): JSX.Element { + const isBrowser = useIsBrowser(); + const [checked, setChecked] = useState(defaultChecked); + const [focused, setFocused] = useState(false); + const inputRef = useRef(null); - useEffect(() => { - setChecked(defaultChecked); - }, [defaultChecked]); + useEffect(() => { + setChecked(defaultChecked); + }, [defaultChecked]); - return ( + return ( +
+ {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */}
- {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */} -
inputRef.current?.click()}> - - -
- - setChecked(!checked)} - onFocus={() => setFocused(true)} - onBlur={() => setFocused(false)} - onKeyDown={(e) => { - if (e.key === 'Enter') { - inputRef.current?.click(); - } - }} + className={styles.toggleButton} + role="button" + tabIndex={-1} + onClick={() => inputRef.current?.click()}> + +
- ); - }, -); -export default function Toggle(props: Props): JSX.Element { - const isBrowser = useIsBrowser(); - - return ; + setChecked(!checked)} + onFocus={() => setFocused(true)} + onBlur={() => setFocused(false)} + onKeyDown={(e) => { + if (e.key === 'Enter') { + inputRef.current?.click(); + } + }} + /> +
+ ); } + +export default React.memo(Toggle); diff --git a/packages/docusaurus-theme-classic/src/validateThemeConfig.ts b/packages/docusaurus-theme-classic/src/validateThemeConfig.ts index d51800463b23..3c7370ea9cff 100644 --- a/packages/docusaurus-theme-classic/src/validateThemeConfig.ts +++ b/packages/docusaurus-theme-classic/src/validateThemeConfig.ts @@ -214,12 +214,10 @@ const ColorModeSchema = Joi.object({ respectPrefersColorScheme: Joi.bool().default( DEFAULT_COLOR_MODE_CONFIG.respectPrefersColorScheme, ), - switchConfig: Joi.any() - .forbidden() - .messages({ - 'any.unknown': - 'colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or Toggle instead.', - }), + switchConfig: Joi.any().forbidden().messages({ + 'any.unknown': + 'colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or Toggle instead.', + }), }).default(DEFAULT_COLOR_MODE_CONFIG); // schema can probably be improved From 857e8faf208dcdc144c0dfd9b00323eb6f5d4849 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 26 Feb 2022 16:42:52 +0800 Subject: [PATCH 3/7] add visual cue --- .../src/theme/Toggle/styles.module.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css index 87d53d11045b..30d98f346e5e 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css @@ -34,6 +34,15 @@ justify-content: center; width: 100%; height: 100%; + border-radius: 50%; +} + +.toggleButton:hover { + background-color: #00000010; +} + +[data-theme='dark'] .toggleButton:hover { + background-color: #ffffff20; } [data-theme='light'] .darkToggleIcon, From 0ddec9025e400584d329f2336dcda5511d1deaa0 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sat, 26 Feb 2022 16:50:31 +0800 Subject: [PATCH 4/7] fix --- .../src/theme/Toggle/styles.module.css | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css index 30d98f346e5e..a5a7bfb73799 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css @@ -7,9 +7,6 @@ .toggle { position: relative; - cursor: pointer; - user-select: none; - -webkit-tap-highlight-color: transparent; width: 32px; height: 32px; } @@ -29,6 +26,9 @@ } .toggleButton { + cursor: pointer; + user-select: none; + -webkit-tap-highlight-color: transparent; align-items: center; display: flex; justify-content: center; From 336f8b160424ed2aca5c5ddcfc24d550eede90c5 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Wed, 2 Mar 2022 15:30:01 +0100 Subject: [PATCH 5/7] rename Toggle to ColorModeToggle --- .../src/__tests__/validateThemeConfig.test.ts | 2 +- packages/docusaurus-theme-classic/src/theme-classic.d.ts | 2 +- .../src/theme/{Toggle => ColorModeToggle}/index.tsx | 6 +++--- .../src/theme/{Toggle => ColorModeToggle}/styles.module.css | 0 .../docusaurus-theme-classic/src/theme/Navbar/index.tsx | 6 +++--- .../docusaurus-theme-classic/src/validateThemeConfig.ts | 2 +- website/src/theme/{Toggle.tsx => ColorModeToggle.tsx} | 4 ++-- 7 files changed, 11 insertions(+), 11 deletions(-) rename packages/docusaurus-theme-classic/src/theme/{Toggle => ColorModeToggle}/index.tsx (95%) rename packages/docusaurus-theme-classic/src/theme/{Toggle => ColorModeToggle}/styles.module.css (100%) rename website/src/theme/{Toggle.tsx => ColorModeToggle.tsx} (91%) diff --git a/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts b/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts index 61fb0a05826f..f9a97d31588d 100644 --- a/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts +++ b/packages/docusaurus-theme-classic/src/__tests__/validateThemeConfig.test.ts @@ -518,7 +518,7 @@ describe('themeConfig', () => { expect(() => testValidateThemeConfig({colorMode}), ).toThrowErrorMatchingInlineSnapshot( - `"colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or Toggle instead."`, + `"colorMode.switchConfig is deprecated. If you want to customize the icons for light and dark mode, swizzle IconLightMode, IconDarkMode, or ColorModeToggle instead."`, ); }); diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 2a1e3a3aa0c5..d1f5be5333d5 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -632,7 +632,7 @@ declare module '@theme/TOCCollapsible' { export default function TOCCollapsible(props: Props): JSX.Element; } -declare module '@theme/Toggle' { +declare module '@theme/ColorModeToggle' { import type {SyntheticEvent} from 'react'; export interface Props { diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx b/packages/docusaurus-theme-classic/src/theme/ColorModeToggle/index.tsx similarity index 95% rename from packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx rename to packages/docusaurus-theme-classic/src/theme/ColorModeToggle/index.tsx index b74c6af439c4..46be740f6fe4 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/ColorModeToggle/index.tsx @@ -6,7 +6,7 @@ */ import React, {useState, useRef, useEffect} from 'react'; -import type {Props} from '@theme/Toggle'; +import type {Props} from '@theme/ColorModeToggle'; import useIsBrowser from '@docusaurus/useIsBrowser'; import {translate} from '@docusaurus/Translate'; import IconLightMode from '@theme/IconLightMode'; @@ -15,7 +15,7 @@ import IconDarkMode from '@theme/IconDarkMode'; import clsx from 'clsx'; import styles from './styles.module.css'; -function Toggle({ +function ColorModeToggle({ className, checked: defaultChecked, onChange, @@ -89,4 +89,4 @@ function Toggle({ ); } -export default React.memo(Toggle); +export default React.memo(ColorModeToggle); diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css b/packages/docusaurus-theme-classic/src/theme/ColorModeToggle/styles.module.css similarity index 100% rename from packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css rename to packages/docusaurus-theme-classic/src/theme/ColorModeToggle/styles.module.css diff --git a/packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx b/packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx index 789b79a96fda..84c92596337a 100644 --- a/packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Navbar/index.tsx @@ -9,7 +9,7 @@ import React, {useCallback, useState, useEffect} from 'react'; import clsx from 'clsx'; import Translate from '@docusaurus/Translate'; import SearchBar from '@theme/SearchBar'; -import Toggle from '@theme/Toggle'; +import ColorModeToggle from '@theme/ColorModeToggle'; import { useThemeConfig, useMobileSecondaryMenuRenderer, @@ -171,7 +171,7 @@ function NavbarMobileSidebar({ titleClassName="navbar__title" /> {!colorModeToggle.disabled && ( - ))} {!colorModeToggle.disabled && ( - Date: Wed, 2 Mar 2022 15:43:42 +0100 Subject: [PATCH 6/7] Add ColorModeToggle/Icon to swizzle config (safe) + sort ensure config is sorted with ESLint --- .../src/getSwizzleConfig.ts | 84 ++++++++++++------- 1 file changed, 53 insertions(+), 31 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts b/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts index 465d078b7f22..f185a7ba2e4f 100644 --- a/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts +++ b/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts @@ -7,94 +7,116 @@ import type {SwizzleConfig} from '@docusaurus/types'; +/* eslint sort-keys: "error" */ + export default function getSwizzleConfig(): SwizzleConfig { return { components: { CodeBlock: { actions: { - wrap: 'safe', eject: 'safe', + wrap: 'safe', }, description: 'The component used to render multi-line code blocks, generally used in Markdown files.', }, - DocSidebar: { + ColorModeToggle: { actions: { + eject: 'safe', wrap: 'safe', + }, + description: + 'The color mode toggle to switch between light and dark mode.', + }, + DocSidebar: { + actions: { eject: 'unsafe', // too much technical code in sidebar, not very safe atm + wrap: 'safe', }, description: 'The sidebar component on docs pages', }, Footer: { actions: { - wrap: 'safe', eject: 'unsafe', // TODO split footer into smaller parts + wrap: 'safe', }, description: "The footer component of you site's layout", }, - NotFound: { + IconArrow: { actions: { - wrap: 'safe', eject: 'safe', - }, - description: - 'The global 404 page of your site, meant to be ejected and customized', - }, - SearchBar: { - actions: { wrap: 'safe', - eject: 'safe', }, - // TODO how to describe this one properly? - // By default it's an empty placeholder for the user to fill - description: - 'The search bar component of your site, appearing in the navbar.', + description: 'The arrow icon component', }, - IconArrow: { + IconDarkMode: { actions: { - wrap: 'safe', eject: 'safe', + wrap: 'safe', }, - description: 'The arrow icon component', + description: 'The dark mode icon component.', }, IconEdit: { actions: { - wrap: 'safe', eject: 'safe', + wrap: 'safe', }, description: 'The edit icon component', }, - IconMenu: { + IconLightMode: { actions: { - wrap: 'safe', eject: 'safe', + wrap: 'safe', }, - description: 'The menu icon component', + description: 'The light mode icon component.', }, - - 'prism-include-languages': { + IconMenu: { actions: { - wrap: 'forbidden', // not a component! eject: 'safe', + wrap: 'safe', }, - description: - 'The Prism languages to include for code block syntax highlighting. Meant to be ejected.', + description: 'The menu icon component', }, MDXComponents: { actions: { - wrap: 'forbidden', /// TODO allow wrapping objects??? eject: 'safe', + wrap: 'forbidden', /// TODO allow wrapping objects??? }, description: 'The MDX components to use for rendering MDX files. Meant to be ejected.', }, - // TODO should probably not even appear here 'NavbarItem/utils': { actions: { - wrap: 'forbidden', eject: 'forbidden', + wrap: 'forbidden', + }, + }, + NotFound: { + actions: { + eject: 'safe', + wrap: 'safe', + }, + description: + 'The global 404 page of your site, meant to be ejected and customized', + }, + SearchBar: { + actions: { + eject: 'safe', + wrap: 'safe', }, + // TODO how to describe this one properly? + // By default it's an empty placeholder for the user to fill + description: + 'The search bar component of your site, appearing in the navbar.', + }, + 'prism-include-languages': { + actions: { + eject: 'safe', + wrap: 'forbidden', // not a component! + }, + description: + 'The Prism languages to include for code block syntax highlighting. Meant to be ejected.', }, }, }; From 848149fed1c9238c9b1478b45d2cda5297a0eab0 Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Wed, 2 Mar 2022 15:44:24 +0100 Subject: [PATCH 7/7] typo --- website/src/theme/ColorModeToggle.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/theme/ColorModeToggle.tsx b/website/src/theme/ColorModeToggle.tsx index 766752ce2896..d0e08f7871e9 100644 --- a/website/src/theme/ColorModeToggle.tsx +++ b/website/src/theme/ColorModeToggle.tsx @@ -6,7 +6,7 @@ */ import React from 'react'; -import OriginalToggle from '@theme-original/Toggle'; +import OriginalToggle from '@theme-original/ColorModeToggle'; import type {Props} from '@theme/ColorModeToggle'; import { lightStorage,