2015-11-24 20:03:57 +08:00
|
|
|
import classNames from 'classnames';
|
2019-09-29 17:56:56 +08:00
|
|
|
import ResizeObserver from 'rc-resize-observer';
|
2022-06-22 14:57:09 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-02-05 10:51:51 +08:00
|
|
|
import React, { createRef, forwardRef, useContext } from 'react';
|
2023-05-18 20:35:09 +08:00
|
|
|
import throttleByAnimationFrame from '../_util/throttleByAnimationFrame';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-22 15:18:03 +08:00
|
|
|
import useStyle from './style';
|
2023-05-18 20:35:09 +08:00
|
|
|
import { getFixedBottom, getFixedTop, getTargetRect } from './utils';
|
|
|
|
|
|
|
|
const TRIGGER_EVENTS = [
|
|
|
|
'resize',
|
|
|
|
'scroll',
|
|
|
|
'touchstart',
|
|
|
|
'touchmove',
|
|
|
|
'touchend',
|
|
|
|
'pageshow',
|
|
|
|
'load',
|
|
|
|
] as const;
|
2016-10-21 17:44:34 +08:00
|
|
|
|
|
|
|
function getDefaultTarget() {
|
2018-05-23 21:03:32 +08:00
|
|
|
return typeof window !== 'undefined' ? window : null;
|
2017-05-25 16:54:15 +08:00
|
|
|
}
|
2016-10-21 17:44:34 +08:00
|
|
|
|
2016-06-22 13:18:43 +08:00
|
|
|
// Affix
|
|
|
|
export interface AffixProps {
|
2023-04-07 23:27:50 +08:00
|
|
|
/** Triggered when the specified offset is reached from the top of the window */
|
2016-07-13 11:14:24 +08:00
|
|
|
offsetTop?: number;
|
2023-04-07 23:27:50 +08:00
|
|
|
/** Triggered when the specified offset is reached from the bottom of the window */
|
2016-07-13 11:14:24 +08:00
|
|
|
offsetBottom?: number;
|
|
|
|
style?: React.CSSProperties;
|
2023-04-07 23:27:50 +08:00
|
|
|
/** Callback function triggered when fixed state changes */
|
2016-10-21 17:44:34 +08:00
|
|
|
onChange?: (affixed?: boolean) => void;
|
2023-04-07 23:27:50 +08:00
|
|
|
/** Set the element that Affix needs to listen to its scroll event, the value is a function that returns the corresponding DOM element */
|
2017-11-22 11:49:35 +08:00
|
|
|
target?: () => Window | HTMLElement | null;
|
2016-09-14 16:18:33 +08:00
|
|
|
prefixCls?: string;
|
2019-01-31 11:20:58 +08:00
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2020-04-18 00:31:31 +08:00
|
|
|
children: React.ReactNode;
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
|
|
|
|
2022-03-22 10:32:54 +08:00
|
|
|
interface InternalAffixProps extends AffixProps {
|
2022-03-14 16:22:18 +08:00
|
|
|
affixPrefixCls: 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;
|
2019-04-18 01:33:05 +08:00
|
|
|
prevTarget: Window | HTMLElement | null;
|
2017-11-22 11:49:35 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 10:42:25 +08:00
|
|
|
class InternalAffix extends React.Component<InternalAffixProps, AffixState> {
|
2020-04-29 20:22:16 +08:00
|
|
|
static contextType = ConfigContext;
|
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,
|
2019-04-18 01:33:05 +08:00
|
|
|
prevTarget: null,
|
2018-11-01 02:46:42 +08:00
|
|
|
};
|
2016-07-13 17:22:23 +08:00
|
|
|
|
2023-02-05 10:51:51 +08:00
|
|
|
private placeholderNodeRef = createRef<HTMLDivElement>();
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-02-05 10:51:51 +08:00
|
|
|
private fixedNodeRef = createRef<HTMLDivElement>();
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2023-02-05 10:51:51 +08:00
|
|
|
private timer: NodeJS.Timeout | null;
|
2017-08-17 11:45:46 +08:00
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
context: ConfigConsumerProps;
|
|
|
|
|
|
|
|
private getTargetFunc() {
|
|
|
|
const { getTargetContainer } = this.context;
|
|
|
|
const { target } = this.props;
|
|
|
|
|
|
|
|
if (target !== undefined) {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:53:06 +08:00
|
|
|
return getTargetContainer ?? getDefaultTarget;
|
2020-04-29 20:22:16 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// Event handler
|
|
|
|
componentDidMount() {
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
|
|
|
if (targetFunc) {
|
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.
|
2023-05-18 20:35:09 +08:00
|
|
|
const target = targetFunc?.() || null;
|
2023-02-05 10:51:51 +08:00
|
|
|
this.timer = setTimeout(() => {
|
2023-05-18 20:35:09 +08:00
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
|
|
|
target?.addEventListener(eventName, this.lazyUpdatePosition);
|
|
|
|
});
|
2019-03-03 11:40:10 +08:00
|
|
|
// Mock Event object.
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
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) {
|
2019-04-18 01:33:05 +08:00
|
|
|
const { prevTarget } = this.state;
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
2021-12-04 00:10:59 +08:00
|
|
|
const newTarget = targetFunc?.() || null;
|
2019-04-18 01:33:05 +08:00
|
|
|
if (prevTarget !== newTarget) {
|
|
|
|
if (newTarget) {
|
2023-05-18 20:35:09 +08:00
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
|
|
|
prevTarget?.removeEventListener(eventName, this.lazyUpdatePosition);
|
|
|
|
newTarget?.addEventListener(eventName, this.lazyUpdatePosition);
|
|
|
|
});
|
2019-03-03 11:40:10 +08:00
|
|
|
// Mock Event object.
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
2019-04-18 01:33:05 +08:00
|
|
|
|
2020-06-08 18:01:50 +08:00
|
|
|
// eslint-disable-next-line react/no-did-update-set-state
|
2019-04-18 01:33:05 +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
|
|
|
|
) {
|
2019-08-05 18:38:10 +08:00
|
|
|
this.updatePosition();
|
2019-03-03 23:19:07 +08:00
|
|
|
}
|
2019-03-03 10:04:21 +08:00
|
|
|
this.measure();
|
2018-03-14 16:22:09 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
componentWillUnmount() {
|
2023-02-05 10:51:51 +08:00
|
|
|
if (this.timer) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
2022-11-24 10:08:44 +08:00
|
|
|
}
|
2023-05-18 20:35:09 +08:00
|
|
|
const { prevTarget } = this.state;
|
|
|
|
const targetFunc = this.getTargetFunc();
|
|
|
|
const newTarget = targetFunc?.();
|
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
|
|
|
newTarget?.removeEventListener(eventName, this.lazyUpdatePosition);
|
|
|
|
});
|
|
|
|
if (prevTarget !== newTarget) {
|
|
|
|
TRIGGER_EVENTS.forEach((eventName) => {
|
|
|
|
prevTarget?.removeEventListener(eventName, this.lazyUpdatePosition);
|
|
|
|
});
|
|
|
|
}
|
2022-11-19 13:45:56 +08:00
|
|
|
this.updatePosition.cancel();
|
2020-03-31 21:25:40 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/22683
|
2022-11-19 13:45:56 +08:00
|
|
|
this.lazyUpdatePosition.cancel();
|
2019-03-03 10:04:21 +08:00
|
|
|
}
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
getOffsetTop = () => {
|
2021-12-04 00:10:59 +08:00
|
|
|
const { offsetBottom, offsetTop } = this.props;
|
2022-03-02 17:05:59 +08:00
|
|
|
return offsetBottom === undefined && offsetTop === undefined ? 0 : offsetTop;
|
2019-04-24 19:53:53 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
getOffsetBottom = () => this.props.offsetBottom;
|
2019-04-24 19:53:53 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// =================== Measure ===================
|
|
|
|
measure = () => {
|
2019-03-03 11:40:10 +08:00
|
|
|
const { status, lastAffix } = this.state;
|
2020-04-29 20:22:16 +08:00
|
|
|
const { onChange } = this.props;
|
|
|
|
const targetFunc = this.getTargetFunc();
|
2023-02-05 10:51:51 +08:00
|
|
|
if (
|
|
|
|
status !== AffixStatus.Prepare ||
|
|
|
|
!this.fixedNodeRef.current ||
|
|
|
|
!this.placeholderNodeRef.current ||
|
|
|
|
!targetFunc
|
|
|
|
) {
|
2019-03-03 10:04:21 +08:00
|
|
|
return;
|
2015-08-04 15:03:00 +08:00
|
|
|
}
|
2019-03-03 11:40:10 +08:00
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
2019-03-03 11:40:10 +08:00
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetNode = targetFunc();
|
2019-03-03 10:04:21 +08:00
|
|
|
if (!targetNode) {
|
2016-12-03 16:19:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
const newState: Partial<AffixState> = {
|
|
|
|
status: AffixStatus.None,
|
|
|
|
};
|
2023-02-24 09:04:03 +08:00
|
|
|
const placeholderRect = getTargetRect(this.placeholderNodeRef.current);
|
2016-08-18 11:27:37 +08:00
|
|
|
|
2022-11-07 11:58:32 +08:00
|
|
|
if (
|
2023-02-24 09:04:03 +08:00
|
|
|
placeholderRect.top === 0 &&
|
|
|
|
placeholderRect.left === 0 &&
|
|
|
|
placeholderRect.width === 0 &&
|
|
|
|
placeholderRect.height === 0
|
2022-11-07 11:58:32 +08:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-26 19:58:59 +08:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
|
|
|
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom);
|
|
|
|
|
2019-04-24 19:53:53 +08:00
|
|
|
if (fixedTop !== undefined) {
|
2019-03-03 10:04:21 +08:00
|
|
|
newState.affixStyle = {
|
|
|
|
position: 'fixed',
|
2019-04-24 19:53:53 +08:00
|
|
|
top: fixedTop,
|
2023-02-24 09:04:03 +08:00
|
|
|
width: placeholderRect.width,
|
|
|
|
height: placeholderRect.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
2023-02-24 09:04:03 +08:00
|
|
|
width: placeholderRect.width,
|
|
|
|
height: placeholderRect.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
2019-04-24 19:53:53 +08:00
|
|
|
} else if (fixedBottom !== undefined) {
|
2019-03-03 10:04:21 +08:00
|
|
|
newState.affixStyle = {
|
|
|
|
position: 'fixed',
|
2019-04-24 19:53:53 +08:00
|
|
|
bottom: fixedBottom,
|
2023-02-24 09:04:03 +08:00
|
|
|
width: placeholderRect.width,
|
|
|
|
height: placeholderRect.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
|
|
|
newState.placeholderStyle = {
|
2023-02-24 09:04:03 +08:00
|
|
|
width: placeholderRect.width,
|
|
|
|
height: placeholderRect.height,
|
2019-03-03 10:04:21 +08:00
|
|
|
};
|
|
|
|
}
|
2017-09-08 09:31:07 +08:00
|
|
|
|
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
|
|
|
};
|
2018-03-14 16:22:09 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
prepareMeasure = () => {
|
|
|
|
// event param is used before. Keep compatible ts define here.
|
|
|
|
this.setState({
|
|
|
|
status: AffixStatus.Prepare,
|
|
|
|
affixStyle: undefined,
|
|
|
|
placeholderStyle: undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test if `updatePosition` called
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
const { onTestUpdatePosition } = this.props as any;
|
2021-02-19 18:26:53 +08:00
|
|
|
onTestUpdatePosition?.();
|
2019-08-05 18:38:10 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-19 13:45:56 +08:00
|
|
|
updatePosition = throttleByAnimationFrame(() => {
|
2019-08-05 18:38:10 +08:00
|
|
|
this.prepareMeasure();
|
2022-11-19 13:45:56 +08:00
|
|
|
});
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-11-19 13:45:56 +08:00
|
|
|
lazyUpdatePosition = throttleByAnimationFrame(() => {
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetFunc = this.getTargetFunc();
|
2019-08-05 18:38:10 +08:00
|
|
|
const { affixStyle } = this.state;
|
|
|
|
|
|
|
|
// Check position change before measure to make Safari smooth
|
2020-04-29 20:22:16 +08:00
|
|
|
if (targetFunc && affixStyle) {
|
2019-08-05 18:38:10 +08:00
|
|
|
const offsetTop = this.getOffsetTop();
|
|
|
|
const offsetBottom = this.getOffsetBottom();
|
|
|
|
|
2020-04-29 20:22:16 +08:00
|
|
|
const targetNode = targetFunc();
|
2023-02-05 10:51:51 +08:00
|
|
|
if (targetNode && this.placeholderNodeRef.current) {
|
2019-08-05 18:38:10 +08:00
|
|
|
const targetRect = getTargetRect(targetNode);
|
2023-02-24 09:04:03 +08:00
|
|
|
const placeholderRect = getTargetRect(this.placeholderNodeRef.current);
|
|
|
|
const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop);
|
|
|
|
const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom);
|
2019-08-05 18:38:10 +08:00
|
|
|
|
|
|
|
if (
|
|
|
|
(fixedTop !== undefined && affixStyle.top === fixedTop) ||
|
|
|
|
(fixedBottom !== undefined && affixStyle.bottom === fixedBottom)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Directly call prepare measure since it's already throttled.
|
|
|
|
this.prepareMeasure();
|
2022-11-19 13:45:56 +08:00
|
|
|
});
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2019-03-03 10:04:21 +08:00
|
|
|
// =================== Render ===================
|
2021-10-15 09:30:56 +08:00
|
|
|
render() {
|
2019-07-16 20:48:03 +08:00
|
|
|
const { affixStyle, placeholderStyle } = this.state;
|
2022-03-22 13:27:12 +08:00
|
|
|
const { affixPrefixCls, rootClassName, children } = this.props;
|
2023-01-20 11:03:50 +08:00
|
|
|
const className = classNames(affixStyle && rootClassName, {
|
2022-03-14 16:22:18 +08:00
|
|
|
[affixPrefixCls]: !!affixStyle,
|
2015-10-09 10:55:52 +08:00
|
|
|
});
|
2015-08-03 16:49:42 +08:00
|
|
|
|
2022-03-14 16:22:18 +08:00
|
|
|
let props = omit(this.props, [
|
|
|
|
'prefixCls',
|
|
|
|
'offsetTop',
|
|
|
|
'offsetBottom',
|
|
|
|
'target',
|
|
|
|
'onChange',
|
|
|
|
'affixPrefixCls',
|
2022-03-22 13:27:12 +08:00
|
|
|
'rootClassName',
|
2022-03-14 16:22:18 +08:00
|
|
|
]);
|
2019-04-07 10:06:49 +08:00
|
|
|
// Omit this since `onTestUpdatePosition` only works on test.
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
2021-01-13 21:00:30 +08:00
|
|
|
props = omit(props as typeof props & { onTestUpdatePosition: any }, ['onTestUpdatePosition']);
|
2019-04-07 10:06:49 +08:00
|
|
|
}
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2015-08-03 16:49:42 +08:00
|
|
|
return (
|
2022-11-19 13:45:56 +08:00
|
|
|
<ResizeObserver onResize={this.updatePosition}>
|
2023-02-05 10:51:51 +08:00
|
|
|
<div {...props} ref={this.placeholderNodeRef}>
|
2019-07-16 20:48:03 +08:00
|
|
|
{affixStyle && <div style={placeholderStyle} aria-hidden="true" />}
|
2023-02-05 10:51:51 +08:00
|
|
|
<div className={className} ref={this.fixedNodeRef} style={affixStyle}>
|
2022-11-19 13:45:56 +08:00
|
|
|
<ResizeObserver onResize={this.updatePosition}>{children}</ResizeObserver>
|
2019-07-16 20:48:03 +08:00
|
|
|
</div>
|
2015-08-04 14:57:18 +08:00
|
|
|
</div>
|
2019-07-16 20:48:03 +08:00
|
|
|
</ResizeObserver>
|
2015-08-03 16:49:42 +08:00
|
|
|
);
|
2021-10-15 09:30:56 +08:00
|
|
|
}
|
2016-03-29 17:33:37 +08:00
|
|
|
}
|
2022-06-06 17:49:55 +08:00
|
|
|
// just use in test
|
2023-02-22 10:42:25 +08:00
|
|
|
export type InternalAffixClass = InternalAffix;
|
2019-03-03 10:04:21 +08:00
|
|
|
|
2023-02-22 10:42:25 +08:00
|
|
|
const Affix = forwardRef<InternalAffix, AffixProps>((props, ref) => {
|
2023-01-20 11:03:50 +08:00
|
|
|
const { prefixCls: customizePrefixCls, rootClassName } = props;
|
2023-02-05 10:51:51 +08:00
|
|
|
const { getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
2022-03-14 16:22:18 +08:00
|
|
|
const affixPrefixCls = getPrefixCls('affix', customizePrefixCls);
|
|
|
|
|
2022-03-22 13:27:12 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(affixPrefixCls);
|
|
|
|
|
2022-03-14 16:22:18 +08:00
|
|
|
const AffixProps: InternalAffixProps = {
|
|
|
|
...props,
|
|
|
|
affixPrefixCls,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName: classNames(rootClassName, hashId),
|
2022-03-14 16:22:18 +08:00
|
|
|
};
|
|
|
|
|
2023-02-22 10:42:25 +08:00
|
|
|
return wrapSSR(<InternalAffix {...AffixProps} ref={ref} />);
|
2022-03-14 16:22:18 +08:00
|
|
|
});
|
2022-03-02 17:05:59 +08:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-02-22 10:42:25 +08:00
|
|
|
Affix.displayName = 'Affix';
|
2022-03-02 17:05:59 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 10:42:25 +08:00
|
|
|
export default Affix;
|