svelte-remote-image 0.2.0 → 0.2.1

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.
@@ -1,22 +1,31 @@
1
- <script>import { fail } from "@sveltejs/kit";
2
- import { afterUpdate } from "svelte";
1
+ <script>import { browser } from "$app/environment";
2
+ import { afterUpdate, tick } from "svelte";
3
3
  export let src;
4
4
  export let style = "";
5
5
  const imgId = `svelte-remote-image-${src.alt.replaceAll(" ", "-")}-${Math.round(Math.random() * 1e7)}`;
6
+ const getImgElement = () => browser ? document.getElementById(imgId) : null;
6
7
  let loadStatus = "loading";
7
- if (src.placeholder) {
8
- if (src.placeholder.dataUri) {
9
- style = `${style} background: url(${src.placeholder.dataUri}) no-repeat center/cover;`;
8
+ $: {
9
+ loadStatus = "loading";
10
+ if (src.placeholder) {
11
+ if (src.placeholder.dataUri) {
12
+ style = `${style} background: url(${src.placeholder.dataUri}) no-repeat center/cover;`;
13
+ }
14
+ if (src.placeholder.color) {
15
+ style = `${style} background-color: ${src.placeholder.color};`;
16
+ }
10
17
  }
11
- if (src.placeholder.color) {
12
- style = `${style} background-color: ${src.placeholder.color};`;
18
+ const img = getImgElement();
19
+ if (img) {
20
+ img.style.visibility = "hidden";
13
21
  }
14
22
  }
15
- afterUpdate(() => {
23
+ afterUpdate(async () => {
16
24
  const img = getImgElement();
17
25
  if (!img) {
18
26
  return;
19
27
  }
28
+ await tick();
20
29
  if (img.naturalWidth !== 0 && img.naturalHeight !== 0) {
21
30
  img.style.visibility = "visible";
22
31
  loadStatus = "loaded";
@@ -32,7 +41,7 @@ const handleImgError = (e) => {
32
41
  if (!img) {
33
42
  return;
34
43
  }
35
- img.style.visibility = "hiddin";
44
+ img.style.visibility = "hidden";
36
45
  let failbackUrl = void 0;
37
46
  const index = src.failback.findIndex(
38
47
  (url) => new URL(url).toString() === new URL(img.src).toString()
@@ -62,7 +71,6 @@ const handleLoaded = (e) => {
62
71
  img.style.visibility = "visible";
63
72
  loadStatus = "loaded";
64
73
  };
65
- const getImgElement = () => document.getElementById(imgId);
66
74
  </script>
67
75
 
68
76
  <picture>
@@ -80,7 +88,7 @@ const getImgElement = () => document.getElementById(imgId);
80
88
  width={src.w}
81
89
  height={src.h}
82
90
  {style}
83
- class={src.blur ? `svelte-remote-image image-blur-${loadStatus}` : 'svelte-remote-image'}
91
+ class={src.blur ? `image-blur-${loadStatus}` : ''}
84
92
  src={src.img}
85
93
  alt={src.alt}
86
94
  on:error={handleImgError}
@@ -90,10 +98,6 @@ const getImgElement = () => document.getElementById(imgId);
90
98
  </picture>
91
99
 
92
100
  <style>
93
- img.svelte-remote-image {
94
- /* initial value */
95
- visibility: hidden;
96
- }
97
101
  .image-blur-loading {
98
102
  animation:
99
103
  0.5s linear 0s normal waiting,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-remote-image",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/ocknamo/svelte-remote-image",
6
6
  "scripts": {