forked from dylan-cancelliere/learning-react-book
-
Notifications
You must be signed in to change notification settings - Fork 4
/
colorizer.html
132 lines (110 loc) · 3.23 KB
/
colorizer.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>The Colorizer!</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<style>
#container {
padding: 50px;
background-color: #FFF;
}
#colorHeading{
padding: 0;
margin: 50px;
margin-bottom: -20px;
font-family: sans-serif;
}
.colorSquare {
box-shadow: 0px 0px 25px 0px #333;
width: 242px;
height: 242px;
margin-bottom: 15px;
}
.colorArea input {
padding: 10px;
font-size: 16px;
border: 2px solid #CCC;
}
.colorArea button {
padding: 10px;
font-size: 16px;
margin: 10px;
background-color: #666;
color: #FFF;
border: 2px solid #666;
}
.colorArea button:hover {
background-color: #111;
border-color: #111;
cursor: pointer;
}
</style>
</head>
<body>
<h1 id="colorHeading">Colorizer</h1>
<div id="container"></div>
<script type="text/babel">
var destination = document.querySelector("#container");
class Colorizer extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
color: "",
bgColor: "white"
}
this.colorValue = this.colorValue.bind(this);
this.setNewColor = this.setNewColor.bind(this);
}
colorValue(e) {
this.setState({color: e.target.value});
}
setNewColor(e){
this.setState({
bgColor: this.state.color
});
this._input.focus();
this._input.value = "";
e.preventDefault();
}
render() {
var squareStyle = {
backgroundColor: this.state.bgColor
};
var self = this;
return (
<div className="colorArea">
<div style={squareStyle} className="colorSquare"></div>
<form onSubmit={this.setNewColor}>
<input onChange={this.colorValue}
ref={
(el) => this._input = el
}
placeholder="Enter a color value"/>
<button type="submit">go</button>
</form>
<ColorLabel color={this.state.bgColor}/>
</div>
);
}
}
var heading = document.querySelector("#colorHeading");
class ColorLabel extends React.Component {
render() {
return ReactDOM.createPortal(
": " + this.props.color,
heading
);
}
}
ReactDOM.render(
<div>
<Colorizer/>
</div>,
destination
);
</script>
</body>
</html>