├── .gitignore ├── InvertibleScrollView.js ├── LICENSE ├── README.md ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # node-waf configuration 20 | .lock-wscript 21 | 22 | # Compiled binary addons (http://nodejs.org/api/addons.html) 23 | build/Release 24 | 25 | # Dependency directory 26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 27 | node_modules 28 | -------------------------------------------------------------------------------- /InvertibleScrollView.js: -------------------------------------------------------------------------------- 1 | import createReactClass from 'create-react-class'; 2 | import PropTypes from 'prop-types'; 3 | import React from 'react'; 4 | import cloneReferencedElement from 'react-clone-referenced-element'; 5 | import { 6 | ScrollView, 7 | StyleSheet, 8 | View, 9 | } from 'react-native'; 10 | import ScrollableMixin from 'react-native-scrollable-mixin'; 11 | 12 | type DefaultProps = { 13 | renderScrollComponent: (props: Object) => ReactElement; 14 | }; 15 | 16 | let InvertibleScrollView = createReactClass({ 17 | mixins: [ScrollableMixin], 18 | 19 | propTypes: { 20 | ...ScrollView.propTypes, 21 | inverted: PropTypes.bool, 22 | renderScrollComponent: PropTypes.func.isRequired, 23 | }, 24 | 25 | getDefaultProps(): DefaultProps { 26 | return { 27 | renderScrollComponent: props => , 28 | }; 29 | }, 30 | 31 | getScrollResponder(): ReactComponent { 32 | return this._scrollComponent.getScrollResponder(); 33 | }, 34 | 35 | setNativeProps(props: Object) { 36 | this._scrollComponent.setNativeProps(props); 37 | }, 38 | 39 | render() { 40 | var { 41 | inverted, 42 | renderScrollComponent, 43 | ...props 44 | } = this.props; 45 | 46 | if (inverted) { 47 | if (this.props.horizontal) { 48 | props.style = [styles.horizontallyInverted, props.style]; 49 | props.children = this._renderInvertedChildren(props.children, styles.horizontallyInverted); 50 | } else { 51 | props.style = [styles.verticallyInverted, props.style]; 52 | props.children = this._renderInvertedChildren(props.children, styles.verticallyInverted); 53 | } 54 | } 55 | 56 | return cloneReferencedElement(renderScrollComponent(props), { 57 | ref: component => { this._scrollComponent = component; }, 58 | }); 59 | }, 60 | 61 | _renderInvertedChildren(children, inversionStyle) { 62 | return React.Children.map(children, child => { 63 | return child ? {child} : child; 64 | }); 65 | }, 66 | }); 67 | 68 | let styles = StyleSheet.create({ 69 | verticallyInverted: { 70 | flex: 1, 71 | transform: [ 72 | { scaleY: -1 }, 73 | ], 74 | }, 75 | horizontallyInverted: { 76 | flex: 1, 77 | transform: [ 78 | { scaleX: -1 }, 79 | ], 80 | }, 81 | }); 82 | 83 | export default InvertibleScrollView; 84 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present 650 Industries 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 | # InvertibleScrollView [![Slack](http://slack.exponentjs.com/badge.svg)](http://slack.exponentjs.com) 2 | 3 | InvertibleScrollView is a React Native scroll view that can be inverted so that content is rendered starting from the bottom, and the user must scroll down to reveal more. This is a common design in chat applications and the command-line terminals. InvertibleScrollView also supports horizontal scroll views to present content from right to left. 4 | 5 | It conforms to [ScrollableMixin](https://github.com/exponentjs/react-native-scrollable-mixin) so you can compose it with other scrollable components. 6 | 7 | [![npm package](https://nodei.co/npm/react-native-invertible-scroll-view.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/react-native-invertible-scroll-view/) 8 | 9 | ## Installation 10 | Use this with react-native 0.8.0-rc or later. 11 | 12 | ``` 13 | npm install react-native-invertible-scroll-view 14 | ``` 15 | 16 | ## Usage 17 | 18 | Compose InvertibleScrollView with the scrollable component you would like to invert. In the case of a ListView, you would write: 19 | 20 | ```js 21 | import React from 'react-native'; 22 | let { 23 | ListView, 24 | Text, 25 | TouchableHighlight, 26 | View, 27 | StyleSheet, 28 | } = React; 29 | import InvertibleScrollView from 'react-native-invertible-scroll-view'; 30 | 31 | class InvertedScrollComponent extends React.Component { 32 | constructor(props, context) { 33 | super(props, context); 34 | this._data = []; 35 | this.state = { 36 | dataSource: new ListView.DataSource({ 37 | rowHasChanged: (r1, r2) => r1 !== r2, 38 | }), 39 | }; 40 | } 41 | 42 | render() { 43 | return ( 44 | } 46 | dataSource={this.state.dataSource} 47 | renderHeader={this._renderHeader.bind(this)} 48 | renderRow={this._renderRow.bind(this)} 49 | style={styles.container} 50 | /> 51 | ); 52 | } 53 | 54 | _renderHeader() { 55 | return ( 56 | 59 | Add a row 60 | 61 | ); 62 | } 63 | 64 | _renderRow(row) { 65 | return {row} 66 | } 67 | 68 | _onPress() { 69 | this._data.push(`${new Date}`); 70 | var rows = this._data; 71 | // It's important to keep row IDs consistent to avoid extra rendering. You 72 | // may need to reverse the list of row IDs so the so that the inversion 73 | // will order the rows correctly. 74 | var rowIds = rows.map((row, index) => index).reverse(); 75 | this.setState({ 76 | dataSource: this.state.dataSource.cloneWithRows(rows, rowIds), 77 | }); 78 | } 79 | } 80 | 81 | let styles = StyleSheet.create({ 82 | container: { 83 | flex: 1, 84 | justifyContent: 'center', 85 | alignItems: 'center', 86 | backgroundColor: '#F5FCFF', 87 | }, 88 | button: { 89 | padding: 20, 90 | borderStyle: 'solid', 91 | borderWidth: 1, 92 | borderColor: 'black', 93 | }, 94 | row: { 95 | padding: 4, 96 | }, 97 | }); 98 | ``` 99 | 100 | **NOTE:** When inverting a ListView, you must create a ListView that delegates to an InvertibleScrollView as shown above and not the other way around. Otherwise it will not be able to invert the rows and the content will look upside down. This is true for any scroll view that adds its own children, not just ListView. 101 | 102 | ## Tips and Caveats 103 | 104 | - Horizontal scroll views are supported 105 | - To scroll to the bottom, call `scrollTo(0)` on a ref to the scroll view 106 | - When the scroll view is inverted, InvertibleScrollView wraps each child in a View that is flipped 107 | - Scroll views that add children (ex: ListViews) must delegate to InvertibleScrollViews so that the children can be properly inverted 108 | - List section headers are unsupported 109 | - Styles like `padding` are not corrected, so top padding will actually pad the bottom of the component 110 | - Properties like `contentOffset` and `contentInset` are not flipped; for example, the top inset adjusts the bottom of an inverted scroll view 111 | 112 | ## Implementation 113 | 114 | InvertibleScrollView uses a scale transformation to efficiently invert the view. The scroll view's viewport is inverted to flip the entire component, and then each child is inverted again so that the content appears unflipped. 115 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-native-invertible-scroll-view", 3 | "version": "2.0.0", 4 | "description": "An invertible ScrollView for React Native", 5 | "main": "InvertibleScrollView.js", 6 | "scripts": {}, 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/exponentjs/react-native-invertible-scroll-view.git" 10 | }, 11 | "keywords": [ 12 | "react-native", 13 | "invertible", 14 | "scroll-view" 15 | ], 16 | "author": "Expo ", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/exponentjs/react-native-invertible-scroll-view/issues" 20 | }, 21 | "homepage": "https://github.com/exponentjs/react-native-invertible-scroll-view", 22 | "dependencies": { 23 | "create-react-class": "^15.6.3", 24 | "prop-types": "^15.7.2", 25 | "react-clone-referenced-element": "^1.1.0", 26 | "react-native-scrollable-mixin": "^1.0.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | asap@~2.0.3: 6 | version "2.0.6" 7 | resolved "https://registry.yarnpkg.com/asap/-/asap-2.0.6.tgz#e50347611d7e690943208bbdafebcbc2fb866d46" 8 | 9 | core-js@^1.0.0: 10 | version "1.2.7" 11 | resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636" 12 | 13 | create-react-class@^15.6.3: 14 | version "15.6.3" 15 | resolved "https://registry.yarnpkg.com/create-react-class/-/create-react-class-15.6.3.tgz#2d73237fb3f970ae6ebe011a9e66f46dbca80036" 16 | integrity sha512-M+/3Q6E6DLO6Yx3OwrWjwHBnvfXXYA7W+dFjt/ZDBemHO1DDZhsalX/NUtnTYclN6GfnBDRh4qRHjcDHmlJBJg== 17 | dependencies: 18 | fbjs "^0.8.9" 19 | loose-envify "^1.3.1" 20 | object-assign "^4.1.1" 21 | 22 | encoding@^0.1.11: 23 | version "0.1.12" 24 | resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb" 25 | dependencies: 26 | iconv-lite "~0.4.13" 27 | 28 | fbjs@^0.8.9: 29 | version "0.8.14" 30 | resolved "https://registry.yarnpkg.com/fbjs/-/fbjs-0.8.14.tgz#d1dbe2be254c35a91e09f31f9cd50a40b2a0ed1c" 31 | dependencies: 32 | core-js "^1.0.0" 33 | isomorphic-fetch "^2.1.1" 34 | loose-envify "^1.0.0" 35 | object-assign "^4.1.0" 36 | promise "^7.1.1" 37 | setimmediate "^1.0.5" 38 | ua-parser-js "^0.7.9" 39 | 40 | iconv-lite@~0.4.13: 41 | version "0.4.18" 42 | resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.18.tgz#23d8656b16aae6742ac29732ea8f0336a4789cf2" 43 | 44 | is-stream@^1.0.1: 45 | version "1.1.0" 46 | resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" 47 | 48 | isomorphic-fetch@^2.1.1: 49 | version "2.2.1" 50 | resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9" 51 | dependencies: 52 | node-fetch "^1.0.1" 53 | whatwg-fetch ">=0.10.0" 54 | 55 | js-tokens@^3.0.0: 56 | version "3.0.2" 57 | resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" 58 | 59 | "js-tokens@^3.0.0 || ^4.0.0": 60 | version "4.0.0" 61 | resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" 62 | integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== 63 | 64 | loose-envify@^1.0.0, loose-envify@^1.3.1: 65 | version "1.3.1" 66 | resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.3.1.tgz#d1a8ad33fa9ce0e713d65fdd0ac8b748d478c848" 67 | dependencies: 68 | js-tokens "^3.0.0" 69 | 70 | loose-envify@^1.4.0: 71 | version "1.4.0" 72 | resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" 73 | integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== 74 | dependencies: 75 | js-tokens "^3.0.0 || ^4.0.0" 76 | 77 | node-fetch@^1.0.1: 78 | version "1.7.1" 79 | resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.1.tgz#899cb3d0a3c92f952c47f1b876f4c8aeabd400d5" 80 | dependencies: 81 | encoding "^0.1.11" 82 | is-stream "^1.0.1" 83 | 84 | object-assign@^4.1.0, object-assign@^4.1.1: 85 | version "4.1.1" 86 | resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" 87 | 88 | promise@^7.1.1: 89 | version "7.3.1" 90 | resolved "https://registry.yarnpkg.com/promise/-/promise-7.3.1.tgz#064b72602b18f90f29192b8b1bc418ffd1ebd3bf" 91 | dependencies: 92 | asap "~2.0.3" 93 | 94 | prop-types@^15.7.2: 95 | version "15.7.2" 96 | resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5" 97 | integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ== 98 | dependencies: 99 | loose-envify "^1.4.0" 100 | object-assign "^4.1.1" 101 | react-is "^16.8.1" 102 | 103 | react-clone-referenced-element@^1.1.0: 104 | version "1.1.0" 105 | resolved "https://registry.yarnpkg.com/react-clone-referenced-element/-/react-clone-referenced-element-1.1.0.tgz#9cdda7f2aeb54fea791f3ab8c6ab96c7a77d0158" 106 | integrity sha512-FKOsfKbBkPxYE8576EM6uAfHC4rnMpLyH6/TJUL4WcHUEB3EUn8AxPjnnV/IiwSSzsClvHYK+sDELKN/EJ0WYg== 107 | 108 | react-is@^16.8.1: 109 | version "16.8.6" 110 | resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16" 111 | integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA== 112 | 113 | react-native-scrollable-mixin@^1.0.1: 114 | version "1.0.1" 115 | resolved "https://registry.yarnpkg.com/react-native-scrollable-mixin/-/react-native-scrollable-mixin-1.0.1.tgz#34a32167b64248594154fd0d6a8b03f22740548e" 116 | 117 | setimmediate@^1.0.5: 118 | version "1.0.5" 119 | resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285" 120 | 121 | ua-parser-js@^0.7.9: 122 | version "0.7.33" 123 | resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.33.tgz#1d04acb4ccef9293df6f70f2c3d22f3030d8b532" 124 | integrity sha512-s8ax/CeZdK9R/56Sui0WM6y9OFREJarMRHqLB2EwkovemBxNQ+Bqu8GAsUnVcXKgphb++ghr/B2BZx4mahujPw== 125 | 126 | whatwg-fetch@>=0.10.0: 127 | version "2.0.3" 128 | resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84" 129 | --------------------------------------------------------------------------------