@@ -56,6 +56,9 @@ import {
56
56
TOP_PROGRESS ,
57
57
TOP_PLAYING ,
58
58
} from './DOMTopLevelEventTypes' ;
59
+ import { DOCUMENT_NODE } from '../shared/HTMLNodeType' ;
60
+
61
+ import { enableLegacyFBPrimerSupport } from 'shared/ReactFeatureFlags' ;
59
62
60
63
const capturePhaseEvents = new Set ( [
61
64
TOP_FOCUS ,
@@ -165,6 +168,44 @@ export function listenToEvent(
165
168
}
166
169
}
167
170
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
+
168
209
export function dispatchEventForPluginEventSystem (
169
210
topLevelType : DOMTopLevelEventType ,
170
211
eventSystemFlags : EventSystemFlags ,
@@ -173,6 +214,17 @@ export function dispatchEventForPluginEventSystem(
173
214
rootContainer : Document | Element ,
174
215
) : void {
175
216
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
+ }
176
228
177
229
batchedEventUpdates ( ( ) =>
178
230
dispatchEventsForPlugins (
0 commit comments