Skip to content

Commit

Permalink
feat: more stable onload event (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayriffy committed Nov 11, 2023
1 parent b7c557b commit 7da0d76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-buckets-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@urami/svelte": major
---

`on:load` dispatch event is now more accurate than native behavior
22 changes: 19 additions & 3 deletions packages/svelte/src/Image.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte'
import { defaultLoader, buildSource } from '@urami/utils'
import type { Loader } from '@urami/types'
const dispatch = createEventDispatcher()
export let src: string
export let width: number
export let height: number | undefined = undefined
Expand All @@ -14,15 +18,27 @@
export let loader: Loader = defaultLoader
$: buildProps = buildSource(loader, src, width, quality)
let imageElement: HTMLImageElement | undefined
let loaded = false
const handleLoad = () => {
if (loaded) return
loaded = true
dispatch('load')
}
$: builtProps = buildSource(loader, src, width, quality)
$: if (imageElement?.complete) handleLoad()
</script>

<!-- svelte-ignore a11y-missing-attribute -->
<img
src={buildProps.src}
srcset={buildProps.srcSet}
bind:this={imageElement}
src={builtProps.src}
srcset={builtProps.srcSet}
decoding="async"
loading="lazy"
on:load={handleLoad}
{...$$restProps}
{...{
alt,
Expand Down

0 comments on commit 7da0d76

Please sign in to comment.