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(brush, drag): call start callbacks consistently, support mobile #1286

Merged
merged 1 commit into from
Jul 21, 2021
Merged
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
49 changes: 32 additions & 17 deletions packages/visx-brush/src/BaseBrush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,10 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
}

handleWindowPointerUp = () => {
const { useWindowMoveEvents } = this.props;
const { useWindowMoveEvents, onBrushEnd, resetOnEnd } = this.props;
const { brushingType } = this.state;

if (
useWindowMoveEvents &&
['top', 'bottom', 'left', 'right', 'select', 'move'].includes(brushingType ?? '')
) {
if (useWindowMoveEvents && brushingType) {
this.updateBrush((prevBrush: BaseBrushState) => {
const { start, end, extent } = prevBrush;

Expand All @@ -155,18 +152,28 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
end.x = Math.max(extent.x0, extent.x1);
end.y = Math.max(extent.y0, extent.y1);

return {
const newState = {
...prevBrush,
activeHandle: null,
isBrushing: false,
brushingType: undefined,
};

if (onBrushEnd) {
onBrushEnd(newState);
}

if (resetOnEnd) {
this.reset();
}

return newState;
});
}
};

handleWindowPointerMove = (event: MouseEvent) => {
const { useWindowMoveEvents, onBrushEnd, resetOnEnd } = this.props;
const { useWindowMoveEvents } = this.props;
const { brushingType, isBrushing, brushPageOffset, start } = this.state;

if (!useWindowMoveEvents || !isBrushing) return;
Expand Down Expand Up @@ -256,14 +263,6 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
extent,
};

if (onBrushEnd) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these shouldn't have been called in the move handler

onBrushEnd(newState);
}

if (resetOnEnd) {
this.reset();
}

return newState;
});
}
Expand Down Expand Up @@ -314,6 +313,20 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
}));
};

handleBrushStart = (drag: DragArgs) => {
const { onBrushStart, left, top, inheritedMargin } = this.props;

if (onBrushStart) {
const marginLeft = inheritedMargin?.left ? inheritedMargin.left : 0;
const marginTop = inheritedMargin?.top ? inheritedMargin.top : 0;
const start = {
x: (drag.x || 0) + drag.dx - left - marginLeft,
y: (drag.y || 0) + drag.dy - top - marginTop,
};
onBrushStart(start);
}
};

handleDragMove = (drag: DragArgs) => {
const { left, top, inheritedMargin, useWindowMoveEvents } = this.props;
if (!drag.isDragging || useWindowMoveEvents) return;
Expand Down Expand Up @@ -550,7 +563,7 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush

return (
<Group className="visx-brush" top={top} left={left}>
{/* overlay */}
{/* stage drag detection */}
<Drag
width={stageWidth}
height={stageHeight}
Expand Down Expand Up @@ -601,9 +614,10 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
height={height}
stageWidth={stageWidth}
stageHeight={stageHeight}
brush={{ ...this.state }}
brush={this.state}
disableDraggingSelection={disableDraggingSelection}
onBrushEnd={onBrushEnd}
onBrushStart={this.handleBrushStart}
onMouseLeave={onMouseLeave}
onMouseMove={onMouseMove}
onMouseUp={onMouseUp}
Expand Down Expand Up @@ -632,6 +646,7 @@ export default class BaseBrush extends React.Component<BaseBrushProps, BaseBrush
stageHeight={stageHeight}
updateBrush={this.updateBrush}
brush={this.state}
onBrushStart={this.handleBrushStart}
onBrushEnd={onBrushEnd}
isControlled={useWindowMoveEvents}
isDragInProgress={useWindowMoveEvents ? brushingType === handleKey : undefined}
Expand Down
12 changes: 6 additions & 6 deletions packages/visx-brush/src/BrushCorner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class BrushCorner extends React.Component<BrushCornerProps, Brush
render() {
const { type, brush, stageWidth, stageHeight, style: styleProp, corner } = this.props;
const cursor =
(styleProp && styleProp.cursor) ||
styleProp?.cursor ||
(type === 'topLeft' || type === 'bottomRight' ? 'nwse-resize' : 'nesw-resize');
const pointerEvents = brush.activeHandle || brush.isBrushing ? 'none' : 'all';

Expand All @@ -154,15 +154,15 @@ export default class BrushCorner extends React.Component<BrushCornerProps, Brush
width={stageWidth}
height={stageHeight}
style={{ cursor }}
onMouseMove={dragMove}
onMouseUp={dragEnd}
onPointerMove={dragMove}
onPointerUp={dragEnd}
/>
)}
<rect
fill="transparent"
onMouseDown={dragStart}
onMouseMove={dragMove}
onMouseUp={dragEnd}
onPointerDown={dragStart}
onPointerMove={dragMove}
onPointerUp={dragEnd}
className={`visx-brush-corner-${type}`}
style={{ cursor, pointerEvents, ...styleProp }}
{...corner}
Expand Down
12 changes: 8 additions & 4 deletions packages/visx-brush/src/BrushHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type BrushHandleProps = {
stageHeight: number;
brush: BrushState;
updateBrush: (update: UpdateBrush) => void;
onBrushStart?: (brush: DragArgs) => void;
onBrushEnd?: (brush: BrushState) => void;
type: ResizeTriggerAreas;
handle: { x: number; y: number; width: number; height: number };
Expand All @@ -21,11 +22,14 @@ export type BrushHandleProps = {
/** BrushHandle's are placed along the bounds of the brush and handle Drag events which update the passed brush. */
export default class BrushHandle extends React.Component<BrushHandleProps> {
handleDragStart = (drag: DragArgs) => {
const { onBrushHandleChange, type } = this.props;
const { onBrushHandleChange, type, onBrushStart } = this.props;

if (onBrushHandleChange) {
onBrushHandleChange(type, getPageCoordinates(drag.event));
}
if (onBrushStart) {
onBrushStart(drag);
}
};

handleDragMove = (drag: DragArgs) => {
Expand Down Expand Up @@ -158,9 +162,9 @@ export default class BrushHandle extends React.Component<BrushHandleProps> {
width={stageWidth}
height={stageHeight}
style={{ cursor }}
onMouseMove={dragMove}
onMouseUp={isControlled ? undefined : dragEnd}
onMouseLeave={isControlled ? undefined : dragEnd}
onPointerMove={dragMove}
onPointerUp={isControlled ? undefined : dragEnd}
onPointerLeave={isControlled ? undefined : dragEnd}
/>
)}
<rect
Expand Down
6 changes: 5 additions & 1 deletion packages/visx-brush/src/BrushSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type BrushSelectionProps = {
brush: BrushState;
updateBrush: (update: UpdateBrush) => void;
onMoveSelectionChange?: (type?: BrushingType, options?: BrushPageOffset) => void;
onBrushStart?: (brush: DragArgs) => void;
onBrushEnd?: (brush: BrushState) => void;
disableDraggingSelection: boolean;
onMouseLeave: PointerHandler;
Expand All @@ -40,11 +41,14 @@ export default class BrushSelection extends React.Component<
};

selectionDragStart = (drag: DragArgs) => {
const { onMoveSelectionChange } = this.props;
const { onMoveSelectionChange, onBrushStart } = this.props;

if (onMoveSelectionChange) {
onMoveSelectionChange('move', getPageCoordinates(drag.event));
}
if (onBrushStart) {
onBrushStart(drag);
}
};

selectionDragMove = (drag: DragArgs) => {
Expand Down
1 change: 1 addition & 0 deletions packages/visx-drag/src/Drag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function Drag({
<rect
width={width}
height={height}
onPointerDown={drag.dragStart}
onPointerMove={drag.dragMove}
onPointerUp={drag.dragEnd}
fill="transparent"
Expand Down