Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useImageDimensions #87

Merged
merged 22 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ yarn add @react-native-community/hooks
- [useCameraRoll](https://github.com/react-native-community/hooks#usecameraroll)
- [useClipboard](https://github.com/react-native-community/hooks#useclipboard)
- [useDimensions](https://github.com/react-native-community/hooks#usedimensions)
- [useImageDimensions](https://github.com/react-native-community/hooks#useImageDimensions)
- [useKeyboard](https://github.com/react-native-community/hooks#usekeyboard)
- [useInteractionManager](https://github.com/react-native-community/hooks#useinteractionmanager)
- [useDeviceOrientation](https://github.com/react-native-community/hooks#usedeviceorientation)
Expand Down Expand Up @@ -104,6 +105,26 @@ const { width, height } = useDimensions().window
const screen = useDimensions().screen
```

### `useImageDimensions`

```js
import {useImageDimensions} from '@react-native-community/hooks'

const imageDimensions = useImageDimensions(require('./assets/yourImage.png'))
// or
const imageDimensions = useImageDimensions({uri: 'https://your.image.URI'})
// or
const imageDimensions = useImageDimensions({uri: 'https://your.image.URI'}, error => {
onImageFetchFailure(error)
})

if (!imageDimensions) {
return null
}

const {width, height} = imageDimensions
```

### `useKeyboard`

```js
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useKeyboard from './useKeyboard'
import useInteractionManager from './useInteractionManager'
import useDeviceOrientation from './useDeviceOrientation'
import useLayout from './useLayout'
import useImageDimensions from './useImageDimensions'
LinusU marked this conversation as resolved.
Show resolved Hide resolved

export {
useDimensions,
Expand All @@ -20,4 +21,5 @@ export {
useInteractionManager,
useDeviceOrientation,
useLayout,
useImageDimensions,
}
59 changes: 59 additions & 0 deletions src/useImageDimensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {useEffect, useState} from 'react'
import {Image, ImageRequireSource} from 'react-native'

export interface URISource {
LinusU marked this conversation as resolved.
Show resolved Hide resolved
uri: string
}

type ImageDimensions = {
width: number
height: number
} | null
type FailureCallback = (error: any) => void

/**
* @param source local file resource.
* @returns Original image width and height.
*/
function useImageDimensions(source: ImageRequireSource): ImageDimensions
/**
* @param source remote URL
* @param failure the function that will be called if there was an error, such as failing to retrieve the image (see https://reactnative.dev/docs/image#getsize).
* @returns Original image width and height.
*/
function useImageDimensions(
source: URISource,
failure?: FailureCallback,
): ImageDimensions
function useImageDimensions(
source: ImageRequireSource | URISource,
failure?: FailureCallback,
): ImageDimensions
function useImageDimensions(
source: ImageRequireSource | URISource,
failure?: FailureCallback,
) {
Greg-Bush marked this conversation as resolved.
Show resolved Hide resolved
const [dimensions, setDimensions] = useState<ImageDimensions>()
useEffect(() => {
if (typeof source === 'object') {
const {uri} = source
Image.getSize(
uri,
(width, height) => setDimensions({width, height}),
error => {
setDimensions(null)
if (failure) {
failure(error)
} else {
console.error(error)
}
},
)
} else {
setDimensions(Image.resolveAssetSource(source))
}
}, [source, failure])
return dimensions
}

export default useImageDimensions