2021-03-31 03:34:08 +02:00
|
|
|
<script lang="typescript">
|
2021-04-09 18:59:57 +02:00
|
|
|
import type { ToolbarItem } from "./types";
|
2021-03-31 03:34:08 +02:00
|
|
|
|
2021-04-09 18:59:57 +02:00
|
|
|
export let button: ToolbarItem;
|
2021-03-31 03:34:08 +02:00
|
|
|
export let menuId: string;
|
|
|
|
|
2021-04-08 18:57:20 +02:00
|
|
|
function extend({
|
|
|
|
className,
|
|
|
|
...rest
|
|
|
|
}: DynamicSvelteComponent): DynamicSvelteComponent {
|
2021-03-31 03:34:08 +02:00
|
|
|
return {
|
|
|
|
className: `${className} dropdown-toggle`,
|
2021-04-08 18:57:20 +02:00
|
|
|
"data-bs-toggle": "dropdown",
|
|
|
|
"aria-expanded": "false",
|
2021-03-31 03:34:08 +02:00
|
|
|
...rest,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDropdown({ detail }: CustomEvent): void {
|
|
|
|
const button: HTMLButtonElement = detail.button;
|
|
|
|
|
|
|
|
/* Prevent focus on menu activation */
|
|
|
|
const noop = () => {};
|
|
|
|
Object.defineProperty(button, "focus", { value: noop });
|
|
|
|
|
|
|
|
/* Set custom menu without using .dropdown
|
|
|
|
* Rendering the menu here would cause the menu to
|
|
|
|
* be displayed outside of the visible area
|
|
|
|
*/
|
|
|
|
const dropdown = new bootstrap.Dropdown(button);
|
|
|
|
const menu = button.getRootNode().getElementById(menuId);
|
|
|
|
|
|
|
|
if (!menu) {
|
|
|
|
console.log(`Could not find menu "${menuId}" for dropdown menu.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
dropdown._menu = menu;
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<svelte:component
|
|
|
|
this={button.component}
|
|
|
|
{...extend(button)}
|
|
|
|
on:mount={createDropdown} />
|