@@ -178,13 +178,77 @@ export const NAVIGATE_OK = "openbot:navigate-ok";
178178/** How long the page waits for that answer before it goes ahead anyway. */
179179export const NAVIGATE_OK_MS = 120 ;
180180
181+ /**
182+ * What is drawn over the page for a moment: a menu, a select's list, a popover, a tooltip, a sheet
183+ * or a dialog, as the primitives portal them (Base UI). The toasts' region, always there, is not.
184+ */
185+ const OVERLAY =
186+ '[data-base-ui-portal] :is([role="menu"], [role="listbox"], [role="dialog"], [role="alertdialog"], [role="tooltip"], [data-slot$="-content"], [data-slot$="-popup"])' ;
187+
188+ function overlayOnScreen ( ) : Element | null {
189+ for ( const element of document . querySelectorAll ( OVERLAY ) )
190+ if ( ! element . closest ( '[data-slot="toast-viewport"]' ) ) return element ;
191+ return null ;
192+ }
193+
194+ /** How long a navigation may be held for an overlay to go, at most, and in frames. */
195+ const OVERLAY_MS = 100 ;
196+ const OVERLAY_FRAMES = 2 ;
197+
198+ /**
199+ * Runs `then` once no overlay is on screen and that has been drawn, so the app's picture of the page
200+ * at `will-navigate` has no menu open in it. With none, at once. An item's own press usually closes
201+ * its menu by the next frame; one still open then is sent Escape, as a person would close it.
202+ * Never more than `OVERLAY_FRAMES` frames and the frame that draws its going, and never more than
203+ * `OVERLAY_MS`. Its exit animation is off in the shell (styles.css), so it goes at once.
204+ */
205+ function withoutOverlays ( then : ( ) => void ) : void {
206+ if ( ! overlayOnScreen ( ) ) {
207+ then ( ) ;
208+ return ;
209+ }
210+ let done = false ;
211+ const finish = ( ) => {
212+ if ( done ) return ;
213+ done = true ;
214+ clearTimeout ( cap ) ;
215+ then ( ) ;
216+ } ;
217+ const cap = setTimeout ( finish , OVERLAY_MS ) ;
218+ let frames = 0 ;
219+ const tick = ( ) => {
220+ if ( done ) return ;
221+ const open = overlayOnScreen ( ) ;
222+ // Gone: one more frame, so its going is drawn before the app takes its picture.
223+ if ( ! open ) return void requestAnimationFrame ( finish ) ;
224+ frames += 1 ;
225+ if ( frames === 1 )
226+ open . dispatchEvent (
227+ new KeyboardEvent ( "keydown" , {
228+ key : "Escape" ,
229+ code : "Escape" ,
230+ bubbles : true ,
231+ cancelable : true ,
232+ } ) ,
233+ ) ;
234+ if ( frames > OVERLAY_FRAMES ) return finish ( ) ;
235+ requestAnimationFrame ( tick ) ;
236+ } ;
237+ requestAnimationFrame ( tick ) ;
238+ }
239+
181240/**
182241 * Tells the app a navigation the page started is coming (`will-navigate`), then runs `go`: at once,
183242 * or on iOS, where the app takes a snapshot of the page for its swipe back, once the app answers
184243 * `openbot:navigate-ok` or after `NAVIGATE_OK_MS`, whichever is first. Never waits where nothing
185244 * received the message, nor on Android, which does not answer.
186245 */
187246function announce ( direction : Direction , next : TabStacks , go : ( ) => void ) : void {
247+ withoutOverlays ( ( ) => say ( direction , next , go ) ) ;
248+ }
249+
250+ /** `announce`, once nothing is drawn over the page. */
251+ function say ( direction : Direction , next : TabStacks , go : ( ) => void ) : void {
188252 const message = {
189253 type : "will-navigate" ,
190254 tab : next . current ,
0 commit comments