MAIS AMOR POR FAVOR

Simple Svelte Responsive Image Gallery

Simple Svelte Responsive Image Gallery

Simple Svelte Responsive Image Gallery

SHARE:

Simple Svelte Responsive Image Gallery: Introduction

We look at a simple Svelte responsive image gallery in this post. By simple I mean to say the functionality is simple. Despite that it lets us explore a couple of Svelte and Vite features which are a little more advanced. In particular we look at glob importing where we can import, for example all files matching a certain pattern into SvelteKit JavaScript code. We also use Svelte dimension bindings to make sure all images from our gallery tall and wide look good, maintaining aspect ratio as the window size changes. As well as being responsive the images will be generated in Next-Gen formats. Finally we add an optimisation which should help with the Core Web Vitals Largest Contentful Paint metric. I should also mention we add lazy loading as another Core Web Vitals optimisation.

Simple Svelte Responsive Image Gallery: What we're Building

Generally, I prefer to roll my own components whenever possible, rather than leaning on libraries so really enjoyed putting this tutorial together. If you are looking for a simple scrolling gallery, supporting modern image formats that is responsive, this should do the trick. Even if you are looking for a fully-featured light box, you will probably find parts here which you can recycle for use with your own code or library.

⚙️ Getting Started

There's a bit to get through so let's get going! I have used a script to generate image data automatically to speed things up, so you will need to download those image data files as well as the images themselves in a moment. First though let's spin up a new skeleton project:

pnpm init [email protected] sveltekit-simple-image-gallery && cd $_
pnpm install
pnpm install @fontsource/inter @rodneylab/sveltekit-components svelte-feather-icons vanilla-lazyload vite-imagetools
pnpm run dev

From the options, choose Skeleton project, Use TypeScript:? No, Add ESLint...? Yes and Add Prettier...? Yes. As well as set up Svelte, we have installed a font and a Svelte component library to help with generating responsive image boiler plate. Together with those two packages, we have some icons for the next / previous buttons to move between images. Finally there's a couple of packages to help with lazy loading and Next-Gen image generation and caching.

As an extra bit of setup, update svelte.config.js for use with vite-imagetools:

svelte.config.js
javascript
1import { imagetools } from 'vite-imagetools';
2
3/** @type {import('@sveltejs/kit').Config} */
4const config = {
5 kit: {
6 // hydrate the <div id="svelte"> element in src/app.html
7 target: '#svelte',
8 vite: {
9 plugins: [imagetools({ force: true })],
10 },
11 },
12};
13
14export default config;

File Download

Lastly create a src/lib/assets/ folder and download the six images from that location in the Git repo . Finally create src/lib/generated and repeat, copying the JavaScript files from the equivalent folder on the GitHub repo . Typically you would want to generate these files in a script, updating for required images formats, widths and pixel densities, though I have done this already to save time. You can take a look at the script which generates this data including low resolution placeholders in the repo.

🗳️ Poll

What's the most next-gen image format you use on new projects?
Voting reveals latest results.

🔨 Server Route

Next we will create a server route. This file will look for the JavaScript image data files we just downloaded and generate a single array image data (spanning all images). Create the file at src/routes/index.json.js and add the following content:

src/routes/index.json.js
javascript
1export async function get() {
2 try {
3 const images = await import.meta.glob('../lib/generated/*.js');
4 const imageDataPromises = Object.keys(images).map((path) => images[path]());
5 const imageData = await Promise.all(imageDataPromises);
6 const imageDataArray = imageData.map((element) => element.default);
7
8 return {
9 body: JSON.stringify({ data: imageDataArray }),
10 };
11 } catch (error) {
12 console.error('Error: ', error);
13 return {
14 status: 500,
15 error: `Error in index.json data retrieval: ${error}`,
16 };
17 }
18}

There are one or two interesting things in here. In line 3, we are importing all of the JavaScript files in the lib/generated folder. To do this, we use a Vite Glob Import . Essentially, Vite expands this to an object:

const images = {
'../lib/generated/image-1.js': () => import('../lib/generated/image-1.js'),
'../lib/generated/image-2.js': () => import('../lib/generated/image-2.js'),
'../lib/generated/image-3.js': () => import('../lib/generated/image-3.js'),
'../lib/generated/image-4.js': () => import('../lib/generated/image-4.js'),
'../lib/generated/image-5.js': () => import('../lib/generated/image-5.js'),
'../lib/generated/image-6.js': () => import('../lib/generated/image-6.js'),
}

Each of the members of the object is a key-value pair, with the key being the path for one of the files in our folder. The value in each case is the import function, so to complete the import, we need to call the function on each field. We do that in line 4, generating a promise for each file we import and mapping all the promises to an array.

Over the following lines, we extract the default export from each of the files using the Promises API . If this is your first time using async/await, you might find the explanation in the post on the SvelteKit Image plugin useful.

Our endpoint generates an array of image data which we will use next on the home page.

🏠 Home Page Svelte

Next, we will replace the code in src/routes/index.svelte with the following:

src/routes/index.svelte
svelte
1<script context="module">
2 export const load = async ({ fetch }) => {
3 try {
4 const response = await fetch('/index.json', {
5 method: 'GET',
6 credentials: 'same-origin',
7 });
8 return {
9 props: { ...(await response.json()) },
10 };
11 } catch (error) {
12 console.error(error);
13 }
14 };
15</script>
16
17<script>
18 import { browser } from '$app/env';
19 import RibbonGallery from '$lib/components/RibbonGallery.svelte';
20 import '@fontsource/inter';
21 import { onMount } from 'svelte';
22 import lazyload from 'vanilla-lazyload';
23
24 export let data;
25
26 onMount(() => {
27 if (browser) {
28 document.lazyloadInstance = new lazyload();
29 }
30 });
31
32 // import image data for caching images
33 (async () => {
34 await import.meta.glob('../lib/generated/*.js');
35 })();
36</script>
37
38<svelte:head>
39 <title>Basic Responsive Ribbon Gallery</title>
40 <html lang="en-GB" />
41 <meta
42 name="description"
43 content="Demo site for basic responsive image library with a ribbon layout"
44 />
45</svelte:head>
46
47<div class="container">
48 <header class="header"><h1>Basic Responsive Ribbon Gallery</h1></header>
49 <main><RibbonGallery {data} /></main>
50</div>
51
52<style>
53 :global(html) {
54 font-family: 'Inter';
55 background: #006e90;
56 min-height: 100vh;
57 }
58
59 :global(body) {
60 margin: 0;
61 }
62
63 .container {
64 max-width: 1280px;
65 margin: 0 auto;
66 }
67
68 .header {
69 background: #01200f;
70 color: #eef5db;
71 font-weight: 900;
72 padding: 1rem 1.5rem;
73 }
74</style>

In line 115 we have a standard SvelteKit load function in which we get the image data array from our endpoint.

The onMount function is called when our home page is created. We initialise our lazyload at this point. You can see more on this in the post on Lazy loading iframes in SvelteKit.

Lines 3235 probably seem pointless as we do not use the result anywhere. In these lines, we are importing the files we use in the endpoint to generate the image data array. In fact we only do this import here to ensure the images are cached . You might find you can omit this code running in dev mode, but switch to build and have no images!

In line 49 we add our image gallery component to the DOM. Let's add the code for this and a couple of ancillary components to our project next.

🧩 Simple Svelte Responsive Image Gallery Components

We will use feather icons for our forward and previous user interface buttons. Create a folder at src/lib/components then add NextIcon.svelte and PreviousIcon.svelte to the folder, and paste in this code:

src/lib/components/NextIcon.svelte
svelte
1<script>
2 import { SkipForwardIcon } from 'svelte-feather-icons';
3</script>
4
5<SkipForwardIcon size="32" />
src/lib/components/PreviousIcon.svelte
svelte
1<script>
2 import { SkipBackIcon } from 'svelte-feather-icons';
3</script>
4
5<SkipBackIcon size="32" />

We're almost done now! Next step is to add the final missing piece; the gallery component.

🖼️ Ribbon Gallery Component

The image gallery will have a few features to make the pictures look their best. This includes preserving the image aspect ratio when the window is resized and keeping all images the same height as we scale. As well as that we want to ensure that for a small-screened device, the widest image in the gallery can be displayed, without panning. For this to happen, we need to work out which is the widest image and use its aspect ratio to set the height for all of the images. To get all of this right, we will use Svelte dimension binding. There is a little maths (math) involved, but it's not too complex.

Lets start putting the image component together. Create a src/lib/components/RibbonGallery.svelte file and paste in the following code:

src/lib/components/RibbonGallery.svelte
svelte
1<script lang>
2 import { browser } from '$app/env';
3 import NextIcon from '$lib/components/NextIcon.svelte';
4 import PreviousIcon from '$lib/components/PreviousIcon.svelte';
5 import { Image } from '@rodneylab/sveltekit-components';
6 import { afterUpdate, onMount } from 'svelte';
7
8 export let data;
9
10 let containerHeight;
11 let containerWidth;
12
13 let maxAspectRatio = 1.0;
14
15 $: aspectRatios = data.map((element) => {
16 const { width, height } = element;
17 const aspectRatio = width / height;
18 if (aspectRatio > maxAspectRatio) {
19 maxAspectRatio = aspectRatio;
20 }
21 return aspectRatio;
22 });
23
24 $: height = 512;
25 $: calculateHeight;
26 function calculateHeight() {
27 if (containerHeight && containerWidth) {
28 const maxHeight = containerHeight - 59;
29 height =
30 containerWidth / maxHeight < maxAspectRatio ? containerWidth / maxAspectRatio : maxHeight;
31 }
32 }
33
34 onMount(() => {
35 if (browser && document.lazyloadInstance) {
36 document.lazyloadInstance.update();
37 calculateHeight();
38 }
39 });
40
41 afterUpdate(() => {
42 calculateHeight();
43 });
44
45 $: widths = [...aspectRatios.map((element) => parseFloat((element * height).toFixed(2)))];
46 $: sizes = [...widths.map((element) => `${element}px`)];
47 $: currentIndex = 0;
48 $: imageTitle = data[currentIndex].title;

Here in lines 10 & 11 we create variables which we need to hold the measurements for our container height and width. Then at lines 15 to 22 we have a utility function to work out the image with highest aspect ratio. Aspect ratio is width divided by height, so the widest image has the largest aspect ratio.

Image Height

Next in line 2432 we work out what height our images should have. To start the ball rolling, we set an initial height of 512px. In a moment we will see that we bind containerHeight and containerWidth to the actual DOM object dimensions. Because of that, we need to wait for the DOM to be ready, before we have a value (hence the guard in line 27). The element we measure will have the images on top and some controls to shuffle through the images below. In between there might be some space, depending on the browser window height. We always want to allow some space for the controls below so in determining in the height for our images, we subtract the height of the controls (59px) in line 28.

Simple Svelte Responsive Image Gallery: Screenshot: Image shows gallery in short, wide browser window.  Three portrait-orientation images and a single landscape-orientation image are shown side-by-side.  Below are previous and next controls.
Simple Svelte Responsive Image Gallery: Responsiveness Test

Moving on to the code in line 30. Let's call the difference between the height of our measured element and the height of the controls the maximum height. Generally, we want the images to be as big as possible, so try to set their height to be equal to the maximum height. In line 30, we look at the widest image and if we find it is just too wide to display at maximum height (without having to pan), we reduced the height of all the images. The height we choose is back calculated from the width of our element and the aspect ratio of this widest image.

So, this block is just working out when we need to reduce the image height, and what that reduced height should be. We call the calculateHeight function when the component first mounts (line 37) and then again when it updates (line 42), to keep the height good.

Previous, Next Image Logic

Let's add some logic to move between images next, by pasting this code at the bottom of the same file:

src/lib/components/RibbonGallery.svelte
svelte
50const imageCount = data.length;
51
52 function advanceIndex() {
53 currentIndex = (currentIndex + 1) % imageCount;
54 }
55
56 function regressIndex() {
57 currentIndex = (currentIndex + imageCount - 1) % imageCount;
58 }
59
60 function prefersReducedMotion() {
61 return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
62 }
63
64 function scrollToNextImage() {
65 advanceIndex();
66 if (prefersReducedMotion()) {
67 document
68 .getElementById(`image-${currentIndex + 1}`)
69 .scrollIntoView({ behavior: 'auto', block: 'center', inline: 'start' });
70 } else {
71 document
72 .getElementById(`image-${currentIndex + 1}`)
73 .scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
74 }
75 }
76
77 function scrollToPreviousImage() {
78 regressIndex();
79 if (prefersReducedMotion()) {
80 document
81 .getElementById(`image-${currentIndex + 1}`)
82 .scrollIntoView({ behavior: 'auto', block: 'center', inline: 'start' });
83 } else {
84 document
85 .getElementById(`image-${currentIndex + 1}`)
86 .scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'start' });
87 }
88 }
89</script>

In lines 53 and 57 we are using the modulus operation (%) so we can loop around to the first or last image when we get to the last image. I really love the way Svelte handles animation and makes it easy to add some polish to image transitions in image galleries. Here though in-built HTML functionality is pretty good and we will rely on that. In particular we are using element.scrollIntoView() .

For this API to work, we add a unique id to each of our images and scroll to the id of whichever image we choose. The rest just works! If you have a lot of images though and scroll from the first to last, scrolling can be quite quick when smooth scrolling in switched on! If the user prefers reduced motion, we revert to auto which scrolls a little slower. That's all the JavaScript so let's add some HTML markup now.

Svelte Dimension Binding

Paste this svelte code at the bottom of the same file:

src/lib/components/RibbonGallery.svelte
svelte
91<div class="container" bind:clientWidth={containerWidth} bind:clientHeight={containerHeight}>
92 <div class="ribbon">
93 <section class="images">
94 {#each data as { alt, src, sources, placeholder }, index}
95 <div id={`image-${index + 1}`}>
96 <Image
97 {alt}
98 width={widths[index]}
99 {height}
100 {src}
101 {sources}
102 {placeholder}
103 sizes={sizes[index]}
104 loading={index === 0 ? 'eager' : 'lazy'}
105 importance={index === 0 ? 'high' : 'auto'}
106 maxWidth="1280px"
107 />
108 </div>
109 {/each}
110 </section>
111 </div>
112 <section class="info">
113 <div class="controls">
114 <span class="prev-next-button">
115 <button
116 on:click={() => {
117 scrollToPreviousImage();
118 }}><PreviousIcon /><span class="screen-reader-text">previous image</span></button
119 ></span
120 >
121 <p>{currentIndex + 1} of {imageCount}</p>
122 <span class="prev-next-button">
123 <button
124 on:click={() => {
125 scrollToNextImage();
126 }}><NextIcon /><span class="screen-reader-text">next image</span></button
127 ></span
128 >
129 </div>
130 <div class="title-text"><h1>{imageTitle}</h1></div>
131 </section>
132</div>

We saw previously we had dimensions of the container element in the JavaScript for this component. In line 91 you see how we bind the Svelte measured dimension to the JavaScript variable. Once more Svelte makes something that could very complicated very simple. Be careful not to use this where it is not necessary as it comes with a performance hit. Learn more about Svelte dimension bindings in Svelte docs .

Image Load Optimisation

We have a few image loading optimisations here to help improve Core Web Vitals together with the user experience as well as SEO of your app. We already mentioned images are lazy loaded. This means the user's browser initially only loads the images that are in view. The others are only loaded when the user scrolls over. The vanilla-lazyload plugin helps with this. On top we give a hint to the browser in line 104 to load images lazily. We want the user to see something when the page first load so the first image loads eagerly.

Next, we add low resolution placeholders. Together with width and height data, which we supply, this lets the browser know how much space to reserve for the images, reducing cumulative layout shift. Because we want the image to scale to the browser width and maintain aspect ratio, there is some potential for CLS for any elements below the images in the DOM. Bear this is mind if you use this code for other projects.

Finally we set importance to high for the first image in line 105. This is another hint to the browser to give the user something to see quicker and should help to improve the First Contentful Paint metric.

As an aside, in line 95 we add a unique id to each image to help with the scroll into view function we looked at earlier.

Style

The last part is to add style. Unlike some other tutorials on this site, styling is needed here for the gallery to work as expected. This is mostly because we set heights on some elements. To finish off paste this CSS code at the end of the RibonGallery.svelte file:

src/lib/components/RibbonGallery.svelte
svelte
134<style>
135 .container {
136 display: flex;
137 flex-direction: column;
138 height: calc(100vh - 7.12rem);
139 max-width: 1280px;
140 width: 100%;
141 margin: 0 auto;
142 }
143
144 .ribbon {
145 display: flex;
146 width: 100%;
147 }
148
149 .images {
150 display: flex;
151 overflow: hidden;
152 }
153
154 .info {
155 display: flex;
156 align-items: center;
157 margin: auto 1rem 1rem;
158 color: #eef5db;
159 }
160
161 .info button {
162 border-style: none;
163 background: transparent;
164 }
165
166 .info p,
167 .info h1 {
168 margin-top: 0;
169 margin-bottom: 0;
170 }
171 .info h1 {
172 font-size: 1.563rem;
173 }
174
175 .controls {
176 display: flex;
177 align-items: center;
178 padding-right: 0.5rem;
179 width: max-content;
180 }
181
182 .controls .prev-next-button {
183 display: flex;
184 vertical-align: middle;
185 color: #eef5db;
186 }
187
188 .controls button {
189 color: #eef5db;
190 padding: 0.75rem 0.5rem;
191 cursor: pointer;
192 }
193
194 .title-text {
195 padding-right: auto;
196 }
197
198 .screen-reader-text {
199 border: 0;
200 clip: rect(1px, 1px, 1px, 1px);
201 clip-path: inset(50%);
202 height: 1px;
203 margin: -1px;
204 width: 1px;
205 overflow: hidden;
206 position: absolute !important;
207 word-wrap: normal !important;
208 }
209</style>

That's all the code and everything should work now. Give it a try!

💯 Simple Svelte Responsive Image Gallery: Testing

That's it, mission complete (apart from testing). First we want to make sure the controls work for moving between images. Make sure you can bring all the images into view using the previous and next buttons. Then try resizing the browser window. All images should maintain aspect ratio as you make the window larger or smaller.

Simple Svelte Responsive Image Gallery: Screenshot: Image shows gallery in tall, narrow browser window.  A single landscape-orientation image takes the full window width.
Simple Svelte Responsive Image Gallery: Responsiveness Test

The final test is to make the browser window tall and narrow and scroll to the fourth image. It should span the width of the window. You should not need to pan to see the entire image.

If that's all work let's recap and look at some extensions.

🙌🏽 Simple Svelte Responsive Image Gallery: What we Learned

In this post we saw:

  • how you can bind the dimensions of an element to a JavaScript variable in Svelte,

  • a way to import all the files in a particular using Vite glob imports,

  • how to optimise images for Core Web Vitals and better user experience.

I do hope there is at least one thing in this article which you can use in your work or a side project. As an extension you might consider infinitely looping the images, so you don't get the disjoint scroll when you reach the last image. You would have to anticipate reaching the last image and tack the first image onto the end of the array (and something similar for scrolling backwards past the first image).

You can see the full code for this using Simple Svelte Responsive Image Gallery tutorial on the Rodney Lab Git Hub repo . As always get in touch with feedback if I have missed a trick somewhere!

🙏🏽 Simple Svelte Responsive Image Gallery: Feedback

Have you found the post useful? Do you have your own methods for solving this problem? Let me know your solution. Would you like to see posts on another topic instead? Get in touch with ideas for new posts. Also if you like my writing style, get in touch if I can write some posts for your company site on a consultancy basis. Read on to find ways to get in touch, further below. If you want to support posts similar to this one and can spare a few dollars, euros or pounds, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.