anki/ts/components/WithTooltip.svelte
BlueGreenMagick 9d98c62d50
Fix when tooltip changes before tooltipObject is created (#1845)
It can happen in TagWithTooltip.svelte
when a tag is normalized beause of leading/trailing delimiter
2022-05-10 11:36:54 +10:00

63 lines
1.7 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import Tooltip from "bootstrap/js/dist/tooltip";
import { onDestroy } from "svelte";
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";
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;
let tooltipObject: Tooltip;
function createTooltip(element: HTMLElement): void {
element.title = tooltip;
tooltipObject = new Tooltip(element, {
placement,
html,
offset,
delay: { show: showDelay, hide: hideDelay },
trigger,
});
}
onDestroy(() => tooltipObject?.dispose());
// hack to update field description tooltips
let previousTooltip: string = tooltip;
$: if (tooltip !== previousTooltip) {
previousTooltip = tooltip;
if (tooltipObject !== undefined) {
const element: HTMLElement = tooltipObject["_element"];
tooltipObject.dispose();
createTooltip(element);
}
}
</script>
<slot {createTooltip} {tooltipObject} />
<style lang="scss">
/* tooltip is inserted under the body tag
/* long tooltips can cause x-overflow */
:global(body) {
overflow-x: hidden;
}
</style>