Skip to content

Commit

Permalink
fix: images honor styles and physical size in "alt" (error) mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Jun 4, 2021
1 parent cb7ad71 commit 059e5d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/render-html/src/renderers/imgRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const imgRenderer: DefaultRenderers['block'][string] = (props) => {
onPress={syntheticAnchorOnLinkPress}
source={{ uri: normalizeUri(src) }}
style={nativeStyle}
width={tnode.attributes.width}
height={tnode.attributes.height}
/>
);
};
Expand Down
50 changes: 38 additions & 12 deletions packages/render-html/src/tags/ImgTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
StyleSheet,
ImageStyle,
StyleProp,
PressableProps,
PressableProps
} from 'react-native';
import PropTypes from 'prop-types';
import GenericPressable from '../GenericPressable';
import { ImageDimensions } from '../types';

export interface ImgDimensions {
width: number;
Expand Down Expand Up @@ -37,8 +38,6 @@ const emptyObject = {};
const styles = StyleSheet.create({
image: { resizeMode: 'cover' },
errorBox: {
width: 50,
height: 50,
borderWidth: 1,
borderColor: 'lightgray',
overflow: 'hidden',
Expand Down Expand Up @@ -245,7 +244,16 @@ function computeImageBoxDimensions(params: any) {
return null;
}

export default class ImgTag extends PureComponent<ImgTagProps, any> {
interface State {
requiredWidth: number;
requiredHeight: number;
imagePhysicalWidth: number | null;
imagePhysicalHeight: number | null;
imageBoxDimensions: ImageDimensions | null;
error: boolean;
}

export default class ImgTag extends PureComponent<ImgTagProps, State> {
private __cachedFlattenStyles: Record<string, any> | null = null;
private __cachedRequirements: ImgDimensions | null = null;
private mounted = false;
Expand All @@ -258,7 +266,8 @@ export default class ImgTag extends PureComponent<ImgTagProps, any> {
imagePhysicalHeight: null,
requiredWidth: this.__cachedRequirements!.width,
requiredHeight: this.__cachedRequirements!.height,
imageBoxDimensions: null
imageBoxDimensions: null,
error: false
};
this.state = {
...state,
Expand Down Expand Up @@ -330,9 +339,7 @@ export default class ImgTag extends PureComponent<ImgTagProps, any> {

componentDidMount() {
this.mounted = true;
if (this.state.requiredWidth == null || this.state.requiredHeight == null) {
this.fetchPhysicalImageDimensions();
}
this.fetchPhysicalImageDimensions();
}

componentWillUnmount() {
Expand Down Expand Up @@ -409,10 +416,28 @@ export default class ImgTag extends PureComponent<ImgTagProps, any> {
}

renderAlt() {
const imageBoxDimensions = this.computeImageBoxDimensions(
this.props,
this.state
);
return (
<View style={styles.errorBox} testID="image-error">
<View
style={[
styles.errorBox,
{
height:
imageBoxDimensions?.height ||
this.props.imagesInitialDimensions.height,
width:
imageBoxDimensions?.width ||
this.props.imagesInitialDimensions.width
}
]}
testID="image-error">
{this.props.alt ? (
<Text style={[styles.errorText, { color: this.props.altColor }]}>{this.props.alt}</Text>
<Text style={[styles.errorText, { color: this.props.altColor }]}>
{this.props.alt}
</Text>
) : (
false
)}
Expand Down Expand Up @@ -441,13 +466,14 @@ export default class ImgTag extends PureComponent<ImgTagProps, any> {
}

render() {
const style = [styles.container, this.props.style];
if (this.props.onPress) {
return (
<GenericPressable onPress={this.props.onPress} style={styles.container}>
<GenericPressable onPress={this.props.onPress} style={style}>
{this.renderContent()}
</GenericPressable>
);
}
return <View style={styles.container}>{this.renderContent()}</View>;
return <View style={style}>{this.renderContent()}</View>;
}
}

0 comments on commit 059e5d6

Please sign in to comment.