|
| 1 | +import React from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import useIsBrowser from '@docusaurus/useIsBrowser'; |
| 4 | +import {translate} from '@docusaurus/Translate'; |
| 5 | +import IconDarkMode from '@theme/Icon/DarkMode'; |
| 6 | +import IconLightMode from '@theme/Icon/LightMode'; |
| 7 | +import styles from './styles.module.css'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Swizzled ColorModeToggle. |
| 11 | + * |
| 12 | + * Docusaurus default shows the current-mode icon (sun in light, moon in dark). |
| 13 | + * This version shows the target-mode icon instead: |
| 14 | + * - Light mode → moon ("switch to dark") |
| 15 | + * - Dark mode → sun ("switch to light") |
| 16 | + */ |
| 17 | +export default function ColorModeToggle({className, buttonClassName, value, onChange}) { |
| 18 | + const isBrowser = useIsBrowser(); |
| 19 | + const title = translate( |
| 20 | + { |
| 21 | + message: 'Switch between dark and light mode (currently {mode})', |
| 22 | + id: 'theme.colorToggle.ariaLabel', |
| 23 | + description: 'The ARIA label for the navbar color mode toggle', |
| 24 | + }, |
| 25 | + { |
| 26 | + mode: |
| 27 | + value === 'dark' |
| 28 | + ? translate({ |
| 29 | + message: 'dark mode', |
| 30 | + id: 'theme.colorToggle.ariaLabel.mode.dark', |
| 31 | + description: 'The name for the dark color mode', |
| 32 | + }) |
| 33 | + : translate({ |
| 34 | + message: 'light mode', |
| 35 | + id: 'theme.colorToggle.ariaLabel.mode.light', |
| 36 | + description: 'The name for the light color mode', |
| 37 | + }), |
| 38 | + }, |
| 39 | + ); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className={clsx(styles.toggle, className)}> |
| 43 | + <button |
| 44 | + className={clsx('clean-btn', buttonClassName, styles.toggleButton, { |
| 45 | + [styles.toggleButtonDisabled]: !isBrowser, |
| 46 | + })} |
| 47 | + type="button" |
| 48 | + onClick={() => onChange(value === 'dark' ? 'light' : 'dark')} |
| 49 | + disabled={!isBrowser} |
| 50 | + title={title} |
| 51 | + aria-label={title} |
| 52 | + aria-live="polite"> |
| 53 | + {/* Moon shown in light mode → "click to switch to dark" */} |
| 54 | + <IconDarkMode className={clsx(styles.icon, styles.lightModeIcon)} /> |
| 55 | + {/* Sun shown in dark mode → "click to switch to light" */} |
| 56 | + <IconLightMode className={clsx(styles.icon, styles.darkModeIcon)} /> |
| 57 | + </button> |
| 58 | + </div> |
| 59 | + ); |
| 60 | +} |
0 commit comments