lucid-ui
Version:
A UI component library from AppNexus.
241 lines (193 loc) • 9.54 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import _keys from "lodash/keys";
import _noop from "lodash/noop";
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
var cx = lucidClassNames.bind('&-DragCaptureZone');
var func = PropTypes.func,
string = PropTypes.string;
var DragCaptureZone = /*#__PURE__*/function (_React$Component) {
_inherits(DragCaptureZone, _React$Component);
var _super = _createSuper(DragCaptureZone);
function DragCaptureZone() {
var _this;
_classCallCheck(this, DragCaptureZone);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _super.call.apply(_super, [this].concat(args));
_defineProperty(_assertThisInitialized(_this), "elementRef", /*#__PURE__*/React.createRef());
_defineProperty(_assertThisInitialized(_this), "state", {
pageX: 0,
pageY: 0
});
_defineProperty(_assertThisInitialized(_this), "handleDrag", function (event) {
var pageX;
var pageY;
/* istanbul ignore next */
if ('touches' in event) {
pageX = event.touches[0].pageX;
pageY = event.touches[0].pageY;
} else {
pageX = event.pageX;
pageY = event.pageY;
}
event.preventDefault();
_this.props.onDrag({
dX: pageX - _this.state.pageX,
dY: pageY - _this.state.pageY,
pageX: pageX,
pageY: pageY
}, {
event: event,
props: _this.props
});
});
_defineProperty(_assertThisInitialized(_this), "handleMouseDragStart", function (event) {
var pageX = event.pageX;
var pageY = event.pageY;
window.document.addEventListener('mousemove', _this.handleDrag);
window.document.addEventListener('mouseup', _this.handleDragEnd);
event.preventDefault();
_this.props.onDragStart({
dX: 0,
dY: 0,
pageX: pageX,
pageY: pageY
}, {
event: event,
props: _this.props
});
_this.setState({
pageX: pageX,
pageY: pageY
});
});
_defineProperty(_assertThisInitialized(_this), "handleTouchDragStart", function (event) {
var pageX = event.touches[0].pageX;
var pageY = event.touches[0].pageY;
event.preventDefault();
_this.props.onDragStart({
dX: 0,
dY: 0,
pageX: pageX,
pageY: pageY
}, {
event: event,
props: _this.props
});
_this.setState({
pageX: pageX,
pageY: pageY
});
});
_defineProperty(_assertThisInitialized(_this), "handleDragEnd", function (event) {
var pageX;
var pageY;
/* istanbul ignore next */
if ('changedTouches' in event) {
pageX = event.changedTouches[0].pageX;
pageY = event.changedTouches[0].pageY;
} else {
pageX = event.pageX;
pageY = event.pageY;
}
window.document.removeEventListener('mousemove', _this.handleDrag);
window.document.removeEventListener('mouseup', _this.handleDragEnd);
event.preventDefault();
_this.props.onDragEnd({
dX: pageX - _this.state.pageX,
dY: pageY - _this.state.pageY,
pageX: pageX,
pageY: pageY
}, {
event: event,
props: _this.props
});
_this.setState({
pageX: 0,
pageY: 0
});
});
_defineProperty(_assertThisInitialized(_this), "handleDragCancel", function (event) {
_this.props.onDragCancel({
event: event,
props: _this.props
});
_this.setState({
pageX: 0,
pageY: 0
});
});
return _this;
}
_createClass(DragCaptureZone, [{
key: "componentDidMount",
value: function componentDidMount() {
//add event listeners directly on the DOM element to allow preventDefault
//calls which are not honored due to react's event delegation
//reference: https://github.com/facebook/react/issues/8968
if (this.elementRef.current) {
this.elementRef.current.addEventListener('touchmove', this.handleDrag);
this.elementRef.current.addEventListener('touchend', this.handleDragEnd);
this.elementRef.current.addEventListener('touchcancel', this.handleDragCancel);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.elementRef.current) {
this.elementRef.current.removeEventListener('touchmove', this.handleDrag);
this.elementRef.current.removeEventListener('touchend', this.handleDragEnd);
this.elementRef.current.removeEventListener('touchcancel', this.handleDragCancel);
}
window.document.removeEventListener('mousemove', this.handleDrag);
window.document.removeEventListener('mouseup', this.handleDragEnd);
}
}, {
key: "render",
value: function render() {
return /*#__PURE__*/React.createElement("div", _extends({}, omitProps(this.props, undefined, _keys(DragCaptureZone.propTypes)), {
className: cx('&', this.props.className),
key: "DragCaptureZone",
onMouseDown: this.handleMouseDragStart,
onTouchStart: this.handleTouchDragStart,
ref: this.elementRef
}));
}
}]);
return DragCaptureZone;
}(React.Component);
_defineProperty(DragCaptureZone, "displayName", 'DragCaptureZone');
_defineProperty(DragCaptureZone, "peek", {
description: "\n\t\t\tThis is a helper component used to capture mouse events to determine\n\t\t\twhen the user starts, is and stops dragging.\n\t\t",
categories: ['utility']
});
_defineProperty(DragCaptureZone, "propTypes", {
className: string,
onDrag: func,
onDragEnd: func,
onDragStart: func,
onDragCancel: func
});
_defineProperty(DragCaptureZone, "defaultProps", {
onDrag: _noop,
onDragEnd: _noop,
onDragStart: _noop,
onDragCancel: _noop
});
export default DragCaptureZone;