Skip to content

Commit

Permalink
fix(admin-ui): Make dropdowns keyboard-accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jun 30, 2023
1 parent 79aab66 commit d9c6cdd
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
clr-icon {
margin-right: 3px;
}
&:focus {
outline: var(--color-dropdown-item-focus-outline) solid 1px;
outline-offset: 1px 0;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Component,
ContentChild,
ElementRef,
HostListener,
Input,
OnDestroy,
OnInit,
Expand Down Expand Up @@ -42,7 +43,11 @@ export type DropdownPosition = 'top-left' | 'top-right' | 'bottom-left' | 'botto
<ng-template #menu>
<div class="dropdown open">
<div class="dropdown-menu" [ngClass]="customClasses">
<div class="dropdown-content-wrapper">
<div
class="dropdown-content-wrapper"
[cdkTrapFocus]="true"
[cdkTrapFocusAutoCapture]="true"
>
<ng-content></ng-content>
</div>
</div>
Expand All @@ -56,10 +61,45 @@ export class DropdownMenuComponent implements AfterViewInit, OnInit, OnDestroy {
@Input('vdrPosition') private position: DropdownPosition = 'bottom-left';
@Input() customClasses: string;
@ViewChild('menu', { static: true }) private menuTemplate: TemplateRef<any>;
private menuPortal: TemplatePortal<any>;
private menuPortal: TemplatePortal;
private overlayRef: OverlayRef;
private backdropClickSub: Subscription;

@HostListener('window:keydown.escape', ['$event'])
onEscapeKeydown(event: KeyboardEvent) {
if (this.dropdown.isOpen) {
if (this.overlayRef.overlayElement.contains(document.activeElement)) {
this.dropdown.toggleOpen();
}
}
}

@HostListener('window:keydown', ['$event'])
onArrowKey(event: KeyboardEvent) {
if (
this.dropdown.isOpen &&
document.activeElement instanceof HTMLElement &&
(event.key === 'ArrowDown' || event.key === 'ArrowUp')
) {
const dropdownItems = Array.from(
this.overlayRef.overlayElement.querySelectorAll<HTMLElement>('.dropdown-item'),
);
const currentIndex = dropdownItems.indexOf(document.activeElement);
if (currentIndex === -1) {
return;
}
if (event.key === 'ArrowDown') {
const nextItem = dropdownItems[(currentIndex + 1) % dropdownItems.length];
nextItem.focus();
}
if (event.key === 'ArrowUp') {
const previousItem =
dropdownItems[(currentIndex - 1 + dropdownItems.length) % dropdownItems.length];
previousItem.focus();
}
}
}

constructor(
private overlay: Overlay,
private viewContainerRef: ViewContainerRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { ChangeDetectionStrategy, Component, ElementRef, Input } from '@angular/
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DropdownComponent {
private isOpen = false;
isOpen = false;
private onOpenChangeCallbacks: Array<(isOpen: boolean) => void> = [];
public trigger: ElementRef;
@Input() manualToggle = false;
Expand Down
2 changes: 2 additions & 0 deletions packages/admin-ui/src/lib/core/src/shared/shared.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DragDropModule } from '@angular/cdk/drag-drop';
import { OverlayModule } from '@angular/cdk/overlay';
import { A11yModule } from '@angular/cdk/a11y';
import { CommonModule } from '@angular/common';
import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
Expand Down Expand Up @@ -182,6 +183,7 @@ const IMPORTS = [
TranslateModule,
OverlayModule,
DragDropModule,
A11yModule,
];

const DECLARATIONS = [
Expand Down
2 changes: 2 additions & 0 deletions packages/admin-ui/src/lib/static/styles/theme/default.scss
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@

--color-login-page-bg: var(--color-weight-100);

--color-dropdown-item-focus-outline: rgba(77, 207, 255, 0.53);

// Layout
--layout-content-max-width: 1400px;
--left-nav-width: 0px;
Expand Down

0 comments on commit d9c6cdd

Please sign in to comment.