1
- import { Key , useLayoutEffect , useRef , useState } from 'react' ;
1
+ import { Key , useEffect , useLayoutEffect , useRef , useState } from 'react' ;
2
2
import { TransitionGroup } from 'react-transition-group' ;
3
3
import { Item } from '@react-stately/collections' ;
4
4
import { useHover } from '@react-aria/interactions' ;
5
- import { useFocusRing , focusSafely } from '@react-aria/focus' ;
5
+ import { focusSafely , useFocusRing } from '@react-aria/focus' ;
6
6
7
7
import { tasty } from '../../../../tasty' ;
8
8
import { CubeNotifyApiPropsWithID } from '../types' ;
9
- import { useNotificationsList , CollectionChildren } from '../hooks' ;
9
+ import { CollectionChildren , useNotificationsList } from '../hooks' ;
10
10
import { mergeProps } from '../../../../utils/react' ;
11
11
import { useChainedCallback , useEvent } from '../../../../_internal' ;
12
12
@@ -59,6 +59,7 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
59
59
60
60
const ref = useRef < HTMLElement | null > ( null ) ;
61
61
const [ realLimit , setRealLimit ] = useState ( limit + 1 ) ;
62
+ const lastShownIdRef = useRef < Key > ( ) ;
62
63
63
64
const { listProps, state } = useNotificationsList ( { items, children, ref } ) ;
64
65
const { hoverProps, isHovered } = useHover ( { } ) ;
@@ -84,9 +85,17 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
84
85
moveFocus ,
85
86
) ;
86
87
87
- const collection = [ ...state . collection ] . reverse ( ) ;
88
+ let collection = [ ...state . collection ] . reverse ( ) ;
88
89
const collectionLength = collection . length ;
89
90
91
+ const lastShownNotificationIndex = collection . findIndex (
92
+ ( notification ) => notification . props . id === lastShownIdRef . current ,
93
+ ) ;
94
+
95
+ if ( lastShownNotificationIndex !== - 1 ) {
96
+ collection = collection . slice ( 0 , lastShownNotificationIndex ) ;
97
+ }
98
+
90
99
/**
91
100
* Handy hack to improve animations if the limit is reached.
92
101
*/
@@ -100,12 +109,25 @@ export function NotificationsBar(props: NotificationsBarProps): JSX.Element {
100
109
}
101
110
} , [ realLimit ] ) ;
102
111
103
- // Auto-dismiss all notifications that are off the limit.
104
- collection
105
- . slice ( realLimit )
106
- . forEach ( ( notification ) =>
107
- chainedOnRemoveNotification ( notification . props . id ) ,
108
- ) ;
112
+ // Set the last notification that was gone off the limit
113
+ collection . slice ( realLimit ) . forEach ( ( notification ) => {
114
+ // It's safe 'cause there is always only a single notification above the limit
115
+ lastShownIdRef . current = notification . props . id ;
116
+ } ) ;
117
+
118
+ useEffect ( ( ) => {
119
+ // Auto-dismiss the last shown notification that was off the limit
120
+ if ( lastShownIdRef . current ) {
121
+ onDismissNotification ( lastShownIdRef . current ) ;
122
+ onRemoveNotification ( lastShownIdRef . current ) ;
123
+
124
+ const lastNotification = collection . find (
125
+ ( notification ) => notification . props . id === lastShownIdRef . current ,
126
+ ) ;
127
+
128
+ lastNotification ?. props . onDismiss ?.( ) ;
129
+ }
130
+ } , [ lastShownIdRef . current ] ) ;
109
131
110
132
return (
111
133
< NotificationsContainer
0 commit comments