Skip to content

Commit

Permalink
fix(color-picker): set focus on swatches when the palette popover is …
Browse files Browse the repository at this point in the history
…opened

This improves accessibility and keyboard navigation.
  • Loading branch information
Kiarokh committed Apr 4, 2024
1 parent 540ea0a commit fe7d91a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/color-picker/color-picker-palette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { brightnesses, colors, getColorName, getCssColor } from './swatches';
*/
@Component({
tag: 'limel-color-picker-palette',
shadow: true,
shadow: { delegatesFocus: true },
styleUrl: 'color-picker-palette.scss',
})
export class Palette implements FormComponent {
Expand Down
20 changes: 20 additions & 0 deletions src/components/color-picker/color-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ export class ColorPicker implements FormComponent {
@State()
private isOpen = false;

public componentDidRender() {
if (this.shouldFocus && this.isOpen) {
this.shouldFocus = false;
this.contentElement?.focus();
}
}

private contentElement?: HTMLLimelColorPickerPaletteElement;

private shouldFocus = false;

public render() {
return [
this.renderTooltip(),
Expand Down Expand Up @@ -109,6 +120,7 @@ export class ColorPicker implements FormComponent {
>
{this.renderPickerTrigger()}
<limel-color-picker-palette
ref={this.setColorPickerPaletteElement}
value={this.value}
label={this.label}
helperText={this.helperText}
Expand All @@ -134,9 +146,17 @@ export class ColorPicker implements FormComponent {
);
};

private setColorPickerPaletteElement = (
element: HTMLLimelColorPickerPaletteElement,
) => {
this.contentElement = element;
};

private openPopover = (event: MouseEvent) => {
event.stopPropagation();
this.isOpen = true;

this.shouldFocus = this.isOpen;
};

private onPopoverClose = (event: CustomEvent) => {
Expand Down

0 comments on commit fe7d91a

Please sign in to comment.