Only mount portal on open

This commit is contained in:
Conor_Mack 2020-06-29 15:32:37 +01:00
parent e66f8c8ca4
commit 1acb04a467
1 changed files with 12 additions and 11 deletions

View File

@ -27,13 +27,10 @@
onMount(() => { onMount(() => {
if (select) { if (select) {
select.addEventListener("keydown", handleEnter) select.addEventListener("keydown", handleEnter)
selectMenu.addEventListener("keydown", handleEscape)
} }
return () => { return () => {
select.removeEventListener("keydown", handleEnter) select.removeEventListener("keydown", handleEnter)
selectMenu.removeEventListener("keydown", handleEscape)
} }
}) })
@ -73,7 +70,6 @@
getDimensions() getDimensions()
if (isOpen) { if (isOpen) {
icon.style.transform = "rotate(180deg)" icon.style.transform = "rotate(180deg)"
selectMenu.focus()
} else { } else {
icon.style.transform = "rotate(0deg)" icon.style.transform = "rotate(0deg)"
} }
@ -99,6 +95,10 @@
? options.find(o => o.value === value) ? options.find(o => o.value === value)
: {} : {}
$: if(open && selectMenu) {
selectMenu.focus()
}
$: displayLabel = $: displayLabel =
selectedOption && selectedOption.label ? selectedOption.label : value || "" selectedOption && selectedOption.label ? selectedOption.label : value || ""
</script> </script>
@ -112,13 +112,15 @@
<span>{displayLabel}</span> <span>{displayLabel}</span>
<i bind:this={icon} class="ri-arrow-down-s-fill" /> <i bind:this={icon} class="ri-arrow-down-s-fill" />
</div> </div>
{#if open}
<Portal> <Portal>
<div <div
tabindex="0" tabindex="0"
class:open
bind:this={selectMenu} bind:this={selectMenu}
style={menuStyle} style={menuStyle}
class="bb-select-menu" on:keydown={handleEscape}
class:open> class="bb-select-menu">
<ul> <ul>
{#if isOptionsObject} {#if isOptionsObject}
{#each options as { value: v, label }} {#each options as { value: v, label }}
@ -141,11 +143,10 @@
{/if} {/if}
</ul> </ul>
</div> </div>
</Portal>
</div>
{#if open}
<div on:click|self={() => toggleSelect(false)} class="overlay" /> <div on:click|self={() => toggleSelect(false)} class="overlay" />
</Portal>
{/if} {/if}
</div>
<style> <style>
.overlay { .overlay {