-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(theme-classic): replace color mode toggle with button; remove switchConfig #6771
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2722084
refactor(theme-classic): replace color mode toggle with button; remov…
Josh-Cena 5d22a0c
refactor
Josh-Cena 857e8fa
add visual cue
Josh-Cena 0ddec90
fix
Josh-Cena 336f8b1
rename Toggle to ColorModeToggle
slorber 86ae0d8
Add ColorModeToggle/Icon to swizzle config (safe) + sort ensure confi…
slorber 848149f
typo
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/docusaurus-theme-classic/src/theme/ColorModeToggle/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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, {useState, useRef, useEffect} from 'react'; | ||
import type {Props} from '@theme/ColorModeToggle'; | ||
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'; | ||
|
||
function ColorModeToggle({ | ||
className, | ||
checked: defaultChecked, | ||
onChange, | ||
}: Props): JSX.Element { | ||
const isBrowser = useIsBrowser(); | ||
const [checked, setChecked] = useState(defaultChecked); | ||
const [focused, setFocused] = useState(false); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
setChecked(defaultChecked); | ||
}, [defaultChecked]); | ||
|
||
return ( | ||
<div | ||
className={clsx(styles.toggle, className, { | ||
[styles.toggleChecked]: checked, | ||
[styles.toggleFocused]: focused, | ||
[styles.toggleDisabled]: !isBrowser, | ||
})}> | ||
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events */} | ||
<div | ||
className={styles.toggleButton} | ||
role="button" | ||
tabIndex={-1} | ||
onClick={() => inputRef.current?.click()}> | ||
<IconLightMode | ||
className={clsx(styles.toggleIcon, styles.lightToggleIcon)} | ||
/> | ||
<IconDarkMode | ||
className={clsx(styles.toggleIcon, styles.darkToggleIcon)} | ||
/> | ||
</div> | ||
|
||
<input | ||
ref={inputRef} | ||
checked={checked} | ||
type="checkbox" | ||
className={styles.toggleScreenReader} | ||
aria-label={translate( | ||
{ | ||
message: 'Switch between dark and light mode (currently {mode})', | ||
id: 'theme.colorToggle.ariaLabel', | ||
description: 'The ARIA label for the navbar color mode toggle', | ||
}, | ||
{ | ||
mode: checked | ||
? translate({ | ||
message: 'dark mode', | ||
id: 'theme.colorToggle.ariaLabel.mode.dark', | ||
description: 'The name for the dark color mode', | ||
}) | ||
: translate({ | ||
message: 'light mode', | ||
id: 'theme.colorToggle.ariaLabel.mode.light', | ||
description: 'The name for the light color mode', | ||
}), | ||
}, | ||
)} | ||
onChange={onChange} | ||
onClick={() => setChecked(!checked)} | ||
onFocus={() => setFocused(true)} | ||
onBlur={() => setFocused(false)} | ||
onKeyDown={(e) => { | ||
if (e.key === 'Enter') { | ||
inputRef.current?.click(); | ||
} | ||
}} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export default React.memo(ColorModeToggle); |
51 changes: 51 additions & 0 deletions
51
packages/docusaurus-theme-classic/src/theme/ColorModeToggle/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
.toggle { | ||
position: relative; | ||
width: 32px; | ||
height: 32px; | ||
} | ||
|
||
.toggleScreenReader { | ||
border: 0; | ||
clip: rect(0 0 0 0); | ||
height: 1px; | ||
margin: -1px; | ||
overflow: hidden; | ||
position: absolute; | ||
width: 1px; | ||
} | ||
|
||
.toggleDisabled { | ||
cursor: not-allowed; | ||
} | ||
|
||
.toggleButton { | ||
cursor: pointer; | ||
user-select: none; | ||
-webkit-tap-highlight-color: transparent; | ||
align-items: center; | ||
display: flex; | ||
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, | ||
[data-theme='dark'] .lightToggleIcon { | ||
display: none; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍