Skip to content

Commit

Permalink
Optional onClick property available (resolves #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikbulaj committed Jan 14, 2020
1 parent 23bec58 commit 3c8a67e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Then use it
| style | Object | {} | CSS styles passed to element <br>**NOTE** prior v1.4.0 default value was `{display: 'inline'}`
| alignToTop | Boolean | false | Whether top of the element should be aligned to the top of the visible area. Default: aligns to bottom of element
| className | String | | Optional class name to be applied to element
| onClick| Function | | Callback fired on click

## Changelog
Please check [releases](https://github.com/dominikbulaj/react-scroll-into-view/releases) tab for full details
Expand Down
22 changes: 3 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'

const ScrollInto = ({ children, selector, smooth = true, style = {}, alignToTop = false, className = '', onClick }) => {
const ScrollInto = ({ children, selector, smooth = true, style = {}, alignToTop = false, className = '' }) => {

const scrollIntoView = _ => {
const behavior = smooth ? 'smooth' : 'instant'
Expand All @@ -16,22 +16,7 @@ const ScrollInto = ({ children, selector, smooth = true, style = {}, alignToTop
el.scrollIntoView(options)
}

/**
* Click event handler
* When provided (optional) `onClick` property which is a function it will call it and wait 16ms (single frame) to use
* `scrollIntoView` as e.g. MaterialUI Menu changes `body` element style to `overflow: hidden` blocking
* `scrollIntoView`
*/
const handleClick = event => {
if(typeof onClick ==='function') {
onClick(event)
setTimeout(scrollIntoView, 1e3/60)
} else {
scrollIntoView()
}
}

return <div style={style} className={className} onClick={handleClick}>
return <div style={style} className={className} onClick={scrollIntoView}>
{children}
</div>
}
Expand All @@ -41,8 +26,7 @@ ScrollInto.propTypes = {
smooth: PropTypes.bool,
style: PropTypes.object,
alignToTop: PropTypes.bool,
className: PropTypes.string,
onClick: PropTypes.func,
className: PropTypes.string
}

export default ScrollInto

0 comments on commit 3c8a67e

Please sign in to comment.