2022-07-07 05:19:05 +08:00
|
|
|
<script>
|
|
|
|
import { onMount, createEventDispatcher } from "svelte";
|
|
|
|
import { fly } from "svelte/transition";
|
|
|
|
|
|
|
|
export let trigger = undefined;
|
|
|
|
export let active = false;
|
|
|
|
export let escClose = true;
|
2023-03-17 01:21:16 +08:00
|
|
|
export let autoScroll = true;
|
2022-07-07 05:19:05 +08:00
|
|
|
export let closableClass = "closable";
|
|
|
|
let classes = "";
|
|
|
|
export { classes as class }; // export reserved keyword
|
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
let container;
|
|
|
|
let containerChild;
|
|
|
|
let activeTrigger;
|
|
|
|
let scrollTimeoutId;
|
|
|
|
let isOutsideMouseDown = false;
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
$: if (container) {
|
|
|
|
bindTrigger(trigger);
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
$: if (active) {
|
2022-10-30 16:28:14 +08:00
|
|
|
activeTrigger?.classList?.add("active");
|
2022-07-07 05:19:05 +08:00
|
|
|
dispatch("show");
|
|
|
|
} else {
|
2022-10-30 16:28:14 +08:00
|
|
|
activeTrigger?.classList?.remove("active");
|
2022-07-07 05:19:05 +08:00
|
|
|
dispatch("hide");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hide() {
|
|
|
|
active = false;
|
2023-03-17 01:21:16 +08:00
|
|
|
isOutsideMouseDown = false;
|
|
|
|
clearTimeout(scrollTimeoutId);
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function show() {
|
|
|
|
active = true;
|
2023-03-17 01:21:16 +08:00
|
|
|
|
|
|
|
clearTimeout(scrollTimeoutId);
|
|
|
|
scrollTimeoutId = setTimeout(() => {
|
|
|
|
if (!autoScroll) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (containerChild?.scrollIntoViewIfNeeded) {
|
|
|
|
containerChild?.scrollIntoViewIfNeeded();
|
|
|
|
} else if (containerChild?.scrollIntoView) {
|
|
|
|
containerChild?.scrollIntoView({
|
|
|
|
behavior: "smooth",
|
|
|
|
block: "nearest",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 180);
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function toggle() {
|
|
|
|
if (active) {
|
|
|
|
hide();
|
|
|
|
} else {
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isClosable(elem) {
|
|
|
|
return (
|
|
|
|
!container ||
|
|
|
|
elem.classList.contains(closableClass) ||
|
|
|
|
// is the trigger itself (or a direct child)
|
2022-10-30 16:28:14 +08:00
|
|
|
(activeTrigger?.contains(elem) && !container.contains(elem)) ||
|
2022-07-07 05:19:05 +08:00
|
|
|
// is closable toggler child
|
|
|
|
(container.contains(elem) && elem.closest && elem.closest("." + closableClass))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleClickToggle(e) {
|
|
|
|
if (!active || isClosable(e.target)) {
|
|
|
|
e.preventDefault();
|
2022-10-30 16:28:14 +08:00
|
|
|
e.stopPropagation();
|
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
toggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleKeydownToggle(e) {
|
|
|
|
if (
|
|
|
|
(e.code === "Enter" || e.code === "Space") && // enter or spacebar
|
|
|
|
(!active || isClosable(e.target))
|
|
|
|
) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
toggle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
function handleEscPress(e) {
|
|
|
|
if (active && escClose && e.code === "Escape") {
|
|
|
|
e.preventDefault();
|
2022-07-07 05:19:05 +08:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
function handleOutsideMousedown(e) {
|
|
|
|
if (active && !container?.contains(e.target)) {
|
|
|
|
isOutsideMouseDown = true;
|
|
|
|
} else if (isOutsideMouseDown) {
|
|
|
|
isOutsideMouseDown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleOutsideClick(e) {
|
|
|
|
if (
|
|
|
|
active &&
|
|
|
|
!container?.contains(e.target) &&
|
|
|
|
!activeTrigger?.contains(e.target) &&
|
|
|
|
isOutsideMouseDown
|
|
|
|
) {
|
2022-07-07 05:19:05 +08:00
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleFocusChange(e) {
|
2023-03-17 01:21:16 +08:00
|
|
|
handleOutsideMousedown(e);
|
|
|
|
handleOutsideClick(e);
|
2022-07-07 05:19:05 +08:00
|
|
|
}
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
function bindTrigger(newTrigger) {
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
activeTrigger = newTrigger || container?.parentNode;
|
|
|
|
|
|
|
|
if (!activeTrigger) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
container?.addEventListener("click", handleClickToggle);
|
|
|
|
activeTrigger.addEventListener("click", handleClickToggle);
|
|
|
|
activeTrigger.addEventListener("keydown", handleKeydownToggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup() {
|
2023-03-17 01:21:16 +08:00
|
|
|
clearTimeout(scrollTimeoutId);
|
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
if (!activeTrigger) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
container?.removeEventListener("click", handleClickToggle);
|
|
|
|
activeTrigger.removeEventListener("click", handleClickToggle);
|
|
|
|
activeTrigger.removeEventListener("keydown", handleKeydownToggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
bindTrigger();
|
2022-07-07 05:19:05 +08:00
|
|
|
|
2022-10-30 16:28:14 +08:00
|
|
|
return () => cleanup();
|
2022-07-07 05:19:05 +08:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
<svelte:window
|
|
|
|
on:mousedown={handleOutsideMousedown}
|
|
|
|
on:click={handleOutsideClick}
|
|
|
|
on:keydown={handleEscPress}
|
|
|
|
on:focusin={handleFocusChange}
|
|
|
|
/>
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
<div bind:this={container} class="toggler-container">
|
|
|
|
{#if active}
|
|
|
|
<div
|
2023-03-17 01:21:16 +08:00
|
|
|
bind:this={containerChild}
|
2022-07-07 05:19:05 +08:00
|
|
|
class={classes}
|
|
|
|
class:active
|
2023-03-17 01:21:16 +08:00
|
|
|
transition:fly|local={{ duration: 150, y: 3 }}
|
2022-07-07 05:19:05 +08:00
|
|
|
>
|
|
|
|
<slot />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|