From bf2a2f36a87621c6b0508a5ad82382247bbc4539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= <48386403+Onxi95@users.noreply.github.com> Date: Thu, 7 Dec 2023 23:47:51 +0100 Subject: [PATCH] feat(brush): disable drag overlay (#1744) * feat: add disable drag overlay * refactor: remove `` * refactor: remove type imports from `BaseBrush` * chore: remove console.logs * fix: remove reset on double click * refactor: rename `disableDragOverlay` to `disableDraggingOverlay` * refactor: adjust import placement --- packages/visx-brush/src/BaseBrush.tsx | 102 ++++++++++++----------- packages/visx-brush/src/Brush.tsx | 4 + packages/visx-brush/src/BrushOverlay.tsx | 18 ++++ 3 files changed, 77 insertions(+), 47 deletions(-) create mode 100644 packages/visx-brush/src/BrushOverlay.tsx diff --git a/packages/visx-brush/src/BaseBrush.tsx b/packages/visx-brush/src/BaseBrush.tsx index 18594a36e..e1bd97d65 100644 --- a/packages/visx-brush/src/BaseBrush.tsx +++ b/packages/visx-brush/src/BaseBrush.tsx @@ -1,11 +1,11 @@ import React from 'react'; import { Group } from '@visx/group'; -import { Bar } from '@visx/shape'; import Drag, { HandlerArgs as DragArgs } from '@visx/drag/lib/Drag'; import BrushHandle, { BrushHandleRenderProps } from './BrushHandle'; import BrushCorner from './BrushCorner'; import BrushSelection from './BrushSelection'; +import BrushOverlay from './BrushOverlay'; import { MarginShape, Point, @@ -17,8 +17,6 @@ import { } from './types'; import { getPageCoordinates } from './utils'; -const BRUSH_OVERLAY_STYLES = { cursor: 'crosshair' }; - type PointerHandlerEvent = React.PointerEvent; export type BaseBrushProps = { @@ -41,6 +39,7 @@ export type BaseBrushProps = { onClick?: (event: PointerHandlerEvent) => void; clickSensitivity: number; disableDraggingSelection: boolean; + disableDraggingOverlay?: boolean; resetOnEnd?: boolean; useWindowMoveEvents?: boolean; renderBrushHandle?: (props: BrushHandleRenderProps) => React.ReactNode; @@ -106,6 +105,7 @@ export default class BaseBrush extends React.Component { let { start, end, extent } = prevBrush; @@ -571,6 +570,7 @@ export default class BaseBrush extends React.Component - {/* stage drag detection */} - - {({ dragStart, isDragging, dragMove, dragEnd }) => ( - this.reset()} - onClick={(event: PointerHandlerEvent) => { - const duration = this.mouseUpTime - this.mouseDownTime; - if (onClick && duration < clickSensitivity) onClick(event); - }} - onPointerDown={(event: PointerHandlerEvent) => { - this.mouseDownTime = Date.now(); - dragStart(event); - }} - onPointerLeave={(event: PointerHandlerEvent) => { - if (onMouseLeave) onMouseLeave(event); - }} - onPointerMove={(event: PointerHandlerEvent) => { - if (!isDragging && onMouseMove) onMouseMove(event); - if (isDragging) dragMove(event); - }} - onPointerUp={(event: PointerHandlerEvent) => { - this.mouseUpTime = Date.now(); - if (onMouseUp) onMouseUp(event); - dragEnd(event); - }} - style={BRUSH_OVERLAY_STYLES} - /> - )} - + {disableDraggingOverlay ? ( + { + const duration = this.mouseUpTime - this.mouseDownTime; + if (onClick && duration < clickSensitivity) onClick(event); + }} + style={{ cursor: 'default' }} + /> + ) : ( + + {({ dragStart, isDragging, dragMove, dragEnd }) => ( + this.reset()} + onClick={(event: PointerHandlerEvent) => { + const duration = this.mouseUpTime - this.mouseDownTime; + if (onClick && duration < clickSensitivity) onClick(event); + }} + onPointerDown={(event: PointerHandlerEvent) => { + this.mouseDownTime = Date.now(); + dragStart(event); + }} + onPointerLeave={(event: PointerHandlerEvent) => { + if (onMouseLeave) onMouseLeave(event); + }} + onPointerMove={(event: PointerHandlerEvent) => { + if (!isDragging && onMouseMove) onMouseMove(event); + if (isDragging) dragMove(event); + }} + onPointerUp={(event: PointerHandlerEvent) => { + this.mouseUpTime = Date.now(); + if (onMouseUp) onMouseUp(event); + dragEnd(event); + }} + style={{ cursor: 'crosshair' }} + /> + )} + + )} + {/* selection */} {start && end && ( { xAxisOrientation, selectedBoxStyle, disableDraggingSelection, + disableDraggingOverlay, resetOnEnd, onMouseLeave, onMouseMove, @@ -236,6 +239,7 @@ class Brush extends Component { top={top} brushDirection={brushDirection} disableDraggingSelection={disableDraggingSelection} + disableDraggingOverlay={disableDraggingOverlay} handleSize={handleSize} inheritedMargin={margin} initialBrushPosition={initialBrushPosition} diff --git a/packages/visx-brush/src/BrushOverlay.tsx b/packages/visx-brush/src/BrushOverlay.tsx new file mode 100644 index 000000000..418933730 --- /dev/null +++ b/packages/visx-brush/src/BrushOverlay.tsx @@ -0,0 +1,18 @@ +import React from 'react'; +import { Bar } from '@visx/shape'; + +type BrushOverlayProps = { + width: number; + height: number; + style?: React.CSSProperties; + onClick?: (event: React.PointerEvent) => void; + onDoubleClick?: (event: React.PointerEvent) => void; + onPointerDown?: (event: React.PointerEvent) => void; + onPointerLeave?: (event: React.PointerEvent) => void; + onPointerMove?: (event: React.PointerEvent) => void; + onPointerUp?: (event: React.PointerEvent) => void; +}; + +export default function BrushOverlay(props: BrushOverlayProps) { + return ; +}