Skip to content

Commit

Permalink
Fix touch handlers and make them passive (#706)
Browse files Browse the repository at this point in the history
Touch support in FDT v2 is wonky.
There's multiple issues here, all of which are fixed with this commit:

## problem 1
We're no longer passing down the `touchEnabled` prop to the cell renderers within FDT.
But the resizer plugin `<ResizeCell />` still expects it, making it not work for touch devices.

## problem 2
The touch/mouse related event listeners were treated as passive. 
This is a problem with React not yet having an API for clients to specify event listener options.

FDT relies on stopping propagation or preventing default behavior of these events in various places, and they did not work as expected.
I'm fixing this by manually registering the handlers via `addEventListener` with the `passive` property turned OFF.
See facebook/react#6436

## problem 3
Sometimes, the first touch events don't seem to work unless the user does a follow-up click.
One particular case is when the user clicks on the reorder knob; FDT will render a "drag proxy" which replaces the original cell thus making the original touch event (which relies on the original cell to be in the document tree) to not work properly.

The docs explain this nicely: https://developer.mozilla.org/en-US/docs/Web/API/Touch/target

> The read-only target property of the Touch interface returns the `EventTarget` on which the touch contact started when it was first placed on the surface, even if the touch point has since moved outside the interactive area of that element or even been removed from the document. **Note that if the target element is removed from the document, events will still be targeted at it, and hence won't necessarily bubble up to the window or document anymore.** If there is any risk of an element being removed while it is being touched, the best practice is to attach the touch listeners directly to the target.

## problem 4
`<ReorderCell />` accidentally renders the children twice in some cases because we accidentally passed its' children to its child renderer...
Check this comment -- #706 (comment) -- for details.
  • Loading branch information
pradeepnschrodinger authored Nov 16, 2023
1 parent 28e518a commit 11b82dd
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 20 deletions.
1 change: 1 addition & 0 deletions examples/ColumnGroupsResizeReorderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class ColumnGroupsExample extends React.Component {
rowsCount={dataList.getSize()}
width={1000}
height={500}
touchScrollEnabled={true}
{...this.props}
>
{this.state.columnGroupOrder.map(function (columnGroupKey, i) {
Expand Down
1 change: 1 addition & 0 deletions examples/ReorderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ReorderExample extends React.Component {
rowsCount={dataList.getSize()}
width={1000}
height={500}
touchScrollEnabled={true}
{...this.props}
>
{this.state.columnOrder.map(function (columnKey, i) {
Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ class FixedDataTable extends React.Component {
fixedRightColumns={fixedRightColumnGroups}
scrollableColumns={scrollableColumnGroups}
visible={true}
touchEnabled={touchScrollEnabled}
onColumnResizeEndCallback={onColumnResizeEndCallback}
onColumnReorderEndCallback={onColumnReorderEndCallback}
showScrollbarY={scrollEnabledY}
Expand Down
2 changes: 2 additions & 0 deletions src/FixedDataTableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class FixedDataTableCell extends React.Component {
isVisible,
columnKey,
isHeaderOrFooter,
touchEnabled,
...props
} = this.props;

Expand Down Expand Up @@ -205,6 +206,7 @@ class FixedDataTableCell extends React.Component {
);

let cellProps = {
touchEnabled,
isVisible,
isHeader: this.props.isHeader,
isGroupHeader: this.props.isGroupHeader,
Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTableCellDefault.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class FixedDataTableCellDefault extends React.Component {
isGroupHeader,
maxWidth,
minWidth,
touchEnabled,
...props
} = this.props;

Expand Down
1 change: 1 addition & 0 deletions src/FixedDataTableCellDefaultDeprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class FixedDataTableCellDefault extends React.Component {
isGroupHeader,
maxWidth,
minWidth,
touchEnabled,
...props
} = this.props;

Expand Down
17 changes: 13 additions & 4 deletions src/plugins/ResizeReorder/ReorderCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ class ReorderCell extends React.PureComponent {
onColumnReorderStart,
onColumnReorderEnd,
reorderStartEvent,
children,
...props
} = this.props;

const { children, left } = props;

let className = joinClasses(
cx({
'public/fixedDataTableCell/resizeReorderCellContainer': true,
Expand Down Expand Up @@ -167,17 +166,27 @@ class ReorderCell extends React.PureComponent {

return (
<div
ref={this.setReorderHandle}
className={cx({
'fixedDataTableCellLayout/columnReorderContainer': true,
'fixedDataTableCellLayout/columnReorderContainer/active': false,
})}
onMouseDown={this.onMouseDown}
onTouchStart={this.onTouchStart}
style={style}
/>
);
}

setReorderHandle = (element) => {
if (element) {
element.addEventListener('mousedown', this.onMouseDown, {
passive: false,
});
element.addEventListener('touchstart', this.onTouchStart, {
passive: false,
});
}
};

onTouchStart = (ev) => {
if (!this.props.touchEnabled) {
return;
Expand Down
74 changes: 66 additions & 8 deletions src/plugins/ResizeReorder/ResizerKnob.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ResizerKnob extends React.PureComponent {
* Ref to ResizerKnob
* @type {HTMLDivElement}
*/
curRef = null;
resizerKnobRef = null;

/**
*
Expand All @@ -62,8 +62,14 @@ class ResizerKnob extends React.PureComponent {

componentDidMount() {
this.setState({
top: this.curRef.getBoundingClientRect().top,
top: this.resizerKnobRef.getBoundingClientRect().top,
});

this.setupHandlers();
}

componentWillUnmount() {
this.cleanupHandlers();
}

render() {
Expand All @@ -82,18 +88,58 @@ class ResizerKnob extends React.PureComponent {
return (
<div
className={cx('fixedDataTableCellLayout/columnResizerContainer')}
ref={(element) => (this.curRef = element)}
ref={this.setResizerKnobRef}
style={resizerKnobStyle}
onMouseDown={this.onMouseDown}
onTouchStart={this.props.touchEnabled ? this.onMouseDown : null}
onTouchEnd={this.props.touchEnabled ? this.suppressEvent : null}
onTouchMove={this.props.touchEnabled ? this.suppressEvent : null}
>
{resizerLine}
</div>
);
}

setResizerKnobRef = (element) => {
this.resizerKnobRef = element;
};

setupHandlers() {
// TODO (pradeep): Remove these and pass to our knob component directly after React
// provides an API where event handlers can be specified to be non-passive (facebook/react#6436).
this.resizerKnobRef.addEventListener('mousedown', this.onMouseDown, {
passive: false,
});
this.resizerKnobRef.addEventListener('touchstart', this.onTouchStart, {
passive: false,
});
this.resizerKnobRef.addEventListener(
'touchmove',
this.suppressEventIfInTouchMode,
{ passive: false }
);
this.resizerKnobRef.addEventListener(
'touchend',
this.suppressEventIfInTouchMode,
{ passive: false }
);
}

cleanupHandlers() {
this.resizerKnobRef.removeEventListener('mousedown', this.onMouseDown, {
passive: false,
});
this.resizerKnobRef.removeEventListener('touchstart', this.onTouchStart, {
passive: false,
});
this.resizerKnobRef.removeEventListener(
'touchmove',
this.suppressEventIfInTouchMode,
{ passive: false }
);
this.resizerKnobRef.removeEventListener(
'touchend',
this.suppressEventIfInTouchMode,
{ passive: false }
);
}

/**
* Registers event listeners for mouse tracking
* @param {MouseEvent} event
Expand All @@ -108,6 +154,15 @@ class ResizerKnob extends React.PureComponent {
this.mouseMoveTracker.captureMouseMoves(event);
};

/**
* @param {TouchEvent} event The touch start event
*/
onTouchStart = (event) => {
if (this.props.touchEnabled) {
this.onMouseDown(event);
}
};

/**
* @param {MouseEvent} ev Mouse down event
*/
Expand Down Expand Up @@ -185,7 +240,10 @@ class ResizerKnob extends React.PureComponent {
/**
* @param {Object} event
*/
suppressEvent = (event) => {
suppressEventIfInTouchMode = (event) => {
if (!this.props.touchEnabled) {
return;
}
event.preventDefault();
event.stopPropagation();
};
Expand Down
13 changes: 8 additions & 5 deletions src/vendor_upstream/dom/DOMMouseMoveTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ class DOMMouseMoveTracker {
this._eventTouchStartToken = EventListener.listen(
this._domNode,
'touchstart',
this._onMouseMove
this._onMouseMove,
{ passive: false }
);
this._eventTouchMoveToken = EventListener.listen(
this._domNode,
event.target,
'touchmove',
this._onMouseMove
this._onMouseMove,
{ passive: false }
);
this._eventTouchEndToken = EventListener.listen(
this._domNode,
event.target,
'touchend',
this._onMouseUp
this._onMouseUp,
{ passive: false }
);
}

Expand Down
7 changes: 4 additions & 3 deletions src/vendor_upstream/stubs/EventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ const EventListener = {
* @param {DOMEventTarget} target DOM element to register listener on.
* @param {string} eventType Event type, e.g. 'click' or 'mouseover'.
* @param {function} callback Callback function.
* @param {object} options Extra options to customize the listener
* @return {object} Object with a `remove` method.
*/
listen(target, eventType, callback) {
listen(target, eventType, callback, options = {}) {
if (target.addEventListener) {
target.addEventListener(eventType, callback, false);
target.addEventListener(eventType, callback, options || false);
return {
remove() {
target.removeEventListener(eventType, callback, false);
target.removeEventListener(eventType, callback, options || false);
},
};
} else if (target.attachEvent) {
Expand Down

0 comments on commit 11b82dd

Please sign in to comment.