Fix Picker Interaction with the Outside Click Action

This commit is contained in:
Gerard Burns 2022-10-21 14:14:00 +01:00
parent 55a3ea4add
commit d2997b3357
4 changed files with 34 additions and 6 deletions

View File

@ -119,6 +119,13 @@
return "var(--spectrum-global-color-static-gray-900)" return "var(--spectrum-global-color-static-gray-900)"
} }
const handleOutsideClick = event => {
if (open) {
event.stopPropagation()
open = false
}
}
</script> </script>
<div class="container"> <div class="container">
@ -131,7 +138,7 @@
</div> </div>
{#if open} {#if open}
<div <div
use:clickOutside={() => (open = false)} use:clickOutside={handleOutsideClick}
transition:fly={{ y: -20, duration: 200 }} transition:fly={{ y: -20, duration: 200 }}
class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open" class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open"
class:spectrum-Popover--align-right={alignRight} class:spectrum-Popover--align-right={alignRight}

View File

@ -85,7 +85,7 @@
class:is-invalid={!!error} class:is-invalid={!!error}
class:is-open={open} class:is-open={open}
aria-haspopup="listbox" aria-haspopup="listbox"
on:mousedown={onClick} on:click={onClick}
> >
{#if fieldIcon} {#if fieldIcon}
<span class="option-extra"> <span class="option-extra">

View File

@ -87,6 +87,20 @@
updateValue(event.target.value) updateValue(event.target.value)
} }
} }
const handlePrimaryOutsideClick = event => {
if (primaryOpen) {
event.stopPropagation()
primaryOpen = false
}
}
const handleSecondaryOutsideClick = event => {
if (secondaryOpen) {
event.stopPropagation()
secondaryOpen = false
}
}
</script> </script>
<div <div
@ -148,7 +162,7 @@
</div> </div>
{#if primaryOpen} {#if primaryOpen}
<div <div
use:clickOutside={() => (primaryOpen = false)} use:clickOutside={handlePrimaryOutsideClick}
transition:fly|local={{ y: -20, duration: 200 }} transition:fly|local={{ y: -20, duration: 200 }}
class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open" class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open"
class:auto-width={autoWidth} class:auto-width={autoWidth}
@ -256,7 +270,7 @@
{disabled} {disabled}
class:is-open={secondaryOpen} class:is-open={secondaryOpen}
aria-haspopup="listbox" aria-haspopup="listbox"
on:mousedown={onClickSecondary} on:click={onClickSecondary}
> >
{#if secondaryFieldIcon} {#if secondaryFieldIcon}
<span class="option-left"> <span class="option-left">
@ -281,7 +295,7 @@
</button> </button>
{#if secondaryOpen} {#if secondaryOpen}
<div <div
use:clickOutside={() => (secondaryOpen = false)} use:clickOutside={handleSecondaryOutsideClick}
transition:fly|local={{ y: -20, duration: 200 }} transition:fly|local={{ y: -20, duration: 200 }}
class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open" class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open"
style="width: 30%" style="width: 30%"

View File

@ -50,6 +50,13 @@
dispatch("change", value) dispatch("change", value)
open = false open = false
} }
const handleOutsideClick = event => {
if (open) {
event.stopPropagation()
open = false
}
}
</script> </script>
<div class="container"> <div class="container">
@ -64,7 +71,7 @@
</div> </div>
{#if open} {#if open}
<div <div
use:clickOutside={() => (open = false)} use:clickOutside={handleOutsideClick}
transition:fly={{ y: -20, duration: 200 }} transition:fly={{ y: -20, duration: 200 }}
class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open" class="spectrum-Popover spectrum-Popover--bottom spectrum-Picker-popover is-open"
class:spectrum-Popover--align-right={alignRight} class:spectrum-Popover--align-right={alignRight}