ant-design/components/affix/index.tsx

297 lines
8.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2019-03-03 10:04:21 +08:00
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
2016-09-14 16:59:45 +08:00
import omit from 'omit.js';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
2019-04-07 10:06:49 +08:00
import ResizeObserver from '../_util/resizeObserver';
2015-08-04 14:57:18 +08:00
2019-03-03 10:04:21 +08:00
import warning from '../_util/warning';
import {
addObserveTarget,
removeObserveTarget,
getTargetRect,
getFixedTop,
getFixedBottom,
} from './utils';
function getDefaultTarget() {
2018-05-23 21:03:32 +08:00
return typeof window !== 'undefined' ? window : null;
}
2016-06-22 13:18:43 +08:00
// Affix
export interface AffixProps {
/**
*
*/
2016-07-13 11:14:24 +08:00
offsetTop?: number;
offset?: number;
/** 距离窗口底部达到指定偏移量后触发 */
2016-07-13 11:14:24 +08:00
offsetBottom?: number;
style?: React.CSSProperties;
/** 固定状态改变时触发的回调函数 */
onChange?: (affixed?: boolean) => void;
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
2017-11-22 11:49:35 +08:00
target?: () => Window | HTMLElement | null;
prefixCls?: string;
2019-01-31 11:20:58 +08:00
className?: string;
2019-03-03 10:04:21 +08:00
}
enum AffixStatus {
None,
Prepare,
2016-06-22 13:18:43 +08:00
}
2017-11-22 11:49:35 +08:00
export interface AffixState {
2019-03-03 10:04:21 +08:00
affixStyle?: React.CSSProperties;
placeholderStyle?: React.CSSProperties;
status: AffixStatus;
lastAffix: boolean;
prevTarget: Window | HTMLElement | null;
2017-11-22 11:49:35 +08:00
}
2019-03-03 10:04:21 +08:00
class Affix extends React.Component<AffixProps, AffixState> {
static defaultProps = {
target: getDefaultTarget,
2016-08-23 21:00:35 +08:00
};
2015-09-01 16:18:46 +08:00
2018-11-01 02:46:42 +08:00
state: AffixState = {
2019-03-03 11:40:10 +08:00
status: AffixStatus.None,
2019-03-03 10:04:21 +08:00
lastAffix: false,
prevTarget: null,
2018-11-01 02:46:42 +08:00
};
2016-07-13 17:22:23 +08:00
2019-03-03 10:04:21 +08:00
placeholderNode: HTMLDivElement;
fixedNode: HTMLDivElement;
2019-03-03 11:40:10 +08:00
private timeout: number;
2017-08-17 11:45:46 +08:00
2019-03-03 10:04:21 +08:00
// Event handler
componentDidMount() {
const { target } = this.props;
if (target) {
2019-03-03 23:19:07 +08:00
// [Legacy] Wait for parent component ref has its value.
// We should use target as directly element instead of function which makes element check hard.
2019-03-03 10:04:21 +08:00
this.timeout = setTimeout(() => {
addObserveTarget(target(), this);
2019-03-03 11:40:10 +08:00
// Mock Event object.
this.updatePosition({} as Event);
2019-03-03 10:04:21 +08:00
});
2016-07-09 16:00:05 +08:00
}
}
2019-03-03 10:04:21 +08:00
componentDidUpdate(prevProps: AffixProps) {
const { prevTarget } = this.state;
2019-03-03 10:04:21 +08:00
const { target } = this.props;
let newTarget = null;
if (target) {
newTarget = target() || null;
}
if (prevTarget !== newTarget) {
2019-03-03 10:04:21 +08:00
removeObserveTarget(this);
if (newTarget) {
addObserveTarget(newTarget, this);
2019-03-03 11:40:10 +08:00
// Mock Event object.
2019-03-03 23:19:07 +08:00
this.updatePosition({} as Event);
2019-03-03 10:04:21 +08:00
}
this.setState({ prevTarget: newTarget });
2016-07-11 14:52:21 +08:00
}
2019-03-03 23:19:07 +08:00
if (
prevProps.offsetTop !== this.props.offsetTop ||
prevProps.offsetBottom !== this.props.offsetBottom
) {
this.updatePosition({} as Event);
}
2019-03-03 10:04:21 +08:00
this.measure();
}
2019-03-03 10:04:21 +08:00
componentWillUnmount() {
clearTimeout(this.timeout);
removeObserveTarget(this);
2019-03-03 11:40:10 +08:00
(this.updatePosition as any).cancel();
2019-03-03 10:04:21 +08:00
}
2015-08-03 16:49:42 +08:00
getOffsetTop = () => {
const { offset, offsetBottom } = this.props;
let { offsetTop } = this.props;
if (typeof offsetTop === 'undefined') {
offsetTop = offset;
warning(
typeof offset === 'undefined',
'Affix',
'`offset` is deprecated. Please use `offsetTop` instead.',
);
}
if (offsetBottom === undefined && offsetTop === undefined) {
offsetTop = 0;
}
return offsetTop;
};
getOffsetBottom = () => {
return this.props.offsetBottom;
};
2019-03-03 10:04:21 +08:00
savePlaceholderNode = (node: HTMLDivElement) => {
this.placeholderNode = node;
};
2019-03-03 10:04:21 +08:00
saveFixedNode = (node: HTMLDivElement) => {
this.fixedNode = node;
};
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
// =================== Measure ===================
// Handle realign logic
@throttleByAnimationFrameDecorator()
updatePosition(event?: Event | null) {
this.prepareMeasure(event);
}
@throttleByAnimationFrameDecorator()
lazyUpdatePosition(event: Event) {
const { target } = this.props;
const { affixStyle } = this.state;
// Check position change before measure to make Safari smooth
if (target && affixStyle) {
const offsetTop = this.getOffsetTop();
const offsetBottom = this.getOffsetBottom();
const targetNode = target();
if (targetNode) {
const targetRect = getTargetRect(targetNode);
const placeholderReact = getTargetRect(this.placeholderNode);
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
if (
(fixedTop !== undefined && affixStyle.top === fixedTop) ||
(fixedBottom !== undefined && affixStyle.bottom === fixedBottom)
) {
return;
}
}
}
// Directly call prepare measure since it's already throttled.
this.prepareMeasure(event);
}
2019-03-03 11:40:10 +08:00
// @ts-ignore TS6133
prepareMeasure = (event?: Event | null) => {
2019-03-03 11:40:10 +08:00
// event param is used before. Keep compatible ts define here.
2019-03-03 10:04:21 +08:00
this.setState({
status: AffixStatus.Prepare,
affixStyle: undefined,
placeholderStyle: undefined,
});
2019-04-07 10:06:49 +08:00
// Test if `updatePosition` called
if (process.env.NODE_ENV === 'test') {
const { onTestUpdatePosition } = this.props as any;
if (onTestUpdatePosition) {
onTestUpdatePosition();
}
}
};
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
measure = () => {
2019-03-03 11:40:10 +08:00
const { status, lastAffix } = this.state;
const { target, onChange } = this.props;
2019-03-03 10:04:21 +08:00
if (status !== AffixStatus.Prepare || !this.fixedNode || !this.placeholderNode || !target) {
return;
2015-08-04 15:03:00 +08:00
}
2019-03-03 11:40:10 +08:00
const offsetTop = this.getOffsetTop();
const offsetBottom = this.getOffsetBottom();
2019-03-03 11:40:10 +08:00
2019-03-03 10:04:21 +08:00
const targetNode = target();
if (!targetNode) {
return;
}
2019-03-03 10:04:21 +08:00
const newState: Partial<AffixState> = {
status: AffixStatus.None,
};
const targetRect = getTargetRect(targetNode);
const placeholderReact = getTargetRect(this.placeholderNode);
const fixedTop = getFixedTop(placeholderReact, targetRect, offsetTop);
const fixedBottom = getFixedBottom(placeholderReact, targetRect, offsetBottom);
if (fixedTop !== undefined) {
2019-03-03 10:04:21 +08:00
newState.affixStyle = {
position: 'fixed',
top: fixedTop,
2019-03-03 23:25:52 +08:00
width: placeholderReact.width,
height: placeholderReact.height,
2019-03-03 10:04:21 +08:00
};
newState.placeholderStyle = {
width: placeholderReact.width,
height: placeholderReact.height,
};
} else if (fixedBottom !== undefined) {
2019-03-03 10:04:21 +08:00
newState.affixStyle = {
position: 'fixed',
bottom: fixedBottom,
2019-03-03 23:25:52 +08:00
width: placeholderReact.width,
height: placeholderReact.height,
2019-03-03 10:04:21 +08:00
};
newState.placeholderStyle = {
width: placeholderReact.width,
height: placeholderReact.height,
};
}
2019-03-03 11:40:10 +08:00
newState.lastAffix = !!newState.affixStyle;
if (onChange && lastAffix !== newState.lastAffix) {
onChange(newState.lastAffix);
}
2019-03-03 10:04:21 +08:00
this.setState(newState as AffixState);
2019-03-03 11:40:10 +08:00
};
2019-03-03 10:04:21 +08:00
// =================== Render ===================
renderAffix = ({ getPrefixCls }: ConfigConsumerProps) => {
2019-03-03 10:04:21 +08:00
const { affixStyle, placeholderStyle, status } = this.state;
const { prefixCls, style, children } = this.props;
const className = classNames({
2019-03-03 10:04:21 +08:00
[getPrefixCls('affix', prefixCls)]: affixStyle,
});
2015-08-03 16:49:42 +08:00
2019-04-07 10:06:49 +08:00
let props = omit(this.props, ['prefixCls', 'offsetTop', 'offsetBottom', 'target', 'onChange']);
// Omit this since `onTestUpdatePosition` only works on test.
if (process.env.NODE_ENV === 'test') {
props = omit(props, ['onTestUpdatePosition']);
}
2019-03-03 10:04:21 +08:00
const mergedPlaceholderStyle = {
...(status === AffixStatus.None ? placeholderStyle : null),
2019-03-03 11:40:10 +08:00
...style,
2019-03-03 10:04:21 +08:00
};
2015-08-03 16:49:42 +08:00
return (
2019-03-03 10:04:21 +08:00
<div {...props} style={mergedPlaceholderStyle} ref={this.savePlaceholderNode}>
<div className={className} ref={this.saveFixedNode} style={this.state.affixStyle}>
<ResizeObserver
onResize={() => {
this.updatePosition();
}}
>
{children}
</ResizeObserver>
2015-08-04 14:57:18 +08:00
</div>
2015-08-03 16:49:42 +08:00
</div>
);
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderAffix}</ConfigConsumer>;
}
}
2019-03-03 10:04:21 +08:00
polyfill(Affix);
export default Affix;