From 58720c93e83ebda089e108a741c8eb6410ccd77f Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Sun, 5 Dec 2021 04:31:57 +0300 Subject: [PATCH] refactor: simplify Toggle component (#6049) --- .../src/theme/Toggle/index.tsx | 56 +++++++------------ .../src/theme/Toggle/styles.module.css | 3 - packages/docusaurus-theme-common/src/index.ts | 1 + 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx index ed449e3cad..59c5d8d881 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/index.tsx @@ -5,46 +5,27 @@ * LICENSE file in the root directory of this source tree. */ -import React, {useState, useRef, memo, CSSProperties} from 'react'; +import React, {useState, useRef, memo} from 'react'; import type {Props} from '@theme/Toggle'; -import {useThemeConfig} from '@docusaurus/theme-common'; +import {useThemeConfig, ColorModeConfig} from '@docusaurus/theme-common'; import useIsBrowser from '@docusaurus/useIsBrowser'; import clsx from 'clsx'; import styles from './styles.module.css'; -interface IconProps { - icon: string; - style: CSSProperties; -} - -function Dark({icon, style}: IconProps): JSX.Element { - return ( - - {icon} - - ); -} -function Light({icon, style}: IconProps): JSX.Element { - return ( - - {icon} - - ); -} - // Based on react-toggle (https://github.com/aaronshaf/react-toggle/). -const Toggle = memo( +const ToggleComponent = memo( ({ className, - icons, + switchConfig, checked: defaultChecked, disabled, onChange, }: Props & { - icons: {checked: JSX.Element; unchecked: JSX.Element}; + 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); @@ -62,8 +43,16 @@ const Toggle = memo( role="button" tabIndex={-1} onClick={() => inputRef.current?.click()}> -
{icons.checked}
-
{icons.unchecked}
+
+ + {darkIcon} + +
+
+ + {lightIcon} + +
@@ -88,21 +77,16 @@ const Toggle = memo( }, ); -export default function (props: Props): JSX.Element { +export default function Toggle(props: Props): JSX.Element { const { - colorMode: { - switchConfig: {darkIcon, darkIconStyle, lightIcon, lightIconStyle}, - }, + colorMode: {switchConfig}, } = useThemeConfig(); const isBrowser = useIsBrowser(); return ( - , - unchecked: , - }} {...props} /> ); 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 c3053a44eb..2cbd0c8cdc 100644 --- a/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/Toggle/styles.module.css @@ -103,6 +103,3 @@ justify-content: center; width: 10px; } -.toggle::before { - position: absolute; -} diff --git a/packages/docusaurus-theme-common/src/index.ts b/packages/docusaurus-theme-common/src/index.ts index a47275476b..c53792b012 100644 --- a/packages/docusaurus-theme-common/src/index.ts +++ b/packages/docusaurus-theme-common/src/index.ts @@ -16,6 +16,7 @@ export type { Footer, FooterLinks, FooterLinkItem, + ColorModeConfig, } from './utils/useThemeConfig'; export {createStorageSlot, listStorageKeys} from './utils/storageUtils';