From 7ae52f116425e8e7b9237015b7a1afd13c6c644e Mon Sep 17 00:00:00 2001 From: Guillermo Peralta Scura Date: Sun, 19 Jan 2020 18:27:36 -0300 Subject: [PATCH] Add ColorInput component (#455) * Add ColorInput component * Use valid color on input blur * Darken input text and add labels --- src/components/ColorPicker.css | 27 ++++--- src/components/ColorPicker.tsx | 127 ++++++++++++++++++++------------- 2 files changed, 96 insertions(+), 58 deletions(-) diff --git a/src/components/ColorPicker.css b/src/components/ColorPicker.css index 2accc9df..e2920fe8 100644 --- a/src/components/ColorPicker.css +++ b/src/components/ColorPicker.css @@ -7,6 +7,11 @@ position: relative; } +.color-picker-control-container { + display: flex; + align-items: center; +} + .color-picker-triangle-shadow { width: 0px; height: 0px; @@ -33,13 +38,17 @@ padding: 15px 9px 9px 15px; } +.colors-gallery { + display: flex; + flex-wrap: wrap; +} + .color-picker-swatch { + position: relative; height: 30px; width: 30px; cursor: pointer; - position: relative; outline: none; - float: left; border-radius: 4px; margin: 0px 6px 6px 0px; box-sizing: border-box; @@ -69,17 +78,20 @@ height: 30px; width: 30px; border-radius: 4px 0px 0px 4px; - float: left; - color: #868e96; + color: #495057; display: flex; align-items: center; justify-content: center; } +.color-input-container { + display: flex; +} + .color-picker-input { width: 100px; font-size: 14px; - color: #868e96; + color: #343a40; border: 0px; outline: none; height: 28px; @@ -92,9 +104,8 @@ } .color-picker-label-swatch { - height: 24px; - width: 24px; - display: inline-block; + height: 30px; + width: 30px; margin-right: 4px; border: 1px solid #dee2e6; } diff --git a/src/components/ColorPicker.tsx b/src/components/ColorPicker.tsx index 181f4492..508ba1cd 100644 --- a/src/components/ColorPicker.tsx +++ b/src/components/ColorPicker.tsx @@ -15,71 +15,105 @@ const Picker = function({ color: string | undefined; onChange: (color: string) => void; }) { - const [innerValue, setInnerValue] = React.useState(color); - React.useEffect(() => { - setInnerValue(color); - }, [color]); return (
- {colors.map(color => ( -
{ - onChange(color); - }} - title={color} - tabIndex={0} - style={{ backgroundColor: color }} - key={color} - > - {color === "transparent" ? ( -
- ) : ( - undefined - )} -
- ))} -
#
-
- { - 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(/^#/, "")} - /> +
+ {colors.map(color => ( +
{ + onChange(color); + }} + title={color} + tabIndex={0} + style={{ backgroundColor: color }} + key={color} + > + {color === "transparent" ? ( +
+ ) : ( + undefined + )} +
+ ))}
-
+ { + onChange(color); + }} + />
); }; +function ColorInput({ + color, + onChange +}: { + color: string | undefined; + onChange: (color: string) => void; +}) { + const colorRegex = /^([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8}|transparent)$/; + const [innerValue, setInnerValue] = React.useState(color); + + React.useEffect(() => { + setInnerValue(color); + }, [color]); + + return ( +
+
#
+ { + const value = e.target.value; + if (value.match(colorRegex)) { + onChange(value === "transparent" ? "transparent" : "#" + value); + } + setInnerValue(value); + }} + value={(innerValue || "").replace(/^#/, "")} + onPaste={e => onChange(e.clipboardData.getData("text"))} + onBlur={() => setInnerValue(color)} + /> +
+ ); +} + export function ColorPicker({ type, color, onChange }: { type: "canvasBackground" | "elementBackground" | "elementStroke"; - color: string | null; + color: string | undefined; onChange: (color: string) => void; }) { const [isActive, setActive] = React.useState(false); + return (
-
{isActive ? ( setActive(false)}> @@ -93,13 +127,6 @@ export function ColorPicker({ ) : null} - onChange(e.clipboardData.getData("text"))} - onChange={e => onChange(e.target.value)} - />
); }