UNPKG

@vnmfify/core

Version:

```shell npm i @vnmfify/core -S ```

110 lines (98 loc) 5.1 kB
var _excluded = ["arrayedIndex", "index", "children", "className"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { View } from "@vnxjs/components"; import { pageScrollTo } from "@vnxjs/vnmf"; import classNames from "classnames"; import * as React from "react"; import { forwardRef, useCallback, useContext, useImperativeHandle, useRef } from "react"; import { prefixClassname } from "../styles"; import { HAIRLINE_BORDER_BOTTOM } from "../styles/hairline"; import { getRect } from "../utils/dom/rect"; import { addUnitPx } from "../utils/format/unit"; import IndexListContext from "./index-list.context"; var IndexListAnchor = /*#__PURE__*/forwardRef((props, ref) => { var { arrayedIndex, index, children, className } = props, restProps = _objectWithoutProperties(props, _excluded); var { activeArrayedIndex, sticky: stickyProp, stickyOffsetTop, getAnchorRects, getListRect } = useContext(IndexListContext); var rootRef = useRef(); var scrollIntoView = useCallback(scrollTop => getRect(rootRef).then(_ref => { var { top } = _ref; return pageScrollTo({ duration: 0, scrollTop: Math.ceil(scrollTop + top) }); }), []); useImperativeHandle(ref, () => ({ scrollIntoView })); var wrapperStyle = {}; var anchorStyle = {}; var active = false; if (stickyProp) { if (arrayedIndex === activeArrayedIndex) { var { top, height } = getAnchorRects()[arrayedIndex]; var activeAnchorSticky = top <= 0; if (activeAnchorSticky) { wrapperStyle = { height: addUnitPx(height) }; anchorStyle = { position: "fixed", top: addUnitPx(stickyOffsetTop) }; } active = true; } else if (arrayedIndex === activeArrayedIndex - 1) { var listRect = getListRect(); var anchorRects = getAnchorRects(); var currentAnchor = anchorRects[arrayedIndex]; var currentOffsetTop = currentAnchor.top; var targetOffsetTop = arrayedIndex === anchorRects.length - 1 ? listRect.top : anchorRects[arrayedIndex + 1].top; var parentOffsetHeight = targetOffsetTop - currentOffsetTop; var translateY = parentOffsetHeight - currentAnchor.height; anchorStyle = { position: "relative", transform: "translate3d(0, ".concat(addUnitPx(translateY), ", 0)") }; active = true; } else { wrapperStyle = {}; anchorStyle = {}; active = false; } } return /*#__PURE__*/React.createElement(View, _objectSpread({ ref: rootRef, className: classNames(prefixClassname("index-list__anchor-wrapper"), className), style: wrapperStyle }, restProps), /*#__PURE__*/React.createElement(View, { className: classNames(prefixClassname("index-list__anchor"), { [prefixClassname("index-list__anchor--sticky")]: active, [HAIRLINE_BORDER_BOTTOM]: active }), style: anchorStyle, children: children !== null && children !== void 0 ? children : index })); }); export default IndexListAnchor; //# sourceMappingURL=index-list-anchor.js.map