Refactor Escape key press event

This commit is contained in:
Helen Chong 2024-07-05 17:21:50 +08:00
parent ae0d9c15fb
commit df9c3859d2
1 changed files with 8 additions and 12 deletions

View File

@ -138,16 +138,12 @@ https://kalechips.net/projects/snippets/burger #}
</style> </style>
<script> <script>
window.onload = () => {
const navbarBurger = document.querySelector(".navbar__burger"); const navbarBurger = document.querySelector(".navbar__burger");
navbarBurger.addEventListener("keydown", (event) => { navbarBurger.addEventListener("keydown", (event) => {
const key = event.code; if (event.key == "Escape") {
if (key == "Escape") {
navbarBurger.removeAttribute("open"); navbarBurger.removeAttribute("open");
navbarBurger.ariaExpanded = false; navbarBurger.ariaExpanded = false;
document.querySelector(".navbar__toggle").focus(); document.querySelector(".navbar__toggle").focus();
} }
}); });
};
</script> </script>