├── .babelrc ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jscsrc ├── README.md ├── dist ├── featureDetect.js ├── react-sticky-state.js ├── react-sticky-state.min.js └── sticky-state.css ├── example ├── .gitignore ├── README.md ├── package.json ├── public │ ├── favicon.ico │ └── index.html └── src │ ├── App.css │ ├── App.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── logo.svg │ └── sticky │ ├── featureDetect.js │ └── react-sticky-state.js ├── examples ├── index.html └── main.css ├── lib ├── featureDetect.js └── react-sticky-state.js ├── package.json ├── src ├── featureDetect.js ├── react-sticky-state.js ├── sticky-state.css └── sticky-state.scss ├── test └── index.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["es2015", {"loose": true}], 4 | "react", 5 | "stage-0" 6 | ], 7 | "plugins": [ 8 | [ 9 | "transform-runtime", { 10 | "helpers": true, 11 | "polyfill": false, 12 | "regenerator": false, 13 | "moduleName": "babel-runtime" 14 | } 15 | ], 16 | "transform-class-properties", 17 | "add-module-exports" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OS or Editor folders 2 | .DS_Store 3 | *.sublime-workspace 4 | npm-debug.log 5 | 6 | # Project folders to ignore 7 | node_modules 8 | TODO 9 | .tern-port 10 | *.js.map 11 | .history 12 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "google", 3 | "esnext": true, 4 | "maximumLineLength" : 200, 5 | "disallowSpacesInAnonymousFunctionExpression": null, 6 | "excludeFiles": [ "node_modules/**" ], 7 | "disallowEmptyBlocks": true, 8 | "disallowKeywords": [ 9 | "with" 10 | ], 11 | "disallowMixedSpacesAndTabs": true, 12 | "disallowMultipleLineStrings": true, 13 | "disallowMultipleVarDecl": "exceptUndefined", 14 | "disallowSpaceAfterPrefixUnaryOperators": [ 15 | "!", 16 | "+", 17 | "++", 18 | "-", 19 | "--", 20 | "~" 21 | ], 22 | "disallowSpaceBeforeBinaryOperators": [ 23 | "," 24 | ], 25 | "disallowSpaceBeforePostfixUnaryOperators": true, 26 | "disallowSpacesInNamedFunctionExpression": { 27 | "beforeOpeningRoundBrace": true 28 | }, 29 | "disallowSpacesInsideArrayBrackets": true, 30 | "disallowSpacesInsideParentheses": true, 31 | "disallowTrailingComma": true, 32 | "disallowTrailingWhitespace": true, 33 | "requireCamelCaseOrUpperCaseIdentifiers": true, 34 | "requireCapitalizedConstructors": true, 35 | "requireCommaBeforeLineBreak": true, 36 | "requireCurlyBraces": true, 37 | "requireDotNotation": true, 38 | "requireLineFeedAtFileEnd": true, 39 | "requireParenthesesAroundIIFE": true, 40 | "requireSpaceAfterBinaryOperators": true, 41 | "requireSpaceAfterKeywords": [ 42 | "catch", 43 | "do", 44 | "else", 45 | "for", 46 | "if", 47 | "return", 48 | "switch", 49 | "try", 50 | "while" 51 | ], 52 | "requireSpaceAfterLineComment": true, 53 | "requireSpaceBeforeBinaryOperators": true, 54 | "requireSpaceBeforeBlockStatements": true, 55 | "requireSpacesInAnonymousFunctionExpression": { 56 | "beforeOpeningCurlyBrace": true 57 | }, 58 | "requireSpacesInConditionalExpression": true, 59 | "requireSpacesInFunctionDeclaration": { 60 | "beforeOpeningCurlyBrace": true 61 | }, 62 | "requireSpacesInFunctionExpression": { 63 | "beforeOpeningCurlyBrace": true 64 | }, 65 | "requireSpacesInNamedFunctionExpression": { 66 | "beforeOpeningCurlyBrace": true 67 | }, 68 | "validateIndentation": 2, 69 | "validateLineBreaks": "LF", 70 | "validateParameterSeparator": ", ", 71 | "validateQuoteMarks": "'" 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-sticky-state 2 | The React Sticky[State] Component makes native position:sticky statefull and polyfills the missing sticky browser feature. 3 | 4 | Its the React version of https://github.com/soenkekluth/sticky-state 5 | 6 | todays browser do not all support the position:sticky feature (which by the way is beeing used (polyfilled) on pretty much every site you visit) - moreover the native supported feature itself comes without a readable state. something like a:hover => div:sticky to add different styles to the element in its sticky state - or to read the state if needed in javacript. 7 | 8 | unlike almost all polyfills you can find in the wild StickyState is high perfomant. the calculations are reduced to a minimum by persisting several attributes. 9 | 10 | # Warning concerning Chromes implementation of native position:sticky 11 | it looks like chromes implementaton of position:sticky is different to all other implementations out there. don't know if thats a bug - but bottom is currently not recognized by chrome. there will be a fix for this soon in sticky-state 12 | 13 | ### Browser support 14 | IE >= 9, * 15 | 16 | ### demo 17 | https://rawgit.com/soenkekluth/react-sticky-state/master/examples/index.html 18 | 19 | ### install 20 | ``` 21 | npm install react-sticky-state 22 | ``` 23 | 24 | ### css 25 | your css should contain the following lines: 26 | (you can specify the classNames in js) 27 | ```css 28 | .sticky { 29 | position: -webkit-sticky; 30 | position: sticky; 31 | } 32 | 33 | .sticky.sticky-fixed.is-sticky { 34 | margin-top: 0; 35 | margin-bottom: 0; 36 | position: fixed; 37 | -webkit-backface-visibility: hidden; 38 | -moz-backface-visibility: hidden; 39 | backface-visibility: hidden; 40 | } 41 | 42 | .sticky.sticky-fixed.is-sticky:not([style*="margin-top"]) { 43 | margin-top: 0 !important; 44 | } 45 | .sticky.sticky-fixed.is-sticky:not([style*="margin-bottom"]) { 46 | margin-bottom: 0 !important; 47 | } 48 | 49 | 50 | .sticky.sticky-fixed.is-absolute{ 51 | position: absolute; 52 | } 53 | 54 | ``` 55 | 56 | ### js 57 | ```javascript 58 | 59 | import Sticky from 'react-sticky-state'; 60 | 61 | 62 | 63 | ........ 64 | 65 | 66 | ``` 67 | 68 | Sticky either takes its only child and adds the behavior and classes to it or wrappes all children inside an element if there are more than one. the tagname can be defined by props. 69 | 70 | ### possible props 71 | 72 | ```javascript 73 | static propTypes = { 74 | initialize: PropTypes.bool, 75 | wrapperClass: PropTypes.string, 76 | stickyClass: PropTypes.string, 77 | fixedClass: PropTypes.string, 78 | stateClass: PropTypes.string, 79 | disabledClass: PropTypes.string, 80 | absoluteClass: PropTypes.string, 81 | disabled: PropTypes.bool, 82 | debug: PropTypes.bool, 83 | wrapFixedSticky: PropTypes.bool, 84 | tagName: PropTypes.string, 85 | scrollClass: PropTypes.shape({ 86 | down : PropTypes.string, 87 | up : PropTypes.string, 88 | none : PropTypes.string, 89 | persist : PropTypes.bool, 90 | active : PropTypes.bool 91 | }) 92 | }; 93 | 94 | static defaultProps = { 95 | initialize: true, 96 | wrapperClass: 'sticky-wrap', 97 | stickyClass: 'sticky', 98 | fixedClass: 'sticky-fixed', 99 | stateClass: 'is-sticky', 100 | disabledClass: 'sticky-disabled', 101 | absoluteClass: 'is-absolute', 102 | wrapFixedSticky: true, 103 | debug: false, 104 | disabled: false, 105 | tagName: 'div', 106 | scrollClass: { 107 | down: null, 108 | up: null, 109 | none: null, 110 | persist: false, 111 | active: false 112 | } 113 | }; 114 | ``` 115 | -------------------------------------------------------------------------------- /dist/featureDetect.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 8 | 9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 10 | 11 | var _canSticky = null; 12 | 13 | var Can = function () { 14 | function Can() { 15 | _classCallCheck(this, Can); 16 | } 17 | 18 | _createClass(Can, null, [{ 19 | key: 'sticky', 20 | get: function get() { 21 | if (_canSticky !== null) { 22 | return _canSticky; 23 | } 24 | 25 | if (typeof window !== 'undefined') { 26 | 27 | if (window.Modernizr && window.Modernizr.hasOwnProperty('csspositionsticky')) { 28 | return _canSticky = window.Modernizr.csspositionsticky; 29 | } 30 | 31 | var documentFragment = document.documentElement; 32 | var testEl = document.createElement('div'); 33 | documentFragment.appendChild(testEl); 34 | var prefixedSticky = ['sticky', '-webkit-sticky']; 35 | 36 | _canSticky = false; 37 | 38 | for (var i = 0; i < prefixedSticky.length; i++) { 39 | testEl.style.position = prefixedSticky[i]; 40 | _canSticky = !!window.getComputedStyle(testEl).position.match('sticky'); 41 | if (_canSticky) { 42 | break; 43 | } 44 | } 45 | documentFragment.removeChild(testEl); 46 | } 47 | return _canSticky; 48 | } 49 | }]); 50 | 51 | return Can; 52 | }(); 53 | 54 | exports.default = Can; 55 | module.exports = exports['default']; -------------------------------------------------------------------------------- /dist/react-sticky-state.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 8 | 9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 10 | 11 | var _react = require('react'); 12 | 13 | var _react2 = _interopRequireDefault(_react); 14 | 15 | var _classnames = require('classnames'); 16 | 17 | var _classnames2 = _interopRequireDefault(_classnames); 18 | 19 | var _scrollfeatures = require('scrollfeatures'); 20 | 21 | var _scrollfeatures2 = _interopRequireDefault(_scrollfeatures); 22 | 23 | var _objectAssign = require('object-assign'); 24 | 25 | var _objectAssign2 = _interopRequireDefault(_objectAssign); 26 | 27 | var _featureDetect = require('./featureDetect'); 28 | 29 | var _featureDetect2 = _interopRequireDefault(_featureDetect); 30 | 31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 32 | 33 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } 34 | 35 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 36 | 37 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 38 | 39 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } 40 | 41 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 42 | 43 | var log = function log() {}; 44 | 45 | var initialState = { 46 | initialized: false, 47 | sticky: false, 48 | absolute: false, 49 | fixedOffset: '', 50 | offsetHeight: 0, 51 | bounds: { 52 | top: null, 53 | left: null, 54 | right: null, 55 | bottom: null, 56 | height: null, 57 | width: null 58 | }, 59 | restrict: { 60 | top: null, 61 | left: null, 62 | right: null, 63 | bottom: null, 64 | height: null, 65 | width: null 66 | }, 67 | wrapperStyle: null, 68 | elementStyle: null, 69 | initialStyle: null, 70 | style: { 71 | top: null, 72 | bottom: null, 73 | left: null, 74 | right: null, 75 | 'margin-top': 0, 76 | 'margin-bottom': 0, 77 | 'margin-left': 0, 78 | 'margin-right': 0 79 | }, 80 | disabled: false 81 | }; 82 | 83 | var getAbsolutBoundingRect = function getAbsolutBoundingRect(el, fixedHeight) { 84 | var rect = el.getBoundingClientRect(); 85 | var top = rect.top + _scrollfeatures2.default.windowY; 86 | var height = fixedHeight || rect.height; 87 | return { 88 | top: top, 89 | bottom: top + height, 90 | height: height, 91 | width: rect.width, 92 | left: rect.left, 93 | right: rect.right 94 | }; 95 | }; 96 | 97 | var addBounds = function addBounds(rect1, rect2) { 98 | var rect = (0, _objectAssign2.default)({}, rect1); 99 | rect.top -= rect2.top; 100 | rect.left -= rect2.left; 101 | rect.right = rect.left + rect1.width; 102 | rect.bottom = rect.top + rect1.height; 103 | return rect; 104 | }; 105 | 106 | var getPositionStyle = function getPositionStyle(el) { 107 | 108 | var result = {}; 109 | var style = window.getComputedStyle(el, null); 110 | 111 | for (var key in initialState.style) { 112 | var value = parseInt(style.getPropertyValue(key)); 113 | value = isNaN(value) ? null : value; 114 | result[key] = value; 115 | } 116 | 117 | return result; 118 | }; 119 | 120 | var getPreviousElementSibling = function getPreviousElementSibling(el) { 121 | var prev = el.previousElementSibling; 122 | if (prev && prev.tagName.toLocaleLowerCase() === 'script') { 123 | prev = getPreviousElementSibling(prev); 124 | } 125 | return prev; 126 | }; 127 | 128 | var ReactStickyState = function (_Component) { 129 | _inherits(ReactStickyState, _Component); 130 | 131 | function ReactStickyState(props, context) { 132 | _classCallCheck(this, ReactStickyState); 133 | 134 | var _this = _possibleConstructorReturn(this, (ReactStickyState.__proto__ || Object.getPrototypeOf(ReactStickyState)).call(this, props, context)); 135 | 136 | _this._updatingBounds = false; 137 | _this._shouldComponentUpdate = true; 138 | _this._updatingState = false; 139 | 140 | _this.state = (0, _objectAssign2.default)({}, initialState, { disabled: props.disabled }); 141 | 142 | if (props.debug === true) { 143 | log = console.log.bind(console); 144 | } 145 | 146 | return _this; 147 | } 148 | 149 | _createClass(ReactStickyState, [{ 150 | key: 'getBoundingClientRect', 151 | value: function getBoundingClientRect() { 152 | return this.refs.el.getBoundingClientRect(); 153 | } 154 | }, { 155 | key: 'getBounds', 156 | value: function getBounds(noCache) { 157 | 158 | var clientRect = this.getBoundingClientRect(); 159 | var offsetHeight = _scrollfeatures2.default.documentHeight; 160 | noCache = noCache === true; 161 | 162 | if (noCache !== true && this.state.bounds.height !== null) { 163 | if (this.state.offsetHeight === offsetHeight && clientRect.height === this.state.bounds.height) { 164 | return { 165 | offsetHeight: offsetHeight, 166 | style: this.state.style, 167 | bounds: this.state.bounds, 168 | restrict: this.state.restrict 169 | }; 170 | } 171 | } 172 | 173 | // var style = noCache ? this.state.style : getPositionStyle(this.el); 174 | var initialStyle = this.state.initialStyle; 175 | if (!initialStyle) { 176 | initialStyle = getPositionStyle(this.refs.el); 177 | } 178 | 179 | var style = initialStyle; 180 | var child = this.refs.wrapper || this.refs.el; 181 | var rect; 182 | var restrict; 183 | var offsetY = 0; 184 | // var offsetX = 0; 185 | 186 | if (!_featureDetect2.default.sticky) { 187 | rect = getAbsolutBoundingRect(child, clientRect.height); 188 | if (this.hasOwnScrollTarget) { 189 | var parentRect = getAbsolutBoundingRect(this.scrollTarget); 190 | offsetY = this.scroll.y; 191 | rect = addBounds(rect, parentRect); 192 | restrict = parentRect; 193 | restrict.top = 0; 194 | restrict.height = this.scroll.scrollHeight || restrict.height; 195 | restrict.bottom = restrict.height; 196 | } 197 | } else { 198 | var elem = getPreviousElementSibling(child); 199 | offsetY = 0; 200 | 201 | if (elem) { 202 | offsetY = parseInt(window.getComputedStyle(elem)['margin-bottom']); 203 | offsetY = offsetY || 0; 204 | rect = getAbsolutBoundingRect(elem); 205 | if (this.hasOwnScrollTarget) { 206 | rect = addBounds(rect, getAbsolutBoundingRect(this.scrollTarget)); 207 | offsetY += this.scroll.y; 208 | } 209 | rect.top = rect.bottom + offsetY; 210 | } else { 211 | elem = child.parentNode; 212 | offsetY = parseInt(window.getComputedStyle(elem)['padding-top']); 213 | offsetY = offsetY || 0; 214 | rect = getAbsolutBoundingRect(elem); 215 | if (this.hasOwnScrollTarget) { 216 | rect = addBounds(rect, getAbsolutBoundingRect(this.scrollTarget)); 217 | offsetY += this.scroll.y; 218 | } 219 | rect.top = rect.top + offsetY; 220 | } 221 | if (this.hasOwnScrollTarget) { 222 | restrict = getAbsolutBoundingRect(this.scrollTarget); 223 | restrict.top = 0; 224 | restrict.height = this.scroll.scrollHeight || restrict.height; 225 | restrict.bottom = restrict.height; 226 | } 227 | 228 | rect.height = child.clientHeight; 229 | rect.width = child.clientWidth; 230 | rect.bottom = rect.top + rect.height; 231 | } 232 | 233 | restrict = restrict || getAbsolutBoundingRect(child.parentNode); 234 | 235 | return { 236 | offsetHeight: offsetHeight, 237 | style: style, 238 | bounds: rect, 239 | initialStyle: initialStyle, 240 | restrict: restrict 241 | }; 242 | } 243 | }, { 244 | key: 'updateBounds', 245 | value: function updateBounds(silent, noCache, cb) { 246 | var _this2 = this; 247 | 248 | noCache = noCache === true; 249 | this._shouldComponentUpdate = silent !== true; 250 | 251 | this.setState(this.getBounds(noCache), function () { 252 | _this2._shouldComponentUpdate = true; 253 | if (cb) { 254 | cb(); 255 | } 256 | }); 257 | } 258 | 259 | // updateFixedOffset() { 260 | // if (this.hasOwnScrollTarget && !Can.sticky) { 261 | 262 | // if (this.state.sticky) { 263 | // this.setState({ fixedOffset: this.scrollTarget.getBoundingClientRect().top + 'px' }); 264 | // if (!this.hasWindowScrollListener) { 265 | // this.hasWindowScrollListener = true; 266 | // ScrollFeatures.getInstance(window).on('scroll:progress', this.updateFixedOffset); 267 | // } 268 | // } else { 269 | // this.setState({ fixedOffset: '' }); 270 | // if (this.hasWindowScrollListener) { 271 | // this.hasWindowScrollListener = false; 272 | // ScrollFeatures.getInstance(window).off('scroll:progress', this.updateFixedOffset); 273 | // } 274 | // } 275 | // } 276 | // } 277 | 278 | 279 | }, { 280 | key: 'updateFixedOffset', 281 | value: function updateFixedOffset() { 282 | 283 | var fixedOffset = this.state.fixedOffset; 284 | if (this.state.sticky) { 285 | this.setState({ fixedOffset: this.scrollTarget.getBoundingClientRect().top + 'px;' }); 286 | } else { 287 | this.setState({ fixedOffset: '' }); 288 | } 289 | // if (fixedOffset !== this.state.fixedOffset) { 290 | // this.render(); 291 | // } 292 | } 293 | }, { 294 | key: 'addSrollHandler', 295 | value: function addSrollHandler() { 296 | if (!this.scroll) { 297 | var hasScrollTarget = _scrollfeatures2.default.hasInstance(this.scrollTarget); 298 | this.scroll = _scrollfeatures2.default.getInstance(this.scrollTarget); 299 | this.onScroll = this.onScroll.bind(this); 300 | this.scroll.on('scroll:start', this.onScroll); 301 | this.scroll.on('scroll:progress', this.onScroll); 302 | this.scroll.on('scroll:stop', this.onScroll); 303 | 304 | if (this.props.scrollClass.active) { 305 | this.onScrollDirection = this.onScrollDirection.bind(this); 306 | this.scroll.on('scroll:up', this.onScrollDirection); 307 | this.scroll.on('scroll:down', this.onScrollDirection); 308 | if (!this.props.scrollClass.persist) { 309 | this.scroll.on('scroll:stop', this.onScrollDirection); 310 | } 311 | } 312 | if (hasScrollTarget && this.scroll.scrollY > 0) { 313 | this.scroll.trigger('scroll:progress'); 314 | } 315 | } 316 | } 317 | }, { 318 | key: 'removeSrollHandler', 319 | value: function removeSrollHandler() { 320 | if (this.scroll) { 321 | this.scroll.off('scroll:start', this.onScroll); 322 | this.scroll.off('scroll:progress', this.onScroll); 323 | this.scroll.off('scroll:stop', this.onScroll); 324 | if (this.props.scrollClass.active) { 325 | this.scroll.off('scroll:up', this.onScrollDirection); 326 | this.scroll.off('scroll:down', this.onScrollDirection); 327 | this.scroll.off('scroll:stop', this.onScrollDirection); 328 | } 329 | if (!this.scroll.hasListeners()) { 330 | this.scroll.destroy(); 331 | } 332 | this.onScroll = null; 333 | this.onScrollDirection = null; 334 | this.scroll = null; 335 | } 336 | } 337 | }, { 338 | key: 'addResizeHandler', 339 | value: function addResizeHandler() { 340 | if (!this.onResize) { 341 | this.onResize = this.update.bind(this); 342 | window.addEventListener('sticky:update', this.onResize, false); 343 | window.addEventListener('resize', this.onResize, false); 344 | window.addEventListener('orientationchange', this.onResize, false); 345 | } 346 | } 347 | }, { 348 | key: 'removeResizeHandler', 349 | value: function removeResizeHandler() { 350 | if (this.onResize) { 351 | window.removeEventListener('sticky:update', this.onResize); 352 | window.removeEventListener('resize', this.onResize); 353 | window.removeEventListener('orientationchange', this.onResize); 354 | this.onResize = null; 355 | } 356 | } 357 | }, { 358 | key: 'destroy', 359 | value: function destroy() { 360 | this._updatingBounds = false; 361 | this._shouldComponentUpdate = false; 362 | this._updatingState = false; 363 | this.removeSrollHandler(); 364 | this.removeResizeHandler(); 365 | this.scrollTarget = null; 366 | } 367 | }, { 368 | key: 'getScrollClasses', 369 | value: function getScrollClasses(obj) { 370 | if (this.options.scrollClass.active) { 371 | obj = obj || {}; 372 | var direction = this.scroll.y <= 0 || this.scroll.y + this.scroll.clientHeight >= this.scroll.scrollHeight ? 0 : this.scroll.directionY; 373 | obj[this.options.scrollClass.up] = direction < 0; 374 | obj[this.options.scrollClass.down] = direction > 0; 375 | } 376 | return obj; 377 | } 378 | }, { 379 | key: 'getScrollClass', 380 | value: function getScrollClass() { 381 | if (this.props.scrollClass.up || this.props.scrollClass.down) { 382 | 383 | var direction = this.scroll.y <= 0 || this.scroll.y + this.scroll.clientHeight >= this.scroll.scrollHeight ? 0 : this.scroll.directionY; 384 | var scrollClass = direction < 0 ? this.props.scrollClass.up : this.props.scrollClass.down; 385 | scrollClass = direction === 0 ? null : scrollClass; 386 | return scrollClass; 387 | } 388 | return null; 389 | } 390 | }, { 391 | key: 'onScrollDirection', 392 | value: function onScrollDirection(e) { 393 | if (this.state.sticky || e && e.type === _scrollfeatures2.default.events.SCROLL_STOP) { 394 | this.setState({ 395 | scrollClass: this.getScrollClass() 396 | }); 397 | } 398 | } 399 | }, { 400 | key: 'onScroll', 401 | value: function onScroll(e) { 402 | this.updateStickyState(false); 403 | if (this.hasOwnScrollTarget && !_featureDetect2.default.sticky) { 404 | this.updateFixedOffset(); 405 | if (this.state.sticky && !this.hasWindowScrollListener) { 406 | this.hasWindowScrollListener = true; 407 | _scrollfeatures2.default.getInstance(window).on('scroll:progress', this.updateFixedOffset); 408 | } else if (!this.state.sticky && this.hasWindowScrollListener) { 409 | this.hasWindowScrollListener = false; 410 | _scrollfeatures2.default.getInstance(window).off('scroll:progress', this.updateFixedOffset); 411 | } 412 | } 413 | } 414 | }, { 415 | key: 'update', 416 | value: function update() { 417 | var _this3 = this; 418 | 419 | // this.scroll.updateScrollPosition(); 420 | this.updateBounds(true, true, function () { 421 | _this3.updateStickyState(false); 422 | }); 423 | } 424 | 425 | // update(force = false) { 426 | 427 | // if (!this._updatingBounds) { 428 | // this._updatingBounds = true; 429 | // this.scroll.updateScrollPosition(); 430 | // this.updateBounds(true, true, () => { 431 | // this.updateBounds(force, true, () => { 432 | // this.scroll.updateScrollPosition(); 433 | // var updateSticky = this.updateStickyState(false, () => { 434 | // if (force && !updateSticky) { 435 | // this.forceUpdate(); 436 | // } 437 | // }); 438 | // this._updatingBounds = false; 439 | // }); 440 | // }); 441 | // } 442 | // } 443 | 444 | 445 | }, { 446 | key: 'getStickyState', 447 | value: function getStickyState() { 448 | 449 | if (this.state.disabled) { 450 | return { sticky: false, absolute: false }; 451 | } 452 | 453 | var scrollY = this.scroll.y; 454 | // var scrollX = this.scroll.x; 455 | var top = this.state.style.top; 456 | var bottom = this.state.style.bottom; 457 | // var left = this.state.style.left; 458 | // var right = this.state.style.right; 459 | var sticky = this.state.sticky; 460 | var absolute = this.state.absolute; 461 | 462 | if (top !== null) { 463 | var offsetBottom = this.state.restrict.bottom - this.state.bounds.height - top; 464 | top = this.state.bounds.top - top; 465 | 466 | if (this.state.sticky === false && (scrollY >= top && scrollY <= offsetBottom || top <= 0 && scrollY < top)) { 467 | sticky = true; 468 | absolute = false; 469 | } else if (this.state.sticky && (top > 0 && scrollY < top || scrollY > offsetBottom)) { 470 | sticky = false; 471 | absolute = scrollY > offsetBottom; 472 | } 473 | } else if (bottom !== null) { 474 | 475 | scrollY += window.innerHeight; 476 | var offsetTop = this.state.restrict.top + this.state.bounds.height - bottom; 477 | bottom = this.state.bounds.bottom + bottom; 478 | 479 | if (this.state.sticky === false && scrollY <= bottom && scrollY >= offsetTop) { 480 | sticky = true; 481 | absolute = false; 482 | } else if (this.state.sticky && (scrollY > bottom || scrollY < offsetTop)) { 483 | sticky = false; 484 | absolute = scrollY <= offsetTop; 485 | } 486 | } 487 | return { sticky: sticky, absolute: absolute }; 488 | } 489 | }, { 490 | key: 'updateStickyState', 491 | value: function updateStickyState(silent) { 492 | var _this4 = this; 493 | 494 | var values = this.getStickyState(); 495 | 496 | if (values.sticky !== this.state.sticky || values.absolute !== this.state.absolute) { 497 | this._shouldComponentUpdate = silent !== true; 498 | values = (0, _objectAssign2.default)(values, this.getBounds(false)); 499 | this._updatingState = true; 500 | this.setState(values, function () { 501 | _this4._shouldComponentUpdate = true; 502 | _this4._updatingState = false; 503 | }); 504 | } 505 | } 506 | 507 | // updateStickyState(bounds = true, cb) { 508 | // if (this._updatingState) { 509 | // return; 510 | // } 511 | // var values = this.getStickyState(); 512 | 513 | // if (values.sticky !== this.state.sticky || values.absolute !== this.state.absolute) { 514 | // this._updatingState = true; 515 | // if (bounds) { 516 | // values = assign(values, this.getBounds(), { scrollClass: this.getScrollClass() }); 517 | // } 518 | // this.setState(values, () => { 519 | // this._updatingState = false; 520 | // if (typeof cb === 'function') { 521 | // cb(); 522 | // } 523 | // }); 524 | // return true; 525 | // } else if (typeof cb === 'function') { 526 | // cb(); 527 | // } 528 | // return false; 529 | // } 530 | 531 | 532 | }, { 533 | key: 'initialize', 534 | value: function initialize() { 535 | var _this5 = this; 536 | 537 | if (!this.state.initialized && !this.state.disabled) { 538 | this.setState({ 539 | initialized: true 540 | }, function () { 541 | var child = _this5.refs.wrapper || _this5.refs.el; 542 | _this5.scrollTarget = _scrollfeatures2.default.getScrollParent(child); 543 | _this5.hasOwnScrollTarget = _this5.scrollTarget !== window; 544 | if (_this5.hasOwnScrollTarget) { 545 | _this5.updateFixedOffset = _this5.updateFixedOffset.bind(_this5); 546 | } 547 | 548 | _this5.addSrollHandler(); 549 | _this5.addResizeHandler(); 550 | _this5.update(); 551 | }); 552 | } 553 | } 554 | }, { 555 | key: 'shouldComponentUpdate', 556 | value: function shouldComponentUpdate(newProps, newState) { 557 | return this._shouldComponentUpdate; 558 | } 559 | }, { 560 | key: 'componentWillReceiveProps', 561 | value: function componentWillReceiveProps(nextProps) { 562 | var _this6 = this; 563 | 564 | var intialize = !this.state.initialized && nextProps.initialize; 565 | 566 | if (nextProps.disabled !== this.state.disabled) { 567 | this.setState({ 568 | disabled: nextProps.disabled 569 | }, function () { 570 | if (intialize) { 571 | _this6.initialize(); 572 | } 573 | }); 574 | } 575 | } 576 | }, { 577 | key: 'componentDidMount', 578 | value: function componentDidMount() { 579 | if (!this.state.initialized && this.props.initialize) { 580 | this.initialize(); 581 | } 582 | } 583 | }, { 584 | key: 'componentWillUnmount', 585 | value: function componentWillUnmount() { 586 | this.destroy(); 587 | } 588 | }, { 589 | key: 'render', 590 | value: function render() { 591 | var _classNames; 592 | 593 | if (!this.state.initialized) { 594 | return this.props.children; 595 | } 596 | 597 | var element = _react2.default.Children.only(this.props.children); 598 | 599 | var _props = this.props, 600 | wrapperClass = _props.wrapperClass, 601 | stickyClass = _props.stickyClass, 602 | fixedClass = _props.fixedClass, 603 | stateClass = _props.stateClass, 604 | disabledClass = _props.disabledClass, 605 | absoluteClass = _props.absoluteClass, 606 | disabled = _props.disabled, 607 | debug = _props.debug, 608 | tagName = _props.tagName, 609 | props = _objectWithoutProperties(_props, ['wrapperClass', 'stickyClass', 'fixedClass', 'stateClass', 'disabledClass', 'absoluteClass', 'disabled', 'debug', 'tagName']); 610 | 611 | var style; 612 | var refName = 'el'; 613 | var className = (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, stickyClass, !this.state.disabled), _defineProperty(_classNames, disabledClass, this.state.disabled), _classNames), _defineProperty({}, fixedClass, !_featureDetect2.default.sticky), _defineProperty({}, stateClass, this.state.sticky && !this.state.disabled), _defineProperty({}, absoluteClass, this.state.absolute), this.state.scrollClass); 614 | 615 | if (!_featureDetect2.default.sticky) { 616 | if (this.state.absolute) { 617 | 618 | style = { 619 | marginTop: this.state.style.top !== null ? this.state.restrict.height - (this.state.bounds.height + this.state.style.top) + (this.state.restrict.top - this.state.bounds.top) + 'px' : '', 620 | marginBottom: this.state.style.bottom !== null ? this.state.restrict.height - (this.state.bounds.height + this.state.style.bottom) + (this.state.restrict.bottom - this.state.bounds.bottom) + 'px' : '' 621 | }; 622 | } else if (this.hasOwnScrollTarget && this.state.fixedOffset !== '') { 623 | style = { 624 | marginTop: this.state.fixedOffset 625 | }; 626 | } 627 | } 628 | 629 | if (element) { 630 | element = _react2.default.cloneElement(element, { ref: refName, style: style, className: (0, _classnames2.default)(element.props.className, className) }); 631 | } else { 632 | var Comp = this.props.tagName; 633 | element = _react2.default.createElement( 634 | Comp, 635 | _extends({ ref: refName, style: style, className: className }, props), 636 | this.props.children 637 | ); 638 | } 639 | 640 | if (_featureDetect2.default.sticky) { 641 | return element; 642 | } 643 | 644 | var height = this.state.disabled || this.state.bounds.height === null /*|| (!this.state.sticky && !this.state.absolute)*/ ? 'auto' : this.state.bounds.height + 'px'; 645 | var marginTop = height === 'auto' ? '' : this.state.style['margin-top'] ? this.state.style['margin-top'] + 'px' : ''; 646 | var marginBottom = height === 'auto' ? '' : this.state.style['margin-bottom'] ? this.state.style['margin-bottom'] + 'px' : ''; 647 | 648 | style = { 649 | height: height, 650 | marginTop: marginTop, 651 | marginBottom: marginBottom 652 | }; 653 | if (this.state.absolute) { 654 | style.position = 'relative'; 655 | } 656 | return _react2.default.createElement( 657 | 'div', 658 | { ref: 'wrapper', className: wrapperClass, style: style }, 659 | element 660 | ); 661 | } 662 | }]); 663 | 664 | return ReactStickyState; 665 | }(_react.Component); 666 | 667 | ReactStickyState.propTypes = { 668 | initialize: _react.PropTypes.bool, 669 | wrapperClass: _react.PropTypes.string, 670 | stickyClass: _react.PropTypes.string, 671 | fixedClass: _react.PropTypes.string, 672 | stateClass: _react.PropTypes.string, 673 | disabledClass: _react.PropTypes.string, 674 | absoluteClass: _react.PropTypes.string, 675 | disabled: _react.PropTypes.bool, 676 | debug: _react.PropTypes.bool, 677 | wrapFixedSticky: _react.PropTypes.bool, 678 | tagName: _react.PropTypes.string, 679 | scrollClass: _react.PropTypes.shape({ 680 | down: _react.PropTypes.string, 681 | up: _react.PropTypes.string, 682 | none: _react.PropTypes.string, 683 | persist: _react.PropTypes.bool, 684 | active: _react.PropTypes.bool 685 | }) 686 | }; 687 | ReactStickyState.defaultProps = { 688 | initialize: true, 689 | wrapperClass: 'sticky-wrap', 690 | stickyClass: 'sticky', 691 | fixedClass: 'sticky-fixed', 692 | stateClass: 'is-sticky', 693 | disabledClass: 'sticky-disabled', 694 | absoluteClass: 'is-absolute', 695 | wrapFixedSticky: true, 696 | debug: false, 697 | disabled: false, 698 | tagName: 'div', 699 | scrollClass: { 700 | down: null, 701 | up: null, 702 | none: null, 703 | persist: false, 704 | active: false 705 | } 706 | }; 707 | exports.default = ReactStickyState; 708 | module.exports = exports['default']; -------------------------------------------------------------------------------- /dist/sticky-state.css: -------------------------------------------------------------------------------- 1 | body::before { 2 | content: ''; 3 | position: fixed; 4 | } 5 | 6 | .sticky { 7 | position: -webkit-sticky; 8 | position: sticky; 9 | } 10 | 11 | .sticky.sticky-fixed.is-sticky { 12 | margin-top: 0; 13 | margin-bottom: 0; 14 | position: fixed; 15 | -webkit-backface-visibility: hidden; 16 | -moz-backface-visibility: hidden; 17 | backface-visibility: hidden; 18 | } 19 | 20 | .sticky.sticky-fixed.is-sticky:not([style*="margin-top"]) { 21 | margin-top: 0 !important; 22 | } 23 | .sticky.sticky-fixed.is-sticky:not([style*="margin-bottom"]) { 24 | margin-bottom: 0 !important; 25 | } 26 | 27 | 28 | .sticky.sticky-fixed.is-absolute{ 29 | position: absolute; 30 | } 31 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | node_modules 5 | 6 | # testing 7 | coverage 8 | 9 | # production 10 | build 11 | 12 | # misc 13 | .DS_Store 14 | .env 15 | npm-debug.log 16 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app). 2 | 3 | Below you will find some information on how to perform common tasks.
4 | You can find the most recent version of this guide [here](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md). 5 | 6 | ## Table of Contents 7 | 8 | - [Updating to New Releases](#updating-to-new-releases) 9 | - [Sending Feedback](#sending-feedback) 10 | - [Folder Structure](#folder-structure) 11 | - [Available Scripts](#available-scripts) 12 | - [npm start](#npm-start) 13 | - [npm test](#npm-test) 14 | - [npm run build](#npm-run-build) 15 | - [npm run eject](#npm-run-eject) 16 | - [Displaying Lint Output in the Editor](#displaying-lint-output-in-the-editor) 17 | - [Installing a Dependency](#installing-a-dependency) 18 | - [Importing a Component](#importing-a-component) 19 | - [Adding a Stylesheet](#adding-a-stylesheet) 20 | - [Post-Processing CSS](#post-processing-css) 21 | - [Adding Images and Fonts](#adding-images-and-fonts) 22 | - [Using the `public` Folder](#using-the-public-folder) 23 | - [Adding Bootstrap](#adding-bootstrap) 24 | - [Adding Flow](#adding-flow) 25 | - [Adding Custom Environment Variables](#adding-custom-environment-variables) 26 | - [Can I Use Decorators?](#can-i-use-decorators) 27 | - [Integrating with a Node Backend](#integrating-with-a-node-backend) 28 | - [Proxying API Requests in Development](#proxying-api-requests-in-development) 29 | - [Using HTTPS in Development](#using-https-in-development) 30 | - [Generating Dynamic `` Tags on the Server](#generating-dynamic-meta-tags-on-the-server) 31 | - [Running Tests](#running-tests) 32 | - [Filename Conventions](#filename-conventions) 33 | - [Command Line Interface](#command-line-interface) 34 | - [Version Control Integration](#version-control-integration) 35 | - [Writing Tests](#writing-tests) 36 | - [Testing Components](#testing-components) 37 | - [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries) 38 | - [Initializing Test Environment](#initializing-test-environment) 39 | - [Focusing and Excluding Tests](#focusing-and-excluding-tests) 40 | - [Coverage Reporting](#coverage-reporting) 41 | - [Continuous Integration](#continuous-integration) 42 | - [Disabling jsdom](#disabling-jsdom) 43 | - [Experimental Snapshot Testing](#experimental-snapshot-testing) 44 | - [Deployment](#deployment) 45 | - [Building for Relative Paths](#building-for-relative-paths) 46 | - [GitHub Pages](#github-pages) 47 | - [Heroku](#heroku) 48 | - [Modulus](#modulus) 49 | - [Netlify](#netlify) 50 | - [Now](#now) 51 | - [Surge](#surge) 52 | - [Something Missing?](#something-missing) 53 | 54 | ## Updating to New Releases 55 | 56 | Create React App is divided into two packages: 57 | 58 | * `create-react-app` is a global command-line utility that you use to create new projects. 59 | * `react-scripts` is a development dependency in the generated projects (including this one). 60 | 61 | You almost never need to update `create-react-app` itself: it delegates all the setup to `react-scripts`. 62 | 63 | When you run `create-react-app`, it always creates the project with the latest version of `react-scripts` so you’ll get all the new features and improvements in newly created apps automatically. 64 | 65 | To update an existing project to a new version of `react-scripts`, [open the changelog](https://github.com/facebookincubator/create-react-app/blob/master/CHANGELOG.md), find the version you’re currently on (check `package.json` in this folder if you’re not sure), and apply the migration instructions for the newer versions. 66 | 67 | In most cases bumping the `react-scripts` version in `package.json` and running `npm install` in this folder should be enough, but it’s good to consult the [changelog](https://github.com/facebookincubator/create-react-app/blob/master/CHANGELOG.md) for potential breaking changes. 68 | 69 | We commit to keeping the breaking changes minimal so you can upgrade `react-scripts` painlessly. 70 | 71 | ## Sending Feedback 72 | 73 | We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues). 74 | 75 | ## Folder Structure 76 | 77 | After creation, your project should look like this: 78 | 79 | ``` 80 | my-app/ 81 | README.md 82 | node_modules/ 83 | package.json 84 | public/ 85 | index.html 86 | favicon.ico 87 | src/ 88 | App.css 89 | App.js 90 | App.test.js 91 | index.css 92 | index.js 93 | logo.svg 94 | ``` 95 | 96 | For the project to build, **these files must exist with exact filenames**: 97 | 98 | * `public/index.html` is the page template; 99 | * `src/index.js` is the JavaScript entry point. 100 | 101 | You can delete or rename the other files. 102 | 103 | You may create subdirectories inside `src`. For faster rebuilds, only files inside `src` are processed by Webpack.
104 | You need to **put any JS and CSS files inside `src`**, or Webpack won’t see them. 105 | 106 | Only files inside `public` can be used from `public/index.html`.
107 | Read instructions below for using assets from JavaScript and HTML. 108 | 109 | You can, however, create more top-level directories.
110 | They will not be included in the production build so you can use them for things like documentation. 111 | 112 | ## Available Scripts 113 | 114 | In the project directory, you can run: 115 | 116 | ### `npm start` 117 | 118 | Runs the app in the development mode.
119 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 120 | 121 | The page will reload if you make edits.
122 | You will also see any lint errors in the console. 123 | 124 | ### `npm test` 125 | 126 | Launches the test runner in the interactive watch mode.
127 | See the section about [running tests](#running-tests) for more information. 128 | 129 | ### `npm run build` 130 | 131 | Builds the app for production to the `build` folder.
132 | It correctly bundles React in production mode and optimizes the build for the best performance. 133 | 134 | The build is minified and the filenames include the hashes.
135 | Your app is ready to be deployed! 136 | 137 | ### `npm run eject` 138 | 139 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 140 | 141 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 142 | 143 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 144 | 145 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 146 | 147 | ## Displaying Lint Output in the Editor 148 | 149 | >Note: this feature is available with `react-scripts@0.2.0` and higher. 150 | 151 | Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint. 152 | 153 | They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do. 154 | 155 | You would need to install an ESLint plugin for your editor first. 156 | 157 | >**A note for Atom `linter-eslint` users** 158 | 159 | >If you are using the Atom `linter-eslint` plugin, make sure that **Use global ESLint installation** option is checked: 160 | 161 | > 162 | 163 | Then add this block to the `package.json` file of your project: 164 | 165 | ```js 166 | { 167 | // ... 168 | "eslintConfig": { 169 | "extends": "react-app" 170 | } 171 | } 172 | ``` 173 | 174 | Finally, you will need to install some packages *globally*: 175 | 176 | ```sh 177 | npm install -g eslint-config-react-app@0.3.0 eslint@3.8.1 babel-eslint@7.0.0 eslint-plugin-react@6.4.1 eslint-plugin-import@2.0.1 eslint-plugin-jsx-a11y@2.2.3 eslint-plugin-flowtype@2.21.0 178 | ``` 179 | 180 | We recognize that this is suboptimal, but it is currently required due to the way we hide the ESLint dependency. The ESLint team is already [working on a solution to this](https://github.com/eslint/eslint/issues/3458) so this may become unnecessary in a couple of months. 181 | 182 | ## Installing a Dependency 183 | 184 | The generated project includes React and ReactDOM as dependencies. It also includes a set of scripts used by Create React App as a development dependency. You may install other dependencies (for example, React Router) with `npm`: 185 | 186 | ``` 187 | npm install --save 188 | ``` 189 | 190 | ## Importing a Component 191 | 192 | This project setup supports ES6 modules thanks to Babel.
193 | While you can still use `require()` and `module.exports`, we encourage you to use [`import` and `export`](http://exploringjs.com/es6/ch_modules.html) instead. 194 | 195 | For example: 196 | 197 | ### `Button.js` 198 | 199 | ```js 200 | import React, { Component } from 'react'; 201 | 202 | class Button extends Component { 203 | render() { 204 | // ... 205 | } 206 | } 207 | 208 | export default Button; // Don’t forget to use export default! 209 | ``` 210 | 211 | ### `DangerButton.js` 212 | 213 | 214 | ```js 215 | import React, { Component } from 'react'; 216 | import Button from './Button'; // Import a component from another file 217 | 218 | class DangerButton extends Component { 219 | render() { 220 | return