Skip to content

Commit 95bb4ec

Browse files
author
Melissa Thompson
authored
refactor(site): programmatic focus is component-specific (#2320)
1 parent 9752d02 commit 95bb4ec

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

site/resources/js/enhancement.js

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,49 @@ var NAVIGATION_KEYS = [
963963
'Esc'
964964
];
965965

966+
var FOCUS_COMPONENTS = [
967+
'assetlist',
968+
'button',
969+
'calendar',
970+
'card',
971+
'closebutton',
972+
'colorarea',
973+
'colorhandle',
974+
'colorslider',
975+
'colorwheel',
976+
'combobox',
977+
'menu',
978+
'picker',
979+
'pickerbutton',
980+
'rating',
981+
'sidenav',
982+
'slider',
983+
'steplist',
984+
'stepper',
985+
'table',
986+
'tag',
987+
'tooltip'
988+
];
989+
990+
var KEYBOARD_FOCUS_COMPONENTS = [
991+
'closebutton',
992+
'combobox',
993+
'datepicker',
994+
'pickerbutton',
995+
'sidenav',
996+
'stepper',
997+
'table',
998+
];
999+
1000+
// If pathname matches a component in the focus or keyboard focus arrays,
1001+
// we know that component should get/is setup to handle the focus class
1002+
function getsFocusClasses(componentArray) {
1003+
return componentArray.some((componentPath) => {
1004+
const currentPath = window.location.pathname;
1005+
return currentPath.includes(componentPath);
1006+
});
1007+
}
1008+
9661009
var keyboardFocus = false;
9671010

9681011
function onKeydownHandler(event) {
@@ -971,25 +1014,26 @@ function onKeydownHandler(event) {
9711014
}
9721015
keyboardFocus = true;
9731016

974-
if (document.activeElement &&
975-
document.activeElement !== document.body) {
976-
document.activeElement.classList.add('is-keyboardFocused');
1017+
if (getsFocusClasses(KEYBOARD_FOCUS_COMPONENTS)
1018+
&& document.activeElement
1019+
&& document.activeElement !== document.body) {
1020+
document.activeElement.classList.add('is-keyboardFocused');
9771021
}
9781022
}
9791023

9801024
function onMousedownHandler() {
9811025
keyboardFocus = false;
9821026

983-
if (document.activeElement &&
984-
document.activeElement !== document.body) {
985-
document.activeElement.classList.add('is-focused');
1027+
if (getsFocusClasses(FOCUS_COMPONENTS)
1028+
&& document.activeElement
1029+
&& document.activeElement !== document.body) {
1030+
document.activeElement.classList.add('is-focused');
9861031
}
9871032
}
9881033

989-
// Programmatic focus
9901034
function onFocusHandler(event) {
9911035
var classList = event.target.classList;
992-
if (classList && keyboardFocus) {
1036+
if (classList && keyboardFocus && getsFocusClasses(KEYBOARD_FOCUS_COMPONENTS)) {
9931037
classList.add('is-keyboardFocused');
9941038
}
9951039
}

0 commit comments

Comments
 (0)