Only mount portal on open

This commit is contained in:
Conor_Mack 2020-06-29 15:32:37 +01:00
parent fab2a4d23b
commit ffa0dcf601
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)
} }
}) })
@ -72,12 +69,11 @@
function toggleSelect(isOpen) { function toggleSelect(isOpen) {
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)"
} }
open = isOpen open = isOpen
} }
@ -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>
<div on:click|self={() => toggleSelect(false)} class="overlay" />
</Portal> </Portal>
{/if}
</div> </div>
{#if open}
<div on:click|self={() => toggleSelect(false)} class="overlay" />
{/if}
<style> <style>
.overlay { .overlay {