@@ -56,6 +56,9 @@ import {
5656 TOP_PROGRESS ,
5757 TOP_PLAYING ,
5858} from './DOMTopLevelEventTypes' ;
59+ import { DOCUMENT_NODE } from '../shared/HTMLNodeType' ;
60+
61+ import { enableLegacyFBPrimerSupport } from 'shared/ReactFeatureFlags' ;
5962
6063const capturePhaseEvents = new Set ( [
6164 TOP_FOCUS ,
@@ -165,6 +168,44 @@ export function listenToEvent(
165168 }
166169}
167170
171+ const validFBLegacyPrimerRels = new Set ( [
172+ 'dialog' ,
173+ 'dialog-post' ,
174+ 'async' ,
175+ 'async-post' ,
176+ 'theater' ,
177+ 'toggle' ,
178+ ] ) ;
179+
180+ function willDeferLaterForFBLegacyPrimer ( nativeEvent : any ) : boolean {
181+ let node = nativeEvent . target ;
182+ const type = nativeEvent . type ;
183+ if ( type !== 'click' ) {
184+ return false ;
185+ }
186+ while ( node !== null ) {
187+ // Primer works by intercepting a click event on an <a> element
188+ // that has a "rel" attribute that matches one of the valid ones
189+ // in the Set above. If we intercept this before Primer does, we
190+ // will need to defer the current event till later and discontinue
191+ // execution of the current event. To do this we can add a document
192+ // event listener and continue again later after propagation.
193+ if ( node . tagName === 'A' && validFBLegacyPrimerRels . has ( node . rel ) ) {
194+ const legacyFBSupport = true ;
195+ const isCapture = nativeEvent . eventPhase === 1 ;
196+ trapEventForPluginEventSystem (
197+ document ,
198+ ( ( type : any ) : DOMTopLevelEventType ) ,
199+ isCapture ,
200+ legacyFBSupport ,
201+ ) ;
202+ return true ;
203+ }
204+ node = node . parentNode ;
205+ }
206+ return false ;
207+ }
208+
168209export function dispatchEventForPluginEventSystem (
169210 topLevelType : DOMTopLevelEventType ,
170211 eventSystemFlags : EventSystemFlags ,
@@ -173,6 +214,17 @@ export function dispatchEventForPluginEventSystem(
173214 rootContainer : Document | Element ,
174215) : void {
175216 let ancestorInst = targetInst ;
217+ if ( rootContainer . nodeType !== DOCUMENT_NODE ) {
218+ // If we detect the FB legacy primer system, we
219+ // defer the event to the "document" with a one
220+ // time event listener so we can defer the event.
221+ if (
222+ enableLegacyFBPrimerSupport &&
223+ willDeferLaterForFBLegacyPrimer ( nativeEvent )
224+ ) {
225+ return ;
226+ }
227+ }
176228
177229 batchedEventUpdates ( ( ) =>
178230 dispatchEventsForPlugins (
0 commit comments