1.2302271541.0 • Published 1 year ago

@bentoproject/lightbox-gallery v1.2302271541.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Bento Lightbox Gallery

Provides a modal "lightbox" experience for images.

When the user interacts with the element, a modal expands to fill the viewport until it is closed by the user.

Web Component

You must include each Bento component's required CSS library to guarantee proper loading and before adding custom styles. As a web component

Import via npm

npm install @bentoproject/lightbox-gallery
import {defineElement as defineBentoLightboxGallery} from '@bentoproject/lightbox-gallery';
defineBentoLightboxGallery();

Import via <script>

<script type="module" async src="https://cdn.ampproject.org/bento.mjs"></script>
<script nomodule src="https://cdn.ampproject.org/bento.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.css"
>
<script type="module" async src="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.mjs"></script>
<script nomodule async src="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.js"></script>

Example

<!DOCTYPE html>
<html>
  <head>
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/bento.mjs"
    ></script>
    <script nomodule src="https://cdn.ampproject.org/bento.js"></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.css"
    />
    <script
      type="module"
      async
      src="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.mjs"
    ></script>
    <script
      nomodule
      async
      src="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.js"
    ></script>
  </head>
  <body>
    <bento-lightbox-gallery></bento-lightbox-gallery>
    <figure>
      <img id="my-img" src="img1.jpwg" lightbox />
      <figcaption>dog wearing yellow shirt.</figcaption>
    </figure>
    <figure>
      <img src="img2.jpeg" lightbox/>
    </figure>
    <figure>
      <img src="img2.jpeg" lightbox/>
    </figure>
  </body>
</html>

To use bento-liightbox-gallery, ensure the required script is included in your <head> section, then add the lightbox attribute on an <img> or <bento-carousel> element.

Add Captions

Optionally, you can specify a caption for each element in the lightbox. these fields are automatically read and displayed by the <bento-lightbox-gallery> in the following order of priority:

  • figcaption (if the lightboxed element is the child of a figure)
  • aria-describedby
  • alt
  • aria-label
  • aria-labelledby

In the following example, <bento-lightbox-gallery> displays the figcaption value as its description, showing "toront's cn tower was ....".

<figure>
  <img
    id="hero-img"
    lightbox="toronto"
    src="https://picsum.photos/1600/900?image=1075"
    alt="picture of cn tower."
  />
  <figcaption class="image">
    toronto's cn tower was built in 1976 and was the tallest free-standing
    structure until 2007.
  </figcaption>
</figure>

In the following example, <bento-lightbox-gallery> displays the alt value as its description, showing "picture of cn tower".

<img
  id="hero-img"
  lightbox="toronto"
  src="https://picsum.photos/1600/900?image=1075"
  alt="picture of cn tower"
/>

Interactivity and API usage

Bento components are highly interactive through their API. The bento-lightbox-gallery component API is accessible by including the following script tag in your document:

await customElements.whenDefined('bento-lightbox-gallery');
const api = await lightboxGallery.getApi();

Actions

The bento-lightbox-gallery API allows you to perform the following action:

open

Opens the lightbox gallery.

api.open();

You can open a specific lightbox gallery group to a specific slide by passing in extra arguments:

api.open(1, 'toronto') // opens gallery with images in the "toronto" group to the 2nd image
api.open(null, 'toronto') // opens gallery with images in the "toronto" group to the 1st image

Attributes

lightbox

Set lightbox attribute to an id to assign different images to different groups. For example, in the example below, clicking any of the group1 images will only show img1.jpeg, img3.jpeg, and img5.jpeg and clicking any of the group2 images will only show img2.jpeg, img4.jpeg, img6.jpeg

<img src="img1.jpeg" lightbox="group1">
<img src="img2.jpeg" lightbox="group2">
<img src="img3.jpeg" lightbox="group1">
<img src="img4.jpeg" lightbox="group2">
<img src="img5.jpeg" lightbox="group1">
<img src="img6.jpeg" lightbox="group2">

Layout And Style

Each bento component has a small css library you must include to guarantee proper loading without content shifts. Because of order-based specificity, you must manually ensure that stylesheets are included before any custom styles.

<link
  rel="stylesheet"
  type="text/css"
  href="https://cdn.ampproject.org/v0/bento-lightbox-gallery-1.0.css"
/>

Preact/React Component

The preact/react version of the bentolightboxgallery functions differently than the web component version. The following example will demonstrate the use of <BentoLightboxGallery> as a functional component.

Import via npm

npm install @bentoproject/lightbox-gallery
import React from 'react';
import {
  BentoLightboxGalleryProvider,
  WithBentoLightboxGallery,
} from '@bentoproject/lightbox-gallery/react';

function App() {
  return (
    <BentoLightboxGalleryProvider>
      <WithBentoLightboxGallery>
        <img src="https://images.unsplash.com/photo-1562907550-096d3bf9b25c" />
      </WithBentoLightboxGallery>
    </BentoLightboxGalleryProvider>
  );
}

Example Using BentoBaseCarousel

<BentoLightboxGallery> can be used with a <BentoBaseCarousel> child in order to lightbox all of the carousel's children. As you navigate throught the carousel items in the lightbox, the original carousel slides are synchronised so that when the lightbox is closed, the user ends up on the same slide as they were originally on.

import React from 'react';
import {BentoBaseCarousel} from '../../../bento-base-carousel/1.0/component';
import {
  BentoLightboxGalleryProvider,
  WithBentoLightboxGallery,
} from '@bentoproject/lightbox-gallery/react';

function App() {
  return (
    <BentoLightboxGalleryProvider>
      <BentoBaseCarousel lightbox style={{width: 240, height: 160}}>
        <img
          src="https://images.unsplash.com/photo-1562907550-096d3bf9b25c"
          thumbnailSrc="https://images.unsplash.com/photo-1562907550-096d3bf9b25c"
        />
      </BentoBaseCarousel>
    </BentoLightboxGalleryProvider>
  );
}

For further examples of how to use the BentoLightboxGallery please check the storybook example found in (Basic.js)./storybook/Basic.js.

Props for BentoLightboxGalleryProvider

onBeforeOpen

A prop which takes a function which is executed before the lightbox is opened.

onAfterOpen

A prop which takes a function which is executed after the lightbox is opened.

onAfterClose

A prop which takes a function which is executed after the lightbox is closed.

onViewGrid

A prop which takes a function which is executed when the user enters the grid view.

onToggleCaption

A prop which takes a function which is executed when the captions are toggled.

Props for WithBentoLightboxGallery

enableActivation

A boolean prop which defaults to true which lets the child image activate the lightbox experience.

onClick

A prop which takes a functhion which is executed when the image is clicked.

1.2302171719.0

1 year ago

1.2302271541.0

1 year ago

1.2302031721.0

1 year ago

1.2301301810.0

1 year ago

1.2301041800.0

1 year ago

1.2301112346.0

1 year ago

1.2302021743.0

1 year ago

1.2301031703.0

1 year ago

1.2301181928.0

1 year ago

1.2301032153.0

1 year ago

1.2301261900.0

1 year ago

1.2301100345.0

1 year ago

1.2302012254.0

1 year ago

1.2212151632.2

1 year ago

1.2211042305.0

2 years ago

1.2211111611.0

1 year ago

1.2211060024.0

2 years ago

1.2210191347.0

2 years ago

1.2211302304.0

1 year ago

1.2211302304.2

1 year ago

1.2211302304.1

1 year ago

1.2211151402.0

1 year ago

1.2210251605.0

2 years ago

1.2210272257.0

2 years ago

1.2211182146.0

1 year ago

1.2211250451.0

1 year ago

1.2210172057.0

2 years ago

1.2212092023.0

1 year ago

1.2212151632.1

1 year ago

1.2210211855.0

2 years ago

1.2212151632.0

1 year ago

1.2212071834.0

1 year ago

1.2210142102.0

2 years ago

1.2210041838.0

2 years ago

1.2210112333.0

2 years ago

1.2210052034.0

2 years ago

1.2210010655.0

2 years ago

1.2210071758.0

2 years ago

1.2208242209.0

2 years ago

1.2208242209.1

2 years ago

1.2209142312.0

2 years ago

1.2209072154.0

2 years ago

1.2209131909.0

2 years ago

1.2208172101.0

2 years ago

1.2207201724.0

2 years ago

1.2205270638.4

2 years ago

1.2206071918.0

2 years ago

1.2206071918.1

2 years ago

1.2205120110.1

2 years ago

1.2205270638.0

2 years ago

1.2206131450.0

2 years ago

1.2207071723.0

2 years ago

1.2208121708.0

2 years ago

1.2208021808.0

2 years ago

1.2206291614.0

2 years ago

1.2207181727.0

2 years ago

1.2208042017.0

2 years ago

1.2205232225.0

2 years ago

1.2207261725.0

2 years ago

1.2208112015.0

2 years ago

1.2206082301.0

2 years ago

1.2208081650.0

2 years ago

1.2208092047.0

2 years ago

1.2206221455.0

2 years ago

1.2207221643.0

2 years ago

1.2208051912.0

2 years ago

1.2208051912.1

2 years ago

1.2206072228.0

2 years ago

1.2207281718.2

2 years ago

1.2207281718.1

2 years ago

1.2207281718.0

2 years ago

1.2206101637.0

2 years ago

1.2205251942.0

2 years ago

1.2206162023.0

2 years ago

1.2204281949.0

2 years ago

1.2205120110.0

2 years ago

1.2205051832.0

2 years ago

1.2205101904.0

2 years ago

1.2205041533.0

2 years ago

1.2205181800.0

2 years ago

1.2204252045.0

2 years ago

1.2204292129.0

2 years ago

1.2204272205.0

2 years ago

1.2205090810.0

2 years ago

1.2204221712.0

2 years ago

1.2205161914.0

2 years ago

1.2205191749.0

2 years ago

1.2205031420.0

2 years ago

1.2203101844.0

2 years ago

1.2204182206.0

2 years ago

1.2203151539.0

2 years ago

1.2203281422.0

2 years ago

1.2204122000.0

2 years ago

1.2203221937.0

2 years ago

1.2203231914.0

2 years ago

1.2204160405.0

2 years ago

1.2203172113.0

2 years ago

1.2204132005.0

2 years ago

1.2204142048.0

2 years ago

1.2204211736.0

2 years ago

1.2203041950.0

2 years ago

1.2204121632.0

2 years ago

1.2204191429.0

2 years ago

1.2202241911.0

2 years ago

1.2202252101.0

2 years ago

1.2201101902.0

2 years ago

1.2201042109.0

2 years ago

1.2112231523.2

2 years ago

1.2112231523.1

2 years ago

1.2112231523.0

2 years ago

1.2202162256.0

2 years ago

1.2202012258.0

2 years ago

1.2202230359.0

2 years ago

1.2111242025.1

2 years ago

1.2112062322.2

2 years ago

1.2112091956.0

2 years ago

1.2112062322.1

2 years ago

1.2112062322.0

2 years ago

1.2202042210.0

2 years ago

1.2202042210.1

2 years ago

1.2112201724.0

2 years ago

1.2112162055.0

2 years ago

1.2112221739.0

2 years ago

1.2201061922.0

2 years ago

1.2112132331.0

2 years ago

1.2201122239.0

2 years ago

1.2202142035.0

2 years ago

1.2202152253.0

2 years ago

1.2202112230.0

2 years ago

1.2202082004.0

2 years ago

1.2201120015.0

2 years ago

1.2112082232.0

2 years ago

1.2201212122.3

2 years ago

1.2202022130.0

2 years ago

1.2202031816.0

2 years ago

1.2112212349.0

2 years ago

1.2201052144.0

2 years ago

1.2202180255.0

2 years ago

1.2112080230.1

2 years ago

1.2201262038.1

2 years ago

1.2112080230.0

2 years ago

1.2201141909.0

2 years ago

1.2201141909.1

2 years ago

1.2201141909.3

2 years ago

1.2201141909.4

2 years ago

1.2112160003.0

2 years ago

1.2201071715.0

2 years ago

1.2202072236.0

2 years ago

1.2112170012.0

2 years ago

1.2202092050.0

2 years ago

1.2201131702.0

2 years ago

1.2112102136.0

2 years ago

1.2202232232.0

2 years ago

1.2111191951.0

2 years ago

1.2111292120.0

2 years ago

1.2111180040.0

2 years ago

1.2111230636.0

2 years ago

1.2111230244.0

2 years ago

1.2111152338.2

2 years ago

1.2112010058.0

2 years ago

1.2111060251.3

2 years ago

1.2111242025.0

2 years ago

1.2111182332.0

2 years ago

1.2112032204.0

2 years ago

1.2112021656.0

2 years ago

1.2112012300.0

2 years ago

1.2111060251.9

2 years ago

1.2111022043.0

3 years ago

1.2110212130.2

3 years ago

1.2110152252.3

3 years ago

1.2111011823.0

3 years ago

1.2110290545.0

3 years ago

1.2110281959.0

3 years ago

1.2110252343.0

3 years ago

1.2110261518.0

3 years ago