@jdcfe/yep-react
Version:
一套移动端的React组件库
151 lines (135 loc) • 6.08 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import * as React from 'react';
import * as PropTypes from 'prop-types';
import * as ReactDOM from 'react-dom';
import StickyContainer from './StickyContainer';
/**
* @deprecated please use CSS: position: sticky; top: 0;
*/
var Sticky = /*#__PURE__*/function (_React$PureComponent) {
_inherits(Sticky, _React$PureComponent);
var _super = _createSuper(Sticky);
function Sticky(props) {
var _this;
_classCallCheck(this, Sticky);
_this = _super.call(this, props);
_this.createPlaceholderRef = _this.createPlaceholderRef.bind(_assertThisInitialized(_this));
_this.createContentRef = _this.createContentRef.bind(_assertThisInitialized(_this));
_this.handleContainerEvent = _this.handleContainerEvent.bind(_assertThisInitialized(_this));
_this.state = {
isSticky: false,
wasSticky: false,
style: {},
calculatedHeight: 0
};
return _this;
}
_createClass(Sticky, [{
key: "componentWillMount",
value: function componentWillMount() {
if (!this.context.subscribe) throw new TypeError('Expected Sticky to be mounted within StickyContainer');
this.context.subscribe(this.handleContainerEvent);
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.placeholder.style.paddingBottom = this.props.disableCompensation ? 0 : "".concat(this.state.isSticky ? this.state.calculatedHeight : 0, "px");
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.context.unsubscribe(this.handleContainerEvent);
} // @ts-ignore
}, {
key: "handleContainerEvent",
value: function handleContainerEvent(_ref) {
var distanceFromTop = _ref.distanceFromTop,
distanceFromBottom = _ref.distanceFromBottom,
eventSource = _ref.eventSource;
var _this$props = this.props,
relative = _this$props.relative,
bottomOffset = _this$props.bottomOffset,
topOffset = _this$props.topOffset,
disableHardwareAcceleration = _this$props.disableHardwareAcceleration;
var parent = this.context.getParent();
var preventingStickyStateChanges = false;
if (relative) {
preventingStickyStateChanges = eventSource !== parent;
distanceFromTop = -(eventSource.scrollTop + eventSource.offsetTop) + this.placeholder.offsetTop;
}
var placeholderClientRect = this.placeholder.getBoundingClientRect();
var contentClientRect = this.content.getBoundingClientRect();
var calculatedHeight = contentClientRect.height;
var bottomDifference = distanceFromBottom - bottomOffset - calculatedHeight;
var wasSticky = this.state.isSticky;
var isSticky = preventingStickyStateChanges ? wasSticky : distanceFromTop <= -topOffset && distanceFromBottom > -bottomOffset;
distanceFromBottom = (this.props.relative ? parent.scrollHeight - parent.scrollTop : distanceFromBottom) - calculatedHeight;
var style = !isSticky ? {} : {
position: 'fixed',
top: bottomDifference > 0 ? this.props.relative ? parent.offsetTop - parent.offsetParent.scrollTop : 0 : bottomDifference,
left: placeholderClientRect.left,
width: placeholderClientRect.width
};
if (!disableHardwareAcceleration) {
style.transform = 'translateZ(0)';
}
this.setState({
isSticky: isSticky,
wasSticky: wasSticky,
distanceFromTop: distanceFromTop,
distanceFromBottom: distanceFromBottom,
calculatedHeight: calculatedHeight,
style: style
});
}
}, {
key: "createPlaceholderRef",
value: function createPlaceholderRef(placeholder) {
this.placeholder = placeholder;
}
}, {
key: "createContentRef",
value: function createContentRef(content) {
this.content = ReactDOM.findDOMNode(content);
}
}, {
key: "render",
value: function render() {
var element = /*#__PURE__*/React.cloneElement(this.props.children({
isSticky: this.state.isSticky,
wasSticky: this.state.wasSticky,
distanceFromTop: this.state.distanceFromTop,
distanceFromBottom: this.state.distanceFromBottom,
calculatedHeight: this.state.calculatedHeight,
style: this.state.style
}), {
ref: this.createContentRef
});
return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
ref: this.createPlaceholderRef
}), element);
}
}]);
return Sticky;
}(React.PureComponent);
Sticky.StickyContainer = StickyContainer;
Sticky.defaultProps = {
relative: false,
topOffset: 0,
bottomOffset: 0,
disableCompensation: false,
disableHardwareAcceleration: false
};
Sticky.contextTypes = {
subscribe: PropTypes.func,
unsubscribe: PropTypes.func,
getParent: PropTypes.func
};
export default Sticky;