2021-06-30 14:01:33 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
|
|
|
import Dropdown from "bootstrap/js/dist/dropdown";
|
|
|
|
|
|
|
|
import { setContext, onDestroy } from "svelte";
|
2021-06-30 19:55:56 +02:00
|
|
|
import { dropdownKey } from "./context-keys";
|
2021-06-30 14:01:33 +02:00
|
|
|
|
2021-08-06 03:53:44 +02:00
|
|
|
export let autoOpen = false;
|
|
|
|
export let autoClose: boolean | "inside" | "outside" = true;
|
|
|
|
|
2021-07-02 13:13:58 +02:00
|
|
|
export let toggleOpen = true;
|
2021-09-08 13:09:31 +02:00
|
|
|
export let drop: "down" | "up" = "down";
|
2021-09-09 19:03:06 +02:00
|
|
|
export let align: "start" | "end" | "auto" = "auto";
|
|
|
|
|
|
|
|
let placement: string;
|
|
|
|
|
|
|
|
$: {
|
|
|
|
let blockPlacement: string;
|
|
|
|
|
|
|
|
switch (drop) {
|
|
|
|
case "down":
|
|
|
|
blockPlacement = "bottom";
|
|
|
|
break;
|
|
|
|
case "up":
|
|
|
|
blockPlacement = "top";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inlinePlacement: string;
|
|
|
|
|
|
|
|
switch (align) {
|
|
|
|
case "start":
|
|
|
|
case "end":
|
|
|
|
inlinePlacement = `-${align}`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
inlinePlacement = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
placement = `${blockPlacement}${inlinePlacement}`;
|
2021-09-08 13:08:56 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:47:23 +02:00
|
|
|
$: dropClass = `drop${drop}`;
|
2021-08-06 03:53:44 +02:00
|
|
|
|
2021-09-15 22:26:32 +02:00
|
|
|
export let skidding = 0;
|
|
|
|
export let distance = 2;
|
|
|
|
|
2021-06-30 14:01:33 +02:00
|
|
|
setContext(dropdownKey, {
|
|
|
|
dropdown: true,
|
|
|
|
"data-bs-toggle": "dropdown",
|
|
|
|
});
|
|
|
|
|
|
|
|
let dropdown: Dropdown;
|
2021-07-01 20:26:41 +02:00
|
|
|
let api: Dropdown & { isVisible: () => boolean };
|
|
|
|
|
|
|
|
function isVisible() {
|
|
|
|
return (dropdown as any)._menu
|
|
|
|
? (dropdown as any)._menu.classList.contains("show")
|
|
|
|
: false;
|
|
|
|
}
|
2021-06-30 14:01:33 +02:00
|
|
|
|
|
|
|
const noop = () => {};
|
|
|
|
function createDropdown(toggle: HTMLElement): Dropdown {
|
|
|
|
/* avoid focusing element toggle on menu activation */
|
|
|
|
toggle.focus = noop;
|
2021-07-01 20:26:41 +02:00
|
|
|
|
2021-07-02 13:13:58 +02:00
|
|
|
if (!toggleOpen) {
|
|
|
|
/* do not open on clicking toggle */
|
|
|
|
toggle.addEventListener = noop;
|
|
|
|
}
|
|
|
|
|
2021-08-06 03:53:44 +02:00
|
|
|
dropdown = new Dropdown(toggle, {
|
|
|
|
autoClose,
|
2021-09-15 22:26:32 +02:00
|
|
|
offset: [skidding, distance],
|
2021-09-08 19:18:37 +02:00
|
|
|
popperConfig: { placement },
|
2021-08-06 03:53:44 +02:00
|
|
|
} as any);
|
|
|
|
|
|
|
|
if (autoOpen) {
|
|
|
|
dropdown.show();
|
|
|
|
}
|
|
|
|
|
2021-09-15 16:16:42 +02:00
|
|
|
api = {
|
2021-08-06 03:53:44 +02:00
|
|
|
show: dropdown.show.bind(dropdown),
|
|
|
|
toggle: dropdown.toggle.bind(dropdown),
|
|
|
|
hide: dropdown.hide.bind(dropdown),
|
|
|
|
update: dropdown.update.bind(dropdown),
|
|
|
|
dispose: dropdown.dispose.bind(dropdown),
|
2021-07-01 20:26:41 +02:00
|
|
|
isVisible,
|
2021-08-06 03:53:44 +02:00
|
|
|
};
|
|
|
|
|
2021-07-01 20:26:41 +02:00
|
|
|
return api;
|
2021-06-30 14:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy(() => dropdown?.dispose());
|
|
|
|
</script>
|
|
|
|
|
2021-07-02 12:47:23 +02:00
|
|
|
<div class={dropClass}>
|
2021-07-01 20:26:41 +02:00
|
|
|
<slot {createDropdown} dropdownObject={api} />
|
2021-06-30 14:01:33 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
div {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
</style>
|