Skip to content

Commit

Permalink
[DISCO-146] Fix colorpicker focus
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Mar 21, 2024
1 parent b78ee4d commit 6cfe5e8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/app/content/highlights/components/ColorPicker.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('ColorPicker', () => {
const onRemove = jest.fn();
const {root} = renderToDom(
<TestContainer>
<ColorPicker onChange={onChange} onRemove={onRemove} />
<ColorPicker color={highlightStyles[0].label} onChange={onChange} onRemove={onRemove} />
</TestContainer>
);
const rg = root.querySelector('[role="radiogroup"]') as HTMLDivElement;
Expand Down
22 changes: 9 additions & 13 deletions src/app/content/highlights/components/ColorPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,18 @@ const ColorPicker = ({className, ...props}: Props) => {
},
[]
);
const initialFocus = React.useCallback(
(event: React.FocusEvent) => {
if (!ref.current || event.currentTarget !== event.target) {
return;
}
const firstChecked = ref.current.querySelector<HTMLInputElement>('input[checked]');

if (firstChecked) {
firstChecked.focus();
} else {
ref.current.querySelector<HTMLInputElement>('input')?.focus();
const color = (props as SingleSelectProps).color;
const focusOnSelected = React.useCallback(
() => {
if (color && document?.activeElement === ref.current) {
ref.current?.querySelector<HTMLInputElement>(`input[name="${color}"]`)?.focus();
}
},
[]
[color]
);

React.useEffect(focusOnSelected, [focusOnSelected]);

return (
<div
className={className}
Expand All @@ -128,7 +124,7 @@ const ColorPicker = ({className, ...props}: Props) => {
aria-label='colors'
ref={ref}
onKeyDown={handleKeyNavigation}
onFocus={initialFocus}
onFocus={focusOnSelected}
>
{highlightStyles.map((style) => <ColorButton key={style.label}
name={style.label}
Expand Down

0 comments on commit 6cfe5e8

Please sign in to comment.