-
Notifications
You must be signed in to change notification settings - Fork 1
/
VariableColorPicker.tsx
55 lines (50 loc) · 1.61 KB
/
VariableColorPicker.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
import { Heading, Text, Label } from "ui";
import React from "react";
function hexToRgb(hex: string) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}
const VariableColorPicker = () => {
const [color, setColor] = React.useState<string>(undefined);
React.useLayoutEffect(() => {
const style = getComputedStyle(document.body);
const defaultValue = style.getPropertyValue("--example-brand");
setColor(defaultValue);
}, []);
return (
<div>
<div className="flex items-center space-x-2">
<Label htmlFor="color-picker">Color Picker</Label>
<input
name="color-picker"
id="color-picker"
type="color"
// TODO: defaultValue is missing
onChange={(e) => {
const { r, g, b } = hexToRgb(e.target.value);
setColor(`${r} ${g} ${b}`);
document.documentElement.style.setProperty(
"--example-brand",
`${r} ${g} ${b}`
);
}}
/>
</div>
<div className="border border-brand/10 -mx-6 p-6 my-6 rounded grid md:grid-cols-3 gap-6 items-center">
<button className="md:col-span-1 border rounded px-3 py-2 border-brand/10 bg-brand/5 text-brand/90">
Look at me!
</button>
<div className="text-sm md:col-span-2">
<code>{`:root { --example-brand: "${color}" }`}</code>
</div>
</div>
</div>
);
};
export default VariableColorPicker;