Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mobile users to drag slider components #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion static/js/st/components/slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export default class Slider extends React.PureComponent {
let startValue = this.currentValue()

let moveListener = (e) => {
let x = e.pageX, y = e.pageX
let x = e.pageX
let y = e.pageY
let dx = x - startX

let newValue = dx / width * (this.props.max - this.props.min) + startValue
Expand All @@ -65,6 +66,36 @@ export default class Slider extends React.PureComponent {
document.body.addEventListener("mouseup", upListener)
}

startTouchDrag(startX, startY) {
if (this.props.disabled) {
return true;
}

let width = this.refs.track.clientWidth
let startValue = this.currentValue()

let moveListener = (e) => {
let x = e.changedTouches[0].pageX
let y = e.changedTouches[0].pageY
let dx = x - startX

let newValue = dx / width * (this.props.max - this.props.min) + startValue
newValue = Math.min(this.props.max, Math.max(this.props.min, newValue))

if (newValue != this.currentValue()) {
this.onChange(newValue)
}
}

let upListener = (e) => {
document.body.removeEventListener("touchmove", moveListener)
document.body.removeEventListener("touchend", upListener)
}

document.body.addEventListener("touchmove", moveListener)
document.body.addEventListener("touchend", upListener)
}

currentValue() {
if ("value" in this.state) {
return this.state.value
Expand Down Expand Up @@ -102,6 +133,7 @@ export default class Slider extends React.PureComponent {
<button
ref="sliderNub"
onMouseDown={(e) => this.startDrag(e.pageX, e.pageY)}
onTouchStart={(e) => this.startTouchDrag(e.changedTouches[0].pageX, e.changedTouches[0].pageY)}
onKeyDown={e => {
let delta = 0

Expand Down