Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Focus first element (e.g. an input) when opening the drop modal

Henriette Dargeb505aebfae3c
diff --git a/frontend/src/app/core/setup/globals/global-listeners/top-menu.ts b/frontend/src/app/core/setup/globals/global-listeners/top-menu.ts
index c763ff972ca..ec2b4b55dae 100644
--- a/frontend/src/app/core/setup/globals/global-listeners/top-menu.ts
+++ b/frontend/src/app/core/setup/globals/global-listeners/top-menu.ts
@@ -25,6 +25,8 @@
//
// See COPYRIGHT and LICENSE files for more details.
//++
+import { findAllFocusableElementsWithin } from 'core-app/shared/helpers/focus-helpers';
+
export const ANIMATION_RATE_MS = 100;
export class TopMenu {
@@ -222,8 +224,8 @@ export class TopMenu {
// If there is ANY input, it will have precedence over links,
// i.e. links will only get focused, if there is NO input whatsoever
focusFirstInputOrLink(dropdown:JQuery):void {
- const focusable = dropdown.find('.op-app-menu--dropdown').find('input:not([disabled]):not([type="hidden"]), a[href], area[href], select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]');
- const toFocus = focusable[0];
+ const focusable = findAllFocusableElementsWithin(dropdown.find('.op-app-menu--dropdown')[0]);
+ const toFocus = focusable[0] as HTMLElement;
if (!toFocus) {
return;
}
diff --git a/frontend/src/app/shared/helpers/focus-helpers.ts b/frontend/src/app/shared/helpers/focus-helpers.ts
new file mode 100644
index 00000000000..b96f98dbe4c
--- /dev/null
+++ b/frontend/src/app/shared/helpers/focus-helpers.ts
@@ -0,0 +1,24 @@
+/**
+ * Elements that can receive focus
+ */
+const focusableElements = [
+ 'a[href]:not([tabindex^="-"])',
+ 'area[href]:not([tabindex^="-"])',
+ 'input:not([type="hidden"]):not([type="radio"]):not([disabled]):not([tabindex^="-"])',
+ 'input[type="radio"]:not([disabled]):not([tabindex^="-"])',
+ 'select:not([disabled]):not([tabindex^="-"])',
+ 'textarea:not([disabled]):not([tabindex^="-"])',
+ 'button:not([disabled]):not([tabindex^="-"])',
+ 'iframe:not([tabindex^="-"])',
+ 'audio[controls]:not([tabindex^="-"])',
+ 'video[controls]:not([tabindex^="-"])',
+ '[contenteditable]:not([tabindex^="-"])',
+ '[tabindex]:not([tabindex^="-"]):not(.cdk-focus-trap-anchor)',
+];
+
+/**
+ * Find all focusable element within a given container
+ */
+export function findAllFocusableElementsWithin(container:HTMLElement):NodeListOf<Element> {
+ return container.querySelectorAll(focusableElements.toString());
+}
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts b/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
index 82882f7fb98..a7ff743dbe9 100644
--- a/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
@@ -1,14 +1,16 @@
import {
Component,
+ ElementRef,
+ EventEmitter,
HostBinding,
Input,
- Output,
- EventEmitter,
OnDestroy,
+ Output,
} from '@angular/core';
import { KeyCodes } from 'core-app/shared/helpers/keyCodes.enum';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import SpotDropAlignmentOption from '../../drop-alignment-options';
+import { findAllFocusableElementsWithin } from 'core-app/shared/helpers/focus-helpers';
@Component({
selector: 'spot-drop-modal',
@@ -36,6 +38,9 @@ export class SpotDropModalComponent implements OnDestroy {
setTimeout(() => {
document.body.addEventListener('click', this.closeEventListener);
document.body.addEventListener('keydown', this.escapeListener);
+
+ // Index 1 because the element at index 0 is the trigger button to open the modal
+ (findAllFocusableElementsWithin(this.elementRef.nativeElement)[1] as HTMLElement).focus();
});
} else {
document.body.removeEventListener('click', this.closeEventListener);
@@ -56,7 +61,10 @@ export class SpotDropModalComponent implements OnDestroy {
close: this.i18n.t('js.spot.drop_modal.close'),
};
- constructor(readonly i18n:I18nService) {}
+ constructor(
+ readonly i18n:I18nService,
+ readonly elementRef:ElementRef,
+ ) {}
close():void {
this.open = false;