Skip to content

Commit 1d31778

Browse files
committed
Golf a bit internal/ui/static/js/modal_handler.js
- Re-use an existing function instead of copy-pasting its code - Use optional chaining instead of a condition - Use the modern `.at` method instead of `[focusableElements.length - 1], as it's supported on all browsers since 2016: https://caniuse.com/mdn-javascript_builtins_string_at
1 parent 99450b6 commit 1d31778

File tree

2 files changed

+5
-10
lines changed

2 files changed

+5
-10
lines changed

Diff for: .github/workflows/linters.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
run: |
1818
1919
- name: Run jshint
20-
run: jshint internal/ui/static/js/*.js
20+
run: jshint --config .jshintrc internal/ui/static/js/*.js
2121
- name: Run ESLint
2222
run: eslint internal/ui/static/js/*.js
2323

Diff for: internal/ui/static/js/modal_handler.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
class ModalHandler {
22
static exists() {
3-
return document.getElementById("modal-container") !== null;
3+
return getModalContainer() !== null;
44
}
55

66
static getModalContainer() {
77
return document.getElementById("modal-container");
88
}
99

1010
static getFocusableElements() {
11-
const container = this.getModalContainer();
12-
13-
if (container === null) {
14-
return null;
15-
}
16-
17-
return container.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
11+
const container = this.getModalContainer()
12+
return container?.querySelectorAll('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');
1813
}
1914

2015
static setupFocusTrap() {
@@ -25,7 +20,7 @@ class ModalHandler {
2520
}
2621

2722
const firstFocusableElement = focusableElements[0];
28-
const lastFocusableElement = focusableElements[focusableElements.length - 1];
23+
const lastFocusableElement = focusableElements.at(-1);
2924

3025
this.getModalContainer().onkeydown = (e) => {
3126
if (e.key !== 'Tab') {

0 commit comments

Comments
 (0)