-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
CreateThemeContent.js
159 lines (142 loc) · 5.13 KB
/
CreateThemeContent.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
import React, { useState, useEffect } from 'react';
import styled from "styled-components";
import { generate } from 'shortid';
import _ from 'lodash';
import { useTheme } from './theme/useTheme';
const Container = styled.div`
display: grid;
gap: 1rem;
grid-template-columns: repeat(2, 1fr);
margin-top: 3rem;
`;
const Section = styled.div`
vertical-align: top;
margin-right: 10px;
padding: 10px;
`;
const Row = styled.div`
padding: 5px;
`;
const Preview = styled.div`
border: 1px solid #000000;
border-radius: 4px;
width: 100%;
height: 200px;
padding: 5px;
`;
const CreateThemeContent = props => {
const defaultTheme = {
themeName: "",
bgColor: "#Cf4307",
txtColor: "#FFFFFF",
btnBgColor: "#000000",
btnTxtColor: "#FFFFFF",
linkColor: "#10BEEA",
font: "Roboto"
};
const { getFonts } = useTheme();
const [state, setState] = useState(defaultTheme);
const [newTheme, setNewTheme] = useState({});
const getThemeObj = () => {
const themeObj = {};
themeObj[_.camelCase(state.themeName)] = {
"id": generate(),
"name": state.themeName,
"colors": {
"body": state.bgColor,
"text": state.txtColor,
"button": {
"text": state.btnBgColor,
"background": state.btnTxtColor
},
"link": {
"text": state.linkColor,
"opacity": 1
}
},
"font": state.font
};
return themeObj;
}
useEffect(() => {
const updated = getThemeObj();
setNewTheme({...updated});
}, [state]);
const handleChange = evt => {
const value = evt.target.value;
setState({
...state,
[evt.target.name]: value
});
}
const createTheme = () => {
setState({...defaultTheme});
props.create(newTheme);
}
return(
<>
<button
style={{float:'right'}}
onClick={ createTheme }
disabled={ state.themeName.trim().length === 0 }>
Happy? Let's Create
</button>
<Container>
<Section>
<Row>
<label htmlFor="th_name">Theme Name:</label> {' '}
<input
type="text"
id="themeName"
name="themeName"
value={ state.themeName }
placeholder="Specify a name"
onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="bg_color">Background Color:</label> {' '}
<input type="color" id="bg_color" name="bgColor" value= { state.bgColor } onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="txt_color">Text Color:</label> {' '}
<input type="color" id="txt_color" name="txtColor" value={ state.txtColor } onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="btn_bg_color">Button Background Color:</label> {' '}
<input type="color" id="btn_bg_color" name="btnBgColor" value={ state.btnBgColor } onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="btn_txt_color">Button Text Color:</label> {' '}
<input type="color" id="btn_txt_color" name="btnTxtColor" value={ state.btnTxtColor } onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="link_color">Link Color:</label> {' '}
<input type="color" id="link_color" name="linkColor" value={ state.linkColor } onChange={ handleChange }/>
</Row>
<Row>
<label htmlFor="font">Select a Font:</label> {' '}
<select name="font" id="font" onChange={ handleChange } value={state.font}>
{getFonts().map((font, index) =>
<option value={ font } key={ index }>{ font }</option>
)}
</select>
</Row>
</Section>
<Section>
<span><b>Preview</b></span>
<Preview style={{backgroundColor: state.bgColor, color: state.txtColor, fontFamily: state.font}}>
<p>
This is for preview only. Pick the color and font from the
left side to see it working.
</p>
<button className="btn" style={{backgroundColor:state.btnBgColor, color:state.btnTxtColor, fontFamily: state.font}}>
I am a Button
</button> {' '}
<a href="#" style={{color:state.linkColor, fontFamily: state.font}}>I am Link</a>
</Preview>
</Section>
</Container>
</>
)
};
export default CreateThemeContent;