Lib: Add tooltip capability to LazyImage

This commit is contained in:
2025-03-18 12:29:49 +01:00
parent 19170ebef9
commit 901bcc1c05

View File

@ -2,6 +2,8 @@
import type { HTMLImgAttributes } from "svelte/elements"; import type { HTMLImgAttributes } from "svelte/elements";
import { lazyload } from "$lib/lazyload"; import { lazyload } from "$lib/lazyload";
import { fetch_image_base64 } from "$lib/image"; import { fetch_image_base64 } from "$lib/image";
import { popup, type PopupSettings } from "@skeletonlabs/skeleton";
import { v4 as uuidv4 } from "uuid";
interface LazyImageProps extends HTMLImgAttributes { interface LazyImageProps extends HTMLImgAttributes {
/** The URL to the image resource to lazyload */ /** The URL to the image resource to lazyload */
@ -24,6 +26,9 @@
/** Slightly zoom the image on mouse-hover */ /** Slightly zoom the image on mouse-hover */
hoverzoom?: boolean; hoverzoom?: boolean;
/** Optional tooltip text */
tooltip?: string;
} }
let { let {
@ -34,6 +39,7 @@
containerstyle = undefined, containerstyle = undefined,
imgclass = "", imgclass = "",
hoverzoom = false, hoverzoom = false,
tooltip = undefined,
...restProps ...restProps
}: LazyImageProps = $props(); }: LazyImageProps = $props();
@ -49,6 +55,14 @@
const img_opacity_handler = (node: HTMLElement) => { const img_opacity_handler = (node: HTMLElement) => {
setTimeout(() => (node.style.opacity = "1"), 20); setTimeout(() => (node.style.opacity = "1"), 20);
}; };
// Tooltip handling
const tooltipId: string = uuidv4();
const popupTooltip: PopupSettings = {
event: "hover",
target: tooltipId,
placement: "top",
};
</script> </script>
<!-- Show a correctly sized div so the layout doesn't jump. --> <!-- Show a correctly sized div so the layout doesn't jump. -->
@ -67,8 +81,16 @@
class="bg-surface-100 transition-all {imgclass} {hoverzoom ? 'hover:scale-105' : ''}" class="bg-surface-100 transition-all {imgclass} {hoverzoom ? 'hover:scale-105' : ''}"
style="opacity: 0; transition-duration: 300ms; {imgstyle ?? ''}" style="opacity: 0; transition-duration: 300ms; {imgstyle ?? ''}"
draggable="false" draggable="false"
use:popup={popupTooltip}
{...restProps} {...restProps}
/> />
{/await} {/await}
{/if} {/if}
</div> </div>
{#if tooltip}
<div class="card variant-filled-surface p-2 shadow" data-popup={tooltipId}>
<p>{tooltip}</p>
<div class="variant-filled-surface arrow"></div>
</div>
{/if}