Skip to content

Commit 05d733c

Browse files
FIX #6193 : bind enter and tab key on color picker input to close color board (#6305)
This is done to improve the keyboard navigation on the property pane
1 parent 1082cb9 commit 05d733c

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

app/client/src/components/ads/ColorPickerComponent.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ interface ColorPickerProps {
151151

152152
function ColorPickerComponent(props: ColorPickerProps) {
153153
const [color, setColor] = React.useState(props.color);
154+
const [isOpen, setOpen] = React.useState(false);
154155
const debouncedOnChange = React.useCallback(
155156
debounce(props.changeColor, 500),
156157
[props.changeColor],
@@ -164,6 +165,7 @@ function ColorPickerComponent(props: ColorPickerProps) {
164165
<Popover
165166
enforceFocus={false}
166167
interactionKind={PopoverInteractionKind.CLICK}
168+
isOpen={isOpen}
167169
minimal
168170
modifiers={{
169171
offset: {
@@ -176,22 +178,41 @@ function ColorPickerComponent(props: ColorPickerProps) {
176178
<StyledInputGroup
177179
leftIcon={
178180
color ? (
179-
<ColorIcon color={color} />
181+
<ColorIcon color={color} onClick={() => setOpen(true)} />
180182
) : (
181-
<NoColorIconWrapper>
183+
<NoColorIconWrapper onClick={() => setOpen(true)}>
182184
<NoColorIcon>
183185
<div className="line" />
184186
</NoColorIcon>
185187
</NoColorIconWrapper>
186188
)
187189
}
190+
onBlur={() => {
191+
// Case 1
192+
// On Tab key press Input loses focus and onKeyUp handler is not called
193+
// To handle Tab key press onBlur event is used instead
194+
// As input will lose focus on Tab press
195+
// Case 2
196+
// if user clicks on ColorBoard blur is called first and color is not updated
197+
// to prevent that make sure to wait for color update before onBlur
198+
setTimeout(() => {
199+
setOpen(false);
200+
}, 100);
201+
}}
188202
onChange={handleChangeColor}
203+
onFocus={() => setOpen(true)}
204+
onKeyUp={(e) => {
205+
if (e.key === "Enter") {
206+
setOpen((state) => !state);
207+
}
208+
}}
189209
placeholder="enter color name or hex"
190210
value={color}
191211
/>
192212
<ColorBoard
193213
selectColor={(color) => {
194214
setColor(color);
215+
setOpen(false);
195216
props.changeColor(color);
196217
}}
197218
selectedColor={color}

0 commit comments

Comments
 (0)