Skip to content

Commit 45640f2

Browse files
committed
refactor: move hover cleanup into Notification
1 parent 18bc46c commit 45640f2

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎src/Notification.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface NotificationProps {
6363
pauseOnHover?: boolean;
6464

6565
// Function
66+
onHover?: (hovering: boolean) => void;
6667
onClick?: React.MouseEventHandler<HTMLDivElement>;
6768
onMouseEnter?: React.MouseEventHandler<HTMLDivElement>;
6869
onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
@@ -99,6 +100,7 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
99100
pauseOnHover = true,
100101

101102
// Function
103+
onHover,
102104
onClick,
103105
onMouseEnter,
104106
onMouseLeave,
@@ -136,7 +138,18 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
136138
}, [forcedHovering, hovering, onPause, onResume, pauseOnHover]);
137139

138140
// ========================= Hover ==========================
141+
const hoveringRef = React.useRef(false);
142+
const onInternalHover = useEvent((nextHovering: boolean) => {
143+
if (nextHovering || hoveringRef.current) {
144+
onHover?.(nextHovering);
145+
}
146+
hoveringRef.current = nextHovering;
147+
});
148+
149+
React.useEffect(() => () => onInternalHover(false), [onInternalHover]);
150+
139151
function onInternalMouseEnter(event: React.MouseEvent<HTMLDivElement>) {
152+
onInternalHover(true);
140153
setHovering(true);
141154
if (pauseOnHover) {
142155
onPause();
@@ -145,6 +158,8 @@ const Notification = React.forwardRef<HTMLDivElement, NotificationProps>((props,
145158
}
146159

147160
function onInternalMouseLeave(event: React.MouseEvent<HTMLDivElement>) {
161+
// Normal exits are handled by the list, including movement through its gaps.
162+
hoveringRef.current = false;
148163
setHovering(false);
149164
if (pauseOnHover && !forcedHovering) {
150165
onResume();

‎src/NotificationList/index.tsx‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Content from './Content';
1717
export type Placement = 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight';
1818
export type { StackConfig } from '../hooks/useStack';
1919

20-
export interface NotificationListConfig extends Omit<NotificationProps, 'prefixCls'> {
20+
export interface NotificationListConfig extends Omit<NotificationProps, 'prefixCls' | 'onHover'> {
2121
key: React.Key;
2222
placement?: Placement;
2323
times?: number;
@@ -139,15 +139,6 @@ const NotificationListItem: React.FC<NotificationListItemProps> = (props) => {
139139
[setNodeSize, strKey],
140140
);
141141
const ref = useComposeRef(nodeRef, setItemRef);
142-
const hoveringRef = React.useRef(false);
143-
const onInternalHover = useEvent((hovering: boolean) => {
144-
if (hovering || hoveringRef.current) {
145-
onHover(strKey, hovering);
146-
}
147-
hoveringRef.current = hovering;
148-
});
149-
150-
React.useEffect(() => () => onInternalHover(false), [onInternalHover]);
151142

152143
return (
153144
<Notification
@@ -164,15 +155,7 @@ const NotificationListItem: React.FC<NotificationListItemProps> = (props) => {
164155
}}
165156
hovering={stackEnabled && listHovering}
166157
pauseOnHover={config.pauseOnHover ?? pauseOnHover}
167-
onMouseEnter={(event) => {
168-
onInternalHover(true);
169-
config.onMouseEnter?.(event);
170-
}}
171-
onMouseLeave={(event) => {
172-
// The list handles normal exits, including movement through its gaps.
173-
hoveringRef.current = false;
174-
config.onMouseLeave?.(event);
175-
}}
158+
onHover={(hovering) => onHover(strKey, hovering)}
176159
onClose={() => {
177160
config.onClose?.();
178161
onNoticeClose?.(key);

0 commit comments

Comments
 (0)