Home / File/ drag-box.js — react Source File

drag-box.js — react Source File

Architecture documentation for drag-box.js, a javascript file in the react codebase. 0 imports, 1 dependents.

File javascript BabelCompiler Entrypoint 1 dependents 1 classes

Entity Profile

Dependency Diagram

graph LR
  cbf5ef00_28b9_2aea_2734_f190929feb5b["drag-box.js"]
  8ba3b810_4f97_fa8c_556c_4d21963c5fba["drag.js"]
  8ba3b810_4f97_fa8c_556c_4d21963c5fba --> cbf5ef00_28b9_2aea_2734_f190929feb5b
  style cbf5ef00_28b9_2aea_2734_f190929feb5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

const React = window.React;

const CIRCLE_SIZE = 80;

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>
    );
  }
}

export default DragBox;

Domain

Subdomains

Classes

Frequently Asked Questions

What does drag-box.js do?
drag-box.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Entrypoint subdomain.
What files import drag-box.js?
drag-box.js is imported by 1 file(s): drag.js.
Where is drag-box.js in the architecture?
drag-box.js is located at fixtures/dom/src/components/fixtures/pointer-events/drag-box.js (domain: BabelCompiler, subdomain: Entrypoint, directory: fixtures/dom/src/components/fixtures/pointer-events).

Analyze Your Own Codebase

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

Try Supermodel Free