This is an illustration of ImageViewer component.
import { Center } from "@/components/ui/center"import { Image } from "@/components/ui/image"import {ImageViewer,ImageViewerBackdrop,ImageViewerContent,ImageViewerImage,ImageViewerCloseButton,} from "@/components/ui/image-viewer"import { Pressable } from "@/components/ui/pressable"import { StatusBar } from "@/components/ui/statusbar"import { Icon, CloseIcon } from "@/components/ui/icon"import React from "react"function App() {const [visible, setVisible] = React.useState(false)const Images = [{id: 1,url: "https://img.freepik.com/free-photo/young-boy-learning-how-ride-horse_23-2150460636.jpg",},]return (<Center><StatusBar hidden={visible} /><Pressable onPress={() => setVisible(true)}><Imagesource={{ uri: Images[0].url }}className="w-[200px] h-[200px]"resizeMode="cover"/></Pressable><ImageViewer isOpen={visible} onClose={() => setVisible(false)}><ImageViewerBackdrop><ImageViewerContentimages={Images}renderImages={({ item, index }) => (<ImageViewerImage source={{ uri: item.url }} />)}keyExtractor={(item, index) => item.id + "-" + index}><ImageViewerCloseButton><Icon as={CloseIcon} className="text-secondary-500" /></ImageViewerCloseButton></ImageViewerContent></ImageViewerBackdrop></ImageViewer></Center>)}
Installation
CLI
Manual
Run the following command:
npx gluestack-ui add image-viewer
API Reference
To use this component in your project, include the following import statement in your file.
import {ImageViewer,ImageViewerBackdrop,ImageViewerContent,ImageViewerCloseButton,ImageViewerImage,} from "@/components/ui/image-viewer"
export default () => (<ImageViewer><ImageViewerBackdrop><ImageViewerContent><ImageViewerCloseButton></ImageViewerCloseButton></ImageViewerContent></ImageViewerBackdrop></ImageViewer>)
Component Props
This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.
ImageViewer
TheImageViewer component serves as the main container for displaying images in a modal view. It provides a user-friendly interface for viewing images with features like pinch-to-zoom, double-tap zoom, and swipe gestures for dismissal. It is built on top of React Native's Modal component, inheriting all its properties and behaviors.
Prop | Type | Default | Description | Required |
---|---|---|---|---|
isOpen | boolean | - | If true, the image-viewer modal will open. Useful for controllable state behavior. | Yes |
onClose | () => any | - | Callback invoked when the image-viewer modal is closed. | Yes |
ImageViewerContent
The ImageViewerContent component is responsible for rendering the images within the ImageViewer. It supports gestures for zooming and panning, allowing users to interact with the images. This component leverages React Native's Animated & View components, as well as gesture handling from the react-native-gesture-handler library.
Prop | Type | Default | Description | Required |
---|---|---|---|---|
images | Array<any> | - | Array of image objects to display | Yes |
renderImages | ({item, index}) => ReactNode | - | Function to render each image item | Yes |
keyExtractor | (item: any, index: number) => string | - | Function to extract the key for each image item | Yes |
ImageViewerCloseButton
The ImageViewerCloseButton component provides a customizable button for closing the ImageViewer. It is typically placed within the ImageViewerContent and can be styled to match the application's design. It inherits properties from React Native's View component.
ImageViewerBackdrop
The ImageViewerBackdrop component serves as the background layer of the ImageViewer, providing a dimmed or blurred effect behind the content. It enhances the focus on the images being viewed. This component is built using React Native's Animated & View components, allowing for smooth transitions and animations.
ImageViewerImage
The ImageViewerImage component is used to display individual images within the ImageViewerContent. It supports all the properties of React Native's Image component, making it easy to customize the appearance and behavior of the images.
Examples
The Examples section offers visual previews of the component, letting you quickly identify the best fit for your needs. Just copy the code and use it in your project.
Basic ImageViewer
Basic ImageViewer component is created using ImageViewer component from gluestack-ui. It extends all the props supported by React Native Image component.
import {ImageViewer,ImageViewerBackdrop,ImageViewerContent,ImageViewerCloseButton,ImageViewerImage,} from "@/components/ui/image-viewer"import { CloseIcon, Icon } from "@/components/ui/icon"import React, { useState } from "react"import { Pressable, Image } from "react-native"import { Center } from "@/components/ui/center"import { StatusBar } from "expo-status-bar"
import { Center } from "@/components/ui/center"import { Image } from "@/components/ui/image"import {ImageViewer,ImageViewerBackdrop,ImageViewerContent,ImageViewerImage,ImageViewerCloseButton,} from "@/components/ui/image-viewer"import { Pressable } from "@/components/ui/pressable"import { StatusBar } from "@/components/ui/statusbar"import { Icon, CloseIcon } from "@/components/ui/icon"import React from "react"function App() {const [visible, setVisible] = React.useState(false)const Images = [{id: 1,url: "https://img.freepik.com/free-photo/young-boy-learning-how-ride-horse_23-2150460636.jpg",},]return (<Center><StatusBar hidden={visible} /><Pressable onPress={() => setVisible(true)}><Imagesource={{ uri: Images[0].url }}className="w-[200px] h-[200px]"resizeMode="cover"/></Pressable><ImageViewer isOpen={visible} onClose={() => setVisible(false)}><ImageViewerBackdrop><ImageViewerContentimages={Images}renderImages={({ item, index }) => (<ImageViewerImage source={{ uri: item.url }} />)}keyExtractor={(item, index) => item.id + "-" + index}><ImageViewerCloseButton><Icon as={CloseIcon} className="text-secondary-500" /></ImageViewerCloseButton></ImageViewerContent></ImageViewerBackdrop></ImageViewer></Center>)}
Important Note
Note: Currently, the ImageViewer component is limited to only single image, multiple image support will be added in the future.