Skip to content
This repository has been archived by the owner on Aug 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #67 from makotot/offevent
Browse files Browse the repository at this point in the history
add offEvent & onEvent
  • Loading branch information
makotot authored Jun 11, 2017
2 parents e20459a + ef04819 commit a4aa83b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`

Expand All @@ -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

Expand Down
18 changes: 13 additions & 5 deletions src/js/lib/Scrollspy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
5 changes: 3 additions & 2 deletions webpack.config.babel.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -14,7 +14,7 @@ export default {
output: {
path: path.join(__dirname, 'dist'),
filename: 'js/[name].js',
publicPath: 'http://localhost:8080',
publicPath: '/',
},
resolve: {
modules: [
Expand Down Expand Up @@ -57,6 +57,7 @@ export default {
},
devServer: {
contentBase: './dist',
publicPath: '/',
port: 8080,
},
plugins: [
Expand Down

0 comments on commit a4aa83b

Please sign in to comment.