AMP Conf 2019. April 17/18. Tokyo.
AMP
  • websites

amp-lightbox

Introduction

The amp-lightbox component allows for a “lightbox” or similar experience - where upon user interaction a component expands to fill the viewport, until it is closed again by the user.

Setup

Import the amp-lightbox component in the header

<script async custom-element="amp-lightbox" src="https://cdn.ampproject.org/v0/amp-lightbox-0.1.js"></script>

You can combine the lightbox component with amp-bind.

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

Basic usage

The amp-lightbox component defines the child elements that will be displayed in a full-viewport overlay.

To close the lightbox via click or tap use the on attribute on one or more elements inside the lightbox. In this example the user can click anywhere in the lightbox to close it.

The lighbox is shown when the user taps or clicks on an element with on attribute that targets the id of an amp-lightbox element.

<div>
  <amp-lightbox id="my-lightbox"
    layout="nodisplay">
    <div class="lightbox"
      on="tap:my-lightbox.close"
      role="button"
      tabindex="0">
      <h1>Hello World!</h1>
    </div>
  </amp-lightbox>
  <button on="tap:my-lightbox">
    Open lightbox
  </button>
</div>

For showing images in a lightbox, AMP also features the amp-image-lightbox component, which doesn't need the image to specified twice.

Combination with amp-bind

amp-lightbox can be opened as a result of amp-bind evaluations by binding a boolean value to the open attribute.

<div>
  <amp-lightbox id="my-bindable-lightbox"
    [open]="showLightbox"
    layout="nodisplay">
    <div class="lightbox"
      role="button"
      tabindex="0"
      on="tap:AMP.setState({showLightbox: false})">
      <h1>Hello World!</h1>
    </div>
  </amp-lightbox>
  <button on="tap:AMP.setState({showLightbox: true})">
    Open lightbox
  </button>
</div>
Need further explanation?

If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.

Go to Stack Overflow
An unexplained feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Edit sample on GitHub