Repository: exponentjs/react-native-invertible-scroll-view Branch: master Commit: da307fdc6805 Files: 5 Total size: 8.3 KB Directory structure: gitextract_h4v_47hl/ ├── .gitignore ├── InvertibleScrollView.js ├── LICENSE ├── README.md └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Logs logs *.log # Runtime data pids *.pid *.seed # Directory for instrumented libs generated by jscoverage/JSCover lib-cov # Coverage directory used by tools like istanbul coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt # node-waf configuration .lock-wscript # Compiled binary addons (http://nodejs.org/api/addons.html) build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules ================================================ FILE: InvertibleScrollView.js ================================================ import createReactClass from 'create-react-class'; import PropTypes from 'prop-types'; import React from 'react'; import cloneReferencedElement from 'react-clone-referenced-element'; import { ScrollView, StyleSheet, View, } from 'react-native'; import ScrollableMixin from 'react-native-scrollable-mixin'; type DefaultProps = { renderScrollComponent: (props: Object) => ReactElement; }; let InvertibleScrollView = createReactClass({ mixins: [ScrollableMixin], propTypes: { ...ScrollView.propTypes, inverted: PropTypes.bool, renderScrollComponent: PropTypes.func.isRequired, }, getDefaultProps(): DefaultProps { return { renderScrollComponent: props => , }; }, getScrollResponder(): ReactComponent { return this._scrollComponent.getScrollResponder(); }, setNativeProps(props: Object) { this._scrollComponent.setNativeProps(props); }, render() { var { inverted, renderScrollComponent, ...props } = this.props; if (inverted) { if (this.props.horizontal) { props.style = [styles.horizontallyInverted, props.style]; props.children = this._renderInvertedChildren(props.children, styles.horizontallyInverted); } else { props.style = [styles.verticallyInverted, props.style]; props.children = this._renderInvertedChildren(props.children, styles.verticallyInverted); } } return cloneReferencedElement(renderScrollComponent(props), { ref: component => { this._scrollComponent = component; }, }); }, _renderInvertedChildren(children, inversionStyle) { return React.Children.map(children, child => { return child ? {child} : child; }); }, }); let styles = StyleSheet.create({ verticallyInverted: { flex: 1, transform: [ { scaleY: -1 }, ], }, horizontallyInverted: { flex: 1, transform: [ { scaleX: -1 }, ], }, }); export default InvertibleScrollView; ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015-present 650 Industries Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # InvertibleScrollView [![Slack](http://slack.exponentjs.com/badge.svg)](http://slack.exponentjs.com) 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. It conforms to [ScrollableMixin](https://github.com/exponentjs/react-native-scrollable-mixin) so you can compose it with other scrollable components. [![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/) ## Installation Use this with react-native 0.8.0-rc or later. ``` npm install react-native-invertible-scroll-view ``` ## Usage Compose InvertibleScrollView with the scrollable component you would like to invert. In the case of a ListView, you would write: ```js import React from 'react-native'; let { ListView, Text, TouchableHighlight, View, StyleSheet, } = React; import InvertibleScrollView from 'react-native-invertible-scroll-view'; class InvertedScrollComponent extends React.Component { constructor(props, context) { super(props, context); this._data = []; this.state = { dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2, }), }; } render() { return ( } dataSource={this.state.dataSource} renderHeader={this._renderHeader.bind(this)} renderRow={this._renderRow.bind(this)} style={styles.container} /> ); } _renderHeader() { return ( Add a row ); } _renderRow(row) { return {row} } _onPress() { this._data.push(`${new Date}`); var rows = this._data; // It's important to keep row IDs consistent to avoid extra rendering. You // may need to reverse the list of row IDs so the so that the inversion // will order the rows correctly. var rowIds = rows.map((row, index) => index).reverse(); this.setState({ dataSource: this.state.dataSource.cloneWithRows(rows, rowIds), }); } } let styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, button: { padding: 20, borderStyle: 'solid', borderWidth: 1, borderColor: 'black', }, row: { padding: 4, }, }); ``` **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. ## Tips and Caveats - Horizontal scroll views are supported - To scroll to the bottom, call `scrollTo(0)` on a ref to the scroll view - When the scroll view is inverted, InvertibleScrollView wraps each child in a View that is flipped - Scroll views that add children (ex: ListViews) must delegate to InvertibleScrollViews so that the children can be properly inverted - List section headers are unsupported - Styles like `padding` are not corrected, so top padding will actually pad the bottom of the component - Properties like `contentOffset` and `contentInset` are not flipped; for example, the top inset adjusts the bottom of an inverted scroll view ## Implementation 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. ================================================ FILE: package.json ================================================ { "name": "react-native-invertible-scroll-view", "version": "2.0.0", "description": "An invertible ScrollView for React Native", "main": "InvertibleScrollView.js", "scripts": {}, "repository": { "type": "git", "url": "https://github.com/exponentjs/react-native-invertible-scroll-view.git" }, "keywords": [ "react-native", "invertible", "scroll-view" ], "author": "Expo ", "license": "MIT", "bugs": { "url": "https://github.com/exponentjs/react-native-invertible-scroll-view/issues" }, "homepage": "https://github.com/exponentjs/react-native-invertible-scroll-view", "dependencies": { "create-react-class": "^15.6.3", "prop-types": "^15.7.2", "react-clone-referenced-element": "^1.1.0", "react-native-scrollable-mixin": "^1.0.1" } }