Home / Class/ DragBox Class — react Architecture

DragBox Class — react Architecture

Architecture documentation for the DragBox class in drag-box.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  b024f756_c22f_e4ec_06c3_6633345febc9["DragBox"]
  cbf5ef00_28b9_2aea_2734_f190929feb5b["drag-box.js"]
  b024f756_c22f_e4ec_06c3_6633345febc9 -->|defined in| cbf5ef00_28b9_2aea_2734_f190929feb5b
  eccd939c_f4ae_562a_1cfd_068a8a1dc650["render()"]
  b024f756_c22f_e4ec_06c3_6633345febc9 -->|method| eccd939c_f4ae_562a_1cfd_068a8a1dc650

Relationship Graph

Source Code

fixtures/dom/src/components/fixtures/pointer-events/drag-box.js lines 5–88

class DragBox extends React.Component {
  state = {
    hasCapture: false,
    circleLeft: 80,
    circleTop: 80,
  };
  isDragging = false;
  previousLeft = 0;
  previousTop = 0;

  onDown = event => {
    this.isDragging = true;
    event.target.setPointerCapture(event.pointerId);

    // We store the initial coordinates to be able to calculate the changes
    // later on.
    this.extractPositionDelta(event);
  };

  onMove = event => {
    if (!this.isDragging) {
      return;
    }
    const {left, top} = this.extractPositionDelta(event);

    this.setState(({circleLeft, circleTop}) => ({
      circleLeft: circleLeft + left,
      circleTop: circleTop + top,
    }));
  };

  onUp = event => (this.isDragging = false);
  onGotCapture = event => this.setState({hasCapture: true});
  onLostCapture = event => this.setState({hasCapture: false});

  extractPositionDelta = event => {
    const left = event.pageX;
    const top = event.pageY;
    const delta = {
      left: left - this.previousLeft,
      top: top - this.previousTop,
    };
    this.previousLeft = left;
    this.previousTop = top;
    return delta;
  };

  render() {
    const {hasCapture, circleLeft, circleTop} = this.state;

    const boxStyle = {
      border: '1px solid #d9d9d9',
      margin: '10px 0 20px',
      minHeight: 400,
      width: '100%',
      position: 'relative',
    };

    const circleStyle = {
      width: CIRCLE_SIZE,
      height: CIRCLE_SIZE,
      borderRadius: CIRCLE_SIZE / 2,
      position: 'absolute',
      left: circleLeft,
      top: circleTop,
      backgroundColor: hasCapture ? 'blue' : 'green',
      touchAction: 'none',
    };

    return (
      <div style={boxStyle}>
        <div
          style={circleStyle}
          onPointerDown={this.onDown}
          onPointerMove={this.onMove}
          onPointerUp={this.onUp}
          onPointerCancel={this.onUp}
          onGotPointerCapture={this.onGotCapture}
          onLostPointerCapture={this.onLostCapture}
        />
      </div>

Domain

Frequently Asked Questions

What is the DragBox class?
DragBox is a class in the react codebase, defined in fixtures/dom/src/components/fixtures/pointer-events/drag-box.js.
Where is DragBox defined?
DragBox is defined in fixtures/dom/src/components/fixtures/pointer-events/drag-box.js at line 5.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free