picture here is the design can we able to implement color picker like this without any packages in react
is it possible to customize a color picker like the above image with react(without any package)? I tried a lot, but cannot able to find the proper solution. if anyone can help thanks in advance.
click this image link https://i.stack.imgur.com/1RFki.png
Advertisement
Answer
Here is the solution for my question also you can run this solution on code sandbox.
thanks, everyone ๐
JavaScript
โx
64
64
1
import React, { useState } from "react";
2
import styled from "styled-components";
3
import "./styles.css";
4
โ
5
const Container = styled.span`
6
display: inline-flex;
7
align-items: center;
8
width: 150px;
9
max-width: 150px;
10
padding: 4px 12px;
11
border: 1px solid #bfc9d9;
12
border-radius: 4px;
13
โ
14
input[type="color"] {
15
margin-right: 8px;
16
-webkit-appearance: none;
17
border: none;
18
width: auto;
19
height: auto;
20
cursor: pointer;
21
background: none;
22
&::-webkit-color-swatch-wrapper {
23
padding: 0;
24
width: 30px;
25
height: 30px;
26
}
27
&::-webkit-color-swatch {
28
border: 1px solid #bfc9d9;
29
border-radius: 50px;
30
padding: 0;
31
}
32
}
33
โ
34
input[type="text"] {
35
border: none;
36
width: 100%;
37
font-size: 14px;
38
}
39
`;
40
โ
41
const ColorPicker = props => {
42
return (
43
<Container>
44
<input type="color" {props} />
45
<input type="text" {props} />
46
</Container>
47
);
48
};
49
โ
50
export default function App() {
51
const [state, updateState] = useState("#FFFFFF");
52
โ
53
const handleInput = e => {
54
updateState(e.target.value);
55
};
56
โ
57
return (
58
<div className="App">
59
<ColorPicker onChange={handleInput} value={state} />
60
</div>
61
);
62
}
63
โ
64
โ