File size: 6,401 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
import React, { ChangeEvent, memo, useCallback, useState } from 'react'
import { config as springConfig } from '@react-spring/web'
import { isString } from 'lodash'
import styled from 'styled-components'
import { ChartPropertyWithControl, Flavor } from '../../../types'
import { ControlContext, MotionConfigControlConfig } from '../types'
import { Control, Radio, Select, Switch } from '../ui'

const presetOptions = Object.keys(springConfig).map(presetId => ({
    value: presetId,
    label: presetId,
}))
export type MotionConfigPresetOption = (typeof presetOptions)[number]

const defaultConfig = {
    mass: 1,
    tension: 170,
    friction: 26,
    clamp: false,
    precision: 0.01,
    velocity: 0,
}

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

export const MotionConfigControl = memo(
    ({
        id,
        property,
        flavors,
        currentFlavor,
        value,
        onChange,
        context,
    }: MotionConfigControlProps) => {
        const type = isString(value) ? 'preset' : 'custom'
        const [preset, setPreset] = useState(type === 'preset' ? value : 'default')
        const [customConfig, setCustomConfig] = useState(type === 'custom' ? value : defaultConfig)

        const handleTypeChange = useCallback(
            (event: ChangeEvent<HTMLInputElement>) => {
                const newType = event.target.value
                if (newType === 'preset') {
                    onChange(preset)
                } else {
                    onChange(customConfig)
                }
            },
            [onChange]
        )

        const handlePresetChange = useCallback(
            (option: MotionConfigPresetOption | null) => {
                setPreset(option!.value)
                onChange(option!.value)
            },
            [onChange]
        )

        const handleMassChange = (event: ChangeEvent<HTMLInputElement>) => {
            const mass = Number(event.target.value)
            const newCustomConfig = {
                ...customConfig,
                mass,
            }
            setCustomConfig(newCustomConfig)
            onChange(newCustomConfig)
        }

        const handleTensionChange = (event: ChangeEvent<HTMLInputElement>) => {
            const tension = Number(event.target.value)
            const newCustomConfig = {
                ...customConfig,
                tension,
            }
            setCustomConfig(newCustomConfig)
            onChange(newCustomConfig)
        }

        const handleFrictionChange = (event: ChangeEvent<HTMLInputElement>) => {
            const friction = Number(event.target.value)
            const newCustomConfig = {
                ...customConfig,
                friction,
            }
            setCustomConfig(newCustomConfig)
            onChange(newCustomConfig)
        }

        const handleClampChange = (clamp: boolean) => {
            const newCustomConfig = {
                ...customConfig,
                clamp,
            }
            setCustomConfig(newCustomConfig)
            onChange(newCustomConfig)
        }

        return (
            <Control
                id={id}
                property={property}
                flavors={flavors}
                currentFlavor={currentFlavor}
                context={context}
            >
                <Row>
                    <Radio
                        options={[
                            { value: 'preset', label: 'preset' },
                            { value: 'custom', label: 'custom' },
                        ]}
                        value={type}
                        onChange={handleTypeChange}
                    />
                    {type === 'preset' && (
                        <Select<MotionConfigPresetOption>
                            options={presetOptions}
                            value={presetOptions.find(option => option.value === value)}
                            onChange={handlePresetChange}
                        />
                    )}
                    {type === 'custom' && (
                        <CustomControls>
                            <label>mass</label>
                            <code className="code-number">{value.mass}</code>
                            <input
                                type="range"
                                value={value.mass}
                                onChange={handleMassChange}
                                min={1}
                                max={500}
                            />

                            <label>tension</label>
                            <code className="code-number">{value.tension}</code>
                            <input
                                type="range"
                                value={value.tension}
                                onChange={handleTensionChange}
                                min={1}
                                max={500}
                            />

                            <label>friction</label>
                            <code className="code-number">{value.friction}</code>
                            <input
                                type="range"
                                value={value.friction}
                                onChange={handleFrictionChange}
                                min={1}
                                max={500}
                            />

                            <Switch
                                value={value.clamp}
                                id={`${id}clamp`}
                                onChange={handleClampChange}
                            />
                            <span />
                            <label htmlFor={`${id}clamp.switch`}>clamp</label>
                        </CustomControls>
                    )}
                </Row>
            </Control>
        )
    }
)

const Row = styled.div`
    display: grid;
    grid-template-columns: 1fr;
    grid-row-gap: 9px;
`

const CustomControls = styled.div`
    display: grid;
    grid-template-columns: 50px 25px auto 50px 25px auto;
    align-items: center;
    column-gap: 9px;
    row-gap: 9px;
    margin-bottom: 9px;
`