Skip to content

Commit e2da4e0

Browse files
feature(react-dock): add touch events (#596)
* Touch events Enable to resize the dock on touch devices by handling touch events in addition to mouse events * Prettifying `yarn run prettify` command prettifying. Co-authored-by: Nathan Bierema <[email protected]>
1 parent 5a6074a commit e2da4e0

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

packages/react-dock/src/Dock.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ export default class Dock extends Component {
244244
};
245245

246246
componentDidMount() {
247+
window.addEventListener('touchend', this.handleMouseUp);
247248
window.addEventListener('mouseup', this.handleMouseUp);
249+
window.addEventListener('touchmove', this.handleMouseMove);
248250
window.addEventListener('mousemove', this.handleMouseMove);
249251
window.addEventListener('resize', this.handleResize);
250252

@@ -254,7 +256,9 @@ export default class Dock extends Component {
254256
}
255257

256258
componentWillUnmount() {
259+
window.removeEventListener('touchend', this.handleMouseUp);
257260
window.removeEventListener('mouseup', this.handleMouseUp);
261+
window.removeEventListener('touchmove', this.handleMouseMove);
258262
window.removeEventListener('mousemove', this.handleMouseMove);
259263
window.removeEventListener('resize', this.handleResize);
260264
}
@@ -329,7 +333,11 @@ export default class Dock extends Component {
329333
<div style={dimStyles} onClick={this.handleDimClick} />
330334
)}
331335
<div style={dockStyles}>
332-
<div style={resizerStyles} onMouseDown={this.handleMouseDown} />
336+
<div
337+
style={resizerStyles}
338+
onMouseDown={this.handleMouseDown}
339+
onTouchStart={this.handleMouseDown}
340+
/>
333341
<div style={styles.dockContent}>
334342
{typeof children === 'function'
335343
? children({
@@ -401,11 +409,18 @@ export default class Dock extends Component {
401409

402410
handleMouseMove = (e) => {
403411
if (!this.state.isResizing || this.state.isWindowResizing) return;
404-
e.preventDefault();
412+
413+
if (!e.touches) e.preventDefault();
405414

406415
const { position, fluid } = this.props;
407416
const { fullWidth, fullHeight, isControlled } = this.state;
408-
const { clientX: x, clientY: y } = e;
417+
let { clientX: x, clientY: y } = e;
418+
419+
if (e.touches) {
420+
x = e.touches[0].clientX;
421+
y = e.touches[0].clientY;
422+
}
423+
409424
let size;
410425

411426
switch (position) {

0 commit comments

Comments
 (0)