Skip to content

Commit 5607888

Browse files
committed
Refactor for v1 release
1 parent fc4f8d8 commit 5607888

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

src/lib/SvelteImg.svelte

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
<script>
22
export let src = []
3+
export let sizes = undefined
34
export let loading = 'lazy'
45
export let decoding = 'async'
5-
export let ref = {}
6+
export let ref = undefined
67
7-
const priority = ['heic', 'heif', 'avif', 'webp', 'jpeg', 'jpg', 'png', 'gif', 'tiff']
8-
const blacklist = ['src', 'srcset', 'loading', 'decoding', 'style', 'ref']
9-
let props = {}
108
let image = {}
119
let sources = []
1210
1311
$: if (src.length) {
1412
const { list, lqip } = src.reduce(
1513
(a, c) => {
16-
if (c.base64) a.lqip = c.base64
14+
if (c.base64) a.lqip = `url('${c.base64}') no-repeat center/cover`
1715
else a.list.push(c)
1816
return a
1917
},
20-
{ list: [], lqip: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=' }
18+
{ list: [], lqip: undefined }
2119
)
2220
const groups = []
23-
for (const f of priority) {
24-
const group = list.filter((i) => i.format === f)
21+
for (const format of ['heic', 'heif', 'avif', 'webp', 'jpeg', 'jpg', 'png', 'gif', 'tiff']) {
22+
const group = list.filter((i) => i.format === format)
2523
if (group.length) {
2624
group.sort((a, b) => a.width - b.width)
2725
groups.push({
28-
format: f,
26+
format,
2927
srcset: group.reduce((a, c) => [...a, `${c.src} ${c.width}w`], []).join(','),
3028
src: group[0].src
3129
})
@@ -34,31 +32,25 @@ $: if (src.length) {
3432
image = { ...groups.pop(), lqip }
3533
sources = groups
3634
}
37-
38-
$: {
39-
props = {}
40-
for (const tag in $$props) {
41-
if (!blacklist.includes(tag)) props[tag] = $$props[tag]
42-
}
43-
}
4435
</script>
4536

4637
{#if image.src}
4738
<picture>
4839
{#each sources as { format, srcset }}
49-
<source type="image/{format}" {srcset} />
40+
<source type="image/{format}" {srcset} {sizes} />
5041
{/each}
5142
<!-- svelte-ignore a11y-missing-attribute -->
5243
<img
5344
src={image.src}
5445
srcset={image.srcset}
46+
{sizes}
5547
{loading}
5648
{decoding}
57-
style="background:url('{image.lqip}') no-repeat center/cover"
49+
style:background={image.lqip}
5850
bind:this={ref}
5951
on:click
6052
on:load
61-
{...props}
53+
{...$$restProps}
6254
/>
6355
</picture>
6456
{/if}

src/lib/vite.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ function lqip(cfg, ctx) {
1515
}
1616

1717
function set(url, defaults = 'width=480;1024;1920&format=avif;webp;jpg') {
18-
const { searchParams } = url
19-
const out = new URLSearchParams(defaults)
20-
for (const [key, val] of searchParams.entries()) out.set(key, val)
21-
return out
18+
const merge = new URLSearchParams(defaults)
19+
for (const [key, val] of url.searchParams.entries()) merge.set(key, val)
20+
return merge
2221
}
2322

24-
function main(opts) {
23+
function main(overrides = {}) {
2524
return imagetools({
2625
defaultDirectives: (url) => set(url),
2726
extendTransforms: (builtins) => [...builtins, lqip],
@@ -31,11 +30,10 @@ function main(opts) {
3130
}),
3231
resolveConfigs: (e, f) => {
3332
const idx = e.findIndex((i) => i[0] === 'lqip')
34-
let lqip = 16
35-
if (idx > -1) lqip = parseInt(e.splice(idx, 1)[0][1][0])
33+
const lqip = idx > -1 ? parseInt(e.splice(idx, 1)[0][1][0]) : 16
3634
return [...resolveConfigs(e, f), { lqip }]
3735
},
38-
...opts
36+
...overrides
3937
})
4038
}
4139

src/routes/+layout.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dev } from '$app/env'
55

66
<svelte:head>
77
<title>svelte-img</title>
8+
<meta name="description" content="Elegant responsive images for SvelteKit." />
89
{#if !dev}
910
<script>
1011
;(function () {

src/routes/+page.svelte

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ import cat15 from './cat15.jpg?run&width=640&height=640&fit=cover'
1717
import Img from '$lib/index.js'
1818
1919
const features = [
20-
['Full width responsive images served in multiple widths and next-gen formats.', cat02],
21-
['Native lazy loading with a <code>16px</code> wide placeholder image background.', cat03],
22-
['Pre-rendered pages that still work without Javascript.', cat04]
20+
[
21+
'Render the bare minimum, minimally invasive, HTML code to represent responsive images, served in multiple widths and next-gen formats.',
22+
cat02
23+
],
24+
[
25+
'Delegate the heavy lifting to native browser handlers and existing build pipelines. Render <strong>&lt;img&gt;</strong> tags with modern defaults, with other component props spread into it.',
26+
cat03
27+
],
28+
[
29+
'Images still work without Javascript. Try disabling JS and refreshing the page. Also try checking the lighthouse score.',
30+
cat04
31+
]
2332
]
2433
const dynamic = [cat05, cat06, cat07, cat08, cat09, cat10, cat11, cat12, cat13, cat14, cat15]
2534
let selected = 0
@@ -31,7 +40,7 @@ let selected = 0
3140
<h1>svelte-img</h1>
3241
<p>Elegant responsive images for SvelteKit.</p>
3342
<a
34-
class="h-14 flex items-center justify-center rounded-full text-xs font-bold tracking-wider bg-blue-500 hover:bg-blue-900 px-6 mt-8 mb-6"
43+
class="h-14 flex items-center justify-center rounded-full text-xs font-bold tracking-wider bg-blue-600 hover:bg-blue-800 px-6 mt-8 mb-6"
3544
href="https://github.com/zerodevx/svelte-img">VISIT GITHUB REPO</a
3645
>
3746
</div>
@@ -43,28 +52,30 @@ let selected = 0
4352
<Img class="w-full h-128 object-cover" src={f[1]} alt="cat" />
4453
{/each}
4554

46-
<p><code>src</code> can be dynamically changed. Well this requires Javascript.</p>
47-
<div class="flex justify-center">
48-
<Img
49-
class="w-96 h-96 sm:w-128 sm:h-128 aspect-square object-cover"
50-
src={dynamic[selected]}
51-
alt="cat"
52-
width="1"
53-
height="1"
54-
/>
55-
</div>
56-
<div class="flex flex-wrap mt-4">
57-
{#each dynamic as src, i}
58-
<div>
55+
<p>
56+
Image <code>src</code> can be dynamically updated - well, this requires Javascript because interactivity.
57+
</p>
58+
<div class="flex flex-col md:flex-row">
59+
<div class="flex justify-center">
60+
<Img
61+
class="w-96 h-96 md:w-128 md:h-128 object-cover"
62+
src={dynamic[selected]}
63+
alt="cat"
64+
width="1"
65+
height="1"
66+
/>
67+
</div>
68+
<div class="flex flex-wrap justify-center mt-4 md:flex-1 md:mt-6">
69+
{#each dynamic as src, i}
5970
<Img
60-
class="w-36 h-36 m-2 border-4 cursor-pointer {selected === i
71+
class="w-32 h-32 md:w-36 md:h-36 m-2 border-4 cursor-pointer {selected === i
6172
? 'border-red-500'
62-
: 'border-transparent'}"
73+
: 'border-gray-100'}"
6374
{src}
6475
alt="cat"
6576
on:click={() => (selected = i)}
6677
/>
67-
</div>
68-
{/each}
78+
{/each}
79+
</div>
6980
</div>
7081
</div>

0 commit comments

Comments
 (0)