Skip to content

Commit 6cf86b5

Browse files
authored
Merge pull request #225 from thibmaek/patch-1
Add the option to pass headers to useImageDimensions
2 parents 0f4eac4 + 482d61f commit 6cf86b5

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@testing-library/react-native": "7.2.0",
2727
"@types/jest": "27.0.2",
2828
"@types/react": "16.14.17",
29-
"@types/react-native": "0.62.18",
29+
"@types/react-native": "0.64.12",
3030
"all-contributors-cli": "6.15.0",
3131
"auto": "9.26.8",
3232
"eslint": "7.3.1",

src/useImageDimensions.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useEffect, useState} from 'react'
2-
import {Image, ImageRequireSource} from 'react-native'
2+
import {Image, ImageRequireSource, ImageURISource} from 'react-native'
33

44
export interface URISource {
55
uri: string
@@ -21,10 +21,12 @@ export interface ImageDimensionsResult {
2121

2222
/**
2323
* @param source either a remote URL or a local file resource.
24+
* @param headers headers to be passed to a remote URL resource.
2425
* @returns original image dimensions (width, height and aspect ratio).
2526
*/
2627
export function useImageDimensions(
2728
source: ImageDimensionsSource,
29+
headers?: ImageURISource['headers'],
2830
): ImageDimensionsResult {
2931
const [result, setResult] = useState<ImageDimensionsResult>({loading: true})
3032

@@ -44,15 +46,28 @@ export function useImageDimensions(
4446
if (typeof source === 'object' && source.uri) {
4547
setResult({loading: true})
4648

47-
Image.getSize(
48-
source.uri,
49-
(width, height) =>
50-
setResult({
51-
dimensions: {width, height, aspectRatio: width / height},
52-
loading: false,
53-
}),
54-
(error) => setResult({error, loading: false}),
55-
)
49+
if (typeof headers === 'object') {
50+
Image.getSizeWithHeaders(
51+
source.uri,
52+
headers,
53+
(width, height) =>
54+
setResult({
55+
dimensions: {width, height, aspectRatio: width / height},
56+
loading: false,
57+
}),
58+
(error) => setResult({error, loading: false}),
59+
)
60+
} else {
61+
Image.getSize(
62+
source.uri,
63+
(width, height) =>
64+
setResult({
65+
dimensions: {width, height, aspectRatio: width / height},
66+
loading: false,
67+
}),
68+
(error) => setResult({error, loading: false}),
69+
)
70+
}
5671

5772
return
5873
}
@@ -61,7 +76,7 @@ export function useImageDimensions(
6176
} catch (error) {
6277
setResult({error, loading: false})
6378
}
64-
}, [source])
79+
}, [source, headers])
6580

6681
return result
6782
}

src/useKeyboard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const initialValue = {
1515
export function useKeyboard() {
1616
const [shown, setShown] = useState(false)
1717
const [coordinates, setCoordinates] = useState<{
18-
start: ScreenRect
18+
start: undefined | ScreenRect
1919
end: ScreenRect
2020
}>(initialValue)
2121
const [keyboardHeight, setKeyboardHeight] = useState<number>(0)

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,10 +1875,10 @@
18751875
dependencies:
18761876
"@types/react" "*"
18771877

1878-
"@types/react-native@0.62.18":
1879-
version "0.62.18"
1880-
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.62.18.tgz#ad63691e7c44edef2beeb6af52b2eb942c3ed8a1"
1881-
integrity sha512-7QfU8EzIYxYqeXpPf8QNv2xi8hrePlgTbRATRo+plRSdVfJu7N6sAXqrFxKJp6bGLvp82GV1gczl93gqiAfXPA==
1878+
"@types/react-native@0.64.12":
1879+
version "0.64.12"
1880+
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.64.12.tgz#1c6a3226c26d7a5949cdf8878e6cfe95fe0951d6"
1881+
integrity sha512-sw6WGSaL219zqrgdb4kQUtFB9iGXC/LmecLZ+UUWEgwYvD0YH81FqWYmONa2HuTkOFAsxu2bK4DspkWRUHIABQ==
18821882
dependencies:
18831883
"@types/react" "*"
18841884

0 commit comments

Comments
 (0)