File size: 3,592 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as Toolbar from '@radix-ui/react-toolbar'
import { MoonStars, Sun } from 'phosphor-react'
import { animated, useSpring } from '@react-spring/web'

import { AccessibleIcon } from '../AccessibleIcon'
import { themeActiveBlob, themeGroup, themePicker } from './SiteThemePicker.css'
import { useOptimisticThemeMode, useTheme } from '../../hooks/useTheme'
import { useFetcher } from '@remix-run/react'
import { action } from '../../root'

export enum ThemeValue {
  Dark = 'dark',
  Light = 'light',
}

export const SiteThemePicker = () => {
  const fetcher = useFetcher<typeof action>()

  const optimisticMode = useOptimisticThemeMode()
  const userPreference = useTheme()
  const mode = optimisticMode ?? userPreference ?? 'light'

  const [styles, api] = useSpring(
    () => ({
      width: 42,
      left: mode === 'light' ? '2px' : 'unset',
      right: mode === 'light' ? 'unset' : '2px',
      config: {
        tension: 300,
        clamp: true,
      },
    }),
    [mode]
  )

  const isDarkMode = mode === ThemeValue.Dark

  const handleValueChange = async (value: ThemeValue) => {
    if (value && value !== mode) {
      const css = document.createElement('style')
      css.type = 'text/css'
      css.appendChild(
        document.createTextNode(
          `* {
       -webkit-transition: none !important;
       -moz-transition: none !important;
       -o-transition: none !important;
       -ms-transition: none !important;
       transition: none !important;
    }`
        )
      )
      document.head.appendChild(css)

      fetcher.submit(
        {
          theme: value,
        },
        {
          method: 'POST',
          encType: 'application/json',
          action: '/',
        }
      )

      api.start({
        to: async animate => {
          await animate({ width: 88 })
          api.set({
            left: value === 'light' ? '2px' : 'unset',
            right: value === 'light' ? 'unset' : '2px',
          })
          await animate({ width: 42 })

          const _ = window.getComputedStyle(css).opacity
          document.head.removeChild(css)
        },
      })
    }
  }

  const handlePointerEnter = (value: ThemeValue) => () => {
    if (mode !== value) {
      api.start({
        width: 52,
      })
    }
  }

  const handlePointerOut = (value: ThemeValue) => () => {
    if (mode !== value) {
      api.start({
        width: 42,
      })
    }
  }

  return (
    <Toolbar.ToggleGroup
      className={themeGroup}
      onValueChange={handleValueChange}
      value={mode}
      type="single"
    >
      <Toolbar.ToggleItem
        className={themePicker}
        onPointerEnter={handlePointerEnter(ThemeValue.Light)}
        onPointerOut={handlePointerOut(ThemeValue.Light)}
        value="light"
      >
        <AccessibleIcon label="Enable light mode">
          <Sun
            size={20}
            weight={isDarkMode ? 'light' : 'regular'}
            style={{ pointerEvents: 'none' }}
          />
        </AccessibleIcon>
      </Toolbar.ToggleItem>
      <Toolbar.ToggleItem
        className={themePicker}
        onPointerEnter={handlePointerEnter(ThemeValue.Dark)}
        onPointerOut={handlePointerOut(ThemeValue.Dark)}
        value="dark"
      >
        <AccessibleIcon label="Enable dark mode">
          <MoonStars
            size={20}
            weight={isDarkMode ? 'light' : 'regular'}
            style={{ pointerEvents: 'none' }}
          />
        </AccessibleIcon>
      </Toolbar.ToggleItem>
      <animated.div className={themeActiveBlob} style={styles} />
    </Toolbar.ToggleGroup>
  )
}