From 4938e775cf3599c064053113eef341246e1ab233 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 29 Mar 2021 20:49:27 +0200 Subject: [PATCH 1/2] Fix color input styling --- lib/components/src/controls/Color.tsx | 14 +++++++++++--- lib/components/src/tooltip/TooltipNote.tsx | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/components/src/controls/Color.tsx b/lib/components/src/controls/Color.tsx index f07ab6a0fef1..806d6ca7cfa4 100644 --- a/lib/components/src/controls/Color.tsx +++ b/lib/components/src/controls/Color.tsx @@ -23,6 +23,7 @@ const PickerTooltip = styled(WithTooltip)({ }); const TooltipContent = styled.div({ + width: 200, margin: 5, '.react-colorful__saturation': { @@ -36,6 +37,10 @@ const TooltipContent = styled.div({ }, }); +const Note = styled(TooltipNote)(({ theme }) => ({ + fontFamily: theme.typography.fonts.base, +})); + const Swatches = styled.div({ display: 'grid', gridTemplateColumns: 'repeat(9, 16px)', @@ -62,11 +67,13 @@ const Swatch = ({ value, active, onClick, style, ...props }: SwatchProps) => { return ; }; -const Input = styled(Form.Input)({ +const Input = styled(Form.Input)(({ theme }) => ({ width: '100%', paddingLeft: 30, paddingRight: 30, -}); + boxSizing: 'border-box', + fontFamily: theme.typography.fonts.base, +})); const ToggleIcon = styled(Icons)(({ theme }) => ({ position: 'absolute', @@ -76,6 +83,7 @@ const ToggleIcon = styled(Icons)(({ theme }) => ({ width: 20, height: 20, padding: 4, + boxSizing: 'border-box', cursor: 'pointer', color: theme.input.color, })); @@ -278,7 +286,7 @@ export const ColorControl: FC = ({ } + tooltip={} > = ({ note }) => { - return {note}; +export const TooltipNote: FunctionComponent = ({ note, ...props }) => { + return {note}; }; From cdb8777f904eecf948138767b1b22e58051cf3c7 Mon Sep 17 00:00:00 2001 From: Gert Hengeveld Date: Mon, 29 Mar 2021 22:17:05 +0200 Subject: [PATCH 2/2] Fix invalid presets --- lib/components/src/controls/Color.tsx | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/components/src/controls/Color.tsx b/lib/components/src/controls/Color.tsx index 806d6ca7cfa4..2f72ba42b476 100644 --- a/lib/components/src/controls/Color.tsx +++ b/lib/components/src/controls/Color.tsx @@ -102,6 +102,7 @@ const HEX_REGEXP = /^\s*#?([0-9a-f]{3}|[0-9a-f]{6})\s*$/i; const SHORTHEX_REGEXP = /^\s*#?([0-9a-f]{3})\s*$/i; type ParsedColor = { + valid: boolean; value: string; keyword: string; colorSpace: ColorSpace; @@ -131,11 +132,13 @@ const stringToArgs = (value: string) => { const parseValue = (value: string): ParsedColor => { if (!value) return undefined; + let valid = true; if (RGB_REGEXP.test(value)) { const [r, g, b, a] = stringToArgs(value); const [h, s, l] = convert.rgb.hsl([r, g, b]) || [0, 0, 0]; return { + valid, value, keyword: convert.rgb.keyword([r, g, b]), colorSpace: ColorSpace.RGB, @@ -149,6 +152,7 @@ const parseValue = (value: string): ParsedColor => { const [h, s, l, a] = stringToArgs(value); const [r, g, b] = convert.hsl.rgb([h, s, l]) || [0, 0, 0]; return { + valid, value, keyword: convert.hsl.keyword([h, s, l]), colorSpace: ColorSpace.HSL, @@ -166,7 +170,18 @@ const parseValue = (value: string): ParsedColor => { if (/[^#a-f0-9]/i.test(value)) mapped = plain; else if (HEX_REGEXP.test(value)) mapped = `#${plain}`; + if (mapped.startsWith('#')) { + valid = HEX_REGEXP.test(mapped); + } else { + try { + convert.keyword.hex(mapped as any); + } catch (e) { + valid = false; + } + } + return { + valid, value: mapped, keyword: convert.rgb.keyword(rgb), colorSpace: ColorSpace.HEX, @@ -177,7 +192,7 @@ const parseValue = (value: string): ParsedColor => { }; const getRealValue = (value: string, color: ParsedColor, colorSpace: ColorSpace) => { - if (!value) return fallbackColor[colorSpace]; + if (!value || !color?.valid) return fallbackColor[colorSpace]; if (colorSpace !== ColorSpace.HEX) return color?.[colorSpace] || fallbackColor[colorSpace]; if (!color.hex.startsWith('#')) { try { @@ -244,7 +259,7 @@ const usePresets = ( const addPreset = useCallback( (color) => { - if (!color?.[colorSpace]) return; + if (!color?.valid) return; if (presets.some((preset) => id(preset[colorSpace]) === id(color[colorSpace]))) return; setSelectedColors((arr) => arr.concat(color)); },