-
Notifications
You must be signed in to change notification settings - Fork 1
/
GradientPicker.js
351 lines (306 loc) 路 9.15 KB
/
GradientPicker.js
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
import React from 'react'
import gradient from 'tinygradient'
import PropTypes from 'prop-types'
import { TinyColor } from '@ctrl/tinycolor'
import styled, { css } from 'react-emotion'
import clipboard from 'clipboard-polyfill'
import Container from '../components/Container'
import ColorInputField from '../components/ColorInputField'
import Slider from '../components/Slider'
import { BasicTools } from '../components/Tools'
import { Provider as ColorProvider, Consumer } from '../utils/context'
import {
DEFAULT_COLOR_ONE,
DEFAULT_COLOR_TWO,
GRADIENT_CONTAINER_WIDTH,
GRADIENT_CONTAINER_HEIGHT,
DEFAULT_COLOR_STOP
} from '../utils/constants'
import { randomColors } from '../utils/colors'
import getThemeVariants from '../utils/theme'
const StyledLabel = styled('label')`
display: inline-block;
width: 80px;
position: relative;
top: 3px;
left: 4px;
font-size: 14px;
margin-bottom: 5px;
color: ${props => props.color};
`
// Colors should be sorted by the color stops position values
const createGradient = colors =>
gradient(
colors.sort((a, b) => {
// We need to shift the value of either color stop because tinygradient
// throws an error when two stops are equal.
if (a.pos && b.pos && a.pos === b.pos) {
/* eslint-disable no-param-reassign */
a.pos += 0.01
}
return a.pos - b.pos
})
)
const ColorBlock = ({ gradient: gradientCss }) => (
<div
className={css`
display: flex;
align-items: center;
justify-content: center;
position: relative;
height: 110px;
border-radius: 6px 6px 0px 0px;
`}
style={{ backgroundImage: gradientCss }}
/>
)
ColorBlock.propTypes = {
gradient: PropTypes.string.isRequired
}
const ColorStop = ({
color: inputColor,
onChangeColor,
value,
onChangeStop
}) => (
<Consumer>
{color => (
<div>
<ColorInputField value={inputColor} onChange={onChangeColor} />
<span>
<StyledLabel color={color} className="label">
Stops
</StyledLabel>
<Slider
min="0"
max="10"
step="0.01"
value={value}
onChange={onChangeStop}
color={color}
style={{ marginTop: 10, marginBottom: 10 }}
/>
</span>
</div>
)}
</Consumer>
)
ColorStop.propTypes = {
color: PropTypes.string.isRequired,
onChangeColor: PropTypes.func.isRequired,
value: PropTypes.number.isRequired,
onChangeStop: PropTypes.func.isRequired
}
export default class GradientPicker extends React.Component {
// Clipboard icon element
clipboardIcon = null
state = {
// Returns a gradient object
gradient: this.props.reverse
? gradient(this.props.colorOne, this.props.colorTwo).reverse()
: gradient(this.props.colorOne, this.props.colorTwo),
// Default colors for creating a gradient
colorOne: this.props.colorOne,
colorTwo: this.props.colorTwo,
// Color stops are stopping points in a gradient that show a specific color
// at the exact location we set.
// Stop loc. for color one
colorStopOne: 0,
// Stop loc. for color two
colorStopTwo: 0,
// Show copy msg
showMsg: false
}
static defaultProps = {
colorOne: DEFAULT_COLOR_ONE,
colorTwo: DEFAULT_COLOR_TWO,
// When set to true, reverse the gradient
reverse: false,
// Returns a css gradient string. It is invoked on every operation like
// (setting stop values, or updating the color input field)
/* eslint-disable no-unused-vars */
getGradient: grad => {},
theme: 'light'
// These defaults are built-in in tinygradient module
// mode: 'linear',
// direction: 'to bottom'
}
static propTypes = {
colorOne: PropTypes.string,
colorTwo: PropTypes.string,
getGradient: PropTypes.func,
theme: PropTypes.oneOf(['light', 'dark']),
/* eslint-disable react/require-default-props */
mode: PropTypes.oneOf(['linear', 'radial']),
direction: PropTypes.string,
reverse: PropTypes.bool
}
componentDidMount() {
this.propCallback()
this.clipboardIcon = document.getElementById('gradient-clipboard')
this.clipboardIcon &&
this.clipboardIcon.addEventListener('mouseleave', this.hideMsg)
this.clipboardIcon &&
this.clipboardIcon.addEventListener('blur', this.hideMsg)
}
componentWillUnmount() {
this.clipboardIcon &&
this.clipboardIcon.removeEventListener('mouseleave', this.hideMsg)
this.clipboardIcon &&
this.clipboardIcon.removeEventListener('blur', this.hideMsg)
}
hideMsg = () => this.setState({ showMsg: false })
setColorStopOne = pos => {
// Only set the stop property if it's non-zero
/* eslint-disable operator-linebreak */
const colorOne =
pos !== 0 ? { color: this.state.colorOne, pos } : this.state.colorOne
const colorTwo =
pos !== 0
? { color: this.state.colorTwo, pos: DEFAULT_COLOR_STOP }
: this.state.colorTwo
return {
colorOne,
colorTwo
}
}
setColorStopTwo = pos => {
/* eslint-disable operator-linebreak */
const colorOne =
pos !== 0
? { color: this.state.colorOne, pos: DEFAULT_COLOR_STOP }
: this.state.colorOne
const colorTwo =
pos !== 0 ? { color: this.state.colorTwo, pos } : this.state.colorTwo
return {
colorOne,
colorTwo
}
}
/* eslint-disable indent */
propCallback = () =>
this.props.reverse
? this.props.getGradient(
this.state.gradient
.reverse()
.css(this.props.mode, this.props.direction)
)
: this.props.getGradient(
this.state.gradient.css(this.props.mode, this.props.direction)
)
updateColorStop = (e, color) => {
const value = parseInt(e.target.value)
// color stop position value should be between 0 and 1
const pos = value / 10
// Create the gradient depending on the color stop value and color state
if (color === 'colorStopOne') {
const { colorOne, colorTwo } = this.setColorStopOne(pos)
this.setState(
{ gradient: createGradient([colorOne, colorTwo]), [color]: value },
this.propCallback
)
} else if (color === 'colorStopTwo') {
const { colorOne, colorTwo } = this.setColorStopTwo(pos)
this.setState(
{ gradient: createGradient([colorOne, colorTwo]), [color]: value },
this.propCallback
)
}
}
// Create the gradient when the color stop value for color changes
updateStopOne = e => this.updateColorStop(e, 'colorStopOne')
updateStopTwo = e => this.updateColorStop(e, 'colorStopTwo')
// Create the gradient when the either color changes
updateColorOne = color => {
this.setState({ colorOne: color })
const newColor = new TinyColor(color)
if (newColor.isValid) {
this.setState(
state => ({
gradient: gradient(color, state.colorTwo)
}),
this.propCallback
)
}
}
updateColorTwo = color => {
this.setState({ colorTwo: color })
const newColor = new TinyColor(color)
if (newColor.isValid) {
this.setState(
state => ({
gradient: gradient(state.colorOne, color)
}),
this.propCallback
)
}
}
// Generate different color inputs and create a gradient using those input values
generateGradient = () => {
const iterator = randomColors().values()
const colorOne = iterator.next().value
const colorTwo = iterator.next().value
this.setState(
{ colorOne, colorTwo, gradient: gradient(colorOne, colorTwo) },
this.propCallback
)
}
copyColor = () => {
clipboard.writeText(this.state.gradient.css())
this.setState({ showMsg: true })
}
render() {
const {
gradient: grad,
colorOne,
colorTwo,
colorStopOne,
colorStopTwo,
showMsg
} = this.state
const { bg, iconColor } = getThemeVariants(this.props.theme)
return (
<Container
background={bg}
width={GRADIENT_CONTAINER_WIDTH}
height={GRADIENT_CONTAINER_HEIGHT}
>
<ColorBlock gradient={grad.css()} />
<ColorProvider value={iconColor}>
<div style={{ padding: 10 }}>
<ColorStop
color={colorOne}
value={colorStopOne}
onChangeColor={this.updateColorOne}
onChangeStop={this.updateStopOne}
/>
<ColorStop
color={colorTwo}
value={colorStopTwo}
onChangeColor={this.updateColorTwo}
onChangeStop={this.updateStopTwo}
/>
<div
className={css`
display: flex;
justify-content: center;
margin-top: 10px;
`}
>
<BasicTools.Clipboard
id="gradient-clipboard"
showMsg={showMsg}
copyColor={this.copyColor}
/>
<div style={{ marginLeft: 10 }}>
<BasicTools.GradientGenerator
generateGradient={this.generateGradient}
/>
</div>
</div>
</div>
</ColorProvider>
</Container>
)
}
}