Skip to content

Commit 5ddb565

Browse files
committed
refactor NotificationList content
1 parent e6ec9d8 commit 5ddb565

2 files changed

Lines changed: 52 additions & 17 deletions

File tree

src/NotificationList/Content.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { clsx } from 'clsx';
2+
import * as React from 'react';
3+
4+
export interface ContentProps extends React.HTMLAttributes<HTMLDivElement> {
5+
listPrefixCls: string;
6+
height: number;
7+
}
8+
9+
const Content = React.forwardRef<HTMLDivElement, ContentProps>((props, ref) => {
10+
const { listPrefixCls, height, className, style, ...restProps } = props;
11+
12+
const contentPrefixCls = `${listPrefixCls}-content`;
13+
14+
// ========================= Height =========================
15+
const prevHeightRef = React.useRef(height);
16+
const prevHeight = prevHeightRef.current;
17+
const heightStatus = height < prevHeight ? 'decrease' : 'increase';
18+
19+
prevHeightRef.current = height;
20+
21+
// ========================= Render =========================
22+
return (
23+
<div
24+
{...restProps}
25+
className={clsx(contentPrefixCls, `${contentPrefixCls}-${heightStatus}`, className)}
26+
style={{
27+
...style,
28+
height,
29+
}}
30+
ref={ref}
31+
/>
32+
);
33+
});
34+
35+
if (process.env.NODE_ENV !== 'production') {
36+
Content.displayName = 'NotificationListContent';
37+
}
38+
39+
export default Content;
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import { CSSMotionList } from '@rc-component/motion';
22
import type { CSSMotionProps } from '@rc-component/motion';
3+
import { composeRef } from '@rc-component/util/lib/ref';
34
import { clsx } from 'clsx';
45
import * as React from 'react';
5-
import type { StackConfig } from './interface';
6-
import { NotificationContext } from './legacy/NotificationProvider';
6+
import useListPosition from '../hooks/useListPosition';
7+
import useStack from '../hooks/useStack';
8+
import type { StackConfig } from '../interface';
9+
import { NotificationContext } from '../legacy/NotificationProvider';
710
import Notification, {
811
type ComponentsType,
912
type NotificationClassNames,
1013
type NotificationProps,
1114
type NotificationStyles,
12-
} from './Notification';
13-
import useListPosition from './hooks/useListPosition';
14-
import useStack from './hooks/useStack';
15-
import { composeRef } from '@rc-component/util/lib/ref';
15+
} from '../Notification';
16+
import Content from './Content';
1617

1718
export type Placement = 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight';
18-
export type { StackConfig } from './interface';
19+
export type { StackConfig } from '../interface';
1920

2021
export interface NotificationListConfig extends Omit<NotificationProps, 'prefixCls'> {
2122
key: React.Key;
@@ -94,6 +95,7 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
9495
stackPosition,
9596
gap,
9697
);
98+
const hasConfigList = !!configList.length;
9799

98100
React.useEffect(() => {
99101
const listNode = contentRef.current;
@@ -106,7 +108,7 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
106108
const nextGap = parseFloat(rowGap || cssGap) || 0;
107109

108110
setGap((prevGap) => (prevGap === nextGap ? prevGap : nextGap));
109-
}, [!!configList.length]);
111+
}, [hasConfigList]);
110112

111113
// ========================= Render =========================
112114
const listPrefixCls = `${prefixCls}-list`;
@@ -132,13 +134,7 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
132134
}}
133135
style={style}
134136
>
135-
<div
136-
className={`${listPrefixCls}-content`}
137-
style={{
138-
height: totalHeight,
139-
}}
140-
ref={contentRef}
141-
>
137+
<Content listPrefixCls={listPrefixCls} height={totalHeight} ref={contentRef}>
142138
<CSSMotionList
143139
component={false}
144140
keys={keys}
@@ -220,10 +216,10 @@ const NotificationList: React.FC<NotificationListProps> = (props) => {
220216
);
221217
}}
222218
</CSSMotionList>
223-
</div>
219+
</Content>
224220
</div>
225221
);
226222
};
227223

228224
export default NotificationList;
229-
export type { NotificationClassNames, NotificationStyles } from './Notification';
225+
export type { NotificationClassNames, NotificationStyles } from '../Notification';

0 commit comments

Comments
 (0)