refactor: simplify Toggle component (#6049)

This commit is contained in:
Alexey Pyltsyn 2021-12-05 04:31:57 +03:00 committed by GitHub
parent 866babb6ad
commit 58720c93e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 39 deletions

View file

@ -5,46 +5,27 @@
* LICENSE file in the root directory of this source tree. * 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 type {Props} from '@theme/Toggle';
import {useThemeConfig} from '@docusaurus/theme-common'; import {useThemeConfig, ColorModeConfig} from '@docusaurus/theme-common';
import useIsBrowser from '@docusaurus/useIsBrowser'; import useIsBrowser from '@docusaurus/useIsBrowser';
import clsx from 'clsx'; import clsx from 'clsx';
import styles from './styles.module.css'; import styles from './styles.module.css';
interface IconProps {
icon: string;
style: CSSProperties;
}
function Dark({icon, style}: IconProps): JSX.Element {
return (
<span className={clsx(styles.toggleIcon, styles.dark)} style={style}>
{icon}
</span>
);
}
function Light({icon, style}: IconProps): JSX.Element {
return (
<span className={clsx(styles.toggleIcon, styles.light)} style={style}>
{icon}
</span>
);
}
// Based on react-toggle (https://github.com/aaronshaf/react-toggle/). // Based on react-toggle (https://github.com/aaronshaf/react-toggle/).
const Toggle = memo( const ToggleComponent = memo(
({ ({
className, className,
icons, switchConfig,
checked: defaultChecked, checked: defaultChecked,
disabled, disabled,
onChange, onChange,
}: Props & { }: Props & {
icons: {checked: JSX.Element; unchecked: JSX.Element}; switchConfig: ColorModeConfig['switchConfig'];
disabled: boolean; disabled: boolean;
}): JSX.Element => { }): JSX.Element => {
const {darkIcon, darkIconStyle, lightIcon, lightIconStyle} = switchConfig;
const [checked, setChecked] = useState(defaultChecked); const [checked, setChecked] = useState(defaultChecked);
const [focused, setFocused] = useState(false); const [focused, setFocused] = useState(false);
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
@ -62,8 +43,16 @@ const Toggle = memo(
role="button" role="button"
tabIndex={-1} tabIndex={-1}
onClick={() => inputRef.current?.click()}> onClick={() => inputRef.current?.click()}>
<div className={styles.toggleTrackCheck}>{icons.checked}</div> <div className={styles.toggleTrackCheck}>
<div className={styles.toggleTrackX}>{icons.unchecked}</div> <span className={styles.toggleIcon} style={darkIconStyle}>
{darkIcon}
</span>
</div>
<div className={styles.toggleTrackX}>
<span className={styles.toggleIcon} style={lightIconStyle}>
{lightIcon}
</span>
</div>
<div className={styles.toggleTrackThumb} /> <div className={styles.toggleTrackThumb} />
</div> </div>
@ -88,21 +77,16 @@ const Toggle = memo(
}, },
); );
export default function (props: Props): JSX.Element { export default function Toggle(props: Props): JSX.Element {
const { const {
colorMode: { colorMode: {switchConfig},
switchConfig: {darkIcon, darkIconStyle, lightIcon, lightIconStyle},
},
} = useThemeConfig(); } = useThemeConfig();
const isBrowser = useIsBrowser(); const isBrowser = useIsBrowser();
return ( return (
<Toggle <ToggleComponent
switchConfig={switchConfig}
disabled={!isBrowser} disabled={!isBrowser}
icons={{
checked: <Dark icon={darkIcon} style={darkIconStyle} />,
unchecked: <Light icon={lightIcon} style={lightIconStyle} />,
}}
{...props} {...props}
/> />
); );

View file

@ -103,6 +103,3 @@
justify-content: center; justify-content: center;
width: 10px; width: 10px;
} }
.toggle::before {
position: absolute;
}

View file

@ -16,6 +16,7 @@ export type {
Footer, Footer,
FooterLinks, FooterLinks,
FooterLinkItem, FooterLinkItem,
ColorModeConfig,
} from './utils/useThemeConfig'; } from './utils/useThemeConfig';
export {createStorageSlot, listStorageKeys} from './utils/storageUtils'; export {createStorageSlot, listStorageKeys} from './utils/storageUtils';