svelte-remote-image 0.2.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@ Display optimized, responsive and progressive images for Svelte.
6
6
  With remote image URL of CDN or other means.
7
7
 
8
8
  - Fade-in and blur on image reveal.
9
- - An image URL can be set to failback if an image cannot be retrieved from the source URL due to an error.
9
+ - An image URL can be set to fallback if an image cannot be retrieved from the source URL due to an error.
10
10
  - A pre-rendered low-quality dataURI can be set as a placeholder.
11
11
 
12
12
 
@@ -47,7 +47,7 @@ Sample code.
47
47
  { src: `${optimazerPrefix}width=1600,quality=50,format=jpeg/${originalImageUrl}`, w: 1600 },
48
48
  { src: `${optimazerPrefix}width=800,quality=50,format=jpeg/${originalImageUrl}`, w: 800 }
49
49
  ],
50
- failback: originalImageUrl,
50
+ fallback: originalImageUrl,
51
51
  alt: 'blog top',
52
52
  placeholder: { dataUri: '', color: '#c5c5c5' },
53
53
  blur: true
@@ -104,7 +104,7 @@ Image sources for jpeg.
104
104
 
105
105
  Image sources for png.
106
106
 
107
- #### failback: string[]
107
+ #### fallback: string[]
108
108
 
109
109
  Failback image urls.
110
110
  The order is important because the images are tested in order from the top of the array.
@@ -0,0 +1,74 @@
1
+ <script>import { browser } from "$app/environment";
2
+ import { afterUpdate } from "svelte";
3
+ export let src;
4
+ export let style;
5
+ export const alt = "";
6
+ export const title = "";
7
+ const imgId = `svelte-remote-image-${alt.replaceAll(" ", "-")}-${Math.round(Math.random() * 1e7)}`;
8
+ const getImgElement = () => browser ? document.getElementById(imgId) : null;
9
+ afterUpdate(async () => {
10
+ const img = getImgElement();
11
+ if (!img) {
12
+ return;
13
+ }
14
+ if (img.naturalWidth !== 0 && img.naturalHeight !== 0) {
15
+ img.style.visibility = "visible";
16
+ } else {
17
+ handleImgError();
18
+ }
19
+ });
20
+ const handleImgError = (e) => {
21
+ if (e && e.type !== "error") {
22
+ return;
23
+ }
24
+ if (!src.fallback) {
25
+ return;
26
+ }
27
+ const img = getImgElement();
28
+ if (!img) {
29
+ return;
30
+ }
31
+ img.style.visibility = "hidden";
32
+ let fallbackUrl = void 0;
33
+ const index = src.fallback.findIndex(
34
+ (url) => new URL(url).toString() === new URL(img.src).toString()
35
+ );
36
+ if (index === -1) {
37
+ fallbackUrl = src.fallback[0];
38
+ } else {
39
+ fallbackUrl = src.fallback[index + 1];
40
+ }
41
+ if (!fallbackUrl) {
42
+ return;
43
+ }
44
+ src = {
45
+ ...src,
46
+ img: fallbackUrl,
47
+ srssets: []
48
+ };
49
+ };
50
+ const handleLoaded = (e) => {
51
+ const img = e.currentTarget;
52
+ img.style.visibility = "visible";
53
+ };
54
+ </script>
55
+
56
+ <img
57
+ id={imgId}
58
+ width={src.w}
59
+ height={src.h}
60
+ {style}
61
+ src={src.img}
62
+ srcset={src.srssets.map((s) => `${s.src} ${s.w}w`).join(', ')}
63
+ alt={alt}
64
+ title={title}
65
+ on:error={handleImgError}
66
+ on:load={handleLoaded}
67
+ loading="lazy"
68
+ />
69
+
70
+ <style>
71
+ img {
72
+ visibility: hidden;
73
+ }
74
+ </style>
@@ -0,0 +1,24 @@
1
+ import { SvelteComponent } from "svelte";
2
+ import type { ImgSrc } from './type.js';
3
+ declare const __propDef: {
4
+ props: {
5
+ src: ImgSrc;
6
+ style: any;
7
+ alt?: string | undefined;
8
+ title?: string | undefined;
9
+ };
10
+ events: {
11
+ [evt: string]: CustomEvent<any>;
12
+ };
13
+ slots: {};
14
+ exports?: {} | undefined;
15
+ bindings?: string | undefined;
16
+ };
17
+ export type ImgProps = typeof __propDef.props;
18
+ export type ImgEvents = typeof __propDef.events;
19
+ export type ImgSlots = typeof __propDef.slots;
20
+ export default class Img extends SvelteComponent<ImgProps, ImgEvents, ImgSlots> {
21
+ get alt(): string;
22
+ get title(): string;
23
+ }
24
+ export {};
@@ -24,7 +24,7 @@ $: {
24
24
  }
25
25
  afterUpdate(async () => {
26
26
  const img = getImgElement();
27
- if (!img || !img.complete) {
27
+ if (!img) {
28
28
  return;
29
29
  }
30
30
  if (img.naturalWidth !== 0 && img.naturalHeight !== 0) {
@@ -38,7 +38,7 @@ const handleImgError = (e) => {
38
38
  if (e && e.type !== "error") {
39
39
  return;
40
40
  }
41
- if (!src.failback) {
41
+ if (!src.fallback) {
42
42
  return;
43
43
  }
44
44
  const img = getImgElement();
@@ -46,19 +46,32 @@ const handleImgError = (e) => {
46
46
  return;
47
47
  }
48
48
  img.style.visibility = "hidden";
49
- let failbackUrl = void 0;
50
- const index = src.failback.findIndex(
49
+ if (!img.complete) {
50
+ return;
51
+ }
52
+ let fallbackUrl = void 0;
53
+ const index = src.fallback.findIndex(
51
54
  (url) => new URL(url).toString() === new URL(img.src).toString()
52
55
  );
53
56
  if (index === -1) {
54
- failbackUrl = src.failback[0];
57
+ fallbackUrl = src.fallback[0];
55
58
  } else {
56
- failbackUrl = src.failback[index + 1];
59
+ fallbackUrl = src.fallback[index + 1];
57
60
  }
58
- if (!failbackUrl) {
61
+ if (!fallbackUrl) {
59
62
  return;
60
63
  }
61
- img.src = failbackUrl;
64
+ src = {
65
+ ...src,
66
+ img: fallbackUrl,
67
+ webp: [],
68
+ jpeg: [],
69
+ png: [],
70
+ placeholder: {
71
+ color: src.placeholder?.color,
72
+ dataUri: src.placeholder?.dataUri
73
+ }
74
+ };
62
75
  };
63
76
  const handleLoaded = (e) => {
64
77
  const img = e.currentTarget;
@@ -1,8 +1,8 @@
1
1
  import { SvelteComponent } from "svelte";
2
- import type { ImageSrc } from './image.type.js';
2
+ import type { PictureSrc } from './type.js';
3
3
  declare const __propDef: {
4
4
  props: {
5
- src: ImageSrc;
5
+ src: PictureSrc;
6
6
  style?: string | undefined;
7
7
  alt?: string | undefined;
8
8
  title?: string | undefined;
@@ -14,10 +14,10 @@ declare const __propDef: {
14
14
  exports?: {} | undefined;
15
15
  bindings?: string | undefined;
16
16
  };
17
- export type ImageProps = typeof __propDef.props;
18
- export type ImageEvents = typeof __propDef.events;
19
- export type ImageSlots = typeof __propDef.slots;
20
- export default class Image extends SvelteComponent<ImageProps, ImageEvents, ImageSlots> {
17
+ export type PictureProps = typeof __propDef.props;
18
+ export type PictureEvents = typeof __propDef.events;
19
+ export type PictureSlots = typeof __propDef.slots;
20
+ export default class Picture extends SvelteComponent<PictureProps, PictureEvents, PictureSlots> {
21
21
  get alt(): string;
22
22
  get title(): string;
23
23
  }
@@ -2,17 +2,24 @@ export interface Srcset {
2
2
  src: string;
3
3
  w: number;
4
4
  }
5
- export interface ImageSrc {
5
+ export interface PictureSrc {
6
6
  img: string;
7
7
  w?: number;
8
8
  h?: number;
9
9
  webp?: Srcset[];
10
10
  jpeg?: Srcset[];
11
11
  png?: Srcset[];
12
- failback?: string[];
12
+ fallback?: string[];
13
13
  placeholder?: {
14
14
  dataUri?: string;
15
15
  color?: string;
16
16
  };
17
17
  blur?: boolean;
18
18
  }
19
+ export interface ImgSrc {
20
+ img: string;
21
+ srssets: Srcset[];
22
+ w?: number;
23
+ h?: number;
24
+ fallback?: string[];
25
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from './components/image.type'
2
- import Image from './components/Image.svelte'
1
+ export * from './components/type'
2
+ import Picture from './components/Picture.svelte'
3
+ import Img from './components/Picture.svelte'
3
4
 
4
- export { Image }
5
+ export { Picture, Img }
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  // Reexport your entry components here
2
- import Image from './components/Image.svelte';
3
- export { Image };
2
+ import Picture from './components/Picture.svelte';
3
+ import Img from './components/Img.svelte';
4
+ export { Picture, Img };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-remote-image",
3
- "version": "0.2.5",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/ocknamo/svelte-remote-image",
6
6
  "scripts": {
@@ -27,7 +27,8 @@
27
27
  "!dist/**/*.spec.*"
28
28
  ],
29
29
  "peerDependencies": {
30
- "svelte": "^4.0.0"
30
+ "svelte": "^4.0.0",
31
+ "@sveltejs/kit": "^2.0.0"
31
32
  },
32
33
  "devDependencies": {
33
34
  "@biomejs/biome": "^1.8.1",
File without changes