/
ThemeSelector.tsx
75 lines (68 loc) · 1.88 KB
/
ThemeSelector.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useCallback, useMemo } from 'react'
import styled, { useTheme } from 'styled-components'
import { useSetTheme } from '../theming/SwitchableThemeProvider'
import { Switch } from './controls/Switch'
const ThemeSelector = () => {
const theme = useTheme()
const themeId = theme.id
const setThemeId = useSetTheme()
const toggleTheme = useCallback(() => {
setThemeId(themeId === 'light' ? 'dark' : 'light')
}, [theme, themeId])
const setTheme = useCallback(
theme => {
setThemeId(theme)
},
[setThemeId]
)
const colors = useMemo(
() => ({
on: theme.colors.cardBackground,
off: theme.colors.cardBackground,
dot: theme.colors.gradientColor0,
}),
[theme]
)
return (
<ToggleContainer>
<ToggleItem
id="lightTheme"
isActive={themeId === 'light'}
onClick={() => setTheme('light')}
>
light
</ToggleItem>
<Switch
id="themeToggle"
value={themeId === 'dark'}
onChange={toggleTheme}
colors={colors}
/>
<ToggleItem
id="darkTheme"
isActive={themeId === 'dark'}
onClick={() => setTheme('dark')}
>
dark
</ToggleItem>
</ToggleContainer>
)
}
export default ThemeSelector
const ToggleContainer = styled.div`
display: grid;
grid-template-columns: 32px 36px 32px;
grid-column-gap: 6px;
align-items: center;
color: white;
margin-left: 20px;
`
const ToggleItem = styled.label<{
isActive: boolean
}>`
font-size: 0.9rem;
text-align: center;
font-weight: 500;
cursor: pointer;
opacity: ${({ isActive }) => (isActive ? 1 : 0.6)};
`