2022-03-02 05:21:19 +01:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2022-09-10 10:46:59 +02:00
|
|
|
import type {
|
|
|
|
FloatingElement,
|
|
|
|
Placement,
|
|
|
|
ReferenceElement,
|
|
|
|
} from "@floating-ui/dom";
|
2022-09-05 09:20:00 +02:00
|
|
|
import { createEventDispatcher, onDestroy } from "svelte";
|
|
|
|
import type { ActionReturn } from "svelte/action";
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
import type { Callback } from "../lib/typing";
|
|
|
|
import { singleCallback } from "../lib/typing";
|
2022-03-02 05:21:19 +01:00
|
|
|
import isClosingClick from "../sveltelib/closing-click";
|
|
|
|
import isClosingKeyup from "../sveltelib/closing-keyup";
|
2022-09-05 09:20:00 +02:00
|
|
|
import type { EventPredicateResult } from "../sveltelib/event-predicate";
|
2022-03-02 05:21:19 +01:00
|
|
|
import { documentClick, documentKeyup } from "../sveltelib/event-store";
|
|
|
|
import portal from "../sveltelib/portal";
|
2022-09-05 09:20:00 +02:00
|
|
|
import type { PositioningCallback } from "../sveltelib/position/auto-update";
|
|
|
|
import autoUpdate from "../sveltelib/position/auto-update";
|
|
|
|
import type { PositionAlgorithm } from "../sveltelib/position/position-algorithm";
|
|
|
|
import positionFloating from "../sveltelib/position/position-floating";
|
|
|
|
import subscribeToUpdates from "../sveltelib/subscribe-updates";
|
|
|
|
import FloatingArrow from "./FloatingArrow.svelte";
|
|
|
|
|
2022-09-12 11:22:22 +02:00
|
|
|
export let portalTarget: HTMLElement | null = null;
|
2022-09-05 09:20:00 +02:00
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
export let placement: Placement | Placement[] | "auto" = "bottom";
|
2022-09-05 09:20:00 +02:00
|
|
|
export let offset = 5;
|
|
|
|
export let shift = 5;
|
|
|
|
export let inline = false;
|
|
|
|
export let hideIfEscaped = false;
|
|
|
|
export let hideIfReferenceHidden = false;
|
2022-03-02 05:21:19 +01:00
|
|
|
|
|
|
|
/** This may be passed in for more fine-grained control */
|
2022-09-05 09:20:00 +02:00
|
|
|
export let show = true;
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
const dispatch = createEventDispatcher();
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
let arrow: HTMLElement;
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
$: positionCurried = positionFloating({
|
2022-03-11 06:48:49 +01:00
|
|
|
placement,
|
2022-09-05 09:20:00 +02:00
|
|
|
offset,
|
|
|
|
shift,
|
|
|
|
inline,
|
2022-03-11 06:48:49 +01:00
|
|
|
arrow,
|
2022-09-05 09:20:00 +02:00
|
|
|
hideIfEscaped,
|
|
|
|
hideIfReferenceHidden,
|
|
|
|
hideCallback: (reason: string) => dispatch("close", { reason }),
|
|
|
|
});
|
|
|
|
|
|
|
|
let autoAction: ActionReturn = {};
|
|
|
|
|
|
|
|
$: {
|
|
|
|
positionCurried;
|
|
|
|
autoAction.update?.(positioningCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
export let closeOnInsideClick = false;
|
|
|
|
export let keepOnKeyup = false;
|
2022-09-27 04:16:45 +02:00
|
|
|
export let hideArrow = false;
|
2022-03-11 06:48:49 +01:00
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
export let reference: ReferenceElement | undefined = undefined;
|
2022-09-05 09:20:00 +02:00
|
|
|
let floating: FloatingElement;
|
2022-03-11 06:48:49 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
function applyPosition(
|
2022-09-10 10:46:59 +02:00
|
|
|
reference: ReferenceElement,
|
2022-09-05 09:20:00 +02:00
|
|
|
floating: FloatingElement,
|
|
|
|
position: PositionAlgorithm,
|
|
|
|
): Promise<void> {
|
|
|
|
return position(reference, floating);
|
|
|
|
}
|
2022-03-11 06:48:49 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
async function position(
|
|
|
|
callback: (
|
2022-09-10 10:46:59 +02:00
|
|
|
reference: ReferenceElement,
|
2022-09-05 09:20:00 +02:00
|
|
|
floating: FloatingElement,
|
|
|
|
position: PositionAlgorithm,
|
|
|
|
) => Promise<void> = applyPosition,
|
|
|
|
): Promise<void> {
|
|
|
|
if (reference && floating) {
|
|
|
|
return callback(reference, floating, positionCurried);
|
|
|
|
}
|
2022-03-11 06:48:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
function asReference(referenceArgument: Element) {
|
2022-09-05 09:20:00 +02:00
|
|
|
reference = referenceArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
function positioningCallback(
|
2022-09-10 10:46:59 +02:00
|
|
|
reference: ReferenceElement,
|
2022-09-05 09:20:00 +02:00
|
|
|
callback: PositioningCallback,
|
|
|
|
): Callback {
|
|
|
|
const innerFloating = floating;
|
|
|
|
return callback(reference, innerFloating, () =>
|
|
|
|
positionCurried(reference, innerFloating),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cleanup: Callback | null = null;
|
|
|
|
|
|
|
|
function updateFloating(
|
2022-09-10 10:46:59 +02:00
|
|
|
reference: ReferenceElement | undefined,
|
2022-09-05 09:20:00 +02:00
|
|
|
floating: FloatingElement,
|
|
|
|
isShowing: boolean,
|
|
|
|
) {
|
|
|
|
cleanup?.();
|
|
|
|
cleanup = null;
|
|
|
|
|
|
|
|
if (!reference || !floating || !isShowing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
autoAction = autoUpdate(reference, positioningCallback);
|
|
|
|
|
|
|
|
// For virtual references, we cannot provide any
|
|
|
|
// default closing behavior
|
|
|
|
if (!(reference instanceof EventTarget)) {
|
|
|
|
cleanup = autoAction.destroy!;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
const closingClick = isClosingClick(documentClick, {
|
|
|
|
reference,
|
|
|
|
floating,
|
|
|
|
inside: closeOnInsideClick,
|
|
|
|
outside: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const subscribers = [
|
|
|
|
subscribeToUpdates(closingClick, (event: EventPredicateResult) =>
|
|
|
|
dispatch("close", event),
|
|
|
|
),
|
2022-03-11 06:48:49 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if (!keepOnKeyup) {
|
2022-09-05 09:20:00 +02:00
|
|
|
const closingKeyup = isClosingKeyup(documentKeyup, {
|
|
|
|
reference,
|
|
|
|
floating,
|
|
|
|
});
|
|
|
|
|
|
|
|
subscribers.push(
|
|
|
|
subscribeToUpdates(closingKeyup, (event: EventPredicateResult) =>
|
|
|
|
dispatch("close", event),
|
|
|
|
),
|
2022-03-11 06:48:49 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
cleanup = singleCallback(...subscribers, autoAction.destroy!);
|
|
|
|
}
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
$: updateFloating(reference, floating, show);
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
onDestroy(() => cleanup?.());
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<slot {position} {asReference} />
|
|
|
|
|
|
|
|
{#if $$slots.reference}
|
|
|
|
{#if inline}
|
|
|
|
<span class="floating-reference" use:asReference>
|
|
|
|
<slot name="reference" />
|
|
|
|
</span>
|
|
|
|
{:else}
|
|
|
|
<div class="floating-reference" use:asReference>
|
|
|
|
<slot name="reference" />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/if}
|
|
|
|
|
2022-09-27 04:16:45 +02:00
|
|
|
<div bind:this={floating} class="floating" class:show use:portal={portalTarget}>
|
2022-09-05 09:20:00 +02:00
|
|
|
{#if show}
|
|
|
|
<slot name="floating" />
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<div bind:this={arrow} class="floating-arrow" hidden={!show}>
|
2022-09-27 04:16:45 +02:00
|
|
|
{#if !hideArrow}
|
|
|
|
<FloatingArrow />
|
|
|
|
{/if}
|
2022-09-05 09:20:00 +02:00
|
|
|
</div>
|
2022-03-02 05:21:19 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
@use "sass/elevation" as elevation;
|
|
|
|
|
|
|
|
.floating {
|
|
|
|
position: absolute;
|
|
|
|
border-radius: 5px;
|
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
z-index: 890;
|
2022-09-27 04:16:45 +02:00
|
|
|
&.show {
|
|
|
|
@include elevation.elevation(8);
|
|
|
|
}
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-05 09:20:00 +02:00
|
|
|
&-arrow {
|
|
|
|
position: absolute;
|
2022-03-02 05:21:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|