0.4.3 • Published 7 months ago

expo-image-viewer v0.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Expo Image Viewer

‼️ Do not use in production yet. See prerequisites below. ‼️

This is a simple image viewer built with React Native Reanimated and React Native Gesture Handler. Images use Expo Image, which is highly performant and allows for the prefetching of images to display in the image viewer.

This package is fully compatible with Expo Go.

Prerequisites

  • Expo SDK 49
  • Expo Image - Currently this package uses an unreleased version of Expo Image while waiting for Expo to publish changes fixing a memory leak while scaling images.
  • Expo Haptics
  • Reanimated 3.3.0+
  • React Native Gesture Handler 2.12.0+

Installation

yarn add expo-image-viewer

Usage

First, you will want to wrap your app inside a <GestureHandlerRootView> component. You should place this somewhere near the root of your app, such as in your App.tsx file.

import { GestureHandlerRootView } from 'react-native-gesture-handler';

export default function App() {
    return (
        <GestureHandlerRootView>
          <App />
        </GestureHandlerRootView>
    );
}

Next, you should additionally wrap your app inside of ImageViewerProvider.

import { ImageViewerProvider } from 'expo-image-viewer';

export default function App() {
    return (
        <GestureHandlerRootView>
          <ImageViewerProvider>
            <App />
          </ImageViewerProvider>
        </GestureHandlerRootView>
    );
}

Finally, you can use the ViewerImage component to display images in the image viewer.

import { ViewerImage } from 'expo-image-viewer';

export default function MyScreen() {
    return (
        <View style={{flex: 1}}>
            <ViewerImage
              source="https://images.unsplash.com/photo-1634179127707-4b8b9b5b2b0b?ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwzNnx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
              initialDimensions={{
                height: 500,
                width: 500
              }}
              heightModifier={0.6}
              title="My Image"
            />
        </View>
    );
}

Whenever the ViewerImage component is pressed, the image viewer will open and display the image.

Props

ImageViewerProvider

PropTypeDescription
childrenReact.ReactNodeThe children to render inside of the provider.
optionsImageViewerOptionsThe options to use for the image viewer.

ImageViewerOptions

OptionRequiredDefaultTypeDescription
showHeaderNotruebooleanShow the header.
showFooterNotruebooleanShow the footer.
showCloseButtonNotruebooleanShow the close button.
preloadImagesNotruebooleanWhen supplying an array of images, the each of the images will be preloaded. See more below.
heightModifierNo1numberA number between 0 and 1 that defines the modifier for the height. Useful if displaying images in a feed and want to maintain aspect ratio without having images disappear off the screen
swipeToDismissNotruebooleanWhether to allow the user to swipe to dismiss the viewer.
FooterComponentNonull() => React.JSX.ElementA footer component to render. Background color should be transparent. The animated displaying/hideing of this will be handled for you.
ActivityIndicatorComponentNonull() => React.JSX.ElementA component to render for an activity indicator. It will automatically be positioned in the center of your initial dimensions.

ViewerImage

PropRequiredDefaultTypeDescription
sourceYesNAstring string[]The source of the image.
contentFitNocontainImageContentFit (see Expo Image)How the image should be displayed within its bounds.
titleNoNAstringThe title of the image. Will be shown in the image viewer if the header is enabled.
heightModifierNoNAnumberOverrides the heightModifier provided to the ImageViewerProvider.
blurRadiusNoNAnumberA blur radius to apply to the image (see Expo Image)
recyclingKeyNoNAstringA recycling key to be used with a recycler view such as FlashList (see Expo Image)
initialDimensionsYes{height: 300, width: 300}|{height: number, width: number}A height and width that should be the default for the image.
onLoadNoNA(e: ImageLoadEventData) => voidA function to run when the onLoad event is called.
onPressNoNA() => unknownA function to run when the image is pressed
useInitialDimensionsNofalsebooleanWhether to keep the initial dimensions of the image even after the load.
showActivityIndicatorNotruebooleanWhether to show the activity indicator while the image is loading. You can supply your own component to display, otherwise will use the React Native ActivityIndicator component.
activityIndicatorSizeNolargesmall \| largeSize of the activity indicator
activityIndicatorColorNowhiteColorValueColor of the activity indicator

useImageViewer()

If you need to get any of the current values (such as the current index of the displayed image or the image source) or if you wish to create your own implementation of ViewerImage, you can use the useImageViewer hook.

PropertyTypeDescription
optionsImageViewerOptionsThe options passed to the ImageViewerProvider. These are only provided for reference. They should be changed in the ImageViewerProvider.
propsIImageViewerStateThe current state of the image viewer. See below.
dispatch(action: React.Dispatch<IAction>) => voidA dispatch function to dispatch actions to the image viewer. See below.

IImageViewerState

PropertyTypeDescription
paramsIImageViewerParamsThe params passed to the image viewer. See below.
visiblebooleanWhether the image viewer is visible.
viewerOpacityAnimated.SharedValue<number>The opacity of the image viewer. Do not change this unless you are creating your own implementation.
viewerRefReact.RefObject<View> \| nullThe ref of the image's container. For example, the pressable that you are wrapping your <Image>in. Used to perform a measurement of the view when opening. Do not change this value unless you are creating your own implementation.

IImageViewerParams

PropertyTypeDescription
sourcestring string[]The source of the image.
titlestringThe title of the image.
indexnumberThe index of the image being displayed in the viewer.

IAction

IAction is an object that takes a type and a payload. These are the possible actions.

ActionPayload TypeDescription
setStateIImageViewerStateSets the state of the image viewer.
setVisiblebooleanSets the visibility of the image viewer.
setIndexnumberSets the index of the image to display in the viewer.
0.4.3

7 months ago

0.4.2

7 months ago

0.4.1

7 months ago

0.4.0

7 months ago

0.3.5

7 months ago

0.3.4

7 months ago

0.3.3

7 months ago

0.3.2

7 months ago

0.3.1

7 months ago

0.3.0

7 months ago

0.2.0

7 months ago

0.1.1

7 months ago