-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch theme color mode when docs color mode is changed
- Loading branch information
1 parent
c1be2e0
commit 26ece97
Showing
6 changed files
with
103 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'ignorePatterns': [ 'node_modules/**/*', 'build/**/*' ] | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
import React from 'react'; | ||
import type { Props } from '@theme/ColorModeToggle'; | ||
|
||
declare function ColorModeToggle( { className, value, onChange }: Props ): JSX.Element; | ||
declare const _default: React.MemoExoticComponent<typeof ColorModeToggle>; | ||
export default _default; |
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,63 @@ | ||
import React, { useEffect } from 'react'; | ||
import clsx from 'clsx'; | ||
import { useThemeColorModeSwtich } from '@amalgama/react-native-ui-kit'; | ||
import useIsBrowser from '@docusaurus/useIsBrowser'; | ||
import { translate } from '@docusaurus/Translate'; | ||
import IconLightMode from '@theme/Icon/LightMode'; | ||
import IconDarkMode from '@theme/Icon/DarkMode'; | ||
import styles from './styles.module.css'; | ||
|
||
function ColorModeToggle( { className, value, onChange } ) { | ||
const isBrowser = useIsBrowser(); | ||
const switchColorMode = useThemeColorModeSwtich(); | ||
|
||
const title = 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: | ||
value === 'dark' | ||
? 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' | ||
} ) | ||
} | ||
); | ||
|
||
useEffect( () => { | ||
switchColorMode( value ); | ||
}, [ value ] ); | ||
|
||
return ( | ||
<div className={clsx( styles.toggle, className )}> | ||
<button | ||
className={clsx( | ||
'clean-btn', | ||
styles.toggleButton, | ||
!isBrowser && styles.toggleButtonDisabled | ||
)} | ||
type="button" | ||
onClick={() => onChange( value === 'dark' ? 'light' : 'dark' )} | ||
disabled={!isBrowser} | ||
title={title} | ||
aria-label={title}> | ||
<IconLightMode | ||
className={clsx( styles.toggleIcon, styles.lightToggleIcon )} | ||
/> | ||
<IconDarkMode | ||
className={clsx( styles.toggleIcon, styles.darkToggleIcon )} | ||
/> | ||
</button> | ||
</div> | ||
); | ||
} | ||
export default React.memo( ColorModeToggle ); |
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,28 @@ | ||
.toggle { | ||
width: 2rem; | ||
height: 2rem; | ||
} | ||
|
||
.toggleButton { | ||
-webkit-tap-highlight-color: transparent; | ||
align-items: center; | ||
display: flex; | ||
justify-content: center; | ||
width: 100%; | ||
height: 100%; | ||
border-radius: 50%; | ||
transition: background var(--ifm-transition-fast); | ||
} | ||
|
||
.toggleButton:hover { | ||
background: var(--ifm-color-emphasis-200); | ||
} | ||
|
||
[data-theme='light'] .darkToggleIcon, | ||
[data-theme='dark'] .lightToggleIcon { | ||
display: none; | ||
} | ||
|
||
.toggleButtonDisabled { | ||
cursor: not-allowed; | ||
} |
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