Skip to content

Commit

Permalink
feat(select): close on escape
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjahr committed Apr 30, 2024
1 parent e976994 commit 36af251
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ export class CdkOverlayDirective {
backdropClass: this.backdropClass(),
panelClass: this.panelClass(),
positionStrategy,
scrollStrategy: this.overlay.scrollStrategies.reposition({
autoClose: true,
}),
});

this.syncOverlayWidth();
Expand Down
1 change: 1 addition & 0 deletions libs/sketch/src/lib/components/select/select.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
padding: var(--sk-select-label-padding);
border: 1px solid var(--sk-select-label-border-color);
border-radius: var(--sk-select-label-border-radius);
cursor: pointer;
}

.sk-select-trigger:focus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[skCdkOverlayShow]="panelIsVisible()"
[skCdkOverlayDisposeDelay]="animationDelay()"
[tabIndex]="0"
(click)="togglePanel(true, false)"
(click)="togglePanel(true)"
(keydown.enter)="togglePanel(!this.panelIsVisible())"
(keydown.space)="togglePanel(!this.panelIsVisible())"
(skCdkOverlayVisible)="togglePanel($event)"
Expand Down
19 changes: 12 additions & 7 deletions libs/sketch/src/lib/components/select/select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
computed,
effect,
forwardRef,
HostListener,
inject,
input,
output,
Expand Down Expand Up @@ -39,7 +40,8 @@ export class SelectComponent<T> implements ControlValueAccessor {
animationDelay = input(0);
closeOnSelect = input(false);
multiple = input(false, { transform: booleanAttribute });
readonly autoFocus = signal(false);

readonly autoFocus = signal(true);
readonly selectedValue = signal<T | T[] | undefined>(undefined);
readonly panelIsVisible = signal(false);
readonly showPlaceholder = computed(() => {
Expand All @@ -49,8 +51,16 @@ export class SelectComponent<T> implements ControlValueAccessor {
(!isArray && !selectedValue) || (isArray && selectedValue.length <= 0)
);
});

readonly open = output<boolean>();

@HostListener('document:keydown.escape')
closePanel(): void {
if (this.panelIsVisible()) {
this.togglePanel(false);
}
}

protected readonly updateSelectionMode = effect(
() => {
const selectedValue = untracked(this.selectedValue);
Expand All @@ -73,9 +83,8 @@ export class SelectComponent<T> implements ControlValueAccessor {
{ allowSignalWrites: true }
);

togglePanel(visible: boolean, focus = true): void {
togglePanel(visible: boolean): void {
this.panelIsVisible.set(visible);
this.autoFocus.set(focus);
this.open.emit(visible);
}

Expand Down Expand Up @@ -104,19 +113,15 @@ export class SelectComponent<T> implements ControlValueAccessor {
}

keyArrowUp({ target }: Event): void {
console.log('Arrow Down', target);
if (target instanceof HTMLElement) {
console.log('Element', target.previousElementSibling);
if (target.previousElementSibling instanceof HTMLElement) {
target.previousElementSibling.focus();
}
}
}

keyArrowDown({ target }: Event): void {
console.log('Arrow Down', target);
if (target instanceof HTMLElement) {
console.log('Element', target.nextElementSibling);
if (target.nextElementSibling instanceof HTMLElement) {
target.nextElementSibling.focus();
}
Expand Down

0 comments on commit 36af251

Please sign in to comment.