2020-01-11 19:10:41 -08:00
|
|
|
import React from "react";
|
2020-01-07 07:50:59 +05:00
|
|
|
import { Popover } from "./Popover";
|
2020-01-06 21:29:44 +04:00
|
|
|
|
2020-01-11 19:10:41 -08:00
|
|
|
import "./ColorPicker.css";
|
|
|
|
|
|
|
|
// This is a narrow reimplementation of the awesome react-color Twitter component
|
|
|
|
// https://github.com/casesandberg/react-color/blob/master/src/components/twitter/Twitter.js
|
|
|
|
|
|
|
|
const Picker = function({
|
|
|
|
colors,
|
|
|
|
color,
|
|
|
|
onChange
|
|
|
|
}: {
|
|
|
|
colors: string[];
|
|
|
|
color: string | undefined;
|
|
|
|
onChange: (color: string) => void;
|
|
|
|
}) {
|
|
|
|
const [innerValue, setInnerValue] = React.useState(color);
|
|
|
|
React.useEffect(() => {
|
|
|
|
setInnerValue(color);
|
|
|
|
}, [color]);
|
|
|
|
return (
|
|
|
|
<div className="color-picker">
|
|
|
|
<div className="color-picker-triangle-shadow"></div>
|
|
|
|
<div className="color-picker-triangle"></div>
|
|
|
|
<div className="color-picker-content">
|
|
|
|
{colors.map(color => (
|
|
|
|
<div
|
|
|
|
className="color-picker-swatch"
|
|
|
|
onClick={() => {
|
|
|
|
onChange(color);
|
|
|
|
}}
|
|
|
|
title={color}
|
|
|
|
tabIndex={0}
|
|
|
|
style={{ backgroundColor: color }}
|
2020-01-15 22:07:19 +03:00
|
|
|
key={color}
|
2020-01-11 19:10:41 -08:00
|
|
|
>
|
|
|
|
{color === "transparent" ? (
|
|
|
|
<div className="color-picker-transparent"></div>
|
|
|
|
) : (
|
|
|
|
undefined
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
<div className="color-picker-hash">#</div>
|
|
|
|
<div style={{ position: "relative" }}>
|
|
|
|
<input
|
|
|
|
spellCheck={false}
|
|
|
|
className="color-picker-input"
|
|
|
|
onChange={e => {
|
|
|
|
const value = e.target.value;
|
|
|
|
if (value.match(/^([0-9a-f]{3}|[0-9a-f]{6}|transparent)$/)) {
|
|
|
|
onChange(value === "transparent" ? "transparent" : "#" + value);
|
|
|
|
}
|
|
|
|
setInnerValue(value);
|
|
|
|
}}
|
|
|
|
value={(innerValue || "").replace(/^#/, "")}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div style={{ clear: "both" }}></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-01-07 23:37:22 +05:00
|
|
|
|
2020-01-06 21:29:44 +04:00
|
|
|
export function ColorPicker({
|
2020-01-11 23:58:44 +01:00
|
|
|
type,
|
2020-01-06 21:29:44 +04:00
|
|
|
color,
|
|
|
|
onChange
|
|
|
|
}: {
|
2020-01-11 23:58:44 +01:00
|
|
|
type: "canvasBackground" | "elementBackground" | "elementStroke";
|
2020-01-06 21:29:44 +04:00
|
|
|
color: string | null;
|
|
|
|
onChange: (color: string) => void;
|
|
|
|
}) {
|
|
|
|
const [isActive, setActive] = React.useState(false);
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<button
|
2020-01-15 20:42:02 +05:00
|
|
|
className="color-picker-label-swatch"
|
2020-01-06 21:29:44 +04:00
|
|
|
style={color ? { backgroundColor: color } : undefined}
|
|
|
|
onClick={() => setActive(!isActive)}
|
|
|
|
/>
|
2020-01-07 23:37:22 +05:00
|
|
|
<React.Suspense fallback="">
|
|
|
|
{isActive ? (
|
|
|
|
<Popover onCloseRequest={() => setActive(false)}>
|
2020-01-11 19:10:41 -08:00
|
|
|
<Picker
|
2020-01-11 23:58:44 +01:00
|
|
|
colors={colors[type]}
|
2020-01-07 23:37:22 +05:00
|
|
|
color={color || undefined}
|
|
|
|
onChange={changedColor => {
|
2020-01-11 19:10:41 -08:00
|
|
|
onChange(changedColor);
|
2020-01-07 23:37:22 +05:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Popover>
|
|
|
|
) : null}
|
|
|
|
</React.Suspense>
|
2020-01-06 21:29:44 +04:00
|
|
|
<input
|
|
|
|
type="text"
|
2020-01-15 20:42:02 +05:00
|
|
|
className="color-picker-swatch-input"
|
2020-01-06 21:29:44 +04:00
|
|
|
value={color || ""}
|
2020-01-06 20:28:14 +01:00
|
|
|
onPaste={e => onChange(e.clipboardData.getData("text"))}
|
2020-01-06 21:29:44 +04:00
|
|
|
onChange={e => onChange(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-01-11 23:58:44 +01:00
|
|
|
|
2020-01-16 19:14:00 +02:00
|
|
|
// https://yeun.github.io/open-color/
|
2020-01-11 23:58:44 +01:00
|
|
|
const colors = {
|
2020-01-16 19:14:00 +02:00
|
|
|
// Shade 0
|
2020-01-11 23:58:44 +01:00
|
|
|
canvasBackground: [
|
2020-01-16 19:14:00 +02:00
|
|
|
"#FFFFFF",
|
|
|
|
"#F8F9FA",
|
|
|
|
"#F1F3F5",
|
|
|
|
"#FFF5F5",
|
|
|
|
"#FFF0F6",
|
|
|
|
"#F8F0FC",
|
|
|
|
"#F3F0FF",
|
|
|
|
"#EDF2FF",
|
|
|
|
"#E7F5FF",
|
|
|
|
"#E3FAFC",
|
|
|
|
"#E6FCF5",
|
|
|
|
"#EBFBEE",
|
|
|
|
"#F4FCE3",
|
|
|
|
"#FFF9DB",
|
|
|
|
"#FFF4E6"
|
2020-01-11 23:58:44 +01:00
|
|
|
],
|
2020-01-16 19:14:00 +02:00
|
|
|
// Shade 6
|
2020-01-11 23:58:44 +01:00
|
|
|
elementBackground: [
|
2020-01-16 19:14:00 +02:00
|
|
|
"transparent",
|
|
|
|
"#CED4DA",
|
|
|
|
"#868E96",
|
|
|
|
"#FA5252",
|
|
|
|
"#E64980",
|
|
|
|
"#BE4BDB",
|
|
|
|
"#7950F2",
|
|
|
|
"#4C6EF5",
|
|
|
|
"#228BE6",
|
|
|
|
"#15AABF",
|
|
|
|
"#12B886",
|
|
|
|
"#40C057",
|
|
|
|
"#82C91E",
|
|
|
|
"#FAB005",
|
|
|
|
"#FD7E14"
|
2020-01-11 23:58:44 +01:00
|
|
|
],
|
2020-01-16 19:14:00 +02:00
|
|
|
// Shade 9
|
2020-01-11 23:58:44 +01:00
|
|
|
elementStroke: [
|
2020-01-16 19:14:00 +02:00
|
|
|
"#000000",
|
|
|
|
"#343A40",
|
|
|
|
"#495057",
|
|
|
|
"#C92A2A",
|
|
|
|
"#A61E4D",
|
|
|
|
"#862E9C",
|
|
|
|
"#5F3DC4",
|
|
|
|
"#364FC7",
|
|
|
|
"#1864AB",
|
|
|
|
"#0B7285",
|
|
|
|
"#087F5B",
|
|
|
|
"#2B8A3E",
|
|
|
|
"#5C940D",
|
|
|
|
"#E67700",
|
|
|
|
"#D9480F"
|
2020-01-11 23:58:44 +01:00
|
|
|
]
|
|
|
|
};
|