11import { InfoIcon } from "lucide-react" ;
2- import { useEffect , useId , useLayoutEffect , useRef , useState , type ReactNode } from "react" ;
2+ import { useId , useLayoutEffect , useRef , useState , type ReactNode } from "react" ;
3+ import { flushSync } from "react-dom" ;
34
45import { cn } from "~/lib/utils" ;
56import { Button } from "../ui/button" ;
@@ -51,39 +52,25 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
5152 const noticesRef = useRef < HTMLDivElement > ( null ) ;
5253 const peekRef = useRef < HTMLButtonElement > ( null ) ;
5354 const expandedItemsRef = useRef < HTMLDivElement > ( null ) ;
54- const pendingFocusRef = useRef < "peek" | "notice" | null > ( null ) ;
5555 const expandedItemsId = useId ( ) ;
5656 const [ requestedExitingItemId , setExitingItemId ] = useState < string | null > ( null ) ;
57- const dismissTimeoutRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
5857 const exitingItemId =
5958 requestedExitingItemId !== null && items . some ( ( item ) => item . id === requestedExitingItemId )
6059 ? requestedExitingItemId
6160 : null ;
6261
63- useEffect ( ( ) => {
64- return ( ) => {
65- if ( dismissTimeoutRef . current ) {
66- clearTimeout ( dismissTimeoutRef . current ) ;
67- }
68- } ;
69- } , [ ] ) ;
70-
7162 if ( items . length < 2 && stackExpanded ) {
7263 setStackExpanded ( false ) ;
7364 }
7465
75- useLayoutEffect ( ( ) => {
76- if ( stackExpanded && pendingFocusRef . current === "notice" ) {
77- pendingFocusRef . current = null ;
78- const firstControl = expandedItemsRef . current ?. querySelector < HTMLElement > (
79- 'button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]' ,
80- ) ;
81- ( firstControl ?? expandedItemsRef . current ) ?. focus ( { preventScroll : true } ) ;
82- } else if ( ! stackExpanded && pendingFocusRef . current === "peek" ) {
83- pendingFocusRef . current = null ;
84- peekRef . current ?. focus ( { preventScroll : true } ) ;
85- }
86- } , [ stackExpanded ] ) ;
66+ const expandAndFocusNotice = ( ) => {
67+ flushSync ( ( ) => setStackExpanded ( true ) ) ;
68+ const container = expandedItemsRef . current ;
69+ const firstControl = container ?. querySelector < HTMLElement > (
70+ 'button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]' ,
71+ ) ;
72+ ( firstControl ?? container ) ?. focus ( { preventScroll : true } ) ;
73+ } ;
8774
8875 const orderedItems = items . toSorted ( ( a , b ) => bannerPriority ( a ) - bannerPriority ( b ) ) ;
8976 const frontItem = orderedItems [ 0 ] ;
@@ -106,13 +93,9 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
10693 return ;
10794 }
10895 setExitingItemId ( item . id ) ;
109- if ( dismissTimeoutRef . current ) {
110- clearTimeout ( dismissTimeoutRef . current ) ;
111- }
112- dismissTimeoutRef . current = setTimeout ( ( ) => {
113- dismissTimeoutRef . current = null ;
114- item . onDismiss ?.( ) ;
115- } , DISMISS_TRANSITION_MS ) ;
96+ // A timer, not transitionend: the dismissal must commit even when the stack unmounts
97+ // (thread switch) or a collapsed composer hides it mid-exit, and transitionend never fires then.
98+ setTimeout ( ( ) => item . onDismiss ?.( ) , DISMISS_TRANSITION_MS ) ;
11699 } ;
117100
118101 return (
@@ -126,7 +109,8 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
126109 onPointerEnter = { ( event ) => {
127110 if ( event . pointerType === "touch" ) return ;
128111 if ( document . activeElement === peekRef . current ) {
129- pendingFocusRef . current = "notice" ;
112+ expandAndFocusNotice ( ) ;
113+ return ;
130114 }
131115 setStackExpanded ( true ) ;
132116 } }
@@ -145,8 +129,8 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
145129 if ( event . key !== "Escape" || ! stackExpanded ) return ;
146130 event . preventDefault ( ) ;
147131 event . stopPropagation ( ) ;
148- pendingFocusRef . current = "peek" ;
149- setStackExpanded ( false ) ;
132+ flushSync ( ( ) => setStackExpanded ( false ) ) ;
133+ peekRef . current ?. focus ( { preventScroll : true } ) ;
150134 } }
151135 >
152136 { showCollapsedStackCap && firstStackedItem ? (
@@ -160,8 +144,7 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
160144 tabIndex = { stackExpanded ? - 1 : 0 }
161145 onClick = { ( event ) => {
162146 event . currentTarget . focus ( { preventScroll : true } ) ;
163- pendingFocusRef . current = "notice" ;
164- setStackExpanded ( true ) ;
147+ expandAndFocusNotice ( ) ;
165148 } }
166149 className = { cn ( stackExpanded && "pointer-events-none invisible opacity-0" ) }
167150 />
0 commit comments