Skip to content

Commit fc4f8d8

Browse files
committed
Refactor src destructuring
1 parent 5132c76 commit fc4f8d8

File tree

5 files changed

+66
-30
lines changed

5 files changed

+66
-30
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
> Elegant responsive images for SvelteKit.
44
5+
Automatically transform local images into multiple widths and next-gen formats, then render its
6+
associated HTML into your SvelteKit project.
7+
58
Demo: https://zerodevx.github.io/svelte-img/
69

710
## Install
@@ -33,12 +36,12 @@ Use anywhere in your Svelte app:
3336
<!-- prettier-ignore -->
3437
```html
3538
<script>
36-
// Import original max-sized image with `?run` query param.
39+
// Import original full-sized image with `?run` query param.
3740
import cat from '$lib/assets/cat.jpg?run'
3841
import Img from '@zerodevx/svelte-img'
3942
</script>
4043

41-
<Img class="any classes" src="{cat}" alt="Cute cat" />
44+
<Img class="my classes" src={cat} alt="Cute cat" />
4245
```
4346

4447
The image component renders into:
@@ -47,16 +50,16 @@ The image component renders into:
4750
<picture>
4851
<source
4952
type="image/avif"
50-
srcset="path/to/avif/480 480w, path/to/avif/1024 1024w, path/to/avif/1920 1920w"
53+
srcset="path/to/avif-480 480w, path/to/avif-1024 1024w, path/to/avif-1920 1920w"
5154
/>
5255
<source
5356
type="image/webp"
54-
srcset="path/to/webp/480 480w, path/to/webp/1024 1024w, path/to/webp/1920 1920w"
57+
srcset="path/to/webp-480 480w, path/to/webp-1024 1024w, path/to/webp-1920 1920w"
5558
/>
5659
<img
57-
class="any classes"
60+
class="my classes"
5861
src="path/to/jpg/480"
59-
srcset="path/to/jpg/480 480w, path/to/jpg/1024 1024w, path/to/jpg/1920 1920w"
62+
srcset="path/to/jpg-480 480w, path/to/jpg-1024 1024w, path/to/jpg-1920 1920w"
6063
loading="lazy"
6164
decoding="async"
6265
style='background: url("data:image/webp;base64,XXX") no-repeat center/cover'

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zerodevx/svelte-img",
3-
"version": "1.0.0-rc.2",
3+
"version": "1.0.0-rc.3",
44
"description": "Elegant responsive images for SvelteKit",
55
"author": "Jason Lee <[email protected]>",
66
"scripts": {

src/lib/SvelteImg.svelte

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
<script>
2-
import { createEventDispatcher } from 'svelte'
3-
42
export let src = []
53
export let loading = 'lazy'
64
export let decoding = 'async'
75
export let ref = {}
86
97
const priority = ['heic', 'heif', 'avif', 'webp', 'jpeg', 'jpg', 'png', 'gif', 'tiff']
108
const blacklist = ['src', 'srcset', 'loading', 'decoding', 'style', 'ref']
11-
const fire = createEventDispatcher()
129
let props = {}
1310
let image = {}
1411
let sources = []
1512
1613
$: if (src.length) {
17-
const list = [...src]
18-
const lqip = list.pop().base64
14+
const { list, lqip } = src.reduce(
15+
(a, c) => {
16+
if (c.base64) a.lqip = c.base64
17+
else a.list.push(c)
18+
return a
19+
},
20+
{ list: [], lqip: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=' }
21+
)
1922
const groups = []
2023
for (const f of priority) {
2124
const group = list.filter((i) => i.format === f)
@@ -40,20 +43,22 @@ $: {
4043
}
4144
</script>
4245

43-
<picture>
44-
{#each sources as { format, srcset }}
45-
<source type="image/{format}" {srcset} />
46-
{/each}
47-
<!-- svelte-ignore a11y-missing-attribute -->
48-
<img
49-
src={image.src}
50-
srcset={image.srcset}
51-
{loading}
52-
{decoding}
53-
style="background:url('{image.lqip}') no-repeat center/cover"
54-
bind:this={ref}
55-
on:click={() => fire('click')}
56-
on:load={() => fire('load')}
57-
{...props}
58-
/>
59-
</picture>
46+
{#if image.src}
47+
<picture>
48+
{#each sources as { format, srcset }}
49+
<source type="image/{format}" {srcset} />
50+
{/each}
51+
<!-- svelte-ignore a11y-missing-attribute -->
52+
<img
53+
src={image.src}
54+
srcset={image.srcset}
55+
{loading}
56+
{decoding}
57+
style="background:url('{image.lqip}') no-repeat center/cover"
58+
bind:this={ref}
59+
on:click
60+
on:load
61+
{...props}
62+
/>
63+
</picture>
64+
{/if}

src/routes/+layout.svelte

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<script>
2+
import '../app.postcss'
3+
import { dev } from '$app/env'
4+
</script>
5+
6+
<svelte:head>
7+
<title>svelte-img</title>
8+
{#if !dev}
9+
<script>
10+
;(function () {
11+
if (location.protocol === 'https:') {
12+
window.ga =
13+
window.ga ||
14+
function () {
15+
;(ga.q = ga.q || []).push(arguments)
16+
}
17+
ga.l = +new Date()
18+
ga('create', 'UA-66946548-3', 'auto')
19+
ga('send', 'pageview')
20+
var el = document.createElement('script')
21+
el.src = 'https://www.google-analytics.com/analytics.js'
22+
document.head.appendChild(el)
23+
}
24+
})()
25+
</script>
26+
{/if}
27+
</svelte:head>
28+
29+
<slot />

src/routes/+page.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script>
2-
import '../app.postcss'
32
import cat01 from './cat01.jpg?run&width=480;1024;1920;2560'
43
import cat02 from './cat02.jpg?run'
54
import cat03 from './cat03.jpg?run'

0 commit comments

Comments
 (0)