anki/ts/components/WithTooltip.svelte

45 lines
1.2 KiB
Svelte
Raw Normal View History

2021-07-08 21:47:27 +02:00
<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import { onDestroy } from "svelte";
import Tooltip from "bootstrap/js/dist/tooltip";
type TriggerType =
| "hover focus"
| "click"
| "hover"
| "focus"
| "manual"
| "click hover"
| "click focus"
| "click hover focus";
export let tooltip: string;
export let trigger: TriggerType = "hover focus";
2021-07-08 23:35:54 +02:00
export let placement: "auto" | "top" | "bottom" | "left" | "right" = "top";
export let html = true;
export let offset: Tooltip.Offset = [0, 0];
export let showDelay = 0;
export let hideDelay = 0;
2021-07-08 21:47:27 +02:00
let tooltipObject: Tooltip;
function createTooltip(element: HTMLElement): void {
element.title = tooltip;
tooltipObject = new Tooltip(element, {
2021-07-08 23:35:54 +02:00
placement,
html,
offset,
delay: { show: showDelay, hide: hideDelay },
2021-07-08 21:47:27 +02:00
trigger,
});
}
onDestroy(() => tooltipObject?.dispose());
</script>
<slot {createTooltip} {tooltipObject} />