Commit 0e286a6c authored by tom's avatar tom

basic toggler

parent 992ed6f2
.root {
display: inline-block;
position: relative;
vertical-align: middle;
line-height: 0;
}
.track {
width: 72px;
height: 32px;
background-color: rgba(16, 17, 18, 0.06);
border-radius: 16px;
display: inline-flex;
flex-shrink: 0;
justify-content: space-between;
box-sizing: content-box;
cursor: pointer;
}
.input {
border: 0px;
clip: rect(0px, 0px, 0px, 0px);
height: 1px;
width: 1px;
margin: -1px;
padding: 0px;
overflow: hidden;
white-space: nowrap;
position: absolute;
}
.thumb {
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
width: 24px;
height: 24px;
border-radius: 12px;
position: absolute;
transform: translate(44px, 4px);
}
.thumb[data-checked] {
transform: translate(4px, 4px);
}
.nightIcon {
margin: 8px;
z-index: 5;
}
.dayIcon {
margin: 8px;
z-index: 5;
}
\ No newline at end of file
import type { UseCheckboxProps } from '@chakra-ui/checkbox';
import { useCheckbox } from '@chakra-ui/checkbox'
import type {
SystemStyleObject,
ThemingProps,
HTMLChakraProps,
} from '@chakra-ui/system';
import {
chakra,
forwardRef,
omitThemingProps,
} from '@chakra-ui/system'
import { dataAttr, __DEV__ } from '@chakra-ui/utils'
import * as React from 'react'
import { SunIcon, MoonIcon } from '@chakra-ui/icons'
import { useColorMode, useColorModeValue } from '@chakra-ui/react';
import styles from './ColorModeToggler.module.css';
export interface ColorModeTogglerProps
extends Omit<UseCheckboxProps, 'isIndeterminate'>,
Omit<HTMLChakraProps<'label'>, keyof UseCheckboxProps>,
ThemingProps<'Switch'> {
}
export const ColorModeToggler = forwardRef<ColorModeTogglerProps, 'input'>((props, ref) => {
const ownProps = omitThemingProps(props);
const { toggleColorMode } = useColorMode();
const {
state,
getInputProps,
getCheckboxProps,
getRootProps,
} = useCheckbox(ownProps);
const trackStyles: SystemStyleObject = {
bg: useColorModeValue('blackAlpha.100', 'whiteAlpha.200'),
}
const thumbStyles: SystemStyleObject = {
bg: useColorModeValue('white', 'black'),
}
return (
<chakra.label
{ ...getRootProps({ onChange: toggleColorMode }) }
className={ styles.root }
>
<input className={ styles.input } { ...getInputProps({}, ref) }/>
<chakra.div
{ ...getCheckboxProps() }
className={ styles.track }
__css={ trackStyles }
>
<MoonIcon className={ styles.nightIcon } boxSize={ 4 } color={ useColorModeValue('blue.600', 'white') }/>
<chakra.div
className={ styles.thumb }
data-checked={ dataAttr(state.isChecked) }
data-hover={ dataAttr(state.isHovered) }
__css={ thumbStyles }
/>
<SunIcon className={ styles.dayIcon } boxSize={ 4 } color={ useColorModeValue('gray.500', 'blue.600') }/>
</chakra.div>
</chakra.label>
)
})
if (__DEV__) {
ColorModeToggler.displayName = 'ColorModeToggler'
}
import React from 'react'; import React from 'react';
import { HStack, InputGroup, Input, InputLeftAddon, InputLeftElement, Center, Switch, useColorMode, useColorModeValue } from '@chakra-ui/react'; import { HStack, InputGroup, Input, InputLeftAddon, InputLeftElement, Center, useColorModeValue } from '@chakra-ui/react';
import { SearchIcon } from '@chakra-ui/icons' import { SearchIcon } from '@chakra-ui/icons'
import Identicon from 'react-identicons'; import Identicon from 'react-identicons';
import { ColorModeToggler } from './ColorModeToggler';
import styles from './Header.module.css'; import styles from './Header.module.css';
const Header = () => { const Header = () => {
const { toggleColorMode } = useColorMode();
return ( return (
<HStack <HStack
as="header" as="header"
...@@ -25,7 +24,7 @@ const Header = () => { ...@@ -25,7 +24,7 @@ const Header = () => {
</InputLeftElement> </InputLeftElement>
<Input paddingInlineStart="50px" placeholder="Search by addresses / transactions /block/ token ... "/> <Input paddingInlineStart="50px" placeholder="Search by addresses / transactions /block/ token ... "/>
</InputGroup> </InputGroup>
<Switch size="lg" onChange={ toggleColorMode }/> <ColorModeToggler/>
<Center minWidth="50px" width="50px" height="50px" bg={ useColorModeValue('blackAlpha.100', 'white') } borderRadius="50%" overflow="hidden"> <Center minWidth="50px" width="50px" height="50px" bg={ useColorModeValue('blackAlpha.100', 'white') } borderRadius="50%" overflow="hidden">
{ /* the displayed size is 40px, but we need to generate x2 for retina displays */ } { /* the displayed size is 40px, but we need to generate x2 for retina displays */ }
<Identicon className={ styles.identicon } string="randomness" size={ 80 }/> <Identicon className={ styles.identicon } string="randomness" size={ 80 }/>
......
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