Skip to content

Commit

Permalink
Add close on tab flag to dropdown controller #562
Browse files Browse the repository at this point in the history
  • Loading branch information
caebr committed Dec 20, 2023
1 parent 52b74da commit 6eee94d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class Header extends LitElement {
this.serviceNavElement?.shadowRoot?.querySelector("bkd-hamburger") ??
null,
() => this.mobileNavElement?.shadowRoot ?? null,
false,
);

private handleLogoClick(event: MouseEvent) {
Expand Down
1 change: 1 addition & 0 deletions src/components/Header/NavGroupToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class NavGroupToggle extends LitElement {
this.menuElement?.shadowRoot?.querySelector<HTMLElement>(
'ul[role="menu"]',
) ?? null,
true,
{
queryItems: () =>
this.menuElement?.shadowRoot?.querySelectorAll<HTMLElement>(
Expand Down
10 changes: 10 additions & 0 deletions src/components/Header/NotificationsToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ export class NotificationsToggle extends LitElement {
this,
() => this.toggleElement ?? null,
() => this.dropdownElement?.shadowRoot ?? null,
true,
{
queryItems: () =>
this.dropdownElement?.shadowRoot?.querySelectorAll<HTMLElement>(
"button",
) ?? null,
queryFocused: () =>
(this.dropdownElement?.shadowRoot?.activeElement ??
null) as HTMLElement | null,
},
);

private handleDeleteAllNotifications() {
Expand Down
1 change: 1 addition & 0 deletions src/components/Header/SubstitutionsToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class SubstitutionsToggle extends LitElement {
this,
() => this.toggleElement ?? null,
() => this.menuElement?.shadowRoot ?? null,
this.isLargeScreen(),
{
queryItems: () =>
this.menuElement?.shadowRoot?.querySelectorAll<HTMLElement>(
Expand Down
1 change: 1 addition & 0 deletions src/components/Header/UserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class UserSettings extends LitElement {
this,
() => this.toggleElement ?? null,
() => this.menuElement ?? null,
true,
{
queryItems: () =>
this.shadowRoot?.querySelectorAll<HTMLElement>("a[role='menuitem']") ??
Expand Down
13 changes: 11 additions & 2 deletions src/controllers/dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class DropdownController implements ReactiveController {
private host: ReactiveControllerHost & HTMLElement,
private queryToggleElement: () => HTMLElement | ShadowRoot | null,
private queryMenuElement: () => HTMLElement | ShadowRoot | null,
private closeOnTab: boolean,
private itemQueries?: {
/**
* Returns all dropdown items
Expand Down Expand Up @@ -78,7 +79,9 @@ export class DropdownController implements ReactiveController {
// Make sure to register events after rendering, for elements to
// be present
setTimeout(() => {
this.menuElement?.addEventListener("focusout", this.closeOnBlur, true);
if (!this.closeOnTab) {
this.menuElement?.addEventListener("focusout", this.closeOnBlur, true);
}

// To detect clicks into iframe, add event listener to iframe
// document
Expand All @@ -94,7 +97,9 @@ export class DropdownController implements ReactiveController {
}

private removeListeners(): void {
this.menuElement?.removeEventListener("focusout", this.closeOnBlur, true);
if (!this.closeOnTab) {
this.menuElement?.removeEventListener("focusout", this.closeOnBlur, true);
}
document.removeEventListener("click", this.handleDocumentClick, true);
this.iframeDocument?.removeEventListener(
"click",
Expand Down Expand Up @@ -141,6 +146,10 @@ export class DropdownController implements ReactiveController {

private handleKeydown = (event: KeyboardEvent) => {
switch (event.key) {
case "Tab": {
if (this.closeOnTab) this.close();
break;
}
case "Escape": {
this.close();
break;
Expand Down

0 comments on commit 6eee94d

Please sign in to comment.