UNPKG

lucid-ui

Version:

A UI component library from Xandr.

242 lines 9.58 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = __importStar(require("lodash")); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("prop-types")); var style_helpers_1 = require("../../util/style-helpers"); var cx = style_helpers_1.lucidClassNames.bind('&-DragCaptureZone'); var func = prop_types_1.default.func, string = prop_types_1.default.string; /** TODO: Remove the nonPassThroughs when the component is converted to a functional component */ var nonPassThroughs = [ 'className', 'onDrag', 'onDragEnd', 'onDragStart', 'onDragCancel', 'initialState', 'callbackId', ]; var DragCaptureZone = /** @class */ (function (_super) { __extends(DragCaptureZone, _super); function DragCaptureZone() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.elementRef = react_1.default.createRef(); _this.state = { pageX: 0, pageY: 0, }; _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, }); }; _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, }); }; _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, }); }; _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, }); }; _this.handleDragCancel = function (event) { _this.props.onDragCancel({ event: event, props: _this.props, }); _this.setState({ pageX: 0, pageY: 0, }); }; return _this; } DragCaptureZone.prototype.componentDidMount = function () { //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); } }; DragCaptureZone.prototype.componentWillUnmount = function () { 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); }; DragCaptureZone.prototype.render = function () { return (react_1.default.createElement("div", __assign({}, (0, lodash_1.omit)(this.props, nonPassThroughs), { className: cx('&', this.props.className), key: 'DragCaptureZone', onMouseDown: this.handleMouseDragStart, onTouchStart: this.handleTouchDragStart, ref: this.elementRef }))); }; DragCaptureZone.displayName = 'DragCaptureZone'; DragCaptureZone.peek = { description: "This is a helper component used to capture mouse events to determine when the user starts, is and stops dragging.", categories: ['utility'], }; DragCaptureZone.propTypes = { /** Appended to the component-specific class names set on the root element. */ className: string, /** Called as the user drags the mouse. Signature: \`({ dx, dy, pageX, pageY }, { event, props }) => {}\` */ onDrag: func, /** Called when the user releases the mouse button after having dragged. Signature: \`({ dx, dy, pageX, pageY }, { event, props }) => {}\` */ onDragEnd: func, /** Called when the user presses the mouse button down while over the component. Signature: \`({ dx, dy, pageX, pageY }, { event, props }) => {}\` */ onDragStart: func, /** Called when the drag event is canceled due to user interaction. For example: if a system alert pops up during a touch event. Signature: \`({ event, props }) => {}\` */ onDragCancel: func, }; DragCaptureZone.defaultProps = { onDrag: lodash_1.default.noop, onDragEnd: lodash_1.default.noop, onDragStart: lodash_1.default.noop, onDragCancel: lodash_1.default.noop, }; return DragCaptureZone; }(react_1.default.Component)); exports.default = DragCaptureZone; //# sourceMappingURL=DragCaptureZone.js.map