feat: tooltip only shown when there are elipsis

This commit is contained in:
Arman committed 2022-08-10 17:29:08 +02:00
1 parent c5719bb1ce
commit 223b50db9e
2 files changed
+41 -5

No files matched your search

+6
View File
@@ -13,6 +13,12 @@ export const tooltip: Action<HTMLElement, Partial<Props>> = (node, config) => {
});
}
},
enable() {
instance.enable();
},
disable() {
instance.disable();
},
destroy() {
instance.destroy();
}
+35 -5
View File
@@ -1,14 +1,44 @@
<script lang="ts">
import { tooltip } from '$lib/actions/tooltip';
import { onMount } from 'svelte';
let data: HTMLSpanElement;
$: content = data?.innerText;
// TODO: enable tooltip only when there are elipsis
// $: if (data?.offsetWidth < data?.scrollWidth) {
// }
onMount(() => {
setTooltip();
});
const setTooltip = () => {
if (data.innerText) {
const content = data.innerText;
const tool = tooltip(data, { content });
tool.disable();
if (data.offsetWidth < data.scrollWidth) {
tool.enable();
} else {
tool.disable();
}
}
};
const throttle = (fn: () => void, delay: number) => {
let timeout = false;
return () => {
if (!timeout) {
timeout = true;
fn.apply(this);
setTimeout(() => {
timeout = false;
}, delay);
}
};
};
//TODO: disable tooltip when text is with elipsis
</script>
<span bind:this={data} class="text u-trim" use:tooltip={{ content, hideOnClick: false }}>
<svelte:window on:resize={throttle(setTooltip, 50)} />
<span bind:this={data} class="text u-trim">
<slot />
</span>