-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combined event wrappers; got controlled component calling back to con…
…sumer when it would normally close
- Loading branch information
Showing
8 changed files
with
455 additions
and
561 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React, { Component } from 'react' | ||
import { element, func } from 'prop-types' | ||
import defaults from './defaults' | ||
|
||
export default class EventsWrapper extends Component { | ||
constructor() { | ||
super() | ||
this.unzoom = this.unzoom.bind(this) | ||
this._handleScroll = this._handleScroll.bind(this) | ||
this._handleKeyUp = this._handleKeyUp.bind(this) | ||
this._handleResize = this._handleResize.bind(this) | ||
this._handleTouchStart = this._handleTouchStart.bind(this) | ||
this._handleTouchMove = this._handleTouchMove.bind(this) | ||
this._handleTouchEnd = this._handleTouchEnd.bind(this) | ||
} | ||
|
||
componentDidMount() { | ||
this.pageYOffset = window.pageYOffset | ||
setTimeout(() => { | ||
window.addEventListener('scroll', this._handleScroll, true) | ||
window.addEventListener('keyup', this._handleKeyUp) | ||
window.addEventListener('ontouchstart', this._handleTouchStart) | ||
window.addEventListener('ontouchmove', this._handleTouchMove) | ||
window.addEventListener('ontouchend', this._handleTouchEnd) | ||
window.addEventListener('ontouchcancel', this._handleTouchEnd) | ||
window.addEventListener('resize', this._handleResize) | ||
}, defaults.transitionDuration) | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('scroll', this._handleScroll, true) | ||
window.removeEventListener('keyup', this._handleKeyUp) | ||
window.removeEventListener('ontouchstart', this._handleTouchStart) | ||
window.removeEventListener('ontouchmove', this._handleTouchMove) | ||
window.removeEventListener('ontouchend', this._handleTouchEnd) | ||
window.removeEventListener('ontouchcancel', this._handleTouchEnd) | ||
window.removeEventListener('resize', this._handleResize) | ||
} | ||
|
||
render() { | ||
return ( | ||
<div onClick={this.unzoom}> | ||
{this._cloneChild()} | ||
</div> | ||
) | ||
} | ||
|
||
unzoom({ force=false }={}) { | ||
if (this.props.controlledEventFn && !force) { | ||
return this.props.controlledEventFn() | ||
} | ||
|
||
return this.refs.child.unzoom() | ||
} | ||
|
||
_cloneChild() { | ||
return React.cloneElement( | ||
React.Children.only(this.props.children), | ||
{ ref: 'child' } | ||
) | ||
} | ||
|
||
_handleKeyUp({ which }) { | ||
const opts = { | ||
27: this.unzoom | ||
} | ||
|
||
if (opts[which]) { | ||
return opts[which]() | ||
} | ||
} | ||
|
||
_handleResize() { | ||
this.forceUpdate() | ||
} | ||
|
||
_handleScroll(e) { | ||
this.forceUpdate() | ||
const scrollChange = Math.abs(window.pageYOffset - this.pageYOffset) | ||
if (scrollChange > 10) { | ||
this.unzoom() | ||
} | ||
} | ||
|
||
_handleTouchStart(e) { | ||
this.yTouchPosition = e.touches[0].clientY | ||
} | ||
|
||
_handleTouchMove(e) { | ||
this.forceUpdate() | ||
const touchChange = Math.abs(e.touches[0].clientY - this.yTouchPosition) | ||
if (touchChange > 10) { | ||
this.unzoom() | ||
} | ||
} | ||
|
||
_handleTouchEnd(e) { | ||
delete this.yTouchPosition | ||
} | ||
} | ||
|
||
EventsWrapper.propTypes = { | ||
children: element.isRequired, | ||
getControlledEventFn: func | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters