Skip to content

Commit 695c7d0

Browse files
committed
Doc profile feat
1 parent 0493cf3 commit 695c7d0

File tree

1 file changed

+59
-24
lines changed

1 file changed

+59
-24
lines changed

README.md

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ Includes special effects:
1212

1313
Hope you like cats. Demo: https://zerodevx.github.io/svelte-img/
1414

15-
> [!WARNING]
16-
> These are the `v2` docs; there are **breaking changes**! Read up on how to
17-
> [migrate](https://github.com/zerodevx/svelte-img/releases/tag/v2.0.0).
18-
1915
## Install
2016

2117
Install the package:
@@ -117,22 +113,50 @@ To change this globally, edit your `vite.config.js`:
117113
```js
118114
import ...
119115

120-
// By default, directives are 'w=480;1024;1920&format=avif;webp;jpg' (9 variants)
116+
// By default, `run` is set to 'w=480;1024;1920&format=avif;webp;jpg' (9 variants)
121117
export default defineConfig({
122118
plugins: [
123119
sveltekit(),
124120
imagetools({
125-
// Now we change it to generate 4 variants instead: 640/1280w in webp/jpg
126-
runDefaultDirectives: new URLSearchParams('w=640;1280&format=webp;jpg')
121+
profiles: {
122+
// Now we change `run` to generate 4 variants instead: 640/1280w in webp/jpg
123+
run: new URLSearchParams('w=640;1280&format=webp;jpg')
124+
}
127125
})
128126
]
129127
})
130128
```
131129

132-
> [!IMPORTANT]
133-
> The `?as=run` preset takes default directives from **`runDefaultDirectives`**! When the preset is
134-
> not used, behaviour falls back to standard `vite-imagetools`, which in turn take defaults from
135-
> `defaultDirectives` as usual, so both can co-exist.
130+
> [!NOTE]
131+
> `runDefaultDirectives` is deprecated and will be removed in the next major; use `profiles`
132+
> instead. When a preset is not used, behaviour falls back to standard `vite-imagetools`, which in
133+
> turn take defaults from `defaultDirectives` as usual, so both can co-exist.
134+
135+
### Profiles
136+
137+
Use profiles to manage multiple defaults. Define in your `vite.config.js`:
138+
139+
```js
140+
export default defineConfig({
141+
plugins: [
142+
sveltekit(),
143+
imagetools({
144+
profiles: {
145+
sm: new URLSearchParams('w=640&format=webp;jpg'),
146+
lg: new URLSearchParams('w=640;1280;1920&format=webp;jpg')
147+
}
148+
})
149+
]
150+
})
151+
```
152+
153+
Then invoke in your app:
154+
155+
```js
156+
import sm from '$lib/a/1.jpg?as=sm' // use `sm` profile
157+
import lg from '$lib/a/2.jpg?as=lg' // use `lg` profile
158+
import normal from '$lib/a/3.jpg?as=run'
159+
```
136160

137161
### On a per-image basis
138162

@@ -149,16 +173,16 @@ Widths/formats can be applied to a particular image. From your `.svelte` file:
149173
<Img {src} alt="cat" />
150174
```
151175

152-
> [!IMPORTANT]
176+
> [!NOTE]
153177
> Order of `format` matters - the _last_ format is used as the fallback image.
154178
155-
If only **one** variant is generated, only the `<img>` tag is rendered, so:
179+
If only **one** variant is generated, then just the `<img>` tag renders, so:
156180

157181
<!-- prettier-ignore -->
158182
```html
159183
<script>
160-
// Generate only 1 variant: 640x640 in png
161-
import src from '$lib/a/cat.jpg?w=640&h=640&format=png&as=run'
184+
// Generate only 1 variant: 640x640 in jpg
185+
import src from '$lib/a/cat.jpg?w=640&h=640&format=jpg&as=run'
162186
import Img from '@zerodevx/svelte-img'
163187
</script>
164188

@@ -175,7 +199,7 @@ Renders into:
175199
decoding="async"
176200
style="background: url(data:image/webp;base64,XXX) no-repeat center/cover"
177201
alt="cat"
178-
src="path/to/png-640"
202+
src="path/to/jpg-640"
179203
/>
180204
```
181205

@@ -318,24 +342,35 @@ for me, but you can apply your own using CSS.
318342
<!-- prettier-ignore -->
319343
```html
320344
<script>
321-
import src from '$lib/a/cat.jpg?as=run'
322345
import Img from '@zerodevx/svelte-img'
346+
import src from '$lib/a/cat.jpg?as=run'
347+
import { onMount } from 'svelte'
323348
324-
let loaded
349+
let ref, mounted, loaded
350+
onMount(() => {
351+
if (ref.complete) loaded = true
352+
mounted = true
353+
})
325354
</script>
326355
327-
<Img class="better-blur {loaded ? 'loaded' : ''}" on:load={() => (loaded = true)} {src} alt="cat" />
356+
<div class="wrap">
357+
<Img {src} bind:ref on:load={() => (loaded = true)} />
358+
<div class="blur" class:mounted class:loaded />
359+
</div>
328360
329361
<style>
330-
:global(img.better-blur)::after {
331-
content: '';
362+
.wrap {
363+
position: relative;
364+
overflow: hidden;
365+
}
366+
.blur {
332367
position: absolute;
333368
inset: 0;
334369
backdrop-filter: blur(20px);
335-
pointer-events: none;
370+
pointer-events: none;
336371
}
337-
:global(img.better-blur.loaded)::after {
338-
backdrop-filter: none;
372+
.mounted.loaded {
373+
display: none;
339374
}
340375
</style>
341376
```

0 commit comments

Comments
 (0)