Skip to content

Commit

Permalink
_content: added 'ESC' event handler to avoid focus trap.
Browse files Browse the repository at this point in the history
https://screencast.googleplex.com/cast/NTYwNTg4MTkwODU1OTg3Mnw3Mzg5NzAyNi04Ng

Change-Id: I4246a560be1dbbd1f7a8126715940373249f5ac8
Reviewed-on: https://go-review.googlesource.com/c/website/+/517778
Auto-Submit: Jamal Carvalho <jamal@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
  • Loading branch information
Prudhvi Krishna Chintala authored and gopherbot committed Aug 10, 2023
1 parent 6cc10c4 commit 343057c
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions _content/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,28 @@ window.initFuncs = [];
e.target.classList.add('forced-closed');
e.target.classList.remove('forced-open');
});
// ensure desktop submenus are closed when esc is pressed
const closeSubmenuOnEsc = e => {

// ensure focus is removed when esc is pressed
const focusOutOnEsc = e => {
if (e.key === 'Escape') {
const forcedOpenItem = document.querySelector('.forced-open');
const target = e.currentTarget;
if (forcedOpenItem) {
forcedOpenItem.classList.remove('forced-open');
forcedOpenItem.blur();
forcedOpenItem.classList.add('forced-closed');
const textarea = document.getElementById('code');
if (e.target == textarea) {
e.preventDefault();
textarea.blur();
}
else {
const forcedOpenItem = document.querySelector('.forced-open');
const target = e.currentTarget;
if (forcedOpenItem) {
forcedOpenItem.classList.remove('forced-open');
forcedOpenItem.blur();
forcedOpenItem.classList.add('forced-closed');
e.target.focus();
}
}
}
e.target.focus();
};
document.addEventListener('keydown', closeSubmenuOnEsc);
document.addEventListener('keydown', focusOutOnEsc);
});

// Mobile menu subnav menus
Expand Down Expand Up @@ -212,18 +220,6 @@ window.initFuncs = [];
handleNavigationDrawerInactive(header);
}

function registerPlaygroundListeners() {
const textarea = document.getElementById('code');
const preContainer = document.querySelector('.Playground-preContainer');

textarea?.addEventListener('keydown', e => {
if (e.key === 'Escape') {
e.preventDefault();
textarea.blur();
}
});
}

/**
* Attempts to detect user's operating system and sets the download
* links accordingly
Expand Down Expand Up @@ -359,7 +355,6 @@ window.initFuncs = [];

const onPageLoad = () => {
registerHeaderListeners();
registerPlaygroundListeners();
setDownloadLinks();
setThemeButtons();
setVersionSpans();
Expand Down

0 comments on commit 343057c

Please sign in to comment.