@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
154 lines (153 loc) • 6.63 kB
JavaScript
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import React, { useCallback, useEffect, useRef, useState } from "react";
import classNames from "classnames";
import { getScrollParent } from "../../utils/get-scroll-parent";
import { getRect } from "../../hooks/use-client-rect";
import useWatch from "../../hooks/use-watch";
import { ComponentDefaults } from "../../utils/typings";
var defaultProps = _object_spread_props(_object_spread({}, ComponentDefaults), {
position: 'top',
threshold: 0,
zIndex: 900
});
var classPrefix = 'nut-sticky';
export var Sticky = function(props) {
var _ref = _object_spread({}, defaultProps, props), position = _ref.position, zIndex = _ref.zIndex, children = _ref.children, container = _ref.container, style = _ref.style, className = _ref.className, threshold = _ref.threshold, onChange = _ref.onChange, rest = _object_without_properties(_ref, [
"position",
"zIndex",
"children",
"container",
"style",
"className",
"threshold",
"onChange"
]);
var stickyRef = useRef(null);
var rootRef = useRef(null);
var _useState = _sliced_to_array(useState(false), 2), isFixed = _useState[0], setIsFixed = _useState[1];
var _obj;
var _useState1 = _sliced_to_array(useState((_obj = {}, _define_property(_obj, position, "".concat(threshold, "px")), _define_property(_obj, "zIndex", zIndex), _obj)), 2), stickyStyle = _useState1[0], setStickyStyle = _useState1[1];
useEffect(function() {
var _obj;
setStickyStyle(_object_spread_props(_object_spread({}, stickyStyle), (_obj = {}, _define_property(_obj, position, "".concat(threshold, "px")), _define_property(_obj, "zIndex", zIndex), _obj)));
}, [
threshold,
position,
zIndex
]);
var _useState2 = _sliced_to_array(useState({}), 2), rootStyle = _useState2[0], setRootStyle = _useState2[1];
var getElement = useCallback(function() {
return getScrollParent(rootRef.current);
}, []);
useEffect(function() {
if (position === 'top') return;
var containerEle = container && container.current;
var rootEle = rootRef.current;
var stickyEle = stickyRef.current;
if (!rootEle && !containerEle) return;
var rootRect = getRect(rootEle);
var containerRect = getRect(containerEle);
var clientHeight = document.documentElement.clientHeight;
var stickyRect = getRect(stickyEle);
var fixed = clientHeight - threshold < rootRect.bottom;
if (containerEle) {
fixed = containerRect.bottom > clientHeight - threshold - stickyRect.height && clientHeight - threshold - stickyRect.height > containerRect.top;
}
var defaultPostVal = fixed ? 'fixed' : 'inherit';
setStickyStyle(function(prev) {
return _object_spread_props(_object_spread({}, prev), {
position: defaultPostVal
});
});
setIsFixed(fixed);
}, [
position,
container,
threshold
]);
var handleScroll = useCallback(function() {
var containerEle = container && container.current;
var rootEle = rootRef.current;
var stickyEle = stickyRef.current;
if (!rootEle && !containerEle) return;
var rootRect = getRect(rootEle);
var stickyRect = getRect(stickyEle);
var containerRect = getRect(containerEle);
if (rootRect.height) {
setRootStyle(function(prev) {
return _object_spread_props(_object_spread({}, prev), {
height: rootRect.height
});
});
}
var getFixed = function() {
var fixed = false;
if (position === 'top') {
fixed = containerEle ? threshold > rootRect.top && containerRect.bottom > 0 : threshold > rootRect.top;
} else {
var clientHeight = document.documentElement.clientHeight;
fixed = containerEle ? containerRect.bottom > 0 && clientHeight - threshold - stickyRect.height > containerRect.top : clientHeight - threshold < rootRect.bottom;
}
return {
position: fixed ? 'fixed' : 'inherit',
fixed: fixed
};
};
var getTransform = function() {
if (position === 'top' && containerEle) {
var diff = containerRect.bottom - threshold - stickyRect.height;
var transform = diff < 0 ? diff : 0;
return {
transform: "translate3d(0, ".concat(transform, "px, 0)")
};
}
if (position === 'bottom' && containerEle) {
var clientHeight = document.documentElement.clientHeight;
var diff1 = containerRect.bottom - (clientHeight - threshold);
var transform1 = diff1 < 0 ? diff1 : 0;
return {
transform: "translate3d(0, ".concat(transform1, "px, 0)")
};
}
return {};
};
var fixed = getFixed();
setStickyStyle(function(prev) {
return _object_spread_props(_object_spread({}, prev, getTransform()), {
position: fixed.position
});
});
setIsFixed(fixed.fixed);
}, [
position,
threshold,
container
]);
useWatch(isFixed, function() {
onChange && onChange(isFixed);
});
useEffect(function() {
var el = getElement();
el === null || el === void 0 ? void 0 : el.addEventListener('scroll', handleScroll, false);
return function() {
el === null || el === void 0 ? void 0 : el.removeEventListener('scroll', handleScroll);
};
}, [
getElement,
handleScroll
]);
return /*#__PURE__*/ React.createElement("div", _object_spread({
ref: rootRef,
style: _object_spread({}, style, rootStyle),
className: classNames(classPrefix, className)
}, rest), /*#__PURE__*/ React.createElement("div", {
className: "nut-sticky-box",
ref: stickyRef,
style: stickyStyle
}, children));
};
Sticky.displayName = 'NutSticky';