Skip to content

Commit 8c276f8

Browse files
authored
chore: update javascript for site to account for removal of focus-ring (#2102)
* update javascript for documentation site to account for removal of focus-ring
1 parent 5a692ab commit 8c276f8

File tree

1 file changed

+70
-57
lines changed

1 file changed

+70
-57
lines changed

site/resources/js/enhancement.js

Lines changed: 70 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ governing permissions and limitations under the License.
160160
// Textfield
161161
(function () {
162162
function setFocus(textfield, input, focused) {
163-
var focusClass = input.classList.contains("focus-ring")
163+
var focusClass = input.classList.contains("is-keyboardFocused")
164164
? "is-keyboardFocused"
165165
: "is-focused";
166166
if (focused) {
@@ -188,65 +188,10 @@ governing permissions and limitations under the License.
188188
});
189189
})();
190190

191-
// Inputgroup
192-
(function () {
193-
function setFocus(inputgroup, focused, event) {
194-
var textfields = inputgroup.querySelectorAll(".spectrum-Textfield");
195-
var inputs = inputgroup.querySelectorAll(".spectrum-InputGroup-input");
196-
var input = inputs[0];
197-
var focusClass = event.target.classList.contains("focus-ring")
198-
? "is-keyboardFocused"
199-
: "is-focused";
200-
var pickerButton = inputgroup.querySelector(".spectrum-PickerButton");
201-
if (focused) {
202-
inputgroup.classList.add(focusClass);
203-
if (pickerButton) pickerButton.classList.add(focusClass);
204-
if (event.target.tagName !== "INPUT") {
205-
input.focus();
206-
}
207-
208-
Array.prototype.forEach.call(textfields, (textfield) => {
209-
textfield.classList.add(focusClass);
210-
});
211-
} else {
212-
if (pickerButton) pickerButton.classList.remove("is-keyboardFocused");
213-
if (pickerButton) pickerButton.classList.remove("is-focused");
214-
inputgroup.classList.remove("is-keyboardFocused");
215-
inputgroup.classList.remove("is-focused");
216-
217-
Array.prototype.forEach.call(textfields, (textfield) => {
218-
textfield.classList.remove("is-focused");
219-
textfield.classList.remove("is-keyboardFocused");
220-
});
221-
}
222-
}
223-
224-
document.addEventListener("focusin", function (event) {
225-
var inputgroup = event.target.closest(".spectrum-InputGroup");
226-
227-
if (event.target.closest(".spectrum-Menu")) {
228-
// Don't mess with focus on menuitems
229-
return;
230-
}
231-
232-
if (inputgroup) {
233-
setFocus(inputgroup, true, event);
234-
}
235-
});
236-
237-
document.addEventListener("focusout", function (event) {
238-
var inputgroup = event.target.closest(".spectrum-InputGroup");
239-
240-
if (inputgroup) {
241-
setFocus(inputgroup, false, event);
242-
}
243-
});
244-
})();
245-
246191
// Stepper
247192
(function () {
248193
function setFocus(stepper, input, focused) {
249-
var focusClass = input.classList.contains("focus-ring")
194+
var focusClass = input.classList.contains("is-keyboardFocused")
250195
? "is-keyboardFocused"
251196
: "is-focused";
252197
if (focused) {
@@ -989,6 +934,74 @@ function enhanceAll() {
989934
);
990935
}
991936

937+
// Focus Indicator Classes
938+
var NAVIGATION_KEYS = [
939+
'Tab',
940+
'ArrowUp',
941+
'ArrowRight',
942+
'ArrowDown',
943+
'ArrowLeft',
944+
'Home',
945+
'End',
946+
'PageUp',
947+
'PageDown',
948+
'Enter',
949+
' ',
950+
'Escape',
951+
952+
/* IE9 and Firefox < 37 */
953+
'Up',
954+
'Right',
955+
'Down',
956+
'Left',
957+
'Esc'
958+
];
959+
960+
var keyboardFocus = false;
961+
962+
function onKeydownHandler(event) {
963+
if (event.ctrlKey || event.altKey || event.metaKey || NAVIGATION_KEYS.indexOf(event.key) === -1) {
964+
return;
965+
}
966+
keyboardFocus = true;
967+
968+
if (document.activeElement &&
969+
document.activeElement !== document.body) {
970+
document.activeElement.classList.add('is-keyboardFocused');
971+
}
972+
}
973+
974+
function onMousedownHandler() {
975+
keyboardFocus = false;
976+
977+
if (document.activeElement &&
978+
document.activeElement !== document.body) {
979+
document.activeElement.classList.add('is-focused');
980+
}
981+
}
982+
983+
// Programmatic focus
984+
function onFocusHandler(event) {
985+
var classList = event.target.classList;
986+
if (classList && keyboardFocus) {
987+
classList.add('is-keyboardFocused');
988+
}
989+
}
990+
991+
// Remove classes on focus out
992+
function onFocusOutHandler(event) {
993+
var classList = event.target.classList;
994+
if (classList) {
995+
classList.remove('is-keyboardFocused');
996+
classList.remove('is-focused');
997+
}
998+
}
999+
1000+
window.addEventListener('keydown', onKeydownHandler, true);
1001+
window.addEventListener('focusin', onFocusHandler, true);
1002+
window.addEventListener('focusout', onFocusOutHandler, true);
1003+
window.addEventListener('mousedown', onMousedownHandler, true);
1004+
9921005
animateCircleLoaders();
9931006
window.addEventListener("PageFastLoaded", enhanceAll);
9941007
window.addEventListener("DOMContentLoaded", enhanceAll);

0 commit comments

Comments
 (0)