yet-another-react-lightbox

repository·main·Indexed 23 days ago

https://github.com/igordanchenko/yet-another-react-lightbox

A modern, performant, and highly customizable lightbox component for React. It features support for responsive images via srcSet, a modular plugin system to minimize bundle size, and an extensible API for creating custom slides and toolbar buttons. Included plugins provide functionality for captions, thumbnails, zoom, video, and slideshows.

Tokens
25.8K
Snippets
50
Records
143
Agent score
76%

What's inside yet-another-react-lightbox

  1. Available plugins in Yet Another React Lightbox

    main

    Yet Another React Lightbox provides several optional features through a plugin system. The following plugins are bundled within the main package:

    • Captions: Adds support for slide titles and descriptions.
    • Counter: Adds a slides counter.
    • Download: Adds a download button.
    • Fullscreen: Adds support for fullscreen mode.
    • Inline: Transforms the lightbox into an image carousel.
    • Share: Adds a sharing button.
    • Slideshow: Adds a slideshow button.
    • Thumbnails: Adds a thumbnails track.
    • Video: Adds support for video slides.
    • Zoom: Adds an image zoom feature.

    For specific configuration and usage details, refer to the documentation for the individual plugin.

  2. How modules and plugins work together

    main

    The lightbox uses a modular design where features are implemented as React components called Modules. By default, modules are arranged in a tree structure: Portal > NoScroll > Controller (which contains Carousel, Toolbar, and Navigation).

    To extend the lightbox, you create Plugins. A plugin is a function that receives methods to manipulate the module tree (like addModule, addChild, replace, etc.) and an augment method to modify lightbox props. Plugins are passed to the plugins prop of the Lightbox component.

  3. Extend functionality with plugins

    main

    Yet Another React Lightbox uses a plugin system to keep the core bundle small. You can add optional features by importing and using specific plugins. Available bundled plugins include:

    • Captions: adds support for slide title and description
    • Counter: adds slides counter
    • Download: adds download button
    • Fullscreen: adds support for fullscreen mode
    • Inline: transforms the lightbox into an image carousel
    • Share: adds sharing button
    • Slideshow: adds slideshow button
    • Thumbnails: adds thumbnails track
    • Video: adds support for video slides
    • Zoom: adds image zoom feature
  4. Enable zoom for custom slide types

    main

    By default, the Zoom plugin only supports image slides. To enable zoom for custom slide types, use the supports and maxZoom properties in the zoom config. You must also provide a render.slide function to handle the custom slide rendering. The plugin will automatically wrap your content in a zoom container that handles gestures and transformations.

    Note: TypeScript users must augment the SlideTypes interface to register custom slide types.

    <Lightbox
      slides={slides}
      plugins={[Zoom]}
      zoom={{
        // enable zoom for custom slide types
        supports: ["custom-slide"],
        // maximum zoom level for custom slide types (default: 8)
        maxZoom: 4,
        // or use a function for per-slide max zoom
        // maxZoom: (slide) => (slide.type === "custom-slide" ? 4 : 8),
      }}
      render={{
        slide: ({ slide, zoom, maxZoom }) => {
          if (slide.type === "custom-slide") {
            return <MyCustomSlide slide={slide} zoom={zoom} maxZoom={maxZoom} />;
          }
        },
      }}
    />
  5. Augment lightbox props with plugins

    main

    Use the augment method within a plugin to modify the lightbox props before rendering. This is useful for adding toolbar buttons or providing default values for custom props.

    Example: Adding a toolbar button

    import { addToolbarButton } from "yet-another-react-lightbox";
    
    function MyPlugin({ augment }) {
      augment(({ toolbar, ...restProps }) => ({
        toolbar: addToolbarButton(toolbar, "my-button", <MyButton />),
        ...restProps,
      }));
    }

    Example: Providing default values for custom props

    function MyPlugin({ augment }) {
      augment(({ myProp, ...restProps }) => ({
        myProp: { ...myPropDefaults, ...myProp },
        ...restProps,
      }));
    }
  6. Hide navigation buttons

    main

    To hide the previous and next navigation buttons (for example, when displaying only a single slide), provide a function that returns null to the render.buttonPrev and render.buttonNext props.

    <Lightbox
      carousel={{ finite: slides.length <= 1 }}
      render={{
        buttonPrev: slides.length <= 1 ? () => null : undefined,
        buttonNext: slides.length <= 1 ? () => null : undefined,
      }}
    />
  7. Style the Lightbox using CSS-in-JS, Global CSS, or Module Scoped CSS

    main

    Depending on your styling preference, you can apply styles in several ways:

    CSS-in-JS

    Pass an object to the styles prop to target slots directly or apply CSS variables to the root slot.

    Global CSS

    Target the built-in CSS classes (e.g., .yarl__container) or apply CSS variables to .yarl__root.

    Module Scoped CSS

    Use the className prop to apply a local class to the Lightbox, then use the :global selector to target internal slots, or apply CSS variables directly to your local class.

    // CSS-in-JS: Target slot
    <Lightbox
      styles={{ container: { backgroundColor: "rgba(0, 0, 0, .8)" } }}
    />
    
    // CSS-in-JS: Target root with CSS variable
    <Lightbox
      styles={{ root: { "--yarl__color_backdrop": "rgba(0, 0, 0, .8)" } }}
    />
    /* Global CSS: Target slot */
    .yarl__container {
      background-color: rgba(0, 0, 0, 0.8);
    }
    
    /* Global CSS: Target root with variable */
    .yarl__root {
      --yarl__color_backdrop: rgba(0, 0, 0, 0.8);
    }
    // Module Scoped CSS
    import Lightbox from "yet-another-react-lightbox";
    import "yet-another-react-lightbox/styles.css";
    import styles from "./Component.module.css";
    
    return (
      <Lightbox
        className={styles.lightbox}
      />
    );
    /* Module Scoped CSS: Using :global */
    .lightbox :global(.yarl__container) {
      background-color: rgba(0, 0, 0, 0.8);
    }
    
    /* Module Scoped CSS: Using variable on local class */
    .lightbox {
      --yarl__color_backdrop: rgba(0, 0, 0, 0.8);
    }
  8. Install and setup the Thumbnails plugin

    main

    To use the Thumbnails plugin, you must import the plugin component and its corresponding CSS stylesheet. The plugin provides a thumbnail preview track for image and video slides. For custom slide types, you can provide a thumbnail property directly on the slide object to specify a custom thumbnail image.

    import Lightbox from "yet-another-react-lightbox";
    import Thumbnails from "yet-another-react-lightbox/plugins/thumbnails";
    import "yet-another-react-lightbox/styles.css";
    import "yet-another-react-lightbox/plugins/thumbnails.css";
    
    // ...
    
    return (
      <Lightbox
        plugins={[Thumbnails]}
        // ...
      />
    );