Use Glightbox in webcomponent #496
-
I'm trying to use GL in a web-component. the thing i'm looking for now is how can i add (initialize) a new element to the lightbox? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Interesting idea! If you only have 1 lightbox, for example each web component acts as a trigger for opening a lightbox, then you could save the lightbox to a static property: import GLightbox from 'path/to/glightbox.js';
window.customElements.define('my-lightbox-trigger', class extends HTMLElement {
constructor() { super() }
static _lightbox = new GLightbox({ selector: '.my-lightbox' });
connectedCallback() {
const potentialLightboxTriggers = Array.from(this.querySelectorAll('.my-lightbox'), (el) => ({
href: el.linkToImage,
width: Infinity,
}));
if (potentialLightboxTriggers.length) this.constructor._lightbox.setElements(potentialLightboxTriggers);
}
}); You're right in your understanding that GLightbox collects elements on the initial run and not on each opening, but using |
Beta Was this translation helpful? Give feedback.
You're exactly right, the selector option is for the usual "on init, find these elements to use as slides." If you don't pass a selector, it will search for the default one (
.glightbox
if I remember right).I may have accidentally misled you,
setElements
doesn't mark the elements pass in as triggers for the lightbox but as slides inside the lightbox. You'd need to add a little event handling: