|
| 1 | +import React, {type ReactNode} from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import useIsBrowser from '@docusaurus/useIsBrowser'; |
| 4 | +import {translate} from '@docusaurus/Translate'; |
| 5 | +import IconLightMode from '@theme/Icon/LightMode'; |
| 6 | +import IconDarkMode from '@theme/Icon/DarkMode'; |
| 7 | +import IconSystemColorMode from '@theme/Icon/SystemColorMode'; |
| 8 | +import type {Props} from '@theme/ColorModeToggle'; |
| 9 | +import type {ColorMode} from '@docusaurus/theme-common'; |
| 10 | + |
| 11 | +import styles from './styles.module.css'; |
| 12 | + |
| 13 | +// The order of color modes is defined here, and can be customized with swizzle |
| 14 | +function getNextColorMode( |
| 15 | + colorMode: ColorMode | null, |
| 16 | + respectPrefersColorScheme: boolean, |
| 17 | +) { |
| 18 | + // 2-value transition |
| 19 | + if (!respectPrefersColorScheme) { |
| 20 | + return colorMode === 'dark' ? 'light' : 'dark'; |
| 21 | + } |
| 22 | + |
| 23 | + // 3-value transition |
| 24 | + switch (colorMode) { |
| 25 | + case null: |
| 26 | + return 'light'; |
| 27 | + case 'light': |
| 28 | + return 'dark'; |
| 29 | + case 'dark': |
| 30 | + return null; |
| 31 | + default: |
| 32 | + throw new Error(`unexpected color mode ${colorMode}`); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function getColorModeLabel(colorMode: ColorMode | null): string { |
| 37 | + switch (colorMode) { |
| 38 | + case null: |
| 39 | + return translate({ |
| 40 | + message: 'system mode', |
| 41 | + id: 'theme.colorToggle.ariaLabel.mode.system', |
| 42 | + description: 'The name for the system color mode', |
| 43 | + }); |
| 44 | + case 'light': |
| 45 | + return translate({ |
| 46 | + message: 'switch to dark mode', |
| 47 | + id: 'theme.colorToggle.ariaLabel.mode.light', |
| 48 | + description: 'The name for the light color mode', |
| 49 | + }); |
| 50 | + case 'dark': |
| 51 | + return translate({ |
| 52 | + message: 'switch to light mode', |
| 53 | + id: 'theme.colorToggle.ariaLabel.mode.dark', |
| 54 | + description: 'The name for the dark color mode', |
| 55 | + }); |
| 56 | + default: |
| 57 | + throw new Error(`unexpected color mode ${colorMode}`); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function getColorModeAriaLabel(colorMode: ColorMode | null) { |
| 62 | + return translate( |
| 63 | + { |
| 64 | + message: 'Switch between dark and light mode (currently {mode})', |
| 65 | + id: 'theme.colorToggle.ariaLabel', |
| 66 | + description: 'The ARIA label for the color mode toggle', |
| 67 | + }, |
| 68 | + { |
| 69 | + mode: getColorModeLabel(colorMode), |
| 70 | + }, |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +function CurrentColorModeIcon(): ReactNode { |
| 75 | + // 3 icons are always rendered for technical reasons |
| 76 | + // We use "data-theme-choice" to render the correct one |
| 77 | + // This must work even before React hydrates |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <IconLightMode |
| 81 | + // a18y is handled at the button level, |
| 82 | + // not relying on button content (svg icons) |
| 83 | + aria-hidden |
| 84 | + className={clsx(styles.toggleIcon, styles.darkToggleIcon)} |
| 85 | + data-theme-choice="dark" |
| 86 | + /> |
| 87 | + <IconDarkMode |
| 88 | + aria-hidden |
| 89 | + className={clsx(styles.toggleIcon, styles.lightToggleIcon)} |
| 90 | + data-theme-choice="light" |
| 91 | + /> |
| 92 | + <IconSystemColorMode |
| 93 | + aria-hidden |
| 94 | + className={clsx(styles.toggleIcon, styles.systemToggleIcon)} |
| 95 | + data-theme-choice="system" |
| 96 | + /> |
| 97 | + </> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function ColorModeToggle({ |
| 102 | + className, |
| 103 | + buttonClassName, |
| 104 | + respectPrefersColorScheme, |
| 105 | + value, |
| 106 | + onChange, |
| 107 | +}: Props): ReactNode { |
| 108 | + const isBrowser = useIsBrowser(); |
| 109 | + return ( |
| 110 | + <div className={clsx(styles.toggle, className)}> |
| 111 | + <button |
| 112 | + className={clsx( |
| 113 | + 'clean-btn', |
| 114 | + styles.toggleButton, |
| 115 | + !isBrowser && styles.toggleButtonDisabled, |
| 116 | + buttonClassName, |
| 117 | + )} |
| 118 | + type="button" |
| 119 | + onClick={() => |
| 120 | + onChange(getNextColorMode(value, respectPrefersColorScheme)) |
| 121 | + } |
| 122 | + disabled={!isBrowser} |
| 123 | + title={getColorModeLabel(value)} |
| 124 | + aria-label={getColorModeAriaLabel(value)} |
| 125 | + |
| 126 | + // For accessibility decisions |
| 127 | + // See https://github.com/facebook/docusaurus/issues/7667#issuecomment-2724401796 |
| 128 | + |
| 129 | + // aria-live disabled on purpose - This is annoying because: |
| 130 | + // - without this attribute, VoiceOver doesn't announce on button enter |
| 131 | + // - with this attribute, VoiceOver announces twice on ctrl+opt+space |
| 132 | + // - with this attribute, NVDA announces many times |
| 133 | + // aria-live="polite" |
| 134 | + > |
| 135 | + <CurrentColorModeIcon /> |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | +export default React.memo(ColorModeToggle); |
0 commit comments