UNPKG

tdesign-react

Version:
138 lines (134 loc) 5.69 kB
/** * tdesign v1.16.2 * (c) 2025 tdesign * @license MIT */ import React, { forwardRef, useRef, useCallback, useImperativeHandle, useEffect } from 'react'; import { isFunction } from 'lodash-es'; import useConfig from '../hooks/useConfig.js'; import { affixDefaultProps } from './defaultProps.js'; import useDefaultProps from '../hooks/useDefaultProps.js'; import { getScrollContainer } from '../_util/scroll.js'; import '../config-provider/ConfigContext.js'; import '../_chunks/dep-d67deb2c.js'; import '../_chunks/dep-8abcbcbc.js'; import '../_chunks/dep-6b235a32.js'; import '../_chunks/dep-b9afa32b.js'; import 'dayjs'; import 'raf'; import '../_chunks/dep-ca8d3fa0.js'; import '../_util/easing.js'; var Affix = /*#__PURE__*/forwardRef(function (props, ref) { var _useDefaultProps = useDefaultProps(props, affixDefaultProps), children = _useDefaultProps.children, content = _useDefaultProps.content, zIndex = _useDefaultProps.zIndex, container = _useDefaultProps.container, offsetBottom = _useDefaultProps.offsetBottom, offsetTop = _useDefaultProps.offsetTop, className = _useDefaultProps.className, style = _useDefaultProps.style, onFixedChange = _useDefaultProps.onFixedChange; var _useConfig = useConfig(), classPrefix = _useConfig.classPrefix; var affixRef = useRef(null); var affixWrapRef = useRef(null); var placeholderEL = useRef(null); var scrollContainer = useRef(null); var ticking = useRef(false); var handleScroll = useCallback(function () { if (!ticking.current) { window.requestAnimationFrame(function () { var _affixWrapRef$current, _affixWrapRef$current2, _scrollContainer$curr; var _ref = (_affixWrapRef$current = (_affixWrapRef$current2 = affixWrapRef.current) === null || _affixWrapRef$current2 === void 0 ? void 0 : _affixWrapRef$current2.getBoundingClientRect()) !== null && _affixWrapRef$current !== void 0 ? _affixWrapRef$current : { top: 0 }, _ref$top = _ref.top, wrapToTop = _ref$top === void 0 ? 0 : _ref$top, _ref$width = _ref.width, wrapWidth = _ref$width === void 0 ? 0 : _ref$width, _ref$height = _ref.height, wrapHeight = _ref$height === void 0 ? 0 : _ref$height; var containerToTop = 0; if (scrollContainer.current instanceof HTMLElement) { containerToTop = scrollContainer.current.getBoundingClientRect().top; } var calcTop = wrapToTop - containerToTop; var containerHeight = ((_scrollContainer$curr = scrollContainer.current) === null || _scrollContainer$curr === void 0 ? void 0 : _scrollContainer$curr[scrollContainer.current instanceof Window ? "innerHeight" : "clientHeight"]) - wrapHeight; var calcBottom = containerToTop + containerHeight - (offsetBottom !== null && offsetBottom !== void 0 ? offsetBottom : 0); var fixedTop; if (calcTop <= offsetTop) { fixedTop = containerToTop + offsetTop; } else if (wrapToTop >= calcBottom) { fixedTop = calcBottom; } else { fixedTop = false; } if (affixRef.current) { var affixed = fixedTop !== false; var placeholderStatus = affixWrapRef.current.contains(placeholderEL.current); var prePlaceholderStatus = placeholderStatus; if (affixed) { affixRef.current.className = "".concat(classPrefix, "-affix"); affixRef.current.style.top = "".concat(fixedTop, "px"); affixRef.current.style.width = "".concat(wrapWidth, "px"); affixRef.current.style.height = "".concat(wrapHeight, "px"); if (zIndex) { affixRef.current.style.zIndex = "".concat(zIndex); } if (!placeholderStatus) { placeholderEL.current.style.width = "".concat(wrapWidth, "px"); placeholderEL.current.style.height = "".concat(wrapHeight, "px"); affixWrapRef.current.appendChild(placeholderEL.current); placeholderStatus = true; } } else { affixRef.current.removeAttribute("class"); affixRef.current.removeAttribute("style"); if (placeholderStatus) { placeholderEL.current.remove(); placeholderStatus = false; } } if (prePlaceholderStatus !== placeholderStatus && isFunction(onFixedChange)) { onFixedChange(affixed, { top: +fixedTop }); } } ticking.current = false; }); } ticking.current = true; }, [classPrefix, offsetBottom, offsetTop, onFixedChange, zIndex]); useImperativeHandle(ref, function () { return { handleScroll: handleScroll }; }); useEffect(function () { placeholderEL.current = document.createElement("div"); }, []); useEffect(function () { scrollContainer.current = getScrollContainer(container); if (scrollContainer.current) { handleScroll(); scrollContainer.current.addEventListener("scroll", handleScroll); window.addEventListener("resize", handleScroll); return function () { scrollContainer.current.removeEventListener("scroll", handleScroll); window.removeEventListener("resize", handleScroll); }; } }, [container, handleScroll]); return /* @__PURE__ */React.createElement("div", { ref: affixWrapRef, className: className, style: style }, /* @__PURE__ */React.createElement("div", { ref: affixRef }, children || content)); }); Affix.displayName = "Affix"; export { Affix as default }; //# sourceMappingURL=Affix.js.map