Skip to content

Commit 01c43b0

Browse files
authored
refactor: Use rc-motion (#128)
* refactor: Use rc-motion * update rc-motion * trigger now * trigger now
1 parent e92262d commit 01c43b0

5 files changed

Lines changed: 68 additions & 35 deletions

File tree

assets/index.less

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
animation-timing-function: cubic-bezier(0.55, 0, 0.55, 0.2);
5555
}
5656

57+
&-fade-appear,
5758
&-fade-enter {
5859
opacity: 0;
5960
.fade-effect();
@@ -65,6 +66,7 @@
6566
animation-play-state: paused;
6667
}
6768

69+
&-fade-appear&-fade-appear-active,
6870
&-fade-enter&-fade-enter-active {
6971
animation-name: rcNotificationFadeIn;
7072
animation-play-state: running;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"dependencies": {
6666
"@babel/runtime": "^7.10.1",
6767
"classnames": "2.x",
68-
"rc-animate": "3.x",
68+
"rc-motion": "^2.1.1",
6969
"rc-util": "^5.0.1"
7070
}
7171
}

src/Notice.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ export default class Notice extends Component<NoticeProps> {
2828
static defaultProps = {
2929
onClose() {},
3030
duration: 1.5,
31-
style: {
32-
right: '50%',
33-
},
3431
};
3532

3633
closeTimer: number | null = null;

src/Notification.tsx

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component, ReactText } from 'react';
22
import ReactDOM from 'react-dom';
3-
import Animate from 'rc-animate';
3+
import classNames from 'classnames';
4+
import { CSSMotionList } from 'rc-motion';
45
import createChainedFunction from 'rc-util/lib/createChainedFunction';
5-
import classnames from 'classnames';
66
import Notice, { NoticeProps } from './Notice';
77
import useNotification from './useNotification';
88

@@ -113,17 +113,31 @@ class Notification extends Component<NotificationProps, NotificationState> {
113113
};
114114

115115
remove = (key: React.Key) => {
116-
this.setState(previousState => ({
117-
notices: previousState.notices.filter(({ notice }) => notice.key !== key),
116+
this.setState(({ notices }) => ({
117+
notices: notices.filter(({ notice }) => notice.key !== key),
118118
}));
119119
};
120120

121+
noticePropsMap: Record<
122+
React.Key,
123+
{
124+
props: NoticeProps & {
125+
key: ReactText;
126+
};
127+
holderCallback?: HolderReadyCallback;
128+
}
129+
> = {};
130+
121131
render() {
122132
const { notices } = this.state;
123133
const { prefixCls, className, closeIcon, style } = this.props;
124-
const noticeNodes = notices.map(({ notice, holderCallback }, index) => {
134+
135+
const noticeKeys: React.Key[] = [];
136+
137+
notices.forEach(({ notice, holderCallback }, index) => {
125138
const update = Boolean(index === notices.length - 1 && notice.updateKey);
126139
const key = notice.updateKey ? notice.updateKey : notice.key;
140+
127141
const onClose = createChainedFunction(
128142
this.remove.bind(this, notice.key!),
129143
notice.onClose,
@@ -139,33 +153,55 @@ class Notification extends Component<NotificationProps, NotificationState> {
139153
onClose,
140154
onClick: notice.onClick,
141155
children: notice.content,
142-
} as (NoticeProps & { key: ReactText });
143-
144-
if (holderCallback) {
145-
return (
146-
<div
147-
key={key}
148-
className={`${prefixCls}-hook-holder`}
149-
ref={div => {
150-
if (typeof key === 'undefined') {
151-
return;
152-
}
153-
if (div) {
154-
this.hookRefs.set(key, div);
155-
holderCallback(div, noticeProps);
156-
} else {
157-
this.hookRefs.delete(key);
158-
}
159-
}}
160-
/>
161-
);
162-
}
156+
} as NoticeProps & { key: ReactText };
163157

164-
return <Notice {...noticeProps} />;
158+
// Give to motion
159+
noticeKeys.push(key);
160+
this.noticePropsMap[key] = { props: noticeProps, holderCallback };
165161
});
162+
166163
return (
167-
<div className={classnames(prefixCls, className)} style={style}>
168-
<Animate transitionName={this.getTransitionName()}>{noticeNodes}</Animate>
164+
<div className={classNames(prefixCls, className)} style={style}>
165+
<CSSMotionList
166+
keys={noticeKeys}
167+
motionName={this.getTransitionName()}
168+
onLeaveEnd={(_, __, { key }) => {
169+
delete this.noticePropsMap[key];
170+
}}
171+
>
172+
{({ key, className: motionClassName, style: motionStyle }) => {
173+
const { props: noticeProps, holderCallback } = this.noticePropsMap[key];
174+
if (holderCallback) {
175+
return (
176+
<div
177+
key={key}
178+
className={classNames(motionClassName, `${prefixCls}-hook-holder`)}
179+
style={{ ...motionStyle }}
180+
ref={div => {
181+
if (typeof key === 'undefined') {
182+
return;
183+
}
184+
185+
if (div) {
186+
this.hookRefs.set(key, div);
187+
holderCallback(div, noticeProps);
188+
} else {
189+
this.hookRefs.delete(key);
190+
}
191+
}}
192+
/>
193+
);
194+
}
195+
196+
return (
197+
<Notice
198+
{...noticeProps}
199+
className={classNames(motionClassName, noticeProps?.className)}
200+
style={{ ...motionStyle, ...noticeProps?.style }}
201+
/>
202+
);
203+
}}
204+
</CSSMotionList>
169205
</div>
170206
);
171207
}

typings.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)