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;
|
|
|
|
|
|
|
|
export let placement = "bottom-start";
|
|
|
|
|
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-08-06 03:53:44 +02:00
|
|
|
dropdown = new Dropdown(toggle, {
|
|
|
|
autoClose,
|
|
|
|
popperConfig: (defaultConfig: Record<string, any>) => ({
|
|
|
|
...defaultConfig,
|
|
|
|
placement,
|
|
|
|
}),
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
if (autoOpen) {
|
|
|
|
dropdown.show();
|
|
|
|
}
|
|
|
|
|
2021-07-01 20:26:41 +02:00
|
|
|
let 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>
|
|
|
|
|
|
|
|
<div class="dropdown">
|
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>
|