Skip to content

Commit

Permalink
feat: support source.headers in useIMGElementState hook
Browse files Browse the repository at this point in the history
Users which want to customize the renderer for img tags can now set
headers to `source` prop. The hook will pre-fetch the image to grab its
natural dimensions, using these headers when available.
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent 399eb54 commit a49e958
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/render-html/src/__mocks__/react-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class Image extends RNImage {
* and invoke the callback with the extracted dimensions. If none can
* be extracted, it will use 640x360.
*/
static getSize(uri, callback) {
static getSizeWithHeaders(uri, headers, callback) {
setTimeout(() => {
let dimensions = [0, 0];
if (typeof uri === 'string') {
Expand Down
1 change: 1 addition & 0 deletions packages/render-html/src/elements/img-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface IMGElementStateLoading {
export interface IMGElementStateError {
type: 'error';
containerStyle: ViewStyle;
error: Error;
/**
* Either the scaled image dimensions, or `initialImageDimensions` if the
* later could not be determined.
Expand Down
23 changes: 14 additions & 9 deletions packages/render-html/src/elements/useIMGElementLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,33 +272,37 @@ function usePhysicalDimensions({
physicalDimensionsFromProps
]
);
const [hasError, setHasError] = useState(false);
const [error, setError] = useState<null | Error>(null);
useEffect(
function fetchPhysicalDimensions() {
let cancelled = false;
if (source.uri) {
Image.getSize(
Image.getSizeWithHeaders(
source.uri,
source.headers || {},
(w, h) => {
!cancelled && setPhysicalDimensions({ width: w, height: h });
},
(e) => {
if (__DEV__) {
console.error(e);
}
!cancelled && setHasError(true);
!cancelled && setError(e || {});
}
);
return () => {
cancelled = true;
};
}
},
[source.uri, physicalDimensions, physicalDimensionsFromProps]
[
source.uri,
source.headers,
physicalDimensions,
physicalDimensionsFromProps
]
);
useEffect(
function resetOnURIChange() {
setPhysicalDimensions(null);
setError(null);
},
[source.uri]
);
Expand All @@ -308,7 +312,7 @@ function usePhysicalDimensions({
physicalDimensions: isPlainImgDimensions(physicalDimensionsFromProps)
? physicalDimensionsFromProps
: physicalDimensions,
error: hasError
error: error
};
}

Expand Down Expand Up @@ -352,9 +356,10 @@ export default function useIMGElementLoader(
]);
return error
? {
type: 'error',
alt,
altColor,
type: 'error',
error,
containerStyle: flatStyle,
imageBoxDimensions: imageBoxDimensions ?? imagesInitialDimensions!
}
Expand Down

0 comments on commit a49e958

Please sign in to comment.