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
|
|
|
|
-->
|
2022-09-27 04:16:45 +02:00
|
|
|
<script lang="ts">
|
2022-12-01 06:45:52 +01:00
|
|
|
import type { Placement } from "@floating-ui/dom";
|
|
|
|
import { getContext, onMount } from "svelte";
|
|
|
|
import { create_in_transition } from "svelte/internal";
|
|
|
|
import type { Writable } from "svelte/store";
|
2022-09-27 04:16:45 +02:00
|
|
|
import { slide } from "svelte/transition";
|
|
|
|
|
2022-12-01 06:45:52 +01:00
|
|
|
import { floatingKey } from "./context-keys";
|
2022-09-27 04:16:45 +02:00
|
|
|
|
|
|
|
export let scrollable = false;
|
2022-12-01 06:45:52 +01:00
|
|
|
let element: HTMLDivElement;
|
|
|
|
let wrapper: HTMLDivElement;
|
|
|
|
let hidden = true;
|
|
|
|
let minHeight = 0;
|
|
|
|
|
|
|
|
let placement: Placement;
|
|
|
|
|
|
|
|
const placementStore = getContext<Writable<Promise<Placement>>>(floatingKey);
|
|
|
|
|
|
|
|
/* await computed placement of floating element to determine animation direction */
|
2022-12-08 13:35:55 +01:00
|
|
|
$: if ($placementStore !== undefined && hidden) {
|
2022-12-01 06:45:52 +01:00
|
|
|
$placementStore.then((computedPlacement) => {
|
|
|
|
if (placement != computedPlacement) {
|
|
|
|
placement = computedPlacement;
|
|
|
|
/* use internal function to animate popover */
|
|
|
|
create_in_transition(element, slide, { duration: 200 }).start();
|
|
|
|
hidden = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
/* set min-height on wrapper to ensure correct
|
|
|
|
popover placement at animation start */
|
|
|
|
minHeight = wrapper.offsetHeight;
|
|
|
|
});
|
2022-03-02 05:21:19 +01:00
|
|
|
</script>
|
|
|
|
|
2022-09-27 04:16:45 +02:00
|
|
|
<div
|
2022-12-01 06:45:52 +01:00
|
|
|
class="popover-wrapper d-flex"
|
|
|
|
style:--min-height="{minHeight}px"
|
|
|
|
bind:this={wrapper}
|
2022-09-27 04:16:45 +02:00
|
|
|
>
|
2022-12-01 06:45:52 +01:00
|
|
|
<div
|
|
|
|
class="popover"
|
|
|
|
class:scrollable
|
|
|
|
class:hidden
|
|
|
|
class:top={placement === "top"}
|
|
|
|
class:right={placement === "right"}
|
|
|
|
class:bottom={placement === "bottom"}
|
|
|
|
class:left={placement === "left"}
|
|
|
|
bind:this={element}
|
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</div>
|
2022-03-02 05:21:19 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
2022-12-01 06:45:52 +01:00
|
|
|
@use "sass/elevation" as elevation;
|
|
|
|
|
|
|
|
.popover-wrapper {
|
|
|
|
min-height: var(--min-height, 0);
|
|
|
|
}
|
|
|
|
|
2022-03-02 05:21:19 +01:00
|
|
|
.popover {
|
2022-12-01 06:45:52 +01:00
|
|
|
@include elevation.elevation(8);
|
|
|
|
|
|
|
|
align-self: flex-start;
|
|
|
|
border-radius: var(--border-radius);
|
|
|
|
background-color: var(--canvas-elevated);
|
|
|
|
border: 1px solid var(--border-subtle);
|
|
|
|
|
2022-09-27 04:16:45 +02:00
|
|
|
min-width: var(--popover-width, 1rem);
|
2022-03-11 06:48:49 +01:00
|
|
|
max-width: 95vw;
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-09-10 10:46:59 +02:00
|
|
|
/* Needs this much space for FloatingArrow to be positioned */
|
|
|
|
padding: var(--popover-padding-block, 6px) var(--popover-padding-inline, 6px);
|
2022-09-05 09:20:00 +02:00
|
|
|
|
2022-12-01 06:45:52 +01:00
|
|
|
&.scrollable {
|
|
|
|
max-height: 400px;
|
|
|
|
overflow: hidden auto;
|
2022-03-02 05:21:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-01 06:45:52 +01:00
|
|
|
&.hidden {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
2022-03-02 05:21:19 +01:00
|
|
|
|
2022-12-01 06:45:52 +01:00
|
|
|
/* alignment determines slide animation direction */
|
|
|
|
&.top,
|
|
|
|
&.left {
|
|
|
|
align-self: flex-end;
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
2022-12-01 06:45:52 +01:00
|
|
|
&.bottom,
|
|
|
|
&.right {
|
|
|
|
align-self: flex-start;
|
2022-03-02 05:21:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|