-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(svelte): correct lazy loading (#485)
This is an attempt to fix #444, based on @adrienrn's example. This is mainly about ensuring the loading=lazy prop is applied after the src attribute.
- Loading branch information
Showing
3 changed files
with
134 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { HTMLImgAttributes } from "svelte/elements"; | ||
|
||
declare global { | ||
namespace svelteHTML { | ||
interface IntrinsicElements { | ||
img: HTMLImgAttributes & { | ||
// Because https://github.com/sveltejs/svelte/issues/8099 | ||
fetchpriority?: "auto" | "low" | "high" | null; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,43 @@ | ||
<script lang="ts"> | ||
import { transformProps } from "@unpic/core"; | ||
import { transformProps, type UnpicImageProps } from "@unpic/core"; | ||
import styleToCss from "style-object-to-css-string"; | ||
import type { ImageProps as BaseImageProps } from "./types"; | ||
import type { HTMLAttributes } from "svelte/elements"; | ||
// This unused import is a hack to get around a bug in svelte2tsx | ||
import type { UrlTransformer, ImageCdn } from "unpic"; | ||
import type { HTMLAttributes, HTMLImgAttributes } from "svelte/elements"; | ||
type $$Props = UnpicImageProps< | ||
HTMLImgAttributes, | ||
HTMLAttributes<HTMLImageElement>["style"] | ||
> & { style?: HTMLAttributes<HTMLImageElement>["style"] }; | ||
type $$Props = BaseImageProps; | ||
$: ({ style: parentStyle, ...props } = $$props as $$Props); | ||
$: ({ style: parentStyle, ...props } = $$props); | ||
$: ({ | ||
alt, | ||
style: styleObj, | ||
...transformedProps | ||
} = transformProps< | ||
Omit<HTMLAttributes<HTMLImageElement>, "style"> & { | ||
style: Record<string, string>; | ||
alt?: string | null; | ||
} | ||
>({ ...props })); | ||
src, | ||
width, | ||
height, | ||
loading, | ||
decoding, | ||
srcset, | ||
role, | ||
sizes, | ||
fetchpriority, | ||
} = transformProps(props as $$Props)); | ||
$: style = [styleToCss(styleObj || {}), parentStyle] | ||
.filter(Boolean) | ||
.join(";"); | ||
</script> | ||
|
||
<img {alt} {style} {...transformedProps} on:load /> | ||
<img | ||
{...$$props} | ||
{style} | ||
{loading} | ||
{width} | ||
{height} | ||
{decoding} | ||
{role} | ||
{fetchpriority} | ||
alt={alt?.toString()} | ||
src={src?.toString()} | ||
srcset={srcset?.toString()} | ||
sizes={sizes?.toString()} | ||
on:load | ||
/> |