@@ -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,43 @@ 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+ trapEventForPluginEventSystem (
196+ document ,
197+ ( ( type : any ) : DOMTopLevelEventType ) ,
198+ false ,
199+ legacyFBSupport ,
200+ ) ;
201+ return true ;
202+ }
203+ node = node . parentNode ;
204+ }
205+ return false ;
206+ }
207+
168208export function dispatchEventForPluginEventSystem (
169209 topLevelType : DOMTopLevelEventType ,
170210 eventSystemFlags : EventSystemFlags ,
@@ -173,6 +213,17 @@ export function dispatchEventForPluginEventSystem(
173213 rootContainer : Document | Element ,
174214) : void {
175215 let ancestorInst = targetInst ;
216+ if ( rootContainer . nodeType !== DOCUMENT_NODE ) {
217+ // If we detect the FB legacy primer system, we
218+ // defer the event to the "document" with a one
219+ // time event listener so we can defer the event.
220+ if (
221+ enableLegacyFBPrimerSupport &&
222+ willDeferLaterForFBLegacyPrimer ( nativeEvent )
223+ ) {
224+ return ;
225+ }
226+ }
176227
177228 batchedEventUpdates ( ( ) =>
178229 dispatchEventsForPlugins (
0 commit comments