Commit 97577c6b authored by N's avatar N Committed by GitHub

Merge pull request #47 from blockscout/dark-theme-2

dark mode: custom switcher
parents 2867abd1 00731b3e
...@@ -35,11 +35,11 @@ ...@@ -35,11 +35,11 @@
height: 24px; height: 24px;
border-radius: 12px; border-radius: 12px;
position: absolute; position: absolute;
transform: translate(44px, 4px); transform: translate(4px, 4px);
} }
.thumb[data-checked] { .thumb[data-checked] {
transform: translate(4px, 4px); transform: translate(44px, 4px);
} }
.nightIcon { .nightIcon {
......
...@@ -17,6 +17,8 @@ import { useColorMode, useColorModeValue } from '@chakra-ui/react'; ...@@ -17,6 +17,8 @@ import { useColorMode, useColorModeValue } from '@chakra-ui/react';
import styles from './ColorModeToggler.module.css'; import styles from './ColorModeToggler.module.css';
const TRANSITION_DURATION = 150;
export interface ColorModeTogglerProps export interface ColorModeTogglerProps
extends Omit<UseCheckboxProps, 'isIndeterminate'>, extends Omit<UseCheckboxProps, 'isIndeterminate'>,
Omit<HTMLChakraProps<'label'>, keyof UseCheckboxProps>, Omit<HTMLChakraProps<'label'>, keyof UseCheckboxProps>,
...@@ -25,7 +27,8 @@ export interface ColorModeTogglerProps ...@@ -25,7 +27,8 @@ export interface ColorModeTogglerProps
export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((props, ref) => { export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((props, ref) => {
const ownProps = omitThemingProps(props); const ownProps = omitThemingProps(props);
const { toggleColorMode } = useColorMode(); const { toggleColorMode, colorMode } = useColorMode();
const [ isOn, setMode ] = React.useState(colorMode === 'light');
const { const {
state, state,
...@@ -34,17 +37,29 @@ export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((prop ...@@ -34,17 +37,29 @@ export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((prop
getRootProps, getRootProps,
} = useCheckbox(ownProps); } = useCheckbox(ownProps);
const trackStyles: SystemStyleObject = { const trackBg = useColorModeValue('blackAlpha.100', 'whiteAlpha.200')
bg: useColorModeValue('blackAlpha.100', 'whiteAlpha.200'), const thumbBg = useColorModeValue('white', 'black')
}
const trackStyles: SystemStyleObject = React.useMemo(() => ({
bg: trackBg,
}), [ trackBg ])
const thumbStyles: SystemStyleObject = React.useMemo(() => ({
bg: thumbBg,
transitionProperty: 'transform',
transitionDuration: `${ TRANSITION_DURATION }ms`,
}), [ thumbBg ])
const thumbStyles: SystemStyleObject = { const handleInputChange = React.useCallback(() => {
bg: useColorModeValue('white', 'black'), // was not able to make transition while consuming flag value from chakra's useColorMode hook
} // that's why there is a local state for toggler and this fancy window.setTimeout
setMode((isOn) => !isOn);
window.setTimeout(toggleColorMode, TRANSITION_DURATION);
}, [ toggleColorMode ]);
return ( return (
<chakra.label <chakra.label
{ ...getRootProps({ onChange: toggleColorMode }) } { ...getRootProps({ onChange: handleInputChange }) }
className={ styles.root } className={ styles.root }
> >
<input className={ styles.input } { ...getInputProps({}, ref) }/> <input className={ styles.input } { ...getInputProps({}, ref) }/>
...@@ -56,7 +71,7 @@ export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((prop ...@@ -56,7 +71,7 @@ export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((prop
<MoonIcon className={ styles.nightIcon } boxSize={ 4 } color={ useColorModeValue('blue.600', 'white') }/> <MoonIcon className={ styles.nightIcon } boxSize={ 4 } color={ useColorModeValue('blue.600', 'white') }/>
<chakra.div <chakra.div
className={ styles.thumb } className={ styles.thumb }
data-checked={ dataAttr(state.isChecked) } data-checked={ dataAttr(isOn) }
data-hover={ dataAttr(state.isHovered) } data-hover={ dataAttr(state.isHovered) }
__css={ thumbStyles } __css={ thumbStyles }
/> />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment