Skip to main content

useScratch

React sensor hook that tracks scratching/dragging gestures on an element with mouse and touch support.

Usage

Live Editor
function Demo() {
  const elementRef = useRef(null);
  const state = useScratch(elementRef, {
    onScratchStart: (s) => {
      console.log('Scratch started:', s);
    },
    onScratch: (s) => {
      console.log('Scratching:', s);
    },
    onScratchEnd: (s) => {
      console.log('Scratch ended:', s);
    },
  });

  return (
    <div style={{ padding: '20px' }}>
      <div
        ref={elementRef}
        style={{
          width: '300px',
          height: '200px',
          border: '1px solid var(--ifm-color-emphasis-300)',
          borderRadius: '8px',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          backgroundColor: state.isScratching ? 'var(--ifm-color-emphasis-200)' : 'var(--ifm-background-surface-color)',
          color: 'var(--ifm-color-content)',
          cursor: 'pointer',
          userSelect: 'none',
        }}
      >
        {state.isScratching ? 'Scratching...' : 'Click and drag to scratch'}
      </div>
      <div style={{ marginTop: '20px', fontFamily: 'monospace', fontSize: '12px', color: 'var(--ifm-color-content)' }}>
        <div><strong>isScratching:</strong> {state.isScratching ? 'true' : 'false'}</div>
        {state.x !== undefined && <div><strong>x:</strong> {Math.round(state.x)}</div>}
        {state.y !== undefined && <div><strong>y:</strong> {Math.round(state.y)}</div>}
        {state.dx !== undefined && <div><strong>dx:</strong> {Math.round(state.dx)}</div>}
        {state.dy !== undefined && <div><strong>dy:</strong> {Math.round(state.dy)}</div>}
      </div>
    </div>
  );
};

Result
Loading...

API

UseScratchState

PropertyDescriptionTypeDefaultValue
isScratchingWhether scratching is in progressboolean (Required)-
startStart timestampnumber-
endEnd timestampnumber-
xx coordinate relative to elementnumber-
yy coordinate relative to elementnumber-
dxDelta in x directionnumber-
dyDelta in y directionnumber-
docXx coordinate in documentnumber-
docYy coordinate in documentnumber-
posXElement x position in documentnumber-
posYElement y position in documentnumber-
elHElement heightnumber-
elWElement widthnumber-
elXElement x positionnumber-
elYElement y positionnumber-

UseScratchOptions

PropertyDescriptionTypeDefaultValue
disabledWhether to disableboolean-
onScratchCallback during scratching(state: UseScratchState) => void-
onScratchStartCallback when scratching starts(state: UseScratchState) => void-
onScratchEndCallback when scratching ends(state: UseScratchState) => void-

useScratch

Returns

UseScratchState: Scratch state

Arguments

ArgumentDescriptionTypeDefaultValue
targetTarget elementBasicTarget<HTMLElement> (Required)-
optionsOptionsUseScratchOptions | undefined-

BasicTarget

export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;

TargetValue

type TargetValue<T> = T | undefined | null;

TargetType

type TargetType = HTMLElement | Element | Window | Document | EventTarget;