Skip to main content

ImageList

The ImageList component is used to display a collection of images in a grid or masonry layout. It supports various customization options including loading states, error handling, and custom layouts.

Import

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

Usage

Basic Grid Layout

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
const images = [
{ source: require('./image1.jpg') },
{ source: require('./image2.jpg') },
{ source: require('./image3.jpg') },
{ source: { uri: 'https://example.com/image4.jpg' } },
];

return <ImageList images={images} />;
}

Masonry Layout

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
const images = [
{ source: require('./image1.jpg'), aspectRatio: 1 },
{ source: require('./image2.jpg'), aspectRatio: 0.5 },
{ source: require('./image3.jpg'), aspectRatio: 1.5 },
];

return (
<ImageList
images={images}
variant="masonry"
columns={2}
/>
);
}

Custom Column Count

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
return (
<>
<ImageList
images={images}
columns={2}
gap={8}
/>

<ImageList
images={images}
columns={4}
gap={4}
/>
</>
);
}

With Loading State

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
return (
<ImageList
images={images}
showLoading
/>
);
}

With Error Handling

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
const handleError = (event) => {
console.log('Image failed to load:', event.nativeEvent.error);
};

return (
<ImageList
images={images}
onError={handleError}
fallback={<Text>Failed to load image</Text>}
/>
);
}

Interactive Images

import { ImageList } from '@lazy-panda-ui/lazy-panda-ui';

function Example() {
const images = [
{
source: require('./image1.jpg'),
onPress: () => console.log('Image 1 pressed'),
},
{
source: require('./image2.jpg'),
onPress: () => console.log('Image 2 pressed'),
},
];

return <ImageList images={images} />;
}

Props

Main Props

PropTypeDefaultDescription
imagesImageItem[]RequiredArray of image items to display
columnsnumber3Number of columns in the grid
gapnumbertheme.spacing.smSpace between images
variant'grid' | 'masonry''grid'Layout variant of the image list
showLoadingbooleantrueWhether to show loading indicator

Style Props

PropTypeDescription
styleViewStyleAdditional styles for the container
imageStyleImageStyleAdditional styles for each image

Custom Components

PropTypeDescription
fallbackReact.ReactNodeCustom placeholder when image fails to load

Event Props

PropTypeDescription
onError(event: ImageErrorEvent) => voidCalled when an image fails to load

ImageItem Props

PropTypeDescription
sourceImageSourcePropTypeSource of the image
aspectRationumberAspect ratio for masonry layout
onPress() => voidCalled when image is pressed

Accessibility Props

PropTypeDescription
testIDstringTest ID for testing

Theme Customization

You can customize the ImageList component through the theme:

const theme = {
components: {
imageList: {
container: {
// container styles
},
image: {
// image styles
},
loading: {
// loading indicator styles
},
fallback: {
// fallback container styles
},
variants: {
grid: {
// grid variant styles
},
masonry: {
// masonry variant styles
},
},
},
},
};

ImageItem Type

interface ImageItem {
/**
* Source of the image
*/
source: ImageSourcePropType;

/**
* Optional aspect ratio for masonry layout
*/
aspectRatio?: number;

/**
* Called when image is pressed
*/
onPress?: () => void;
}