Build a light gallery with Stimulus Components
Tags: rails, stimulus, javascript, tutorial
Published: 2021-09-25 07:56:49

<small>Orginally published at Medium on Feb 17th 2020.</small>

1. [Install the package](#1-install-the-package)
2. [Add the Stimulus Lightbox library](#2-add-the-stimulus-lightbox-library)
3. [Import stylesheets](#3-import-stylesheets)
4. [Add HTML layout](#4-add-basic-html-layout)
5. [Add styling to images](#5-add-styling-to-images)
6. [Add captions](#6-add-captions)
7. [Add additional options](#7-add-additional-options)

In this article, you will learn how to set up a light gallery with Stimulus, a modest JavaScript framework for the HTML you already have. Stimulus is a powerful alternative to SPA’s and enables developers to bring web applications to life.

> Stimulus. A modest JavaScript framework for the HTML you already have.

[Stimulus Components](https://stimulus-components.netlify.app/) is an open-source project that hosts a collection of customizable components for typical JavaScript behavior. One component helps you to create a feature-rich light gallery into your project without writing any custom JavaScript at all. See [lightgallery.js](https://sachinchoolur.github.io/lightgallery.js/) to get an idea of what you can build after reading this article.

Stimulus is back-end and framework agnostic. Use it with your preferred back-end framework of choice. In this tutorial, Ruby on Rails is the chosen back-end framework.

---

## What we will build

A light gallery for a pillow store.

![Pillow gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1r6bheyd33jvghtdc79g.gif)

---

## Before You Start

Make sure you have Stimulus installed. Check the package.json file or run yarn why stimulus. If Stimulus is not yet installed, follow the [documentation](https://stimulus.hotwire.dev/handbook/installing).

Rails users can watch a [GoRails episode](https://www.notion.so/Make-sure-you-have-Stimulus-installed-Check-package-json-file-or-run-yarn-why-stimulus-If-stimul-79f82350d58b4baaaca65d130ba25784). When using webpack, it's as easy as running `rails webpacker:install:stimulus`.

## 1. Install the package

Run `yarn add stimulus-lightbox` in the terminal.

## 2. Add the Stimulus Lightbox library

```js
// app/javascript/controllers/index.js

import { Application } from "stimulus"
import Lightbox from "stimulus-lightbox"

const application = Application.start()
application.register("lightbox", Lightbox)
```

## 3. Import stylesheets

```js
// In your application.js (for example)
import "lightgallery.js/dist/css/lightgallery.min.css"
  
// Or in your application.scss file
@import "lightgallery.js/src/sass/lightgallery"
```

## 4. Add basic HTML layout

The lightgallery.js library looks for a data-scr attribute. The rails image tage does not provide that by default. Manually add in a data-src attribute in the HTML.

```erb
<div data-controller="lightbox" class="images">
 <%= image_tag "pillow1",  data: { src: "../assets/pillow1.jpg" } %>
 <%= image_tag "pillow2",  data: { src: "../assets/pillow2.jpg" } %>
 <%= image_tag "pillow3",  data: { src: "../assets/pillow3.jpg" } %>
 <%= image_tag "pillow4",  data: { src: "../assets/pillow4.jpg" } %>
</div>
```

## 5. Add styling to images
```css
<style>
 .images {
	display: flex;
	justify-content: center;
	margin-top: 25px;
 }

 img {
	height: 200px;
	width: 200px;
	margin: 10px;
	cursor: pointer;
 }
</style>
```

## 6. Add captions

Make the light-gallery descriptive by adding a small, informative text at the bottom of every image.

```erb
<div data-controller="lightbox" class="images">
 <%= image_tag "pillow1", data: { src: "../assets/pillow1.jpg", sub_html: "A companion for other pillows." } %>
 <%= image_tag "pillow2", data: { src: "../assets/pillow2.jpg", sub_html: "Always on duty." } %>
 <%= image_tag "pillow3", data: { src: "../assets/pillow3.jpg", sub_html: "Easy to hold with 2 hands."} %>
 <%= image_tag "pillow4", data: { src: "../assets/pillow4.jpg", sub_html: "Or twist when you feel like."} %>
</div
```

## 7. Add additional options

Enable additional features on the lightgallery. A full list of the options [here](https://sachinchoolur.github.io/lightgallery.js/docs/api.html#lg-thumbnail).

- i.e .visual navigation options
- i.e infinite loop

```erb
<div data-controller="lightbox" class="images" 
data-lightbox-options-value='{"controls": true, "loop":true}'>
...
</div>
```

If you like to avoid writing any data-lightbox-options in the HTML, you can choose to [extend](https://stimulus-components.netlify.app/docs/components/stimulus-lightbox/#-extending-controller) the functionality library and add your lightgallery options as default.

That’s it, your lightgallery is ready. 

Stimulus proves it value by sprinkling JavaScript on the page, without the need the go full-blown SPA. Leverage pre-build components and suddenly developers can easily bring interactivity the HTML-dominant web applications.

Thanks for reading.