design: add image loading animation

This commit is contained in:
Xylight committed 2024-08-25 16:31:43 -07:00
1 parent 3fb92f6e81
commit ef1d13b06f
1 file changed
+49 -21
@@ -13,7 +13,9 @@
import { userSettings } from '$lib/settings.js'
import type { Post } from 'lemmy-js-client'
import PostIframe from './PostIframe.svelte'
import { Button, Material } from 'mono-svelte'
import { Button, Material, modal } from 'mono-svelte'
import { onMount } from 'svelte'
import { ArrowDownTray, Icon } from 'svelte-hero-icons'
export let view: 'card' | 'cozy' | 'list' | 'compact' = 'cozy'
export let post: Post
@@ -22,6 +24,12 @@
export let blur: boolean = post.nsfw && $userSettings.nsfwBlur
let showAltText = false
let imageLoaded: boolean | null = null
onMount(() => {
imageLoaded = false
})
</script>
<!--
@@ -36,8 +44,7 @@
<svelte:element
this={$userSettings.expandImages ? 'button' : 'a'}
href={postLink(post)}
class="container mx-auto z-10 rounded-xl max-h-[60vh] relative overflow-hidden bg-slate-100 dark:bg-zinc-900
"
class="container mx-auto z-10 rounded-xl max-h-[60vh] relative overflow-hidden bg-slate-100 dark:bg-zinc-900"
data-sveltekit-preload-data="off"
aria-label={post.name}
on:click={() => showImage(bestImageURL(post, false, 2048))}
@@ -48,7 +55,8 @@
<img
src={bestImageURL(post, false, 64)}
loading="lazy"
class="-z-10 absolute top-0 left-0 w-full h-full object-cover blur-xl opacity-40"
class="-z-10 absolute top-0 left-0 w-full h-full object-cover blur-xl transition-opacity
{imageLoaded === false ? 'opacity-40' : 'opacity-40'}"
/>
<picture class="max-h-[inherit]">
<source
@@ -72,31 +80,51 @@
src={bestImageURL(post, false, 2048)}
loading="lazy"
class="max-h-[inherit] max-w-full h-auto z-30
transition-opacity duration-300 object-contain mx-auto"
transition-opacity duration-500 object-contain mx-auto
{imageLoaded === false ? 'opacity-0' : 'opacity-100'}"
width={512}
height={300}
class:blur-3xl={blur}
alt={post.alt_text ?? ''}
on:load={() => (imageLoaded = true)}
/>
</picture>
{#if post.alt_text}
<Button
on:click={(e) => {
e.stopPropagation()
showAltText = !showAltText
}}
class="absolute bottom-0 left-0 text-sm m-2 text-left"
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="absolute bottom-0 left-0 right-0 flex justify-between items-center
rounded-full ml-auto w-max m-2 p-0 gap-1
*:bg-white *:border *:border-slate-200 *:dark:border-zinc-800 *:dark:bg-zinc-900
*:bg-opacity-70 *:backdrop-blur-md"
on:click|stopPropagation={() => {}}
>
<!-- <Button
color="tertiary"
size="sm"
rounding="lg"
alignment="left"
class="aspect-square"
rounding="pill"
href={post.url}
download
>
{#if showAltText}
{post.alt_text}
{:else}
<span class="font-bold">ALT</span>
{/if}
</Button>
{/if}
<Icon src={ArrowDownTray} size="16" micro />
</Button> -->
{#if post.alt_text}
<Button
on:click={(e) => {
e.stopPropagation()
modal({
title: 'Alt',
body: post.alt_text ?? '',
})
}}
color="tertiary"
size="md"
rounding="pill"
>
ALT
</Button>
{/if}
</div>
</svelte:element>
{/if}
{:else if (type == 'iframe' || type == 'video') && (view == 'cozy' || view == 'card') && post.url}