Command palette

@yasinsensoy/native

React Native · Components

Gallery

Photo and video gallery (same API as Gallery on web): grid, masonry or rows layout, groups by date, virtual scrolling (even with 100,000 items only the visible ones are drawn), multiple selection by long-pressing and dragging, a full-screen view (Lightbox), infinite scroll and pagination.

20 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Import

import { Gallery, Lightbox, mediaType, formatDuration } from "@yasinsensoy/native";

mediaType

function

Type of the item: inferred from the URL extension when type is omitted.

mediaType(item: Pick<MediaItemFields, "src" | "type">): GalleryMediaType

Formats a duration as 1:05 or 1:02:05.

formatDuration(seconds: number): string

Examples

formatDuration(65) // "1:05"
formatDuration(3725) // "1:02:05"

GalleryAction

interface

An action in the selection bar (e.g. Share, Delete).

NameTypeDescription
labelstringButton text.
icon?IconPropIcon: a name or an element.
tone?"neutral" | "danger"Red when "danger".
onClick(items: GalleryItem[], clear: () => void) => unknownCalled with the selected items. If it returns a Promise the button shows loading until it finishes. Call clear() to end the selection.

Information passed to the GalleryProps.renderItem function.

NameTypeDescription
indexnumberIndex of the item.
widthnumberWidth of the box.
heightnumberHeight of the box.
selectedbooleanThe item is selected.
selectingbooleanThe gallery is in selection mode.
childrenReactNodeThe built-in render: thumbnail, video badge, status and title.

How a selected item is shown.

type GallerySelectionStyle = "shrink" | "ring" | "dim"

Settings of the Gallery page buttons (with pageSize).

type GalleryPaginationOptions = Pick<PaginationProps, "variant" | "siblings" | "boundaries" | "firstLast" | "summary" | "size" | "activeVariant" | "align" | "label">

Event of a Gallery item tap.

NameTypeDescription
preventDefault() => voidPrevents the full-screen view from opening.
nativeEventGestureResponderEvent["nativeEvent"]The press event.

GalleryProps

interface

Props of Gallery.

NameTypeDefaultDescription
itemsreadonly GalleryItem[]Items. When grouping, give them sorted in group order (e.g. by date).
layout?GalleryLayout"grid"Layout: "grid" equal squares, "masonry" Pinterest-style columns, "rows" Google Photos-style rows (each row fills the width).
columns?numberFixed column count. If omitted, fitted to the width based on itemWidth.
itemWidth?number120` (3 columns on a phone)Target item width in grid and masonry.
minColumns?number3` in grid, `2` in masonryMinimum column count when columns is not given.
rowHeight?number160Target row height in the rows layout.
aspectRatio?number1Cell ratio in the grid (width / height).
gap?number2Gap between items.
groupBy?GalleryGroupBy | ((item: GalleryItem, index: number) => string)Grouping: "day" | "month" | "year" by the item's date field ("Today", "Yesterday", "Tuesday, October 28"…), or a function returning a group name from an item.
groupLabel?(key: string, info: { count: number; start: number; }) => ReactNodeGroup heading. If omitted the date or group name is written.
stickyHeaders?booleantrueGroup headings stick to the top while scrolling.
stickyOffset?number0Distance of the sticky heading from the top.
selectable?booleanfalseMultiple selection (like iOS Photos): a long press starts selection; sliding without lifting the finger selects the ones in between too. In selection mode a tap selects/deselects, and a sideways swipe selects by dragging. Near the edge the list scrolls by itself.
selected?readonly string[]Ids of the selected items in controlled use.
defaultSelected?readonly string[]Initially selected ones in uncontrolled use.
onSelectedChange?(selected: string[]) => voidWith the ids when the selection changes.
selectionStyle?GallerySelectionStyle"shrink"Look of a selected item.
selectionActions?readonly GalleryAction[]Actions in the selection bar.
selectionBar?booleantrueA selection bar at the bottom while there is a selection.
effect?EffectPropEffect or effects of the tiles (e.g. "scale", "shine"). Defaults to UIProvider appearance.defaults.gallery.effect.
appear?"fade" | "scale" | "none""fade"Reveal when an image loads.
rounded?booleanfalse` (edge to edge on a phone)Rounded corners.
videoPreview?"hover" | "visible" | "none""none"Preview in videos: "visible" the video nearest the center of the screen plays muted (phone feeds), "none". The "hover" of web does not exist on a phone; it behaves like "none". preview can be given for a light preview.
videoFrames?booleantrueFor videos without a poster, a frame is extracted and used as the poster.
titles?"hover" | "always"Show item titles below the box ("hover" is not shown on a phone).
overlay?(item: GalleryItem, index: number) => ReactNodeExtra content drawn on top of the box (e.g. a like badge).
renderItem?(item: GalleryItem, context: GalleryRenderContext) => ReactNodeRender the inside of the box yourself; the built-in render comes in context.children.
onRetry?(item: GalleryItem, index: number) => voidWhen an item with status="error" is tapped; when given, "Retry" shows on the tile.
lightbox?boolean | LightboxOptionstrueFull-screen view that opens on tap. false turns it off.
onItemClick?(item: GalleryItem, index: number, event: GalleryPressEvent) => voidWhen an item is tapped (outside selection mode). event.preventDefault() does not open full screen.
onLoadMore?() => unknownFetches the next items. If it returns a Promise skeletons show until it finishes.
hasMore?booleanfalseThere are more items to fetch.
loadMore?"scroll" | "button""scroll"Next items: "scroll" near the end, "button" with a button.
loading?booleanLoading: a skeleton grid when there are no items, skeleton boxes at the bottom otherwise.
pageSize?numberPagination: this many items per page. When total is given, items is the current page.
page?numberControlled page (starts at 1).
defaultPage?number1Initial page.
onPageChange?(page: number) => voidWhen the page changes.
total?numberTotal item count for server-side pagination.
pagination?GalleryPaginationOptionsLook of the page buttons.
height?numberHeight of the gallery. If omitted it fills its container (flex: 1). The gallery scrolls in its own scroll area; only visible items are drawn.
overscan?number600Distance drawn ahead above and below the visible area.
empty?ReactNodeContent shown when there are no items.
label?string"Galeri"Screen reader name.
header?ReactNodeContent added above the scroll area (header, filters); scrolls with it.
style?StyleProp<ViewStyle>Container style.
ref?Ref<ViewRef>Access to the container.
testID?stringTest id.

Gallery layout.

type GalleryLayout = "grid" | "masonry" | "rows"

Type of a gallery item.

type GalleryMediaType = "image" | "video"

Built-in grouping: by day, month or year of the item's date field.

type GalleryGroupBy = "day" | "month" | "year"

Content to share (the return value of the share function).

NameTypeDescription
url?stringAddress.
title?stringTitle.
text?stringText.

Extra action button in the top bar.

NameTypeDescription
labelstringButton name; for screen readers.
iconIconPropIcon: a name or an element.
tone?"neutral" | "danger"Red when "danger".
onClick(item: GalleryItem, index: number) => unknownCalled with the current item when tapped. If it returns a Promise the button shows loading until it finishes.

LightboxRect

interface

A rectangle on screen (window coordinates).

NameTypeDescription
xnumberLeft edge (screen coordinates).
ynumberTop edge (screen coordinates).
widthnumberWidth.
heightnumberHeight.

Settings of the full-screen view (also given via Gallery lightbox).

NameTypeDefaultDescription
mode?"horizontal" | "vertical""horizontal"Transition direction: "horizontal" swiping sideways (like photo apps), "vertical" swiping up like Reels / TikTok; videos play when they come on screen.
loop?booleanfalseWrap from the end to the start (and from the start to the end).
thumbnails?booleantrueA thumbnail strip at the bottom (horizontal only, wide screens: tablet).
zoom?booleantrueZoom on images: with two fingers and a double tap. When zoomed in, pan with a finger.
preload?number1Number of items prepared ahead on both sides of the visible one.
download?booleanfalseDownload button: on a phone it opens the system share sheet ("Save image", "Save to Files" are there).
share?boolean | ((item: GalleryItem, index: number) => LightboxShareContent)false` (when on, the item's address and title)Share button (the system share sheet). When a function is given it returns the content to share: share={(item) => ({ url: \https://…/photo/${item.id}\ })}.
slideshow?boolean | numberfalse` (4000 ms when on)Slideshow button. A number is the duration of each item (ms); videos advance when they end.
captions?booleantrueShow the title and description at the bottom.
videoFrames?booleantrueFor videos without a poster, a frame is extracted and used as the poster.
actions?readonly LightboxAction[]Extra action buttons. See LightboxAction.

LightboxProps

interface

Props of Lightbox.

NameTypeDefaultDescription
itemsreadonly GalleryItem[]Items to show.
open?booleanWhether it is open in controlled use.
defaultOpen?booleanfalseOpen initially in uncontrolled use?
onOpenChange?(open: boolean) => voidWhen it opens or closes.
index?numberIndex of the shown item in controlled use.
defaultIndex?number0Initial index in uncontrolled use.
onIndexChange?(index: number) => voidWith the new index when the item changes.
onReachEnd?() => voidWhen nearing the last items; for fetching the next page in infinite lists.
originRect?(index: number) => LightboxRect | null | undefinedOn-screen location (window coordinates) of the thumbnail the image grows out of / returns to on open and close. Gallery provides it itself.
onRatio?(item: GalleryItem, ratio: number) => voidReports the measured aspect ratio.
label?string"Galeri"Screen reader name.
selectable?booleanfalseSelection: a circle in the top bar that selects the current item.
selected?readonly string[]Ids of the selected items (id, otherwise src).
onSelectedChange?(selected: string[]) => voidWith the ids when the selection changes.
mode?"horizontal" | "vertical""horizontal"Transition direction: "horizontal" swiping sideways (like photo apps), "vertical" swiping up like Reels / TikTok; videos play when they come on screen.
loop?booleanfalseWrap from the end to the start (and from the start to the end).
thumbnails?booleantrueA thumbnail strip at the bottom (horizontal only, wide screens: tablet).
zoom?booleantrueZoom on images: with two fingers and a double tap. When zoomed in, pan with a finger.
preload?number1Number of items prepared ahead on both sides of the visible one.
download?booleanfalseDownload button: on a phone it opens the system share sheet ("Save image", "Save to Files" are there).
share?boolean | ((item: GalleryItem, index: number) => LightboxShareContent)false` (when on, the item's address and title)Share button (the system share sheet). When a function is given it returns the content to share: share={(item) => ({ url: \https://…/photo/${item.id}\ })}.
slideshow?boolean | numberfalse` (4000 ms when on)Slideshow button. A number is the duration of each item (ms); videos advance when they end.
captions?booleantrueShow the title and description at the bottom.
videoFrames?booleantrueFor videos without a poster, a frame is extracted and used as the poster.
actions?readonly LightboxAction[]Extra action buttons. See LightboxAction.

Status on the server: "uploading" (percentage with progress), "processing" (e.g. a video being transcoded), "error" failed. Not given for a ready item.

type GalleryItemStatus = "uploading" | "processing" | "error"

GalleryItem

interface

An item of Gallery and Lightbox.

NameTypeDescription
id?stringUnique id; used for selection. Defaults to src.
srcstringFull-size image or video address (opens in the full-screen view).
thumbnail?stringThumbnail shown in the grid. Give it to avoid downloading large files in the grid; otherwise src is used for images and poster for videos.
type?GalleryMediaTypeType. If omitted it is inferred from the address's extension (.mp4, .webm, .mov → video).
width?numberOriginal width and height (px). Only the ratio is used; when given, the masonry and rows layouts draw correctly before images load and the page does not shift. Otherwise the ratio is measured when the image loads.
height?numberOriginal height (px). See width.
alt?stringAlternative text of the image (screen reader).
title?ReactNodeTitle; shown on top and in the caption in full screen.
description?ReactNodeDescription; shown at the bottom in full screen.
poster?stringCover image of the video.
preview?stringA short, light preview video played when it comes on screen (videoPreview="visible"). If omitted src is played muted.
duration?numberVideo duration (seconds); shown in the corner like 1:24.
date?Date | string | numberDate; used for grouping with groupBy="day" | "month" | "year".
placeholder?stringColor shown until the image loads (e.g. the image's dominant color #8a9bb0) or a small blurred image (data:image/...).
disabled?booleanCannot be selected.
status?GalleryItemStatusStatus on the server. When given, the tile shows a progress ring, "Processing" or an error, and the item does not open in full screen (it can be selected; e.g. to delete failed ones). While uploading, a local preview of the file (a file:// address) can be given as src.
progress?numberProgress while status="uploading", 0–1. Spins indeterminately if omitted.
statusText?ReactNodeYour own text next to the status (e.g. "Compressing…", the error reason).