-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
173 lines (173 loc) · 6.62 KB
/
App.tsx
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
import * as React from 'react';
import Matercolor from 'matercolors';
import {useForm} from 'react-hook-form';
import '../styles/ui.css';
const validateHex = hexcolor => {
let color = hexcolor.charAt(0) !== '#' ? hexcolor : hexcolor.substring(1);
if (color.length === 3) {
return (
'#' +
color
.toUpperCase()
.split('')
.map(function(hex) {
return hex + hex;
})
.join('')
);
}
return hexcolor.charAt(0) !== '#' ? '#' + hexcolor.toUpperCase() : hexcolor.toUpperCase();
};
const App = () => {
// Import the App Logo
const logo = require('../assets/logo.svg');
// keep track of button clicks
const [clicks, setClicks] = React.useState(0);
// Form Registration and Handling Actions
const {register, handleSubmit, watch, getValues, setValue} = useForm({
defaultValues: {
paletteName: 'My Cool Palette',
hexColor: '#00BFD8',
hexName: '#00BFD8',
showAccents: false,
selectType: 'p',
makeStyles: false,
},
});
const watchColor = watch('hexColor');
const watchName = watch('hexName');
// Send your data to Figma UI for processing
const createPalette = handleSubmit(data => {
const complementary = getValues('showComplementary');
setClicks(complementary ? clicks + 2 : clicks + 1);
let response = Object.assign({}, data);
response['palette'] = new Matercolor(response.hexColor).palette;
response['clicks'] = clicks;
parent.postMessage(
{
pluginMessage: {
type: 'create-palette',
response,
},
},
'*'
);
});
// Ask Figma UI to execute Cancel Operation
const onCancel = handleSubmit(() => {
parent.postMessage(
{
pluginMessage: {
type: 'cancel',
},
},
'*'
);
});
// Listen to Figma UI's response to your data
React.useEffect(() => {
window.onmessage = event => {
const {type, message} = event.data.pluginMessage;
if (type === 'create-palette') {
console.log(`Figma Says: ${message}`);
}
};
}, []);
return (
<div className="flex column flex-wrap ml-xsmall mr-xxxsmall">
<div className="flex-grow p-xsmall align-self-center">
<img src={logo} />
</div>
<form className="flex-grow p-xxsmall align-self-center">
<label className="section-title" htmlFor="paletteName">
Name your Palette
</label>
<div className="flex align-self-center mr-xxsmall">
<input
className="input__field flex-grow"
id="paletteName"
type="text"
name="paletteName"
ref={register}
/>
</div>
<label className="section-title" htmlFor="hexColor">
Hex Color
</label>
<div className="flex align-self-center mr-xxsmall">
<input
className="input__field"
style={{width: 32}}
id="hexColor"
type="color"
name="hexColor"
ref={register}
onChange={e => watchColor && setValue('hexName', validateHex(e.target.value))}
/>
<input
className="input__field flex-grow"
id="hexName"
type="text"
name="hexName"
ref={register}
onChange={e => watchName && setValue('hexColor', validateHex(e.target.value))}
/>
</div>
<label className="section-title" htmlFor="selectType">
Palette Type
</label>
<div className="flex align-self-center mr-xxsmall">
<select
id="selectType"
name="selectType"
className="select-menu select-menu__button"
ref={register}
>
<option value="p">Primary</option>
<option value="c">Complementary</option>
<option value="a1">Analogous Primary</option>
<option value="a2">Analogous Secondary</option>
<option value="t1">Triadic Primary</option>
<option value="t2">Triadic Secondary</option>
</select>
</div>
<label className="section-title">Optional Features</label>
<div className="flex align-self-center mr-xxsmall">
<div className="checkbox">
<input
name="showAccents"
id="showAccents"
type="checkbox"
className="checkbox__box"
ref={register}
/>
<label htmlFor="showAccents" className="checkbox__label">
Make Accents
</label>
</div>
<div className="checkbox">
<input
name="makeStyles"
id="makeStyles"
type="checkbox"
className="checkbox__box"
ref={register}
/>
<label htmlFor="makeStyles" className="checkbox__label">
Make Paint Styles
</label>
</div>
</div>
<div className="flex align-self-center mt-xsmall w-100">
<button className="button button--primary-destructive mr-xsmall w-30" onClick={onCancel}>
Cancel
</button>
<button className="button button--primary w-60" onClick={createPalette}>
Create Palette
</button>
</div>
</form>
</div>
);
};
export default App;