2022-07-23 13:08:49 -05:00
|
|
|
<script lang="ts">
|
2023-07-01 00:50:47 -04:00
|
|
|
import { quintOut } from 'svelte/easing';
|
|
|
|
import { slide } from 'svelte/transition';
|
2024-05-23 19:56:48 +02:00
|
|
|
import { clickOutside } from '$lib/actions/click-outside';
|
2022-07-23 13:08:49 -05:00
|
|
|
|
2024-06-18 03:52:38 +00:00
|
|
|
export let isVisible: boolean = false;
|
2023-07-01 00:50:47 -04:00
|
|
|
export let direction: 'left' | 'right' = 'right';
|
|
|
|
export let x = 0;
|
|
|
|
export let y = 0;
|
2024-06-18 03:52:38 +00:00
|
|
|
export let id: string | undefined = undefined;
|
|
|
|
export let ariaLabel: string | undefined = undefined;
|
|
|
|
export let ariaLabelledBy: string | undefined = undefined;
|
|
|
|
export let ariaActiveDescendant: string | undefined = undefined;
|
2022-07-23 13:08:49 -05:00
|
|
|
|
2024-06-18 03:52:38 +00:00
|
|
|
export let menuElement: HTMLUListElement | undefined = undefined;
|
2024-06-02 14:20:11 +02:00
|
|
|
export let onClose: (() => void) | undefined = undefined;
|
2024-04-05 21:19:26 +02:00
|
|
|
|
2023-07-01 00:50:47 -04:00
|
|
|
let left: number;
|
|
|
|
let top: number;
|
2022-07-23 13:08:49 -05:00
|
|
|
|
2024-04-05 21:19:26 +02:00
|
|
|
// We need to bind clientHeight since the bounding box may return a height
|
|
|
|
// of zero when starting the 'slide' animation.
|
|
|
|
let height: number;
|
|
|
|
|
|
|
|
$: {
|
|
|
|
if (menuElement) {
|
|
|
|
const rect = menuElement.getBoundingClientRect();
|
|
|
|
const directionWidth = direction === 'left' ? rect.width : 0;
|
|
|
|
const menuHeight = Math.min(menuElement.clientHeight, height) || 0;
|
2022-07-23 13:08:49 -05:00
|
|
|
|
2024-04-05 21:19:26 +02:00
|
|
|
left = Math.min(window.innerWidth - rect.width, x - directionWidth);
|
|
|
|
top = Math.min(window.innerHeight - menuHeight, y);
|
|
|
|
}
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
2022-07-23 13:08:49 -05:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div
|
2024-04-05 21:19:26 +02:00
|
|
|
bind:clientHeight={height}
|
2024-06-18 03:52:38 +00:00
|
|
|
class="fixed z-10 min-w-[200px] w-max max-w-[300px] overflow-hidden rounded-lg shadow-lg"
|
2024-04-05 21:19:26 +02:00
|
|
|
style:left="{left}px"
|
2024-06-18 03:52:38 +00:00
|
|
|
style:top="{top}px"
|
|
|
|
transition:slide={{ duration: 250, easing: quintOut }}
|
|
|
|
use:clickOutside={{ onOutclick: onClose }}
|
2022-07-23 13:08:49 -05:00
|
|
|
>
|
2024-06-18 03:52:38 +00:00
|
|
|
<ul
|
|
|
|
{id}
|
|
|
|
aria-activedescendant={ariaActiveDescendant ?? ''}
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
aria-labelledby={ariaLabelledBy}
|
|
|
|
bind:this={menuElement}
|
|
|
|
class:max-h-[100vh]={isVisible}
|
|
|
|
class:max-h-0={!isVisible}
|
2024-08-27 18:13:17 -04:00
|
|
|
class="flex flex-col transition-all duration-[250ms] ease-in-out outline-none"
|
2024-06-18 03:52:38 +00:00
|
|
|
role="menu"
|
|
|
|
tabindex="-1"
|
|
|
|
>
|
2024-03-21 19:39:33 +01:00
|
|
|
<slot />
|
2024-06-18 03:52:38 +00:00
|
|
|
</ul>
|
2022-07-23 13:08:49 -05:00
|
|
|
</div>
|