File size: 3,425 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { ChartContext, Flavor } from './contexts'
import { ReactNode, useEffect, useState } from 'react'
import { Route, Routes, useLocation } from 'react-router-dom'
import { css } from 'otion'

type ChartContainerProps = {
  children: ReactNode
  title: ReactNode
}

type ButtonProps = {
  children: ReactNode
  onClick: () => void
}

const canvases = [
  'bar',
  'calendar',
  'chord',
  'choropleth',
  'circle-packing',
  'geomap',
  'heatmap',
  'line',
  'network',
  'parallel-coordinates',
  'pie',
  'scatterplot',
  'swarmplot',
  'treemap',
  'waffle',
]
const htmls = ['circle-packing', 'treemap', 'waffle']

function Button({ children, onClick }: ButtonProps) {
  const [isPressing, setIsPressing] = useState(false)

  useEffect(() => {
    const timeout = setTimeout(() => {
      setIsPressing(false)
    }, 250)

    return () => clearTimeout(timeout)
  }, [isPressing])

  return (
    <button
      className={css({
        border: '2px solid #f47560',
        borderRadius: 5,
        color: '#f47560',
        cursor: 'pointer',
        padding: 10,
        transition: 'all 0.2s linear',
        transitionProperty: 'background border-color color opacity',
        width: 150,

        ...(isPressing ? { opacity: 0.8 } : {}),

        ':hover': {
          background: '#f47560',
          borderColor: '#ffffff',
          color: '#ffffff',
        },
      })}
      onClick={() => {
        setIsPressing(true)
        onClick()
      }}>
      {children}
    </button>
  )
}

export default function ChartContainer({
  children,
  title,
}: ChartContainerProps) {
  const [key, setKey] = useState(0)
  const [flavor, setFlavor] = useState<Flavor>('svg')
  const { pathname } = useLocation()

  useEffect(() => {
    setFlavor('svg')
  }, [pathname])

  return (
    <ChartContext.Provider value={[key, flavor]}>
      {title}
      <div
        className={css({ display: 'flex', gap: 10, justifyContent: 'center' })}>
        <Routes>
          <Route path="geomap" />
          <Route path="" />
          <Route
            path="*"
            element={
              <Button onClick={() => setKey((state) => state + 1)}>
                Generate Data
              </Button>
            }
          />
        </Routes>
        <Routes>
          {canvases.map((path) => (
            <Route
              key={path}
              path={path}
              element={
                <Button
                  onClick={() =>
                    setFlavor((value) =>
                      value !== 'canvas' ? 'canvas' : 'svg'
                    )
                  }>
                  Use {flavor !== 'canvas' ? 'Canvas' : 'SVG'}
                </Button>
              }
            />
          ))}
        </Routes>
        <Routes>
          {htmls.map((path) => (
            <Route
              key={path}
              path={path}
              element={
                <Button
                  onClick={() =>
                    setFlavor((value) => (value !== 'html' ? 'html' : 'svg'))
                  }>
                  Use {flavor !== 'html' ? 'HTML' : 'SVG'}
                </Button>
              }
            />
          ))}
        </Routes>
      </div>
      <div
        className={css({
          height: 400,
          margin: '0 auto',
          padding: 30,
          width: 500,
        })}>
        {children}
      </div>
    </ChartContext.Provider>
  )
}