# Discourse Lightbox - Migreren van Magnific popup

**URL:** https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505
**Category:** Feature
**Tags:** completed, lightbox
**Created:** [12 januari 2023 om 03:56 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505 "2023-01-12T03:56:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [12 januari 2023 om 03:56 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/1 "2023-01-12T03:56:08Z")

</div>

Discourse allows users to upload images in posts (and other places). Sometimes those images are too big to display, so Discourse creates a scaled version of the image and adds that to the post. When you click on that scaled image, you get a nice overlay that contains the full-size image - This is commonly referred to as lightboxing.

Discourse currently uses a library called Magnific popup to handle lightboxing behavior. This topic is about revamping lightboxes in Discourse by migrating from Magnific popup to something more in line with the expectations users and developers have today.

### The why

Magnific is a great library, and the number of stars on the [project page](https://github.com/dimsemenov/Magnific-Popup) significantly indicates how many problems it has solved for both users and developers over the years.

It was also ahead of its time in terms of ease of use and feature offerings. This becomes apparent when you consider that almost all the functionality you see in it today was already in v 1.0, which came out in 2014.

So, where does that leave us? I’ll keep it short. Magnific popup was made for an entirely different world where cross-browser compatibility was far behind where it’s at today. This means four things.

1. it has jQuery as a dependency
2. it contains code meant for ancient browsers
3. the average device used to access the web has changed a lot since then
4. it was designed with something other than single-page applications like Discourse as a priority - namely, static pages. This has performance concerns specific to single-page applications, which we’ll get into later.

Given the current state of web standards and how you can do all kinds of magic with vanilla Javascript, it’s understandable that large projects like Discourse are moving away from having jQuery as a dependency. Note that discussions about jQuery are off-topic here, and this is mostly just about context.

So, that’s _the why_… less jQuery, less ancient browser code, better overall performance, and better support for the - now very different - average device.

### The how

There’s no shortage of solutions on offer, and there is a discussion to be had about not reinventing the wheel, but… let’s not go there and let’s keep it short.

Perhaps the shortest way to address this sticking point is to say… no matter how well something off the shelf fits… it won’t fit as nicely as something tailor-made, and let’s leave it at that.

There’s quite a few things to consider here, whether it be feature excess with libraries that cover numerous use cases like images, iframes, and video and the like, or lack of clarity around licensing.

Knowing the specific use cases that Discourse needs to support makes it possible to avoid unnecessary code, allowing additional targeted features to be added without ending up with a lot of overhead.

So, we now have a pretty good baseline. The requirements are

1. no jQuery
2. only focus on images for now
3. support for quality of life improvements like swiping on mobile
4. integrate it with Discourse to allow further customization/improvements using the current theme/plugin systems.

### The what

Discourse is an Ember shop, so the new lightbox would need to be an Ember component to handle the markup and data. Also, since there’s an expectation that you should be able to set up lightboxes anywhere on the app, having a lightbox service makes a lot of sense. This will allow developers to either:

1. inject the service into any component and be able to link the service’s setup/cleanup methods to the component life-cycle
2. lookup the service anywhere on the app and be able to call its setup/cleanup methods

Additionally, as a quality-of-life improvement, we can have a bit of abstraction and create a couple of utility functions that can be used in themes. More on that later.

Since one of the goals is to allow themes/plugins to extend the functionality, the lightbox service will communicate state changes via appEvents. It will emit the following events.

- `lightbox:opened`
- `lightbox:item-will-change`
- `lightbox:item-did-change`
- `lightbox:closed`

Each event will be triggered with the type of data developers would typically expect. For example, the `lightbox:opened` event will contain a list of all the items and the current item that the lightbox opened at. More on this later.

With that out of the way, let’s move on.

Over the last few weeks, I’ve been working on a draft PR that introduces Discourse Lightbox.

[https://github.com/discourse/discourse/pull/19798](https://github.com/discourse/discourse/pull/19798)

At first glance, it might look a bit big so, let’s break it down.

 ![A pie chart with a breakdown of the Discourse Lightbox pull request with Templates, Javascript, Tests, CSS taking 8%, 23%, 53%, and 16%, respectively](https://global.discourse-cdn.com/meta/original/4X/3/4/6/346ba2f26a86d811a0e0f43411352806b6b55c30.png)

Testing something that relies on a lot of user interaction is prickly, which is why the lightbox test suit includes 63 tests with 291 assertions.

With the size of the tests out of the way, let’s make a quick size comparison with Magnific popup (both unminified)

| | Discourse Lightbox LOC | Magnific Popup LOC | Difference |
| --- | --- | --- | --- |
| Javascript | 1197 | 1860 | (35%) |
| CSS | 813 | 351 | 131% |
| Templates | 401 | 0 | - |
| Total | 2411 | 2211 | 9.1% |

So, Discourse Lightbox has about 9% more code than Magnific. Of course, that’s only part of the story for two reasons.

1. We haven’t factored in jQuery, which Magnific depends on. jQuery 3.6 is roughly 11k LOC.
2. Discourse Lightbox adds more features compared to Magnific.

Let’s get into those features,

Any jank you see in the videos below is related to the recording, not what users will experience. All the animations/transitions will run at a solid 60FPS.

So, without any more delay,

#### basic layout

For comparison, here’s the same in the current Magnific implementation

A few points about the videos above

1. The new lightbox will use the image as a backdrop instead of the generic semi-translucent dark background. The image will, by definition, complement itself. Note that this does not add any network overhead. The image used in the backdrop is the one from the post body, which means that it will have already been cached when users open the lightbox.

2. Discourse Lightbox opts for a fixed UI. Instead of having the close button, title, and image metadata attached to the image, they have their own reserved spaces. This will help reduce jumpiness as you navigate between images of different dimensions

3. Navigating between images can be done with the arrows in the lightbox or via keyboard shortcuts. → or ↓ for next and ← or ↑ for previous. On RTL locales, the shortcuts are flipped accordingly.

The layout on mobile is very similar, except that the arrows are not displayed because it adds swipe gestures for navigation.

Swipe left on mobile for next, and swipe right for previous. On RTL locales, the swipe gestures are flipped.

#### Zooming

The new lightbox has a dedicated zoom button that’s visible when the image you’re viewing is larger than the viewport’s dimensions. Clicking the zoom button will zoom the image in, and clicking the button again will zoom the image out. Additionally, there’s now a keyboard shortcut for zooming in/out Z. Lastly, if the image is zoomable, clicking on it will have the same effect.

Here’s a video demonstrating all three methods

Note that while not demonstrated in the video above, the cursor, when you hover over an image that can be zoomed, will change to reflect that. It will also change to a `zoom-out` icon when you hover over an image that’s already zoomed.

Zooming works differently in the new lightbox. On desktop, the zoomed section of the image will follow the cursor.

There’s no hover on mobile; it uses regular touch-scroll to move around in a zoomed image.

#### Rotation

The new lightbox adds a dedicated rotation button to rotate the image in 90-degree increments. Rotation also has a keyboard shortcut. The R key. Here’s what that looks like

Rotation and zooming can be combined.

#### Fullscreen mode

The new lightbox adds a fullscreen button. The button will make the browser window enter fullscreen mode. The keyboard shortcut is M

It will keep track of its state and go back to the regular mode when fullscreen is toggled off or when the lightbox is closed.

#### Download

The current lightbox adds a “download” link below the image. The new lightbox does the same but changes that to an icon and adds it to the footer of the lightbox instead. It still respects the same permissions. If…

```plaintext
prevent_anons_from_downloading_images

```

is enabled, and the user is not logged in, the download icon will not be displayed.

#### New Tab

The current lightbox adds an “original” link below the image that opens it in a new tab. The new lightbox does the same but adds it as an icon in the lightbox header. It will also respect the same permission set for the download icon.

#### Image Title

The new lightbox focuses mainly on the image. Image titles are truncated to one line by default but support expanding. Here’s an example of what that looks like

There’s a keyboard shortcut to expand/collapse the title T, and the title will not be displayed when the image is zoomed/rotated.

#### Carousel

A newly available feature is showing all the images in the gallery in a carousel. The layout will depend on the device’s screen; it can be horizontal or vertical, and here’s what it looks like.

There’s a keyboard shortcut to toggle the carousel. A

and here’s what it looks like on mobile

Swiping down on mobile will toggle the carousel on/off.

#### Closing

The Esc still works like before on desktop to close the lightbox. There’s now an additional swipe gesture on mobile, and you can swipe up to close the lightbox.

#### Accessibility

Beyond the basics like button labels, the new lightbox adds a screen reader announcer element off-screen. When you navigate to an image inside the lightbox, it will read its index and title based on the following format.

```plaintext
image %{current} of %{total}: %{title}

```

Here’s a short example of that

The new lightbox also removes all the unnecessary buttons that serve no purpose on screen readers via `aria-hidden.`

Remember that accessibility is an ongoing mission, and this is by no means complete. There’s always something to improve, but I stopped here to keep things simple for v1.

This covers all the features of the new lightbox.

Let’s move on to a bit of developer-speak.

#### Event-listeners

The current lightbox adds click-event listeners to each individual lightbox image in cooked posts. That means a post with 20 images will have 20 click event listeners for lightboxes.

The new lightbox leverages on event-delegation and only adds one event listener to the post itself.

Here’s an anon user, incognito window count of the current lightbox on a post with 20 images after forcing garbage collection

 ![current lightbox event listner count](https://global.discourse-cdn.com/meta/original/4X/e/7/3/e73361ae10d1dae841d45f8c4fd2edac26856d12.png)

and here’s the same post with the new lightbox

 ![proposed lightbox event listner count](https://global.discourse-cdn.com/meta/original/4X/e/7/3/e730c540aa8752880883ce38adb90ec323b8eedd.png)

Further, navigating within the lightbox currently adds event listeners, which end up being orphaned and thus interfere with garbage collection. Here’s a chart of

1. Load a topic page with one post that contains 20 images.
2. Open the lightbox and navigate through all 20 images three consecutive times.
3. close the lightbox
4. force browser garbage collection

current lightbox:

 ![current lightbox event listener count after navigating in a lightbox with 20 images 3 times is 292](https://global.discourse-cdn.com/meta/original/4X/5/2/6/526cdcbdd00421613536f1ca5f9c5430e37102d5.png)

new lightbox:

 ![propsed lightbox event listener count after navigating in a lightbox with 20 images 3 times is 223](https://global.discourse-cdn.com/meta/original/4X/6/4/d/64dec31e220adcd76d5d98fba10516941073b718.png)

As for the event listeners on the lightboxes themselves, these don’t seem to be cleaned up currently in Magnific (remember, it wasn’t built for single-page-applications).

A quick note about the tests above. They’re very rudimentary, and the numbers are not supposed to be “scientific” the goal here is figure out the direction and not the exact numbers.

#### Developer notes

Let’s talk about setting up and cleaning up lightboxes with the new Discourse Lightbox.

##### Setting up and cleaning up lightboxes

Developers have two options.

1. injecting the lightbox service into a component via

2. If you don’t want to inject the lightbox service, you can import `setupLightboxes` and `cleanupLightboxes` like this

Note that both calling from the service directly or via the utility functions will also accept a nodeList for backward compatibility, but it’s not recommended.

One last note about this is that you can also have a non-image act as a trigger for opening a lightbox that you set up. For example

```markdown
<div class="my-container">
  <img class="my-selector" src="foo">
  <img class="my-selector" src="bar">
  ....
  <button>Open Lightbox</button>
</div>

```

I would do something like this to set up basic lightboxes on the div above.

```javascript
import {
  cleanupLightboxes,
  setupLightboxes,
} from "discourse/lib/lightbox";

//...

setupLightboxes({
   container: document.querySelector(".my-container"),
   selector: ".my-selector"
})

```

To make the button open the lightbox, all you need to do is add `data-lightbox-trigger` like so

```markdown
<button data-lightbox-trigger>Open Lightbox</button>

```

The rest is handled automagically.

Finally, whenever you want to clean up, call

```javascript
cleanupLightboxes()

```

Cleaning up is not really critical since the lightbox service will automatically cleanup whenever the `dom:clean` event fires in the app (on route transitions)

##### Listening to lightbox events

The new lightbox will fire events, as we discussed earlier. Those events are:

- `lightbox:opened`
- `lightbox:item-will-change`
- `lightbox:item-did-change`
- `lightbox:closed`

**lightbox:opened**

This event will fire when the lightbox is opened and contains two objects.

1. `items`: this is an array of all the images in the current lightbox. Each of them will be an object.
2. `currentItem`: this is the object for the current item the lightbox was opened at.

An item object looks like this:

```javascript
{
  "fullsizeURL": "https://global.discourse-cdn.com/meta/original/4X/2/3/c/23c5746c48803deac5c105081dba20555187c3ff.jpeg",
  "smallURL": "https://global.discourse-cdn.com/meta/optimized/4X/2/3/c/23c5746c48803deac5c105081dba20555187c3ff_2_600x750.jpeg",
  "downloadURL": "/uploads/short-url/56rKTvkvmL6c2C8OFQtMo3D8sIn.jpeg?dl=1",
  "title": "Close-up Photo of a Black Microphone on Stand",
  "fileDetails": "1200×1500 88.9 KB",
  "dominantColor": "793C6D",
  "aspectRatio": "400 / 500",
  "index": 0,
  "cssVars": "--dominant-color: #793C6D;--aspect-ratio: 400 / 500;--small-url: url(https://global.discourse-cdn.com/meta/optimized/4X/2/3/c/23c5746c48803deac5c105081dba20555187c3ff_2_600x750.jpeg);",
  "isLoaded": true,
  "hasLoadingError": false,
  "width": 1200,
  "height": 1500,
  "canZoom": true
}

```

**lightbox:item-will-change**

This event fires right before the current item in the lightbox changes. It will have the `currentItem` (the one that’s about to change)

**lightbox:item-did-change**

This event fires right after the item in the lightbox changes and is finished loading, and it will have `currentItem` as an argument.

**lightbox:closed**

This event fires right after the lightbox is closed and has no arguments.

With the events above. A theoretical theme component can easily add analytics to lightbox like so

```javascript
api.onAppEvent('lightbox:opened', ({items, currentItem}) => {
  console.log({items});
  console.log({currentItem});

  // your analytics code here
});

```

or other similar ideas.

##### The CSS

The new lightbox uses the [BEM](https://getbem.com/naming/) naming convention for HTML classes. Here’s a complete list of the selectors you can use

```scss
html.has-lightbox {
  // css for the HTML element when lightboxes are open
}

.d-lightbox {
  &--is-visible {
    // Main lightbox element
  }

  &__content {
    // lightbox inner content wrapper
  }
}

.d-lightbox {
  & __content__ header {
    // lightbox header
  }
}

.d-lightbox {
  & __content__ body {
    // lightbox body (contains the main image)

    &__backdrop {
      // lightbox backdrop
    }

    &__main-image {
      // lightbox main image
    }

    &__error-message {
      // lightbox error message
    }

    &__previous-button,
    &__next-button {
      // main lightbox previous/next buttons
    }
  }
}

.d-lightbox {
  & __content__ footer {
    // lightbox footer

    &__main-title {
      // lightbox image title
     
      &__item-file-details {
        // lightbox file details e.g "1000x582 183KB"
      }
    }
  }
}

.d-lightbox {
  & __content__ carousel {
    // lightbox carousel container

    &__previous-button,
    &__next-button {
      // lightbox carousel previous/next buttons
    }
  }
}

.d-lightbox {
  & __content__ carousel {
    &__carousel-items {
      // lightbox carousel items container

      &__item,
      &__item--is-current {
        // lightbox carousel item
      }

      &__item--is-current {
        // lightbox carousel current item
      }
    }
  }
}

.d-lightbox {
  &--is-vertical & __content__ carousel {
    // lightbox carousel vertical styles
  }
}

.d-lightbox {
  &--is-horizontal & __content__ carousel {
    // lightbox carousel horizontal styles
  }
}

.d-lightbox {
  .btn-flat {
    // styles for all lightbox buttons
  }
}

.d-lightbox {
  &__content {
    &__focus-trap,
    &__screen-reader-announcer {
      // lightbox focus trap and screen reader announcer. These are offscreen
    }
  }
}

/* State styles */

// Carousel
.d-lightbox {
  &--has-carousel {
    // lightbox styles when carousel is open
  }
}

// expanded title
.d-lightbox {
  &--has-expanded-title {
    // lightbox styles when title is expanded
  }
}

// Zoom
.d-lightbox {
  &--can-zoom {
    // lightbox styles when image can be zoomed
  }

  &--is-zoomed {
    // lightbox styles when image is zoomed
  }
}

// Rotate
.d-lightbox {
  &--is-rotated {
    // lightbox styles when image is rotated
  }
}

// Fullscreen
.d-lightbox {
  &--is-fullscreen {
    // lightbox styles when image is fullscreen
  }
}

```

Now that we covered **the what,** we can finally move on to…

### The when

Right now, the PR is ready for review, which needs to happen first. After it passes review, it will be available to sites that update. The PR adds a new temporary site setting as we transition from Magnific popup to Discourse Lightbox. The name of the setting is:

```plaintext
enable_experimental_lightbox

```

If the setting is disabled, the PR will have no effect, and everything will continue to work like before with Magnific popup.

Discourse Lightbox will replace Magnific popup in cooked posts, chat messages, and image uploader components when the setting is toggled on.

### Road map

1. PR Review
2. PR Merge
3. General Feedback window (1-2 weeks)
4. PR to remove Magnific popup from core and remove the experimental site setting.
5. TBD: stretch goals such as a theme component that explores different layouts (should be straightforward since the new lightbox uses CSS grid for the layout)

### Acknowledgments

- This work was generously sponsored by CDCK 🙏
- Much love ❤ goes out to [Dmytro Semenov](https://github.com/dimsemenov), the creator of Magnific popup, for creating something that was way ahead of its time.
- The images used in the demos above are courtesy of [Irina Iriser](https://www.pexels.com/@iriser/) [@pexels](https://www.pexels.com/@iriser/)

---

<div class="post-metadata">

### Author: ![TheBestPessimist](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thebestpessimist/32/247704_2.png) [@TheBestPessimist](https://meta.discourse.org/u/TheBestPessimist)
#### Post date: [12 januari 2023 om 07:56 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/2 "2023-01-12T07:56:40Z")

</div>

> [@Johani](#):
>
> Fullscreen mode

I am used, from youtube and other desktop applications, that F goes fullscreen.  
Is there a reason not to use muscle memory for the shortcut?

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [12 januari 2023 om 09:40 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/3 "2023-01-12T09:40:36Z")

</div>

> [@Johani](#):
>
> #### Carousel
> 
> A newly available feature is showing all the images in the gallery in a carousel. The layout will depend on the device’s screen; it can be horizontal or vertical, and here’s what it looks like.
> 
> [https://global.discourse-cdn.com/meta/original/4X/5/0/c/50c15aecff61e2a61d64113c84987ff2eecc9795.mp4](https://global.discourse-cdn.com/meta/original/4X/5/0/c/50c15aecff61e2a61d64113c84987ff2eecc9795.mp4)
> 
> There’s a keyboard shortcut to toggle the carousel. A
> 
> and here’s what it looks like on mobile
> 
> [https://global.discourse-cdn.com/meta/original/4X/c/7/4/c745dd387fbf572e1bc9566debbb2b9121dab312.mp4](https://global.discourse-cdn.com/meta/original/4X/c/7/4/c745dd387fbf572e1bc9566debbb2b9121dab312.mp4)
> 
> Swiping down on mobile will toggle the carousel on/off.

This one is particularly looking nice. 🙌

One thing I would have wanted is being able to go to next post images while using arrows. That way if Im in a topic like “Cute cats” I can just use arrows to navigate to next post images seamlessly. There would be various things to take into consideration but that would be very nice. ATM I’m forced to close the light box go to next post, click on first image, …

---

<div class="post-metadata">

### Author: ![volanar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/volanar/32/318163_2.png) [@volanar](https://meta.discourse.org/u/volanar)
#### Post date: [19 januari 2023 om 21:25 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/4 "2023-01-19T21:25:53Z")

</div>

> [@Johani](#):
>
> We haven’t factored in jQuery, which Magnific depends on. jQuery 3.6 is roughly 11k LOC.

bootstrap 3 also requires jqwery. Discourse uses bootstrap 3. So jqwery will be installed as a dependency on our forum. Is that true?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [19 januari 2023 om 21:51 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/5 "2023-01-19T21:51:31Z")

</div>

EmberJS also used to require jQuery and Discourse is a EmberJS app. We’ve been working, [for a couple of years now](https://meta.discourse.org/t/issue-with-jquery-in-plugin-component/147086/4), on removing jQuery usage from Discourse itself, official plugins and components.

It’s a lot of work but we are getting closer. This very topic is one of the many efforts towards that.

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [5 september 2023 om 07:10 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/16 "2023-09-05T07:10:27Z")

</div>

This has now been implemented. 🥳

> [@New Discourse Lightbox Enabled on Meta - now disabled](https://meta.discourse.org/t/new-discourse-lightbox-enabled-on-meta/277810/1):
>
> warning This has been disabled as of 2023-09-25T16:00:00Z (UTC) warningWe have enabled the experimental setting enable\_experimental\_lightbox on Meta. This is part of our ongoing efforts to improve your user experience and introduce new features. The [Discourse Lightbox](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505) is a new feature we’re testing that aims to enhance the way images are displayed on the forum. This change is also part of our migration away from the Magnific Popup, and therefore jQuery. Recap (For more details, please vis…

We’ll be collecting feedback in that topic for a short period, so check it out and let us know what you think. 🙂 👍

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [5 september 2023 om 07:10 UTC](https://meta.discourse.org/t/discourse-lightbox-migrating-away-from-magnific-popup/251505/17 "2023-09-05T07:10:34Z")

</div>


