-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathindex.tsx
98 lines (84 loc) · 2.61 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as React from 'react';
import { useRef } from 'react';
import classNames from 'classnames';
import CSSMotion from '@rc-component/motion';
import { offset } from '../../util';
import type { PanelProps, PanelRef } from './Panel';
import Panel from './Panel';
import type { CSSMotionRef } from '@rc-component/motion/es/CSSMotion';
export type CSSMotionStateRef = Pick<CSSMotionRef, 'inMotion' | 'enableMotion'>;
export type ContentRef = PanelRef & CSSMotionStateRef;
export type ContentProps = {
motionName: string;
ariaId: string;
onVisibleChanged: (visible: boolean) => void;
} & PanelProps;
const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
const {
prefixCls,
title,
style,
className,
visible,
forceRender,
destroyOnClose,
motionName,
ariaId,
onVisibleChanged,
mousePosition,
} = props;
const dialogRef = useRef<
{
nativeElement: HTMLElement;
} & CSSMotionStateRef
>();
const panelRef = useRef<PanelRef>();
// ============================== Refs ==============================
React.useImperativeHandle(ref, () => ({
...panelRef.current,
inMotion: dialogRef.current.inMotion,
enableMotion: dialogRef.current.enableMotion,
}));
// ============================= Style ==============================
const [transformOrigin, setTransformOrigin] = React.useState<string>();
const contentStyle: React.CSSProperties = {};
if (transformOrigin) {
contentStyle.transformOrigin = transformOrigin;
}
function onPrepare() {
const elementOffset = offset(dialogRef.current.nativeElement);
setTransformOrigin(
mousePosition && (mousePosition.x || mousePosition.y)
? `${mousePosition.x - elementOffset.left}px ${mousePosition.y - elementOffset.top}px`
: '',
);
}
// ============================= Render =============================
return (
<CSSMotion
visible={visible}
onVisibleChanged={onVisibleChanged}
onAppearPrepare={onPrepare}
onEnterPrepare={onPrepare}
forceRender={forceRender}
motionName={motionName}
removeOnLeave={destroyOnClose}
ref={dialogRef}
>
{({ className: motionClassName, style: motionStyle }, motionRef) => (
<Panel
{...props}
ref={panelRef}
title={title}
ariaId={ariaId}
prefixCls={prefixCls}
holderRef={motionRef}
style={{ ...motionStyle, ...style, ...contentStyle }}
className={classNames(className, motionClassName)}
/>
)}
</CSSMotion>
);
});
Content.displayName = 'Content';
export default Content;