File size: 12,734 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import React, { ChangeEvent } from 'react'
import isNumber from 'lodash/isNumber.js'
import styled from 'styled-components'
import { BorderRadius, BorderRadiusObject } from '@nivo/theming'
import { RoundedRect } from '@nivo/rects'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { BorderRadiusControlConfig, ControlContext } from '../types'
import { Control, Radio, TextInput } from '../ui'

const RECT_WIDTH = 42
const RECT_HEIGHT = 42
const BORDER_WIDTH = 1.5

type Mode = 'uniform' | 'horizontal' | 'vertical' | 'corners'

const MODES = ['uniform', 'horizontal', 'vertical', 'corners']
const MODE_OPTIONS = MODES.map(mode => ({
    value: mode,
    label: mode,
}))

const guessMode = (value: BorderRadius): Mode => {
    if (isNumber(value)) return 'uniform'

    const { top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight } =
        value as BorderRadiusObject

    if (top !== undefined || bottom !== undefined) {
        return 'vertical'
    }

    if (left !== undefined || right !== undefined) {
        return 'horizontal'
    }

    if (
        topLeft !== undefined ||
        topRight !== undefined ||
        bottomLeft !== undefined ||
        bottomRight !== undefined
    ) {
        return 'corners'
    }

    return 'uniform'
}

// Helper function to get value from object with fallbacks.
const getValueWithFallbacks = (obj: BorderRadiusObject, keys: (keyof BorderRadiusObject)[]) => {
    for (const key of keys) {
        if (obj[key] !== undefined) return obj[key] as number
    }
    return 0
}

interface BorderRadiusControlProps {
    id: string
    property: ChartPropertyWithControl<BorderRadiusControlConfig>
    flavors: Flavor[]
    currentFlavor: Flavor
    value: BorderRadius
    onChange: (value: BorderRadius) => void
    context?: ControlContext
}

export const BorderRadiusControl = ({
    id,
    property,
    flavors,
    currentFlavor,
    value,
    onChange,
    context,
}: BorderRadiusControlProps) => {
    const mode = guessMode(value)

    const handleModeChange = (event: ChangeEvent<HTMLInputElement>) => {
        const newMode = event.target.value as Mode
        if (newMode === mode) return

        // Convert from current mode to new mode
        if (mode === 'uniform') {
            const uniformValue = value as number
            switch (newMode) {
                case 'horizontal':
                    onChange({ left: uniformValue, right: uniformValue })
                    break
                case 'vertical':
                    onChange({ top: uniformValue, bottom: uniformValue })
                    break
                case 'corners':
                    onChange({
                        topLeft: uniformValue,
                        topRight: uniformValue,
                        bottomLeft: uniformValue,
                        bottomRight: uniformValue,
                    })
                    break
            }
        } else {
            const valueObj = value as BorderRadiusObject

            if (newMode === 'uniform') {
                // For uniform, just pick the first available value
                const keys =
                    mode === 'horizontal'
                        ? ['left', 'right']
                        : mode === 'vertical'
                          ? ['top', 'bottom']
                          : ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']
                onChange(getValueWithFallbacks(valueObj, keys as (keyof BorderRadiusObject)[]))
            } else if (newMode === 'horizontal') {
                const leftValue =
                    mode === 'vertical'
                        ? getValueWithFallbacks(valueObj, ['top', 'bottom'])
                        : getValueWithFallbacks(valueObj, [
                              'topLeft',
                              'bottomLeft',
                              'topRight',
                              'bottomRight',
                          ])

                const rightValue =
                    mode === 'vertical'
                        ? leftValue
                        : getValueWithFallbacks(valueObj, [
                              'topRight',
                              'bottomRight',
                              'topLeft',
                              'bottomLeft',
                          ])

                onChange({ left: leftValue, right: rightValue })
            } else if (newMode === 'vertical') {
                const topValue =
                    mode === 'horizontal'
                        ? getValueWithFallbacks(valueObj, ['left', 'right'])
                        : getValueWithFallbacks(valueObj, [
                              'topLeft',
                              'topRight',
                              'bottomLeft',
                              'bottomRight',
                          ])

                const bottomValue =
                    mode === 'horizontal'
                        ? topValue
                        : getValueWithFallbacks(valueObj, [
                              'bottomLeft',
                              'bottomRight',
                              'topLeft',
                              'topRight',
                          ])

                onChange({ top: topValue, bottom: bottomValue })
            } else if (newMode === 'corners') {
                if (mode === 'horizontal') {
                    const leftValue = getValueWithFallbacks(valueObj, ['left', 'right'])
                    const rightValue = getValueWithFallbacks(valueObj, ['right', 'left'])
                    onChange({
                        topLeft: leftValue,
                        bottomLeft: leftValue,
                        topRight: rightValue,
                        bottomRight: rightValue,
                    })
                } else {
                    // vertical
                    const topValue = getValueWithFallbacks(valueObj, ['top', 'bottom'])
                    const bottomValue = getValueWithFallbacks(valueObj, ['bottom', 'top'])
                    onChange({
                        topLeft: topValue,
                        topRight: topValue,
                        bottomLeft: bottomValue,
                        bottomRight: bottomValue,
                    })
                }
            }
        }
    }

    const handeValueChange =
        (key: keyof BorderRadiusObject | 'uniform') => (event: ChangeEvent<HTMLInputElement>) => {
            const newValue = Number(event.target.value)
            if (key === 'uniform') {
                onChange(newValue)
            } else {
                onChange({
                    ...(value as BorderRadiusObject),
                    [key]: newValue,
                })
            }
        }

    return (
        <Control
            id={id}
            property={property}
            flavors={flavors}
            currentFlavor={currentFlavor}
            context={context}
        >
            <Container>
                <Grid>
                    <div>
                        <Radio
                            value={mode}
                            columns={MODE_OPTIONS.length}
                            options={MODE_OPTIONS}
                            onChange={handleModeChange}
                        />
                    </div>
                    {mode === 'uniform' && (
                        <BorderRadiusValue
                            propId={id}
                            borderRadiusKey="uniform"
                            value={value as number}
                            onChange={handeValueChange('uniform')}
                        />
                    )}
                    {mode === 'horizontal' && (
                        <>
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="left"
                                value={(value as BorderRadiusObject).left}
                                onChange={handeValueChange('left')}
                            />
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="right"
                                value={(value as BorderRadiusObject).right}
                                onChange={handeValueChange('right')}
                            />
                        </>
                    )}
                    {mode === 'vertical' && (
                        <>
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="top"
                                value={(value as BorderRadiusObject).top}
                                onChange={handeValueChange('top')}
                            />
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="bottom"
                                value={(value as BorderRadiusObject).bottom}
                                onChange={handeValueChange('bottom')}
                            />
                        </>
                    )}
                    {mode === 'corners' && (
                        <>
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="topLeft"
                                value={(value as BorderRadiusObject).topLeft}
                                onChange={handeValueChange('topLeft')}
                            />
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="topRight"
                                value={(value as BorderRadiusObject).topRight}
                                onChange={handeValueChange('topRight')}
                            />
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="bottomLeft"
                                value={(value as BorderRadiusObject).bottomLeft}
                                onChange={handeValueChange('bottomLeft')}
                            />
                            <BorderRadiusValue
                                propId={id}
                                borderRadiusKey="bottomRight"
                                value={(value as BorderRadiusObject).bottomRight}
                                onChange={handeValueChange('bottomRight')}
                            />
                        </>
                    )}
                </Grid>
                <svg width={RECT_WIDTH + BORDER_WIDTH} height={RECT_HEIGHT + BORDER_WIDTH}>
                    <g transform={`translate(${BORDER_WIDTH / 2},${BORDER_WIDTH / 2})`}>
                        <PreviewRect width={RECT_WIDTH} height={RECT_HEIGHT} r={value} />
                    </g>
                </svg>
            </Container>
        </Control>
    )
}

const Container = styled.div`
    display: flex;
    align-items: center;
`

const PreviewRect = styled(RoundedRect)`
    fill: ${({ theme }) => theme.colors.background};
    stroke: ${({ theme }) => theme.colors.accent};
    stroke-width: ${BORDER_WIDTH}px;
`

const Grid = styled.div`
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    column-gap: 8px;
    row-gap: 5px;
    margin-bottom: 5px;
    width: 340px;
    margin-right: 16px;

    & > div:first-child {
        grid-column-start: 1;
        grid-column-end: 3;
        display: flex;

        & > div {
            max-width: unset;
            width: 100%;
        }
    }
`

const BorderRadiusValue = ({
    propId,
    value,
    borderRadiusKey,
    onChange,
}: {
    propId: string
    value?: number
    borderRadiusKey: keyof BorderRadiusObject | 'uniform'
    onChange: (event: ChangeEvent<HTMLInputElement>) => void
}) => {
    const id = `${propId}-${borderRadiusKey}`
    return (
        <ValueContainer>
            <label htmlFor={id}>{borderRadiusKey}</label>
            <TextInput
                id={id}
                type="number"
                unit="px"
                value={value ?? 0}
                onChange={onChange}
                isNumber={true}
                min={0}
                max={100}
            />
        </ValueContainer>
    )
}

const ValueContainer = styled.div`
    display: flex;
    align-items: center;
    justify-content: space-between;

    label {
        flex: 1;
        text-align: right;
        margin-right: 9px;
        overflow: hidden;
    }

    & > div {
        width: 70px;
    }
`