2021-03-31 11:59:07 +02:00
|
|
|
<script>
|
2021-04-21 16:03:12 +02:00
|
|
|
import "@spectrum-css/popover/dist/index-vars.css"
|
2021-03-31 11:59:07 +02:00
|
|
|
import Portal from "svelte-portal"
|
|
|
|
import { createEventDispatcher } from "svelte"
|
|
|
|
import positionDropdown from "../Actions/position_dropdown"
|
|
|
|
import clickOutside from "../Actions/click_outside"
|
2023-01-18 14:56:53 +01:00
|
|
|
import { fly } from "svelte/transition"
|
2023-01-19 17:09:40 +01:00
|
|
|
import { getContext } from "svelte"
|
|
|
|
import Context from "../context"
|
2021-04-21 16:03:12 +02:00
|
|
|
|
2021-03-31 11:59:07 +02:00
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
|
|
|
|
export let anchor
|
|
|
|
export let align = "right"
|
2021-06-08 09:00:54 +02:00
|
|
|
export let portalTarget
|
2022-08-30 11:16:15 +02:00
|
|
|
export let maxWidth
|
2023-01-18 14:56:53 +01:00
|
|
|
export let open = false
|
|
|
|
export let useAnchorWidth = false
|
2023-01-23 01:32:01 +01:00
|
|
|
export let dismissible = true
|
2023-02-01 09:28:50 +01:00
|
|
|
export let offset = 5
|
2022-04-26 13:04:07 +02:00
|
|
|
|
2023-01-19 17:09:40 +01:00
|
|
|
$: target = portalTarget || getContext(Context.PopoverRoot) || ".spectrum"
|
2022-04-19 15:38:09 +02:00
|
|
|
|
2021-03-31 11:59:07 +02:00
|
|
|
export const show = () => {
|
2021-04-21 16:03:12 +02:00
|
|
|
dispatch("open")
|
2021-03-31 11:59:07 +02:00
|
|
|
open = true
|
|
|
|
}
|
|
|
|
|
|
|
|
export const hide = () => {
|
2021-04-21 16:03:12 +02:00
|
|
|
dispatch("close")
|
2021-03-31 11:59:07 +02:00
|
|
|
open = false
|
|
|
|
}
|
|
|
|
|
2022-10-17 20:58:43 +02:00
|
|
|
const handleOutsideClick = e => {
|
|
|
|
if (open) {
|
2023-01-18 14:56:53 +01:00
|
|
|
// Stop propagation if the source is the anchor
|
|
|
|
let node = e.target
|
|
|
|
let fromAnchor = false
|
|
|
|
while (!fromAnchor && node && node.parentNode) {
|
|
|
|
fromAnchor = node === anchor
|
|
|
|
node = node.parentNode
|
|
|
|
}
|
|
|
|
if (fromAnchor) {
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide the popover
|
2022-10-17 20:58:43 +02:00
|
|
|
hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:59:07 +02:00
|
|
|
function handleEscape(e) {
|
|
|
|
if (open && e.key === "Escape") {
|
|
|
|
hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if open}
|
2023-02-02 11:06:41 +01:00
|
|
|
<Portal {target}>
|
|
|
|
<div
|
|
|
|
tabindex="0"
|
|
|
|
use:positionDropdown={{
|
|
|
|
anchor,
|
|
|
|
align,
|
|
|
|
maxWidth,
|
|
|
|
useAnchorWidth,
|
|
|
|
offset,
|
|
|
|
}}
|
|
|
|
use:clickOutside={{
|
|
|
|
callback: dismissible ? handleOutsideClick : () => {},
|
|
|
|
anchor,
|
|
|
|
}}
|
|
|
|
on:keydown={handleEscape}
|
|
|
|
class="spectrum-Popover is-open"
|
|
|
|
role="presentation"
|
|
|
|
transition:fly|local={{ y: -20, duration: 200 }}
|
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
</Portal>
|
2021-03-31 11:59:07 +02:00
|
|
|
{/if}
|
2021-04-23 11:48:19 +02:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.spectrum-Popover {
|
2022-07-18 06:34:40 +02:00
|
|
|
min-width: var(--spectrum-global-dimension-size-2000);
|
2022-08-05 12:11:12 +02:00
|
|
|
border-color: var(--spectrum-global-color-gray-300);
|
2023-01-31 12:22:55 +01:00
|
|
|
overflow: auto;
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
|
|
|
</style>
|