├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .zuul.yml ├── LICENSE ├── README.md ├── lib └── AutoScroll.js ├── package.json └── test └── AutoScroll.test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | 2 | # EditorConfig helps developers define and maintain consistent 3 | # coding styles between different editors and IDEs 4 | # editorconfig.org 5 | 6 | root = true 7 | 8 | 9 | [*] 10 | 11 | # Change these settings to your own preference 12 | indent_style = space 13 | indent_size = 2 14 | 15 | # We recommend you to keep these unchanged 16 | end_of_line = lf 17 | charset = utf-8 18 | trim_trailing_whitespace = true 19 | insert_final_newline = true 20 | 21 | [*.md] 22 | trim_trailing_whitespace = false 23 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Dependency directory 6 | node_modules 7 | 8 | # OS X 9 | .DS_Store 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: node_js 4 | 5 | cache: 6 | directories: 7 | - node_modules 8 | 9 | node_js: 10 | - "5.3" 11 | 12 | env: 13 | global: 14 | - NODE_ENV=test 15 | -------------------------------------------------------------------------------- /.zuul.yml: -------------------------------------------------------------------------------- 1 | ui: tape 2 | 3 | browserify: 4 | - require: phantomjs-polyfill 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Cesar Andreu 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-auto-scroll 2 | 3 | A HOC that will auto-scroll to the bottom when a property changes. 4 | 5 | It'll track a property and scroll to the bottom whenever it changes. If the user has scrolled up, it preserves the user's scroll position when the property changes. 6 | 7 | ## Installation 8 | 9 | ```sh 10 | $ npm i react-auto-scroll 11 | ``` 12 | 13 | ## Example 14 | 15 | ```js 16 | var React = require('react') 17 | var AutoScroll = require('react-auto-scroll') 18 | var Component = AutoScroll({ 19 | property: 'propertyName' 20 | })(React.createClass(/* ... */)) 21 | ``` 22 | 23 | ## API 24 | 25 | * `AutoScroll(options)(Component)` 26 | 27 | * `options.property` (string) Property to track for scrolling 28 | 29 | ## License 30 | 31 | MIT 32 | -------------------------------------------------------------------------------- /lib/AutoScroll.js: -------------------------------------------------------------------------------- 1 | var React = require('react') 2 | var ReactDOM = require('react-dom') 3 | var createReactClass = require('create-react-class') 4 | var PropTypes = require('prop-types') 5 | 6 | module.exports = function AutoScroll (options) { 7 | var property = options.property 8 | 9 | return function (Component) { 10 | var displayName = Component.displayName || Component.name || 'Component' 11 | 12 | var propTypes = {} 13 | propTypes[property] = PropTypes.any 14 | 15 | var AutoScrollComponent = createReactClass({ 16 | componentDidMount: function componentDidMount () { 17 | var node = this._node 18 | node.scrollTop = node.scrollHeight 19 | this._shouldScroll = false 20 | }, 21 | 22 | componentDidUpdate: function componentDidUpdate (prevProps) { 23 | if (this._shouldScroll) { 24 | var node = this._node 25 | node.scrollTop = node.scrollHeight 26 | this._shouldScroll = false 27 | } 28 | }, 29 | 30 | componentWillUpdate: function componentWillUpdate (nextProps) { 31 | if (this.props[property] !== nextProps[property]) { 32 | var node = this._node 33 | this._shouldScroll = node.scrollTop + node.offsetHeight === node.scrollHeight 34 | } 35 | }, 36 | 37 | displayName: 'AutoScroll(' + displayName + ')', 38 | 39 | propTypes: propTypes, 40 | 41 | render: function render () { 42 | var props = Object.assign({ ref: this._setComponent }, this.props) 43 | return React.createElement(Component, props) 44 | }, 45 | 46 | _setComponent: function _setComponent (component) { 47 | this._component = component 48 | this._node = ReactDOM.findDOMNode(component) 49 | } 50 | }) 51 | 52 | return AutoScrollComponent 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-auto-scroll", 3 | "version": "1.1.0", 4 | "description": "A HOC that will auto-scroll to the bottom when a property changes", 5 | "main": "lib/AutoScroll.js", 6 | "scripts": { 7 | "test": "standard && zuul --phantom -- ./test/AutoScroll.test.js", 8 | "test-local": "zuul --local 8080 -- ./test/AutoScroll.test.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git@github.com:cesarandreu/react-auto-scroll.git" 13 | }, 14 | "keywords": [ 15 | "auto", 16 | "hoc", 17 | "react", 18 | "scroll" 19 | ], 20 | "author": "Cesar Andreu (https://github.com/cesarandreu)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/cesarandreu/react-auto-scroll/issues" 24 | }, 25 | "homepage": "https://github.com/cesarandreu/react-auto-scroll#readme", 26 | "devDependencies": { 27 | "object.assign": "^4.0.3", 28 | "phantomjs-polyfill": "0.0.2", 29 | "phantomjs-prebuilt": "^2.1.7", 30 | "react": "^15.0.0", 31 | "react-dom": "^15.0.0", 32 | "standard": "^6.0.8", 33 | "tape": "^4.5.1", 34 | "zuul": "^3.10.1" 35 | }, 36 | "peerDependencies": { 37 | "react": "^0.14.9 || >=15.3.0", 38 | "react-dom": "^0.14.9 || >=15.3.0" 39 | }, 40 | "dependencies": { 41 | "create-react-class": "^15.6.0", 42 | "prop-types": "^15.5.10" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test/AutoScroll.test.js: -------------------------------------------------------------------------------- 1 | require('object.assign').shim() 2 | require('phantomjs-polyfill') 3 | 4 | var test = require('tape') 5 | var React = require('react') 6 | var ReactDOM = require('react-dom') 7 | var AutoScroll = require('../lib/AutoScroll') 8 | 9 | var TestComponent = React.createClass({ 10 | displayName: 'TestComponent', 11 | propTypes: { 12 | text: React.PropTypes.string.isRequired 13 | }, 14 | render: function render () { 15 | return React.createElement('pre', { 16 | style: { height: 5, lineHeight: '5px', overflow: 'auto' } 17 | }, this.props.text) 18 | } 19 | }) 20 | 21 | var WrappedTestComponent = AutoScroll({ 22 | property: 'text' 23 | })(TestComponent) 24 | 25 | test('AutoScroll renders the wrapped component', function (t) { 26 | t.plan(2) 27 | var div = document.createElement('div') 28 | var instance = ReactDOM.render( 29 | React.createElement(WrappedTestComponent, { 30 | text: 'text' 31 | }), 32 | div 33 | ) 34 | var node = ReactDOM.findDOMNode(instance) 35 | t.equal(node.tagName, 'PRE') 36 | t.equal(node.textContent, 'text') 37 | }) 38 | 39 | test('AutoScroll scrolls down when the property changes', function (t) { 40 | t.plan(2) 41 | var div = document.createElement('div') 42 | document.documentElement.appendChild(div) 43 | var instance = ReactDOM.render( 44 | React.createElement(WrappedTestComponent, { 45 | text: 'text\n' 46 | }), 47 | div 48 | ) 49 | var node = ReactDOM.findDOMNode(instance) 50 | t.equal(node.scrollTop + node.offsetHeight, node.scrollHeight) 51 | 52 | ReactDOM.render( 53 | React.createElement(WrappedTestComponent, { 54 | text: 'text\ntext\ntext\ntext\ntext\n' 55 | }), 56 | div 57 | ) 58 | t.equal(node.scrollTop + node.offsetHeight, node.scrollHeight) 59 | document.documentElement.removeChild(div) 60 | }) 61 | 62 | test('AutoScroll scrolls to the bottom when first mounted', function (t) { 63 | t.plan(1) 64 | var div = document.createElement('div') 65 | document.documentElement.appendChild(div) 66 | var instance = ReactDOM.render( 67 | React.createElement(WrappedTestComponent, { 68 | text: 'text\ntext\ntext\ntext\ntext\n' 69 | }), 70 | div 71 | ) 72 | var node = ReactDOM.findDOMNode(instance) 73 | t.equal(node.scrollTop + node.offsetHeight, node.scrollHeight) 74 | document.documentElement.removeChild(div) 75 | }) 76 | 77 | test('AutoScroll does nothing when the user has scrolled up', function (t) { 78 | t.plan(1) 79 | var div = document.createElement('div') 80 | document.documentElement.appendChild(div) 81 | var instance = ReactDOM.render( 82 | React.createElement(WrappedTestComponent, { 83 | text: 'text\ntext\ntext\ntext\n' 84 | }), 85 | div 86 | ) 87 | var node = ReactDOM.findDOMNode(instance) 88 | node.scrollTop = 10 89 | ReactDOM.render( 90 | React.createElement(WrappedTestComponent, { 91 | text: 'text\ntext\ntext\ntext\ntext\ntext\ntext\ntext\n' 92 | }), 93 | div 94 | ) 95 | t.equal(node.scrollTop, 10) 96 | document.documentElement.removeChild(div) 97 | }) 98 | --------------------------------------------------------------------------------