UNPKG

@nutui/nutui-react

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

132 lines (131 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "Drag", { enumerable: true, get: function() { return Drag; } }); var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_spread_props = require("@swc/helpers/_/_object_spread_props"); var _object_without_properties = require("@swc/helpers/_/_object_without_properties"); var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array"); var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react")); var _react1 = require("@use-gesture/react"); var _web = require("@react-spring/web"); var _typings = require("../../utils/typings"); var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), { attract: false, direction: undefined, boundary: { top: 0, left: 0, right: 0, bottom: 0 } }); var Drag = function Drag(props) { var _ref = (0, _object_spread._)({}, defaultProps, props), attract = _ref.attract, direction = _ref.direction, boundary = _ref.boundary, onDrag = _ref.onDrag, onDragStart = _ref.onDragStart, onDragEnd = _ref.onDragEnd, children = _ref.children, className = _ref.className, style = _ref.style, reset = (0, _object_without_properties._)(_ref, [ "attract", "direction", "boundary", "onDrag", "onDragStart", "onDragEnd", "children", "className", "style" ]); var classPrefix = 'nut-drag'; var _useState = (0, _sliced_to_array._)((0, _react.useState)(boundary), 2), boundaryState = _useState[0], setBoundaryState = _useState[1]; var myDrag = (0, _react.useRef)(null); var _useSpring = (0, _sliced_to_array._)((0, _web.useSpring)(function() { return { x: 0, y: 0 }; }), 2), currstyle = _useSpring[0], api = _useSpring[1]; var middleLine = (0, _react.useRef)(0); var getInfo = function getInfo() { var el = myDrag.current; if (el) { var offsetTop = el.offsetTop, offsetLeft = el.offsetLeft; var _el_querySelector = el.querySelector(".".concat(classPrefix, "-inner")), offsetWidth = _el_querySelector.offsetWidth, offsetHeight = _el_querySelector.offsetHeight; var _document_documentElement = document.documentElement, clientWidth = _document_documentElement.clientWidth, clientHeight = _document_documentElement.clientHeight; var top = boundary.top, left = boundary.left, bottom = boundary.bottom, right = boundary.right; setBoundaryState({ top: -offsetTop + top, left: -offsetLeft + left, bottom: clientHeight - offsetHeight - offsetTop - bottom, right: clientWidth - offsetWidth - offsetLeft - right }); middleLine.current = clientWidth - offsetWidth - offsetLeft - (clientWidth - offsetWidth) / 2; } }; var bind = (0, _react1.useDrag)(function(state) { var down = state.down, last = state.last, _state_offset = (0, _sliced_to_array._)(state.offset, 2), x = _state_offset[0], y = _state_offset[1], first = state.first; first && (onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart()); onDrag === null || onDrag === void 0 ? void 0 : onDrag({ offset: [ x, y ] }); last && (onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd({ offset: [ x, y ] })); api.start({ x: x, y: y, immediate: down }); if (last) { if (direction !== 'y' && attract) { if (x < middleLine.current) { api.start({ x: boundaryState.left, y: y, immediate: down }); } else { api.start({ x: boundaryState.right, y: y, immediate: down }); } } } }, { from: function from() { return [ currstyle.x.get(), currstyle.y.get() ]; }, axis: direction, bounds: boundaryState }); (0, _react.useEffect)(function() { getInfo(); }, [ myDrag ]); return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread_props._)((0, _object_spread._)({ style: style, className: "".concat(classPrefix, " ").concat(className) }, reset), { ref: myDrag }), /*#__PURE__*/ _react.default.createElement(_web.animated.div, (0, _object_spread_props._)((0, _object_spread._)({ style: currstyle }, bind()), { className: "".concat(classPrefix, "-inner") }), children)); }; Drag.displayName = 'NutDrag';