A customizable, accessible gallery component for React projects.
npm install @wethegit/react-galleryA slideshow-style gallery component for use in React projects.
- Getting started
- Starter example
- Custom layouts
- Components and options
- Gallery
- GalleryMain
- GalleryItem
- GalleryNav
- GalleryPagination
- GalleryPaginationItem
- Accessibility
- useGallery hook
- Advanced customization
- Usage
- Accessibility. Built with careful attention to focus management and assistive technology.
- Fully-customizable layouts, using CSS Custom Properties.
- Provides a custom React hook for accessing gallery data, state, updater functions, and more. Build anything on top of the existing gallery components!
``bash`
npm install @wethegit/react-gallery
-or-
yarn add @wethegit/react-gallery
`js`
import {
Gallery,
GalleryMain,
GalleryItem,
GalleryNav,
GalleryPagination,
GalleryPaginationItem,
useGallery,
} from "@wethegit/react-gallery"
This is an optional step, but it's highly recommended to use the base styles as a starting point. The most straightforward way to do this is to import the stylesheet into your app directly from the /node_modules/@wethegit/react-gallery/dist/ directory, but you can do this in whichever way is preferable within your build system or framework.
`jsx
// app.js
import "@wethegit/react-gallery/style.css"
`
The component is a React context provider, which gives all child components access to relevant data. All child components that need access to gallery data must live within a . Here's an example of a gallery, given the following contrived data GALLERY_ITEMS. This will be explained in detail shortly.
⚠️ Before continuing, make sure you have properly imported the base stylesheet, if you intend to use it.
`js
// some-data.js
export const GALLERY_ITEMS = [
{ image: "/my-image-1.png", alt: "Description of image!", id: 131789 },
{ image: "/my-image-2.png", alt: "Description of image!", id: 235233 },
{ image: "/my-image-3.png", alt: "Description of image!", id: 987432 },
{ image: "/my-image-4.png", alt: "Description of image!", id: 768324 },
]
`
`jsx
// your-gallery.js
import { GALLERY_ITEMS } from "./some-data"
import {
Gallery,
GalleryMain,
GalleryNav,
GalleryPagination,
GalleryPaginationItem,
GalleryItem,
} from "@wethegit/react-gallery"
const YourGallery = () => {
return (
)}
/>
{i + 1}
)}
/>
)
}
export default YourGallery
`
The first step is to give your data to the component via the items prop. At the very least, items is expected to be an Array. From there, you're free to arrange the child components this package provides as you see fit. Below is a brief description of each of the child components' usage. For a detailed breakdown of this component, jump ahead to the Gallery section.
is the primary gallery view where your item data is rendered. It receives a render prop, renderGalleryItem, which exposes a few arguments you can use in the JSX you return: item, index, activeIndex, and active and expects a to be returned. For a detailed breakdown of this component, jump ahead to the GalleryMain section.
We're using the component to define our "next" and "previous" buttons. These components receive a direction prop, which expects either a 1 or a 0, and corresponds to the direction the gallery should move in when the button in question is clicked (where 0 maps to "previous", and 1 maps to "next"). For a detailed breakdown of this component, see the GalleryNav section.
We're also using the and GalleryPaginationItem components here. If you're not familiar, "pagination" refers to what is often rendered as a set of "dots" below a gallery — but this can be _anything_ (thumbnails, icons, and so on). This component receives the render prop, renderPaginationItem, which exposes a few arguments you can use in the JSX you return: item, i, activeIndex, and active. The easiest way to link up you pagination is to use the component, as shown in the example above. For a detailed breakdown of this component, jump ahead to the GalleryPagination section.
Assuming the base stylesheet is being used, the component renders as a CSS grid, with a single grid area to house the currently-active item. There are a number of CSS custom properties on the various gallery components, which will allow you to customize the look of the gallery. Most of these properties are used to calculate the horizontal positioning of the gallery items around the main grid cell, and can be adjusted to create many different layout variations. Remove the transform property of a .gallery__item entirely, and you'll get a traditional, stacked gallery.
| CSS custom property | Element where defined | Default | Description |
| ------------------- | ---------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| --item-width | .gallery | min(75%, 1000px) | The width of the items rendered by . |pointermove
| --gap | .gallery | 25vw | The horizontal gap between the gallery items. |
| --active-gap | .gallery | 0px | The horizontal gap between the currently-active gallery item and the rest. |
| --duration | .gallery | 0 | The duration of the transition between active gallery items. |
| --touch-offset | .gallery | 0 | The x-offset of the gallery layout, as a result of a action. |-1
| --position-offset | .gallery | 0px | A general x-offset, if needed. |
| --i | .gallery\_\_item | | The index of the gallery item. |
| --center-offset | .gallery\_\_item | | The offset of the item from the "middle" item, in number of items. |
| --index-offset | .gallery\_\_item | | The offset of the item from the active item, in number of items. |
| --side | .gallery\_\_item | | Either , 0, or 1; mapping to "left", "center", and "right" of the middle item. |1
| --active | .gallery\_\_item | | Either or 0, mapping to true or false. |translateX
| --x | .gallery\_\_item | | The final calculation of a gallery item's x position. Use this as a value when customizing transform properties. |
| --gap | .gallery\_\_pagination | 10px | The CSS grid gap between pagination items. |
The parent component for all gallery instances. This serves as a React context provider to its children.
#### Props:
| Prop | Type | Default value | Description |
| ------------ | -------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| ariaLiveText | String | "Item $i of $t." | aria-live region text, which assistive technologies will announce when the active gallery item changes. More information below. |-1
| className | String | | |
| draggable | Boolean | true | Allows user to advance the gallery by dragging their finger or pointing device. |
| items | Array | | The gallery data. |
| loop | Boolean | false | Allows for looping behavior — i.e. when the last gallery item is active, a user can navigate to the "next" slide, which will take them back to the start of the gallery. |
| onChange | Function | | Function to run on gallery state update, as a result of a "next", "previous", or "pagination" action. More information below. |
| startIndex | Number | 0 | The index of the gallery item that should be active to start. |
| visibleRange | Number | -1 | The number of gallery items "visible" to the left or right of the active one. This option adds a CSS class to all elements within this range, allowing for layout customization. The default value of specifies that all items are visible, while setting the value to 1 for example, would add the "gallery__item--visible" class those items within immediate proximity to the active item. |
#### ariaLiveText
Per the WAI-ARIA spec, browsers using assistive technology such as screen readers announce content within a DOM element containing the aria-live attribute, anytime it changes. The gallery component's default value for this prop is "Item $i of $t.". The variables $i and $t here correspond to "current index" and "total number of items", and will be replaced with those values when rendered. This level of customization is provided so that you can localize/translate your content with accuracy.
#### onChange
The onChange callback allows you to run code whenever the active gallery item changes. It receives a single Object as an argument, containing the following properties:
| Argument | Type | Description |
| --------- | ------ | ---------------------------------------------------------- |
| oldIndex | Number | The index before the onChange function fired. |
| newIndex | Number | The index after the onChange function fires. |
| direction | Number | Either 0 or 1, mapping to previous/left or next/right. |
The primary gallery body. Must be used within a . Renders an unordered list () of your gallery items.
#### Props:
| Prop | Type | Description |
| ----------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| className | String | |
| renderGalleryItem | Function | A render prop, returning the JSX to render for each gallery item. More information below. |
#### renderGalleryItem
This render prop expects a to be returned, and receives a handful of arguments:
| Argument | Type | Description |
| ----------- | ------- | -------------------------------------------------------------------------------------------------------------- |
| active | Boolean | Whether the current item being iterated over is the active item. |
| activeIndex | Number | The index of the currently active gallery item. |
| index | Number | The index of the current item being iterated over. |
| item | Any | The current item being iterated over, as defined by the Array fed to the component's items prop. |
prop. Renders a list item () and can accept the following props:| Prop | Type | Description |
| ----------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| children | JSX | |
| className | String | |
| active | Boolean | Whether the current item being iterated over is the active item. |
| index | Number | The index of the currently active gallery item. |
$3
The navigational "next" and "previous" buttons. Must be used within a
. You can render your buttons either by passing regular JSX children to them, or by using the renderNavItem render prop.#### Props:
| Prop | Type | Description |
| ------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| children | JSX | Optional. Pass children to the component to render them as children of the implicit