generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMotionThumb.tsx
169 lines (148 loc) · 4.48 KB
/
MotionThumb.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import classNames from 'classnames';
import CSSMotion from 'rc-motion';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import { composeRef } from 'rc-util/lib/ref';
import * as React from 'react';
import type { SegmentedValue } from '.';
type ThumbReact = {
left: number;
right: number;
width: number;
} | null;
export interface MotionThumbInterface {
containerRef: React.RefObject<HTMLDivElement>;
value?: SegmentedValue;
getValueIndex: (value: SegmentedValue) => number;
prefixCls: string;
motionName: string;
onMotionStart: VoidFunction;
onMotionEnd: VoidFunction;
direction?: 'ltr' | 'rtl';
}
const calcThumbStyle = (
targetElement: HTMLElement | null | undefined,
): ThumbReact =>
targetElement
? {
left: targetElement.offsetLeft,
right:
(targetElement.parentElement!.clientWidth as number) -
targetElement.clientWidth -
targetElement.offsetLeft,
width: targetElement.clientWidth,
}
: null;
const toPX = (value: number) =>
value !== undefined ? `${value}px` : undefined;
export default function MotionThumb(props: MotionThumbInterface) {
const {
prefixCls,
containerRef,
value,
getValueIndex,
motionName,
onMotionStart,
onMotionEnd,
direction,
} = props;
const thumbRef = React.useRef<HTMLDivElement>(null);
const [prevValue, setPrevValue] = React.useState(value);
// =========================== Effect ===========================
const findValueElement = (val?: SegmentedValue) => {
if (!val) return null;
const index = getValueIndex(val);
const ele = containerRef.current?.querySelectorAll<HTMLDivElement>(
`.${prefixCls}-item`,
)[index];
return ele?.offsetParent && ele;
};
const [prevStyle, setPrevStyle] = React.useState<ThumbReact>(null);
const [nextStyle, setNextStyle] = React.useState<ThumbReact>(null);
useLayoutEffect(() => {
if (prevValue !== value) {
const prev = findValueElement(prevValue);
const next = findValueElement(value);
const calcPrevStyle = calcThumbStyle(prev);
const calcNextStyle = calcThumbStyle(next);
setPrevValue(value);
setPrevStyle(calcPrevStyle);
setNextStyle(calcNextStyle);
if (prev && next) {
onMotionStart();
} else {
onMotionEnd();
}
}
}, [value]);
const thumbStart = React.useMemo(
() =>
prevStyle
? direction === 'rtl'
? toPX(-(prevStyle?.right as number))
: toPX(prevStyle?.left as number)
: 'auto',
[direction, prevStyle],
);
const thumbActive = React.useMemo(
() =>
nextStyle
? direction === 'rtl'
? toPX(-(nextStyle?.right as number))
: toPX(nextStyle?.left as number)
: 'auto',
[direction, nextStyle],
);
// =========================== Motion ===========================
const onAppearStart = () => {
return {
transform: `translateX(var(--thumb-start-left))`,
width: `var(--thumb-start-width)`,
};
};
const onAppearActive = () => {
return {
transform: `translateX(var(--thumb-active-left))`,
width: `var(--thumb-active-width)`,
};
};
const onVisibleChanged = () => {
setPrevStyle(null);
setNextStyle(null);
onMotionEnd();
};
// =========================== Render ===========================
// No need motion when nothing exist in queue
if (!prevStyle || !nextStyle) {
return null;
}
return (
<CSSMotion
visible
motionName={motionName}
motionAppear
onAppearStart={onAppearStart}
onAppearActive={onAppearActive}
onVisibleChanged={onVisibleChanged}
>
{({ className: motionClassName, style: motionStyle }, ref) => {
const mergedStyle = {
...motionStyle,
'--thumb-start-left': thumbStart,
'--thumb-start-width': toPX(prevStyle?.width),
'--thumb-active-left': thumbActive,
'--thumb-active-width': toPX(nextStyle?.width),
} as React.CSSProperties;
// It's little ugly which should be refactor when @umi/test update to latest jsdom
const motionProps = {
ref: composeRef(thumbRef, ref),
style: mergedStyle,
className: classNames(`${prefixCls}-thumb`, motionClassName),
};
if (process.env.NODE_ENV === 'test') {
(motionProps as any)['data-test-style'] = JSON.stringify(mergedStyle);
}
return <div {...motionProps} />;
}}
</CSSMotion>
);
}