├── .gitignore ├── LICENSE ├── README.md ├── index.d.ts ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # IntelliJ IDEA files 61 | .idea/ 62 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Wassim Gharbi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-native-fullwidth-image 2 | 3 | **A responsive Image element that takes the full width of its parent element while maintaining aspect ratio** 4 | 5 | The original idea comes from [here](https://stackoverflow.com/questions/29642685/maintain-aspect-ratio-of-image-with-full-width-in-react-native/40207328). 6 | 7 | ## Installation 8 | 9 | ``` 10 | npm install --save react-native-fullwidth-image 11 | ``` 12 | 13 | ## Usage 14 | 15 | Start by importing the module 16 | 17 | ```js 18 | import FullWidthImage from 'react-native-fullwidth-image' 19 | ``` 20 | 21 | Now, you can use the `FullWidthImage` element in multiple ways as follows: 22 | 23 | #### Initial Dimensions 24 | 25 | You can provide an initial width and height, `react-native-fullwidth-image` will 26 | infer the aspect ratio from the provided dimensions and keep it as the image is 27 | scaled up to 100% of its parent's width 28 | 29 | ```html 30 | 31 | ``` 32 | **An aspect ratio of 1 will give you square images** 33 | 34 | #### Aspect Ratio 35 | 36 | You can also provide a predefined aspect ratio (a value between 0 and 1) as follows 37 | 38 | ```html 39 | 40 | ``` 41 | _An aspect ratio of 1 will give you square images_ 42 | 43 | #### Automatic detection 44 | 45 | `react-native-fullwidth-image` can automatically detect the aspect ratio of 46 | remote images, all you need to provide is the `uri` as you would do with the 47 | regular `Image` component. 48 | 49 | ```html 50 | 51 | ``` 52 | 53 | 54 | ## Demo 55 | 56 | We use `react-native-fullwidth-image` in **Apperture** [iOS](https://itunes.apple.com/app/id1314756787) / [Android](https://play.google.com/store/apps/details?id=com.aperture) 57 | 58 | ## License 59 | 60 | [MIT License](http://opensource.org/licenses/mit-license.html). © Wassim Gharbi 61 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'react-native-fullwidth-image' { 2 | import {Component} from 'react'; 3 | import {StyleProp, ImageStyle, ImageSourcePropType, ImageLoadEventData, NativeSyntheticEvent} from 'react-native'; 4 | 5 | type Props = { 6 | style?: StyleProp, 7 | source: ImageSourcePropType, 8 | width?: number; 9 | height?: number; 10 | ratio?: number; 11 | onLoad?: (event: NativeSyntheticEvent) => void, 12 | onLoadEnd?: () => void, 13 | onLoadStart?: () => void 14 | } 15 | 16 | export default class FullWidthImage extends Component {} 17 | } 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | react-native-fullwidth-image 3 | 4 | Original idea taken from 5 | https://stackoverflow.com/questions/29642685/maintain-aspect-ratio-of-image-with-full-width-in-react-native/40207328 6 | */ 7 | 8 | import React, { Component } from 'react' 9 | import { View, Image, StyleSheet } from 'react-native' 10 | 11 | export default class FullWidthImage extends Component { 12 | constructor(props) { 13 | super(props) 14 | 15 | this.state = { 16 | width: props.width || null, 17 | height: props.height || null 18 | } 19 | } 20 | 21 | setNativeProps(nativeProps) { 22 | this._root.setNativeProps(nativeProps) 23 | } 24 | 25 | _onLayout(event) { 26 | const containerWidth = event.nativeEvent.layout.width 27 | 28 | if (this.props.ratio) { 29 | this.setState({ 30 | width: containerWidth, 31 | height: containerWidth * this.props.ratio 32 | }) 33 | } else if (this.props.width && this.props.height) { 34 | this.setState({ 35 | width: containerWidth, 36 | height: containerWidth * (this.props.height / this.props.width) 37 | }) 38 | } else if (this.props.source) { 39 | let source = this.props.source 40 | if (typeof source !== 'string') { 41 | source = this.props.source.uri 42 | } 43 | Image.getSize(source, (width, height) => { 44 | this.setState({ 45 | width: containerWidth, 46 | height: (containerWidth * height) / width 47 | }) 48 | }) 49 | } 50 | } 51 | 52 | render() { 53 | return ( 54 | (this._root = component)} 56 | onLayout={this._onLayout.bind(this)} 57 | style={styles.container} 58 | > 59 | 72 | 73 | ) 74 | } 75 | } 76 | 77 | const styles = StyleSheet.create({ 78 | container: { 79 | alignSelf: 'stretch' 80 | } 81 | }) 82 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-fullwidth-image", 3 | "version": "0.1.3", 4 | "description": 5 | "A responsive Image element that takes the full width of its parent element while maintaining aspect ratio.", 6 | "main": "index.js", 7 | "keywords": [ 8 | "react-native", 9 | "react-component", 10 | "react-native-component", 11 | "react", 12 | "mobile", 13 | "ios", 14 | "android", 15 | "ui", 16 | "full-width", 17 | "width", 18 | "100%", 19 | "square", 20 | "image" 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "https://github.com/wassgha/react-native-fullwidth-image.git" 25 | }, 26 | "author": "Wassim Gharbi ", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/wassgha/react-native-fullwidth-image/issues" 30 | }, 31 | "homepage": "https://github.com/wassgha/react-native-fullwidth-image", 32 | "dependencies": {} 33 | } 34 | --------------------------------------------------------------------------------