svelte-remote-image 0.0.1 → 0.1.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 +66 -8
- package/dist/components/Image.svelte +28 -32
- package/dist/index.d.ts +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,17 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
[demo](https://svelte-remote-image.pages.dev/)
|
|
4
4
|
|
|
5
|
-
Display optimized
|
|
5
|
+
Display optimized, responsive and progressive images for Svelte.
|
|
6
|
+
With remote image URL of CDN or other means.
|
|
7
|
+
|
|
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.
|
|
10
|
+
- A pre-rendered low-quality dataURI can be set as a placeholder.
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
Install the package.
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
$ npm i svelte-remote-image
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
And import components and types.
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
import { Image, type ImageSrc } from "svelte-remote-image";
|
|
25
|
+
```
|
|
6
26
|
|
|
7
27
|
## How to use
|
|
8
28
|
|
|
29
|
+
Sample code.
|
|
30
|
+
|
|
9
31
|
```
|
|
10
32
|
<script lang="ts">
|
|
11
|
-
|
|
12
|
-
import { Image } from "svelte-remote-image";
|
|
33
|
+
import { Image, type ImageSrc } from "svelte-remote-image";
|
|
13
34
|
|
|
14
35
|
// CDN URL
|
|
15
|
-
|
|
36
|
+
const optimazerPrefix = 'https://nostr-image-optimizer.ocknamo.com/image/';
|
|
16
37
|
const originalImageUrl = 'https://ocknamo.com/static/b84d6366deec053ff3fa77df01a54464/dccd3/cat.webp'
|
|
17
38
|
|
|
18
39
|
let src: ImageSrc = {
|
|
@@ -33,9 +54,6 @@ Display optimized images using CDN or other means.
|
|
|
33
54
|
};
|
|
34
55
|
</script>
|
|
35
56
|
|
|
36
|
-
<h1>Welcome to Svelte Remote Image</h1>
|
|
37
|
-
<p>Display optimized images using CDN or other means.</p>
|
|
38
|
-
|
|
39
57
|
<Image {src} />
|
|
40
58
|
```
|
|
41
59
|
|
|
@@ -62,9 +80,49 @@ The image component renders into:
|
|
|
62
80
|
}
|
|
63
81
|
```
|
|
64
82
|
|
|
65
|
-
|
|
66
83
|
Inspired by [svelte-img](https://github.com/zerodevx/svelte-img?tab=readme-ov-file#remote-images-from-an-api).
|
|
67
84
|
|
|
85
|
+
## Config
|
|
86
|
+
|
|
87
|
+
### ImageSrc
|
|
88
|
+
|
|
89
|
+
- img: string
|
|
90
|
+
- image url.
|
|
91
|
+
|
|
92
|
+
- w: number
|
|
93
|
+
- image width.
|
|
94
|
+
|
|
95
|
+
- h?: number
|
|
96
|
+
- image height.
|
|
97
|
+
|
|
98
|
+
- webp?: Srcset[]
|
|
99
|
+
- image source for webp.
|
|
100
|
+
|
|
101
|
+
- jpeg?: Srcset[]
|
|
102
|
+
- image source for jpeg.
|
|
103
|
+
|
|
104
|
+
- png?: Srcset[]
|
|
105
|
+
- image source for png.
|
|
106
|
+
|
|
107
|
+
- failback: string
|
|
108
|
+
- failback image url.
|
|
109
|
+
|
|
110
|
+
- alt: string
|
|
111
|
+
- alt
|
|
112
|
+
|
|
113
|
+
- placeholder?: { dataUri?: string; color?: string }
|
|
114
|
+
- Image data and background color for fastest display.
|
|
115
|
+
|
|
116
|
+
- blur?: boolean
|
|
117
|
+
- Whether to use the blur effect when displaying the image.
|
|
118
|
+
|
|
119
|
+
### Srcset
|
|
120
|
+
|
|
121
|
+
- src: string
|
|
122
|
+
- Image URL.
|
|
123
|
+
- w: number
|
|
124
|
+
- width to display.
|
|
125
|
+
|
|
68
126
|
## Developing
|
|
69
127
|
|
|
70
128
|
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
src = { ...src, blur: src.blur === void 0 ? true : src.blur };
|
|
3
3
|
export let style = "";
|
|
4
4
|
let loadStatus = "loading";
|
|
5
|
-
let imageWrapperStyle = "";
|
|
6
5
|
if (src.placeholder) {
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
if (src.placeholder.dataUri) {
|
|
7
|
+
style = `${style} background: url(${src.placeholder.dataUri}) no-repeat center/cover;`;
|
|
8
|
+
}
|
|
9
|
+
if (src.placeholder.color) {
|
|
10
|
+
style = `${style} background-color: ${src.placeholder.color};`;
|
|
11
|
+
}
|
|
9
12
|
}
|
|
10
13
|
const handleImgError = (e) => {
|
|
11
14
|
if (e.type !== "error") {
|
|
@@ -24,30 +27,28 @@ const handleLoaded = () => {
|
|
|
24
27
|
};
|
|
25
28
|
</script>
|
|
26
29
|
|
|
27
|
-
<
|
|
28
|
-
|
|
29
|
-
{
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
</picture>
|
|
50
|
-
</div>
|
|
30
|
+
<picture>
|
|
31
|
+
{#if src.webp}
|
|
32
|
+
<source srcset={src.webp.map((s) => `${s.src} ${s.w}w`).join(', ')} type="image/webp" />
|
|
33
|
+
{/if}
|
|
34
|
+
{#if src.jpeg}
|
|
35
|
+
<source srcset={src.jpeg.map((s) => `${s.src} ${s.w}w`).join(', ')} type="image/jpeg" />
|
|
36
|
+
{/if}
|
|
37
|
+
{#if src.png}
|
|
38
|
+
<source srcset={src.png.map((s) => `${s.src} ${s.w}w`).join(', ')} type="image/png" />
|
|
39
|
+
{/if}
|
|
40
|
+
<img
|
|
41
|
+
width={src.w}
|
|
42
|
+
height={src.h}
|
|
43
|
+
{style}
|
|
44
|
+
class={src.blur ? `image-blur-${loadStatus}` : ''}
|
|
45
|
+
src={src.img}
|
|
46
|
+
alt={src.alt}
|
|
47
|
+
on:error={handleImgError}
|
|
48
|
+
on:load={handleLoaded}
|
|
49
|
+
loading="lazy"
|
|
50
|
+
/>
|
|
51
|
+
</picture>
|
|
51
52
|
|
|
52
53
|
<style>
|
|
53
54
|
.image-blur-loading {
|
|
@@ -81,11 +82,6 @@ const handleLoaded = () => {
|
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
.picture-wrapper {
|
|
85
|
-
width: fit-content;
|
|
86
|
-
height: fit-content;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
85
|
img {
|
|
90
86
|
display: block;
|
|
91
87
|
}
|
package/dist/index.d.ts
CHANGED