npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@scissorhands/react-photo-album

v0.0.16

Published

Responsive photo gallery component for React

Downloads

3

Readme

React Photo Album

React Photo Album is a responsive photo gallery component for React. React Photo Album supports rows, columns, and masonry layouts. Inspired by react-photo-gallery, re-engineered from the ground up.

Overview

NPM Version Bundle Size License MIT

  • Built for React: works with React 18, 17 and 16.14.0
  • SSR friendly: produces server-side rendered markup that looks pixel perfect on the client even before hydration.
  • Responsive images: responsive images with automatic resolution switching are supported out of the box.
  • Feature packed: supports 3 layout options (rows, columns and masonry), responsive images, custom data attributes and is fully configurable and customizable.
  • TypeScript: type definitions come built-in in the package.
  • Performance: it was built with performance in mind in order to support large photo albums and silky smooth layout adjustments.

Examples

Rows

Rows layout

Columns

Columns layout

Masonry

Masonry layout

All examples are available on CodeSandbox.

Documentation

https://react-photo-album.com/documentation

Installation

npm install react-photo-album

or

yarn add react-photo-album

Minimal Setup Example

import PhotoAlbum from "react-photo-album";

const photos = [
    {
        src: "/images/image1.jpg",
        width: 800,
        height: 600
    },
    {
        src: "/images/image2.jpg",
        width: 1600,
        height: 900
    }
];

<PhotoAlbum layout="rows" photos={photos} />

How It Works

Rows Layout

Rows layout fills the rectangular container space by arranging photos into rows that are similar in size, with the height of each row being as close to the targetRowHeight as possible. This layout uses an algorithm adapted from the Knuth and Plass line breaking algorithm. To calculate the single best layout, it uses Dijkstra's algorithm to find the shortest past in a graph where each photo to break on represents a node, and each row represents an edge. The cost of each edge is calculated as the squared deviation from the targetRowHeight. This algorithm produces rows that are similar in height and photos that are not being stretched or shrunken abnormally (as is what happens in a naive implementation). It solves the issue of panoramas shrinking rows or having stragglers or stretched images in the last row, instead creating a justified grid. The graph is being built as the shortest path is being calculated to improve algorithm's performance, so the entire adjacency list is not calculated ahead of time.

Columns Layout

Columns layout fills the rectangular container space by arranging photos into a predefined number of columns, determined by the columns parameter. This layout uses an algorithm very similar to the one described above, but instead of Dijkstra's algorithm, it uses a dynamic programming algorithm to find the shortest path of length N in a directed weighted graph.

Masonry Layout

Masonry layout arranges photos into columns of equal width by placing each photo into the shortest column. This layout does not completely fill the rectangular container space, but the columns end up being as close in height to each other as possible.

Responsive Images

React Photo Album automatically generates sizes and srcset image attributes. In the case of SSR, React Photo Album includes sizes and srcset image attributes in the server-rendered markup, allowing browsers to pick images of the most appropriate resolution depending on their viewport size.

SSR

React Photo Album extensively uses CSS flexbox and CSS calc function to calculate the dimensions of images on the client. Unlike its predecessor, React Photo Album avoids setting the exact dimensions of images in pixels. Thanks to this approach, server-side rendered markup looks pixel-perfect on the client even before hydration (or even when JavaScript is completely disabled in the browser). React Photo Album calculates spacing, padding, columns, and other responsive parameters on the server-side using the defaultContainerWidth value, which is set to 800px by default. Keep in mind that responsive parameters may contribute to cumulative layout shifts during the initial page load. If CLS becomes an issue in your case, you may want to consider using hard-coded values for columns, spacing , padding, etc., instead of the default responsive values.

Credits

License

MIT © 2021 Igor Danchenko