@aliretail/react-materials-components
Version:
50 lines (44 loc) • 1.87 kB
JavaScript
import _Affix from "@alifd/next/es/affix";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["children", "className", "defaultHash", "container"];
import * as React from 'react';
import classnames from 'classnames';
var FixedNav = function FixedNav(props) {
var children = props.children,
className = props.className,
defaultHash = props.defaultHash,
container = props.container,
restProps = _objectWithoutPropertiesLoose(props, _excluded);
var _React$useState = React.useState(function () {
return decodeURIComponent(window.location.hash) || defaultHash;
}),
hash = _React$useState[0],
setHash = _React$useState[1];
var divRef = React.useRef(null);
React.useEffect(function () {
var handleHashChange = function handleHashChange(e) {
var target = e.target;
setHash(decodeURIComponent(target.location.hash));
};
window.addEventListener('hashchange', handleHashChange);
return function () {
window.removeEventListener('hashchange', handleHashChange);
};
}, []); // fusion 的container返回值React.ReactElement有问题 as 成any避免报错
var fusionAffixContainer = container ? function () {
return container === null || container === void 0 ? void 0 : container(divRef.current);
} : undefined;
return /*#__PURE__*/React.createElement("div", {
ref: divRef,
className: "aliretail-fixed-nav-container"
}, /*#__PURE__*/React.createElement(_Affix, _extends({
className: classnames('aliretail-fixed-nav', className),
container: fusionAffixContainer
}, restProps), React.Children.map(children, function (child) {
return /*#__PURE__*/React.cloneElement(child, {
hashName: hash
});
})));
};
export default FixedNav;