Document Picker
Action button wrapping expo-document-picker with rapid-press guards, error isolation, and typed result/cancel callbacks.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add document-pickerSource files
Expand a file to read the source the installer copies into your app.
components/ui/document-picker/DocumentPicker.tsx
import * as React from 'react'
import { Platform, type GestureResponderEvent } from 'react-native'
import { getDocumentAsync, type DocumentPickerOptions, type DocumentPickerResult } from 'expo-document-picker'
import { Button, type ButtonProps } from '@/components/ui/button'
function revokeUnclaimedWebBlobUrls(result: DocumentPickerResult | undefined) {
if (Platform.OS !== 'web' || !result || result.canceled || !Array.isArray(result.assets)) return
if (typeof URL === 'undefined' || typeof URL.revokeObjectURL !== 'function') return
for (const asset of result.assets) {
if (typeof asset?.uri === 'string' && asset.uri.startsWith('blob:')) {
try {
URL.revokeObjectURL(asset.uri)
} catch {
// Ignore errors when revoking unclaimed web blob URLs.
}
}
}
}
export interface DocumentPickerProps extends ButtonProps {
/** Allowed MIME types (e.g. `'application/pdf'`, `['image/*', 'application/pdf']`). Defaults to all types. */
type?: string | string[]
/** Allow selecting multiple documents when supported by the platform. */
multiple?: boolean
/** Copy selected documents into the app cache directory so they can be read immediately. Defaults to `true`. */
copyToCacheDirectory?: boolean
/** Receives the complete `DocumentPickerResult` returned by `expo-document-picker`, including canceled results (`result.canceled === true`). */
onResult?: (result: DocumentPickerResult) => void
/** Called when the user cancels the document picker (`result.canceled === true`). */
onCancel?: () => void
/** Called when `getDocumentAsync` rejects. */
onError?: (error: Error) => void
}
/**
* Triggers the system document picker via `expo-document-picker` (`getDocumentAsync`).
*
* Bare React Native apps require `expo-modules` (Expo Modules autolinking) to use `expo-document-picker`.
* On web, cancelling the browser file dialog may not reliably resolve with a canceled result or fire `onCancel`
* across browsers. Callers own any delivered web blob URLs (`URL.createObjectURL` assets) and should revoke
* them when finished; unclaimed web `blob:` URLs (if the trigger unmounts before resolution or no `onResult`
* callback is provided) are revoked automatically.
*/
const DocumentPicker = React.forwardRef<React.ComponentRef<typeof Button>, DocumentPickerProps>(
(
{
type,
multiple,
copyToCacheDirectory = true,
onResult,
onCancel,
onError,
onPress,
disabled,
loading = false,
children = 'Choose file',
...props
},
ref,
) => {
const [pending, setPending] = React.useState(false)
const mountedRef = React.useRef(true)
const pendingRef = React.useRef(false)
const onResultRef = React.useRef(onResult)
onResultRef.current = onResult
React.useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
}
}, [])
async function handlePress(event: GestureResponderEvent) {
if (disabled || loading || pendingRef.current) return
onPress?.(event)
if (event?.defaultPrevented) return
const options: DocumentPickerOptions = { copyToCacheDirectory, base64: false }
if (type !== undefined) options.type = type
if (multiple !== undefined) options.multiple = multiple
pendingRef.current = true
setPending(true)
let result: DocumentPickerResult | undefined
let nativeError: Error | undefined
try {
result = await getDocumentAsync(options)
} catch (error) {
nativeError = error instanceof Error ? error : new Error(String(error))
}
pendingRef.current = false
if (!mountedRef.current) {
revokeUnclaimedWebBlobUrls(result)
return
}
setPending(false)
if (nativeError) {
onError?.(nativeError)
return
}
if (!result) return
const deliverResult = onResultRef.current
if (deliverResult) {
deliverResult(result)
} else {
revokeUnclaimedWebBlobUrls(result)
}
if (result.canceled) {
onCancel?.()
}
}
return (
<Button ref={ref} disabled={disabled} loading={loading || pending} onPress={handlePress} {...props}>
{children}
</Button>
)
},
)
DocumentPicker.displayName = 'DocumentPicker'
export { DocumentPicker }
components/ui/document-picker/index.ts
export { DocumentPicker, type DocumentPickerProps } from './DocumentPicker'
Dependencies
Registry items
npm packages
expo-document-picker