@@ -29,6 +29,7 @@ import {
2929 PHX_RELOAD_STATUS ,
3030 PHX_RUNTIME_HOOK ,
3131 PHX_DROP_TARGET_ACTIVE_CLASS ,
32+ PHX_TELEPORTED_SRC ,
3233} from "./constants" ;
3334
3435import {
@@ -872,16 +873,25 @@ export default class LiveSocket {
872873
873874 dispatchClickAway ( e , clickStartedAt ) {
874875 const phxClickAway = this . binding ( "click-away" ) ;
876+ const portal = clickStartedAt . closest ( `[${ PHX_TELEPORTED_SRC } ]` ) ;
877+ const portalStartedAt =
878+ portal && DOM . byId ( portal . getAttribute ( PHX_TELEPORTED_SRC ) ) ;
875879 DOM . all ( document , `[${ phxClickAway } ]` , ( el ) => {
880+ let startedAt = clickStartedAt ;
881+ if ( portal && ! portal . contains ( el ) ) {
882+ // If we have a portal and the click-away element is not inside it,
883+ // then treat the portal source as the starting point instead.
884+ startedAt = portalStartedAt ;
885+ }
876886 if (
877887 ! (
878- el . isSameNode ( clickStartedAt ) ||
879- el . contains ( clickStartedAt ) ||
888+ el . isSameNode ( startedAt ) ||
889+ el . contains ( startedAt ) ||
880890 // When clicking a link with custom method,
881891 // phoenix_html triggers a click on a submit button
882892 // of a hidden form appended to the body. For such cases
883893 // where the clicked target is hidden, we skip click-away.
884- ! JS . isVisible ( clickStartedAt )
894+ ! JS . isVisible ( startedAt )
885895 )
886896 ) {
887897 this . withinOwners ( el , ( view ) => {
0 commit comments