UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

196 lines 9.75 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) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var lodash_1 = __importDefault(require("lodash")); var react_1 = __importDefault(require("react")); var prop_types_1 = __importDefault(require("react-peek/prop-types")); var style_helpers_1 = require("../../util/style-helpers"); var component_types_1 = require("../../util/component-types"); var cx = style_helpers_1.lucidClassNames.bind('&-DragCaptureZone'); var func = prop_types_1.default.func, string = prop_types_1.default.string; 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({}, component_types_1.omitProps(this.props, undefined, lodash_1.default.keys(DragCaptureZone.propTypes)), { className: cx('&', this.props.className), key: 'DragCaptureZone', onMouseDown: this.handleMouseDragStart, onTouchStart: this.handleTouchDragStart, ref: this.elementRef }))); }; DragCaptureZone.displayName = 'DragCaptureZone'; 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'], }; DragCaptureZone.propTypes = { className: string(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"], ["\n\t\t\tAppended to the component-specific class names set on the root element.\n\t\t"]))), onDrag: func(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t\t\tCalled as the user drags the mouse. Signature:\n\t\t\t`({ dx, dy, pageX, pageY }, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled as the user drags the mouse. Signature:\n\t\t\t\\`({ dx, dy, pageX, pageY }, { event, props }) => {}\\`\n\t\t"]))), onDragEnd: func(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t\t\tCalled when the user releases the mouse button after having dragged.\n\t\t\tSignature: `({ dx, dy, pageX, pageY }, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user releases the mouse button after having dragged.\n\t\t\tSignature: \\`({ dx, dy, pageX, pageY }, { event, props }) => {}\\`\n\t\t"]))), onDragStart: func(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t\t\tCalled when the user presses the mouse button down while over the\n\t\t\tcomponent. Signature:\n\t\t\t`({ dx, dy, pageX, pageY }, { event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the user presses the mouse button down while over the\n\t\t\tcomponent. Signature:\n\t\t\t\\`({ dx, dy, pageX, pageY }, { event, props }) => {}\\`\n\t\t"]))), onDragCancel: func(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\t\t\tCalled when the drag event is canceled due to user interaction. For\n\t\t\texample: if a system alert pops up during a touch event. Signature:\n\t\t\t`({ event, props }) => {}`\n\t\t"], ["\n\t\t\tCalled when the drag event is canceled due to user interaction. For\n\t\t\texample: if a system alert pops up during a touch event. Signature:\n\t\t\t\\`({ event, props }) => {}\\`\n\t\t"]))), }; 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; var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5; //# sourceMappingURL=DragCaptureZone.js.map