@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
198 lines (197 loc) • 8.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Sticky", {
enumerable: true,
get: function() {
return Sticky;
}
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _async_to_generator = require("@swc/helpers/_/_async_to_generator");
var _define_property = require("@swc/helpers/_/_define_property");
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 _ts_generator = require("@swc/helpers/_/_ts_generator");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _taro = require("@tarojs/taro");
var _components = require("@tarojs/components");
var _getsysteminfo = require("../../utils/taro/get-system-info");
var _typings = require("../../utils/typings");
var _usewatch = require("../../hooks/use-watch");
var _getrect = require("../../utils/taro/get-rect");
var _getscrollparent = require("../../utils/get-scroll-parent");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
position: 'top',
zIndex: 900
});
var classPrefix = 'nut-sticky';
var Sticky = function Sticky(props) {
var _ref = (0, _object_spread._)({}, defaultProps, props), position = _ref.position, zIndex = _ref.zIndex, threshold = _ref.threshold, style = _ref.style, children = _ref.children, container = _ref.container, className = _ref.className, onChange = _ref.onChange, rest = (0, _object_without_properties._)(_ref, [
"position",
"zIndex",
"threshold",
"style",
"children",
"container",
"className",
"onChange"
]);
var stickyRef = (0, _react.useRef)(null);
var rootRef = (0, _react.useRef)(null);
var _useState = (0, _sliced_to_array._)((0, _react.useState)({}), 2), rootRect = _useState[0], setRootRect = _useState[1];
var _useState1 = (0, _sliced_to_array._)((0, _react.useState)(false), 2), fixed = _useState1[0], setFixed = _useState1[1];
var _useState2 // 相对容器偏移距离
= (0, _sliced_to_array._)((0, _react.useState)(0), 2), transform = _useState2[0], setTransform = _useState2[1];
(0, _usewatch.useWatch)(fixed, function() {
onChange && onChange(fixed);
});
var rootStyle = (0, _react.useMemo)(function() {
if (!fixed) {
return {
height: '',
width: ''
};
}
var _rootRect_height, _rootRect_width;
var style = {
height: (_rootRect_height = rootRect.height) !== null && _rootRect_height !== void 0 ? _rootRect_height : '',
width: (_rootRect_width = rootRect.width) !== null && _rootRect_width !== void 0 ? _rootRect_width : ''
};
return style;
}, [
fixed,
rootRect.height,
rootRect.width
]);
var stickyStyle = (0, _react.useMemo)(function() {
if (!fixed) {
return (0, _define_property._)({
height: '',
width: ''
}, position, '');
}
var _rootRect_height, _rootRect_width;
var _obj;
var style = (_obj = {
height: (_rootRect_height = rootRect.height) !== null && _rootRect_height !== void 0 ? _rootRect_height : '',
width: (_rootRect_width = rootRect.width) !== null && _rootRect_width !== void 0 ? _rootRect_width : '',
transform: "translate3d(0, ".concat(transform, "px, 0)")
}, (0, _define_property._)(_obj, position, threshold), (0, _define_property._)(_obj, "zIndex", zIndex), _obj);
return style;
}, [
fixed,
rootRect.height,
rootRect.width,
transform,
position,
threshold,
zIndex
]);
var handleScroll = function handleScroll(scrollTop) {
return (0, _async_to_generator._)(function() {
var curRootRect, stickyRect, containerRect, difference, curTransform, curFixed, windowHeight;
return (0, _ts_generator._)(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
(0, _getrect.getRectInMultiPlatform)(rootRef.current)
];
case 1:
curRootRect = _state.sent();
return [
4,
(0, _getrect.getRectInMultiPlatform)(stickyRef.current)
];
case 2:
stickyRect = _state.sent();
if (!curRootRect || !stickyRect) {
console.warn('getRectByTaro获取失败', {
stickyRect: stickyRect,
curRootRect: curRootRect
});
return [
2
];
}
setRootRect(curRootRect);
if (!(position === 'top')) return [
3,
6
];
if (!container) return [
3,
4
];
return [
4,
(0, _getrect.getRectInMultiPlatform)(container.current)
];
case 3:
containerRect = _state.sent();
difference = containerRect.bottom - threshold - curRootRect.height;
curTransform = difference < 0 ? difference : 0;
setTransform(curTransform);
curFixed = threshold > curRootRect.top && containerRect.bottom > 0;
setFixed(curFixed);
return [
3,
5
];
case 4:
setFixed(threshold > curRootRect.top);
_state.label = 5;
case 5:
return [
3,
7
];
case 6:
windowHeight = (0, _getsysteminfo.getWindowInfo)().windowHeight;
setFixed(windowHeight - threshold < curRootRect.bottom);
_state.label = 7;
case 7:
return [
2
];
}
});
})();
};
var getElement = (0, _react.useCallback)(function() {
return (0, _getscrollparent.getScrollParent)(rootRef.current);
}, []);
(0, _react.useEffect)(function() {
if ((0, _taro.getEnv)() === 'WEB' && getElement() !== window) {
window.addEventListener('touchmove', handleScroll, true);
window.addEventListener('scroll', handleScroll, true);
return function() {
window.removeEventListener('touchmove', handleScroll);
window.removeEventListener('scroll', handleScroll);
};
}
}, []);
(0, _taro.usePageScroll)(function(res) {
if ((0, _taro.getEnv)() !== 'WEB') {
handleScroll(res.scrollTop);
}
});
return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
ref: rootRef,
style: rootStyle,
className: (0, _classnames.default)(classPrefix, className)
}, rest), /*#__PURE__*/ _react.default.createElement(_components.View, {
className: (0, _classnames.default)('nut-sticky-box', {
'nut-sticky-fixed': fixed
}),
ref: stickyRef,
style: stickyStyle
}, children));
};
Sticky.displayName = 'NutSticky';