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

Fix dragging the slider on iOS #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions example/js/example-app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class ExampleApp extends React.Component {
min: 3,
max: 7,
},
value7: 4,
};
}

Expand Down Expand Up @@ -77,6 +78,16 @@ export default class ExampleApp extends React.Component {
onChange={value => this.setState({ value6: value })}
onChangeComplete={value => console.log(value)}
value={this.state.value6} />

<div className="touch-friendly">
<InputRange
maxValue={20}
formatLabel={() => null}
minValue={0}
value={this.state.value7}
onChange={value => this.setState({ value7: value })}
onChangeComplete={value => console.log(value)} />
</div>
</form>
);
}
Expand Down
21 changes: 21 additions & 0 deletions example/scss/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,24 @@
.input-range {
margin-bottom: 160px;
}

.touch-friendly {
.input-range {
&__track {
height: 30px;

&--active {
background: rgba(#3f51b5, 0.5)
}
}

&__slider {
width: 40px;
height: 40px;

&-container {
top: 5px;
}
}
}
}
34 changes: 32 additions & 2 deletions src/js/input-range/input-range.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ export default class InputRange extends React.Component {
this.lastKeyMoved = null;
}

componentDidMount() {
this.addNodeTouchStartListener();
}

/**
* @ignore
* @override
* @return {void}
*/
componentWillUnmount() {
this.removeNodeTouchStartListener();
this.removeDocumentMouseUpListener();
this.removeDocumentTouchEndListener();
}
Expand Down Expand Up @@ -320,6 +325,22 @@ export default class InputRange extends React.Component {
this.updateValue(key, value);
}

/**
* Start listening to touchstart events on the node
* @private
* @return {void}
*/
addNodeTouchStartListener() {
// Adding a `onTouchStart` prop to the node doesn't work, as the
// event handler will be added as `passive`. The handler needs to
// be active, so it is able to call `preventDefault()`. Otherwise
// e.g. the page scrolls while dragging slightly vertically.
this.node.addEventListener('touchstart', this.handleTouchStart, {
passive: false,
});
}


/**
* Listen to mouseup event
* @private
Expand Down Expand Up @@ -349,6 +370,15 @@ export default class InputRange extends React.Component {
this.node.ownerDocument.removeEventListener('mouseup', this.handleMouseUp);
}

/**
* Stop listening to touchstart events on the node
* @private
* @return {void}
*/
removeNodeTouchStartListener() {
this.node.removeEventListener('touchstart', this.handleTouchStart);
}

/**
* Stop listening to touchend event
* @private
Expand Down Expand Up @@ -565,6 +595,7 @@ export default class InputRange extends React.Component {
handleTouchStart(event) {
this.handleInteractionStart(event);
this.addDocumentTouchEndListener();
event.preventDefault();
}

/**
Expand Down Expand Up @@ -663,8 +694,7 @@ export default class InputRange extends React.Component {
className={componentClassName}
onKeyDown={this.handleKeyDown}
onKeyUp={this.handleKeyUp}
onMouseDown={this.handleMouseDown}
onTouchStart={this.handleTouchStart}>
onMouseDown={this.handleMouseDown}>
<Label
classNames={this.props.classNames}
formatLabel={this.props.formatLabel}
Expand Down