-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #251, New ScrollPanel component
- Loading branch information
Çağatay Çivici
committed
Dec 12, 2017
1 parent
eff3b79
commit b9ec8f5
Showing
5 changed files
with
479 additions
and
1 deletion.
There are no files selected for viewing
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,46 @@ | ||
.ui-scrollpanel-wrapper { | ||
overflow: hidden; | ||
width: 100%; | ||
height: 100%; | ||
position: relative; | ||
z-index: 1; | ||
float: left; | ||
} | ||
|
||
.ui-scrollpanel-content { | ||
height: 100%; | ||
width: calc(100% + 18px); | ||
padding: 0 0 0 0; | ||
position: relative; | ||
overflow: auto; | ||
box-sizing: border-box; | ||
} | ||
|
||
.ui-scrollpanel-bar { | ||
position: relative; | ||
background: #c1c1c1; | ||
width: 9px; | ||
border-radius: 3px; | ||
top: 0; | ||
z-index: 2; | ||
cursor: pointer; | ||
opacity: 0; | ||
transition: opacity 0.25s linear; | ||
} | ||
|
||
.ui-scrollpanel-hidden { | ||
display: none; | ||
} | ||
|
||
.ui-scrollpanel:hover .ui-scrollpanel-bar, | ||
.ui-scrollpanel:active .ui-scrollpanel-bar { | ||
opacity: 1; | ||
} | ||
|
||
.ui-scrollpanel-grabbed { | ||
-o-user-select: none; | ||
-ms-user-select: none; | ||
-moz-user-select: none; | ||
-webkit-user-select: none; | ||
user-select: none; | ||
} |
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,104 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import DomHandler from '../utils/DomHandler'; | ||
import classNames from 'classnames'; | ||
|
||
export class ScrollPanel extends Component { | ||
|
||
static defaultProps = { | ||
style: null, | ||
className: null | ||
} | ||
|
||
static propTypes = { | ||
style: PropTypes.object, | ||
className: PropTypes.string | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.moveBar = this.moveBar.bind(this); | ||
this.onBarMouseDown = this.onBarMouseDown.bind(this); | ||
this.onDocumentMouseMove = this.onDocumentMouseMove.bind(this); | ||
this.onDocumentMouseUp = this.onDocumentMouseUp.bind(this); | ||
} | ||
|
||
componentDidMount() { | ||
this.moveBar(); | ||
this.moveBar = this.moveBar.bind(this); | ||
|
||
window.addEventListener('resize', this.moveBar); | ||
this.initialized = true; | ||
} | ||
|
||
componentWillUnmount() { | ||
if(this.initialized) { | ||
window.removeEventListener('resize', this.moveBar); | ||
} | ||
} | ||
|
||
moveBar() { | ||
let totalHeight = this.content.scrollHeight; | ||
let ownHeight = this.content.clientHeight; | ||
let right = (this.container.clientWidth - this.bar.clientWidth) * -1; | ||
this.scrollRatio = ownHeight / totalHeight; | ||
|
||
this.requestAnimationFrame(() => { | ||
if (this.scrollRatio >= 1) { | ||
DomHandler.addClass(this.bar, 'ui-scrollpanel-hidden'); | ||
} | ||
else { | ||
DomHandler.removeClass(this.bar, 'ui-scrollpanel-hidden'); | ||
this.bar.style.cssText = 'height:' + Math.max(this.scrollRatio * 100, 10) + '%; top:' + (this.content.scrollTop / totalHeight) * 100 + '%;right:' + right + 'px;'; | ||
} | ||
}); | ||
} | ||
|
||
onBarMouseDown(e) { | ||
this.lastPageY = e.pageY; | ||
DomHandler.addClass(this.bar, 'ui-scrollpanel-grabbed'); | ||
DomHandler.addClass(document.body, 'ui-scrollpanel-grabbed'); | ||
|
||
document.addEventListener('mousemove', this.onDocumentMouseMove); | ||
document.addEventListener('mouseup', this.onDocumentMouseUp); | ||
e.preventDefault(); | ||
} | ||
|
||
onDocumentMouseMove(e) { | ||
let delta = e.pageY - this.lastPageY; | ||
this.lastPageY = e.pageY; | ||
|
||
this.requestAnimationFrame(() => { | ||
this.content.scrollTop += delta / this.scrollRatio; | ||
}); | ||
} | ||
|
||
onDocumentMouseUp(e) { | ||
DomHandler.removeClass(this.bar, 'ui-scrollpanel-grabbed'); | ||
DomHandler.removeClass(document.body, 'ui-scrollpanel-grabbed'); | ||
|
||
document.removeEventListener('mousemove', this.onDocumentMouseMove); | ||
document.removeEventListener('mouseup', this.onDocumentMouseUp); | ||
} | ||
|
||
requestAnimationFrame(f) { | ||
let frame = window.requestAnimationFrame || this.timeoutFrame; | ||
frame(f); | ||
} | ||
|
||
render() { | ||
let className = classNames('ui-scrollpanel ui-widget ui-widget-content ui-corner-all', this.props.className); | ||
|
||
return ( | ||
<div ref={(el) => { this.container = el; }} id={this.props.id} className={className} style={this.props.style}> | ||
<div className="ui-scrollpanel-wrapper"> | ||
<div ref={(el) => { this.content = el; }} className="ui-scrollpanel-content" onScroll={this.moveBar} onMouseEnter={this.moveBar}> | ||
{this.props.children} | ||
</div> | ||
</div> | ||
<div ref={(el) => { this.bar = el; }} className="ui-scrollpanel-bar" onMouseDown={this.onBarMouseDown}></div> | ||
</div> | ||
); | ||
} | ||
} |
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
Oops, something went wrong.