Skip to content

Commit

Permalink
Added onSize callback prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor committed Jan 26, 2018
1 parent 4f3930e commit 06a8ddd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
React Native Scalable Image
===========================
React Native ```<Image/>``` component which scales width or height automatically to keep the aspect ratio. Is useful when you need to display an entire network image but don't know its size and can limit it only by width or height.
React Native ```<Image/>``` component [does not keep image aspect ratio](https://github.com/facebook/react-native/issues/858) which results in the image being stretched or cropped. ```react-native-scalable-image``` solves this problem by calculating the image size and resizing the image when rendering.

This library provides an ```<Image/>``` component which scales width or height automatically to keep the aspect ratio. It is useful when you don't know the aspect ratio in advance (e.g. user-uploaded content) but want to display the entire image and limit it only by width or height to fit the container component.

The following example creates an image which fits the full screen width and keeps the aspect ratio:

Expand Down Expand Up @@ -30,9 +32,11 @@ Specify width or height which may be calculated dynamically like in the example

## props

| name | type | default | description |
| ------------- | --------- | --------------------------- | --------------------------------------|
| `height` | number | none | Maximum image height |
| `width` | number | none | Maximum image width |
| `onPress` | function | none | onPress callback |
| `background` | boolean | false | Set to true when used as a background |
| name | type | default | description |
| ------------- | --------- | --------------------------- | --------------------------------------------------------------------------|
| `height` | number | none | Maximum image height |
| `width` | number | none | Maximum image width |
| `background` | boolean | false | Set to true when used as a background |
| `onPress` | function | none | onPress callback |
| `onSize` | function | none | Is called with { width, height } as the arg when image size is calculated |

4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class ScalableImage extends React.Component {
width: sourceWidth * ratio,
height: sourceHeight * ratio
}
});
}, () => this.props.onSize(this.state.size));
}
}

Expand All @@ -98,9 +98,11 @@ ScalableImage.propTypes = {
width: PropTypes.number,
height: PropTypes.number,
onPress: PropTypes.func,
onSize: PropTypes.func,
background: PropTypes.bool,
};

ScalableImage.defaultProps = {
background: false,
onSize: size => {}
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "react-native-scalable-image",
"version": "0.3.5",
"version": "0.4.0",
"license": "MIT",
"description": "React Native Image component which scales width or height automatically to keep the aspect ratio",
"keywords": [
"react",
"react-native",
"image",
"component",
"scalable",
"responsive"
"aspect-ratio",
"scales-width",
"scales-height"
],
"homepage": "https://github.com/huiseoul/react-native-scalable-image",
"bugs": {
Expand Down

0 comments on commit 06a8ddd

Please sign in to comment.