react-native-fast-image
Version:
FastImage, performant React Native image component.
180 lines (154 loc) • 4.96 kB
Flow
// @flow
import type { NativeSyntheticEvent, ViewProps } from 'react-native'
export type OnLoadEvent = NativeSyntheticEvent<
$ReadOnly<{
width: number,
height: number,
// The view's React tag (on iOS and Android; not on the web).
target?: number,
}>,
>
export type OnErrorEvent = NativeSyntheticEvent<
$ReadOnly<{|
error: string,
|}>,
>
export type OnProgressEvent = NativeSyntheticEvent<
$ReadOnly<{|
loaded: number,
total: number,
// loaded / total, from 0 to 1.
progress: number,
|}>,
>
export type Priority = $ReadOnly<{|
low: 'low',
normal: 'normal',
high: 'high',
|}>
export type ResizeMode = $ReadOnly<{|
contain: 'contain',
cover: 'cover',
stretch: 'stretch',
center: 'center',
repeat: 'repeat',
|}>
export type CacheControl = $ReadOnly<{|
immutable: 'immutable',
web: 'web',
cacheOnly: 'cacheOnly',
|}>
export type ObjectFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
export type PositionLength = number | string
export type ObjectPosition =
| string
| $ReadOnly<{|
top?: PositionLength,
bottom?: PositionLength,
left?: PositionLength,
right?: PositionLength,
|}>
export type Priorities = $Values<Priority>
export type CacheControls = $Values<CacheControl>
export type LoadResult =
| {| ok: true, width: number, height: number |}
| {| ok: false, error: string |}
export type CacheLimits = {|
maxDiskSize?: ?number,
maxDiskAge?: ?number,
maxMemorySize?: ?number,
|}
export type CacheState = {|
maxDiskSize?: number,
maxDiskAge?: number,
maxMemorySize?: number,
diskSize?: number,
|}
export type CachePathResult =
| {| ok: true, path: string |}
| {| ok: false, error: string |}
export type ClearCacheResult = {| ok: true |} | {| ok: false, error: string |}
export type Transition = $ReadOnly<{|
duration?: ?number,
betweenImages?: ?boolean,
skipOnCacheHit?: ?('none' | 'memory' | 'all'),
|}>
export type FastImageDefaults = $ReadOnly<{|
transition?: ?(boolean | number | Transition),
|}>
export type PreloadSuccess = {|
uri: string,
ok: true,
width: number,
height: number,
|}
export type PreloadFailure = {|
uri?: string,
ok: false,
error: string,
|}
export type PreloadResult = PreloadSuccess | PreloadFailure
export type PreloadFn = (
sources: Array<FastImageSource>,
) => Promise<Array<PreloadResult>>
export type FastImageSource = {
uri?: string,
headers?: Object,
priority?: Priorities,
cache?: CacheControls,
cacheKey?: string,
memoryCache?: boolean,
width?: number,
height?: number,
scale?: number,
}
export type FastImageProps = $ReadOnly<{|
...ViewProps,
onError?: ?(event: OnErrorEvent) => void,
onLoad?: ?(event: OnLoadEvent) => void,
onLoadEnd?: ?(result: LoadResult) => void,
onLoadStart?: ?() => void,
onProgress?: ?(event: OnProgressEvent) => void,
source?: ?(FastImageSource | Array<FastImageSource> | number),
defaultSource?: ?number,
tintColor?: number | string,
objectFit?: ?ObjectFit,
objectPosition?: ?ObjectPosition,
recyclingKey?: ?string,
loop?: ?(boolean | number),
imageRendering?: ?('auto' | 'smooth' | 'pixelated'),
paused?: ?boolean,
transition?: ?(boolean | number | Transition),
downsample?: ?boolean,
blurRadius?: ?number,
testID?: ?string,
// FastImage is a single native view, which doesn't take children (since
// 10.0): use FastImageBackground.
children?: "FastImage doesn't take children: use FastImageBackground for content over an image",
|}>
export type FastImageBackgroundProps = $ReadOnly<{|
...FastImageProps,
imageStyle?: $PropertyType<ViewProps, 'style'>,
imageRef?: React$Ref<any>,
children?: React$Node,
|}>
declare export var FastImageBackground: React$ComponentType<FastImageBackgroundProps>
declare export default class FastImage extends React$Component<FastImageProps> {
// Deprecated: removed in 10.0, and a resizeMode prop is ignored; use
// objectFit. Kept so code that reads it doesn't throw.
static resizeMode: ResizeMode;
// Deprecated: use the strings instead, e.g. priority: 'high'.
static priority: Priority;
// Deprecated: use the strings instead, e.g. cache: 'immutable'.
static cacheControl: CacheControl;
static preload: PreloadFn;
static clearMemoryCache: () => Promise<ClearCacheResult>;
static clearDiskCache: () => Promise<ClearCacheResult>;
static getCachePath: (source: FastImageSource) => Promise<CachePathResult>;
static configureCache: (limits?: CacheLimits) => Promise<CacheState>;
static setDefaults: (defaults: FastImageDefaults) => void;
static writeToCache: (
source: FastImageSource,
file: string,
) => Promise<CachePathResult>;
}