use Image Picker
The useImagePicker hook provides access to camera and gallery functionality for selecting images.
Anchor to useImagePickeruse Image Picker()
use Image Picker()
UseImagePickerReturnsUseImagePickerReturns
UseImagePickerReturns
- openCamera
Opens the camera to take a photo.
(params?: OpenCameraParams) => Promise<File> - openGallery
Opens the gallery to select an image.
(params?: OpenGalleryParams) => Promise<File>
OpenCameraParams
- cameraFacing
CameraFacing - customQuality
CustomImageQuality - quality
ImageQuality
CameraFacing
'front' | 'back'CustomImageQuality
- compression
number - size
number
ImageQuality
'low' | 'medium' | 'high' | 'original'OpenGalleryParams
- customQuality
CustomImageQuality - quality
ImageQuality
Examples
tsx
import {useState} from 'react'
import {useImagePicker, Button, Image} from '@shopify/shop-minis-react'
export default function MyComponent() {
const {openCamera, openGallery} = useImagePicker()
const [selectedFile, setSelectedFile] = useState<File | null>(null)
const handleCameraCapture = async () => {
try {
const file = await openCamera('front')
setSelectedFile(file)
console.log('Captured file:', file.name, file.size)
} catch (error) {
console.error('Failed to capture image:', error)
}
}
const handleGallerySelect = async () => {
try {
const file = await openGallery()
setSelectedFile(file)
console.log('Selected file:', file.name, file.size)
} catch (error) {
console.error('Failed to select image:', error)
}
}
const clearImage = () => {
setSelectedFile(null)
}
return (
<>
<Button onClick={handleCameraCapture}>Open Camera</Button>
<Button onClick={handleGallerySelect}>Open Gallery</Button>
{selectedFile && (
<>
{/* Image component handles blob URL creation and cleanup automatically */}
<Image
file={selectedFile}
alt="Selected image"
className="w-full max-w-md mt-4"
/>
<div className="mt-2 space-x-2">
<Button onClick={clearImage} variant="secondary">
Clear
</Button>
</div>
</>
)}
{/* Image component also supports regular src for remote images */}
<Image
src="https://example.com/image.jpg"
alt="Remote image"
className="w-full max-w-md mt-4"
/>
</>
)
}
Examples
tsx
import {useState} from 'react' import {useImagePicker, Button, Image} from '@shopify/shop-minis-react' export default function MyComponent() { const {openCamera, openGallery} = useImagePicker() const [selectedFile, setSelectedFile] = useState<File | null>(null) const handleCameraCapture = async () => { try { const file = await openCamera('front') setSelectedFile(file) console.log('Captured file:', file.name, file.size) } catch (error) { console.error('Failed to capture image:', error) } } const handleGallerySelect = async () => { try { const file = await openGallery() setSelectedFile(file) console.log('Selected file:', file.name, file.size) } catch (error) { console.error('Failed to select image:', error) } } const clearImage = () => { setSelectedFile(null) } return ( <> <Button onClick={handleCameraCapture}>Open Camera</Button> <Button onClick={handleGallerySelect}>Open Gallery</Button> {selectedFile && ( <> {/* Image component handles blob URL creation and cleanup automatically */} <Image file={selectedFile} alt="Selected image" className="w-full max-w-md mt-4" /> <div className="mt-2 space-x-2"> <Button onClick={clearImage} variant="secondary"> Clear </Button> </div> </> )} {/* Image component also supports regular src for remote images */} <Image src="https://example.com/image.jpg" alt="Remote image" className="w-full max-w-md mt-4" /> </> ) }
Was this page helpful?