Focus menu on open and key events

This commit is contained in:
Conor_Mack 2020-06-29 13:38:08 +01:00
parent 42530d215e
commit fab2a4d23b
1 changed files with 14 additions and 19 deletions

View File

@ -26,20 +26,22 @@
onMount(() => { onMount(() => {
if (select) { if (select) {
select.addEventListener("keydown", addSelectKeyEvents) select.addEventListener("keydown", handleEnter)
selectMenu.addEventListener("keydown", handleEscape)
} }
containerEl = document.querySelector(".design-view-property-groups")
return () => { return () => {
select.removeEventListener("keydown", addSelectKeyEvents) select.removeEventListener("keydown", handleEnter)
containerEl.removeEventListener("scroll", disableScroll) selectMenu.removeEventListener("keydown", handleEscape)
} }
}) })
function disableScroll(e) { function handleEscape(e) {
containerEl.scrollTop = scrollTop if(open && e.key === "Escape") {
toggleSelect(false)
}
} }
function getDimensions() { function getDimensions() {
@ -61,28 +63,19 @@
dimensions = {[positionSide]: y, left} dimensions = {[positionSide]: y, left}
} }
function addSelectKeyEvents(e) { function handleEnter(e) {
if (e.key === "Enter") { if (!open && e.key === "Enter") {
if (!open) {
toggleSelect(true) toggleSelect(true)
} }
} else if (e.key === "Escape") {
if (open) {
toggleSelect(false)
}
}
} }
function toggleSelect(isOpen) { function toggleSelect(isOpen) {
getDimensions() getDimensions()
if (isOpen) { if (isOpen) {
icon.style.transform = "rotate(180deg)" icon.style.transform = "rotate(180deg)"
let containerWidth = containerEl.offsetWidth selectMenu.focus()
scrollTop = containerEl.scrollTop
containerEl.addEventListener("scroll", disableScroll)
} else { } else {
icon.style.transform = "rotate(0deg)" icon.style.transform = "rotate(0deg)"
containerEl.removeEventListener("scroll", disableScroll)
} }
open = isOpen open = isOpen
} }
@ -121,6 +114,7 @@
</div> </div>
<Portal> <Portal>
<div <div
tabindex="0"
bind:this={selectMenu} bind:this={selectMenu}
style={menuStyle} style={menuStyle}
class="bb-select-menu" class="bb-select-menu"
@ -204,6 +198,7 @@
.bb-select-menu { .bb-select-menu {
position: absolute; position: absolute;
display: flex; display: flex;
outline: none;
box-sizing: border-box; box-sizing: border-box;
flex-direction: column; flex-direction: column;
opacity: 0; opacity: 0;