Skip to content

Commit f67943b

Browse files
InputColor empty state (#986)
* first commit * Swatch working with empty color but not updating InputColor correctly * allow a default transparent value in InputColor * update InputColor state to handle empty color * fix license header Co-authored-by: Daniel Christopher <[email protected]>
1 parent db807a0 commit f67943b

File tree

6 files changed

+67
-39
lines changed

6 files changed

+67
-39
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@
4040
"xxxsmall",
4141
"xxxxlarge"
4242
],
43-
"prettier.arrowParens": "always"
43+
"prettier.arrowParens": "always",
44+
"extensions.ignoreRecommendations": true,
45+
"files.autoSave": "off",
46+
"debug.allowBreakpointsEverywhere": true
4447
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- update InputColor to have an empty `Swatch`
1213
- `Status` component
1314
- `Tree`, `TreeItem` components
1415
- Includes docs and test suite

packages/components/src/Form/Inputs/InputColor/InputColor.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ import React, {
3333
useEffect,
3434
} from 'react'
3535
import styled from 'styled-components'
36+
import get from 'lodash/get'
3637
import { useID, useWrapEvent } from '../../../utils'
3738
import { usePopover, PopoverContent } from '../../../Popover'
3839
import { InputText, InputTextProps } from '../InputText'
3940
import { useFormContext } from '../../Form'
4041
import { Flex } from '../../../Layout/Flex'
41-
import {
42-
HueSaturation,
43-
polarbrightness2hsv,
44-
SimpleHSV,
45-
white,
46-
} from './ColorWheel/color_wheel_utils'
42+
import { HueSaturation, SimpleHSV } from './ColorWheel/color_wheel_utils'
4743
import { ColorWheel } from './ColorWheel'
4844
import { LuminositySlider } from './LuminositySlider'
4945
import { Swatch } from './Swatch'
@@ -86,8 +82,7 @@ const createEventWithHSVValue = (
8682
}
8783

8884
function getColorFromText(text?: string) {
89-
const initialWhite = polarbrightness2hsv(white())
90-
return text && isValidColor(text) ? str2simpleHsv(text) : initialWhite
85+
return text && isValidColor(text) ? str2simpleHsv(text) : undefined
9186
}
9287

9388
export const InputColorComponent = forwardRef(
@@ -106,10 +101,9 @@ export const InputColorComponent = forwardRef(
106101
) => {
107102
const inputID = useID(id)
108103
const validationMessage = useFormContext(props)
109-
const initialWhite = polarbrightness2hsv(white())
110104
const initialColor = getColorFromText(value || defaultValue)
111105

112-
const [color, setColor] = useState<SimpleHSV>(initialColor)
106+
const [color, setColor] = useState<SimpleHSV | undefined>(initialColor)
113107
const [inputTextValue, setInputTextValue] = useState(
114108
value || defaultValue || ''
115109
)
@@ -127,7 +121,7 @@ export const InputColorComponent = forwardRef(
127121
}
128122
}, [isFocused, value, inputTextValue])
129123

130-
const callOnChange = (newColor: SimpleHSV | string) => {
124+
const callOnChange = (newColor?: SimpleHSV | string) => {
131125
if (!onChange || !newColor) return
132126
onChange(createEventWithHSVValue(newColor))
133127
}
@@ -138,12 +132,13 @@ export const InputColorComponent = forwardRef(
138132
callOnChange(newColor)
139133
}
140134

141-
const handleColorChange = (hs: HueSaturation) =>
142-
setColorState({ ...hs, v: color.v })
135+
const handleColorChange = (hs: HueSaturation) => {
136+
setColorState({ h: 0, s: 100, ...hs, v: get(color, 'v', 1) })
137+
}
143138

144139
const handleSliderChange = (event: FormEvent<HTMLInputElement>) =>
145140
setColorState({
146-
...color,
141+
...(color || { h: 0, s: 100, v: 100 }),
147142
v: Number(event.currentTarget.value) / 100,
148143
})
149144

@@ -152,24 +147,24 @@ export const InputColorComponent = forwardRef(
152147
setInputTextValue(newValue)
153148

154149
const isValid = isValidColor(newValue)
155-
callOnChange(isValid ? newValue : initialWhite)
150+
callOnChange(isValid ? newValue : undefined)
156151
setColor(getColorFromText(event.currentTarget.value))
157152
}
158153

159154
const content = (
160155
<PopoverContent display="flex" flexDirection="column">
161156
<ColorWheel
162157
size={colorWheelSize}
163-
hue={color.h}
164-
saturation={color.s}
165-
value={color.v}
158+
hue={get(color, 'h')}
159+
saturation={get(color, 's')}
160+
value={get(color, 'v')}
166161
onColorChange={handleColorChange}
167162
/>
168163
<LuminositySlider
169164
min={0}
170165
max={100}
171166
step={1}
172-
value={color.v * 100}
167+
value={get(color, 'v', 1) * 100}
173168
width={colorWheelSize}
174169
onChange={handleSliderChange}
175170
/>
@@ -182,7 +177,7 @@ export const InputColorComponent = forwardRef(
182177
<Flex>
183178
<Swatch
184179
ref={triggerRef}
185-
color={hsv2hex(color)}
180+
color={color ? hsv2hex(color) : undefined}
186181
borderRadius={hideInput ? 'medium' : 'none'}
187182
borderTopLeftRadius="medium"
188183
borderBottomLeftRadius="medium"

packages/components/src/Form/Inputs/InputColor/Swatch/Swatch.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
SOFTWARE.
2424
2525
*/
26+
2627
import omit from 'lodash/omit'
2728
import styled from 'styled-components'
2829
import { CompatibleHTMLProps, reset } from '@looker/design-tokens'
@@ -51,25 +52,38 @@ export interface SwatchProps
5152
color?: string
5253
}
5354

55+
const emptySwatch = `position: relative;
56+
&:after {
57+
position: absolute;
58+
display: block;
59+
content: '';
60+
height: 1px;
61+
width: calc(100% + 12px);
62+
background: red;
63+
/* left: 50%; */
64+
top: 50%;
65+
margin-left: -6px;
66+
transform: rotate(-45deg);
67+
}`
68+
5469
export const Swatch = styled.div<SwatchProps>`
5570
${reset}
5671
${width}
5772
${height}
5873
${border}
59-
6074
&:hover {
6175
${inputTextHover}
6276
}
63-
6477
${(props) => (props.disabled ? inputTextDisabled : '')}
65-
6678
background-color: ${(props) => props.color};
6779
margin-top: auto;
6880
flex-shrink: 0;
81+
82+
${(props) => props.color === 'transparent' && emptySwatch}
6983
`
7084

7185
Swatch.defaultProps = {
7286
...omit(inputTextDefaults, 'fontSize'),
73-
color: 'white',
87+
color: 'transparent',
7488
width: inputTextDefaults.height,
7589
}

packages/components/src/Form/Inputs/InputColor/Swatch/__snapshots__/Swatch.test.tsx.snap

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ exports[`Default Swatch 1`] = `
77
border: solid 1px;
88
border-color: #DEE1E5;
99
border-radius: 0.25rem;
10-
background-color: white;
10+
background-color: transparent;
1111
margin-top: auto;
1212
-webkit-flex-shrink: 0;
1313
-ms-flex-negative: 0;
1414
flex-shrink: 0;
15+
position: relative;
1516
}
1617
1718
.c0:hover {
1819
border-color: #C1C6CC;
1920
}
2021
22+
.c0:after {
23+
position: absolute;
24+
display: block;
25+
content: '';
26+
height: 1px;
27+
width: calc(100% + 12px);
28+
background: red;
29+
top: 50%;
30+
margin-left: -6px;
31+
-webkit-transform: rotate(-45deg);
32+
-ms-transform: rotate(-45deg);
33+
transform: rotate(-45deg);
34+
}
35+
2136
<div
2237
className="c0"
23-
color="white"
38+
color="transparent"
2439
height="36px"
2540
width="36px"
2641
/>

packages/playground/src/index.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@
2525
*/
2626
import React from 'react'
2727
import { render } from 'react-dom'
28-
import { ComponentsProvider } from '@looker/components'
29-
import { ComboboxDemo } from './Select/ComboboxDemo'
30-
import { SelectContent } from './Select/SelectDemo'
31-
import { SelectMultiDemo } from './Select/SelectMultiDemo'
32-
33-
const App = () => (
34-
<ComponentsProvider>
35-
<ComboboxDemo />
36-
<SelectContent />
37-
<SelectMultiDemo />
38-
</ComponentsProvider>
39-
)
28+
import { ComponentsProvider, InputColor, Swatch } from '@looker/components'
29+
30+
const App = () => {
31+
return (
32+
<ComponentsProvider>
33+
<InputColor />
34+
<InputColor value="green" />
35+
<InputColor defaultValue="purple" />
36+
<Swatch />
37+
</ComponentsProvider>
38+
)
39+
}
4040

4141
/*
4242
This is the binding site for the playground. If you want to edit the

0 commit comments

Comments
 (0)