@@ -257,43 +257,59 @@ class ThemeEditor {
257257 document . addEventListener (
258258 'mouseover' ,
259259 ( e ) => {
260- if ( ! ( e . target instanceof Element ) ) {
260+ const target = e . target as Element | null ;
261+ const from = e . relatedTarget as Element | null ;
262+
263+ if ( ! target ) {
261264 return ;
262265 }
263266
264- const section = ( e . target as Element ) . closest ( `[${ ATTRS . SectionType } ]` ) as HTMLElement ;
267+ const enteredSection = target . closest ( `[${ ATTRS . SectionType } ]` ) as HTMLElement | null ;
268+ const leftSection = from ?. closest ?.( `[${ ATTRS . SectionType } ]` ) as HTMLElement | null ;
265269
266- if ( section ) {
270+ if ( enteredSection && enteredSection !== leftSection ) {
267271 this . handleUnhighlightSection ( ) ;
268272 this . buttonsContainer . style . display = 'flex' ;
269- this . debounceFocusSection ( section ) ;
273+ this . debounceFocusSection ( enteredSection ) ;
270274 }
271275 } ,
272276 { passive : true }
273277 ) ;
274278
275279 document . addEventListener (
276- 'mouseleave ' ,
280+ 'mouseout ' ,
277281 ( e ) => {
278- if ( ! ( e . target instanceof Element ) ) {
282+ const target = e . target as Element | null ;
283+ const to = e . relatedTarget as Element | null ;
284+
285+ const leftSection = target ?. closest ?.( `[${ ATTRS . SectionType } ]` ) as HTMLElement | null ;
286+ const enteredSection = to ?. closest ?.( `[${ ATTRS . SectionType } ]` ) as HTMLElement | null ;
287+
288+ const goingIntoOverlay = to && this . sectionOverlay . contains ( to ) ;
289+
290+ if ( leftSection && leftSection !== enteredSection && ! goingIntoOverlay ) {
279291 this . buttonsContainer . style . display = 'none' ;
280292 this . clearActiveSection ( ) ;
281- return ;
282- }
283-
284- const section = ( e . target as Element ) . closest ( `[${ ATTRS . SectionType } ]` ) as HTMLElement ;
285- if ( ! section ) {
286- return ;
287293 }
294+ } ,
295+ { passive : true }
296+ ) ;
288297
289- const toEl = e . relatedTarget ;
290- if ( ! toEl || ! this . sectionOverlay . contains ( toEl as Node ) ) {
298+ document . addEventListener (
299+ 'mouseleave' ,
300+ ( e ) => {
301+ if ( ! e . relatedTarget ) {
291302 this . buttonsContainer . style . display = 'none' ;
292303 this . clearActiveSection ( ) ;
293304 }
294305 } ,
295306 { passive : true }
296307 ) ;
308+
309+ window . addEventListener ( 'blur' , ( ) => {
310+ this . buttonsContainer . style . display = 'none' ;
311+ this . clearActiveSection ( ) ;
312+ } ) ;
297313 }
298314
299315 private handleWindowResize ( ) {
0 commit comments