From ef04819751f0a697a0a2776868b962ba618b2c90 Mon Sep 17 00:00:00 2001 From: makotot Date: Sun, 11 Jun 2017 11:11:28 +0900 Subject: [PATCH] add offEvent & onEvent --- README.md | 12 +++++++++++- src/js/lib/Scrollspy.jsx | 18 +++++++++++++----- webpack.config.babel.js | 5 +++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ebebaa0..e3b69c6 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ HTML tag for Scrollspy component if you want to use other than `ul` [optional]. ### `style={ Object }` -Style attribute to be passed to the generated <ul/> element [optional]. +Style attribute to be passed to the generated <ul/> element [optional]. ### `offset={ Number }` @@ -72,6 +72,16 @@ Offset value that adjusts to determine the elements are in the viewport [optiona Name of the element of scrollable container that can be used with querySelector [optional]. +## Methods + +### `offEvent` + +Remove event listener of scrollspy. + +### `onEvent` + +Add event listener of scrollspy. + ## Development diff --git a/src/js/lib/Scrollspy.jsx b/src/js/lib/Scrollspy.jsx index 6840c6e..e022f47 100644 --- a/src/js/lib/Scrollspy.jsx +++ b/src/js/lib/Scrollspy.jsx @@ -199,17 +199,25 @@ export default class Scrollspy extends React.Component { this._spy(targetItems) } - componentDidMount () { - this._initFromProps() + offEvent() { const el = this.props.rootEl ? document.querySelector(this.props.rootEl) : window - el.addEventListener('scroll', this._handleSpy) + el.removeEventListener('scroll', this._handleSpy) } - componentWillUnmount () { + onEvent() { const el = this.props.rootEl ? document.querySelector(this.props.rootEl) : window - el.removeEventListener('scroll', this._handleSpy) + el.addEventListener('scroll', this._handleSpy) + } + + componentDidMount () { + this._initFromProps() + this.onEvent() + } + + componentWillUnmount () { + this.offEvent() } componentWillReceiveProps () { diff --git a/webpack.config.babel.js b/webpack.config.babel.js index db47121..c2ae5c3 100644 --- a/webpack.config.babel.js +++ b/webpack.config.babel.js @@ -1,6 +1,6 @@ import webpack from 'webpack' import path from 'path' -import HtmlWebpackPlguin from 'html-webpack-plugin'; +import HtmlWebpackPlguin from 'html-webpack-plugin' export default { entry: { @@ -14,7 +14,7 @@ export default { output: { path: path.join(__dirname, 'dist'), filename: 'js/[name].js', - publicPath: 'http://localhost:8080', + publicPath: '/', }, resolve: { modules: [ @@ -57,6 +57,7 @@ export default { }, devServer: { contentBase: './dist', + publicPath: '/', port: 8080, }, plugins: [