Skip to content

Commit f24a9e7

Browse files
authored
Unify Flare FocusWithin responder with useFocusWithin (facebook#18636)
1 parent b1a0831 commit f24a9e7

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

packages/react-dom/src/client/ReactDOMHostConfig.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,10 @@ function dispatchBeforeDetachedBlur(target: HTMLElement): void {
555555
function dispatchAfterDetachedBlur(target: HTMLElement): void {
556556
if (enableDeprecatedFlareAPI) {
557557
DEPRECATED_dispatchEventForResponderEventSystem(
558-
'blur',
558+
'afterblur',
559559
null,
560560
({
561-
isTargetAttached: false,
562-
target,
561+
relatedTarget: target,
563562
timeStamp: Date.now(),
564563
}: any),
565564
target,

packages/react-interactions/events/src/dom/DeprecatedFocus.js

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {DiscreteEvent} from 'shared/ReactTypes';
2222
*/
2323

2424
type FocusEvent = {|
25-
isTargetAttached: boolean,
25+
relatedTarget: null | Element | Document,
2626
target: Element | Document,
2727
type: FocusEventType | FocusWithinEventType,
2828
pointerType: PointerType,
@@ -53,6 +53,7 @@ type FocusEventType = 'focus' | 'blur' | 'focuschange' | 'focusvisiblechange';
5353
type FocusWithinProps = {
5454
disabled?: boolean,
5555
onFocusWithin?: (e: FocusEvent) => void,
56+
onAfterBlurWithin?: (e: FocusEvent) => void,
5657
onBeforeBlurWithin?: (e: FocusEvent) => void,
5758
onBlurWithin?: (e: FocusEvent) => void,
5859
onFocusWithinChange?: boolean => void,
@@ -65,7 +66,8 @@ type FocusWithinEventType =
6566
| 'focuswithinchange'
6667
| 'blurwithin'
6768
| 'focuswithin'
68-
| 'beforeblurwithin';
69+
| 'beforeblurwithin'
70+
| 'afterblurwithin';
6971

7072
/**
7173
* Shared between Focus and FocusWithin
@@ -116,8 +118,7 @@ const focusVisibleEvents = hasPointerEvents
116118

117119
const targetEventTypes = ['focus', 'blur', 'beforeblur', ...focusVisibleEvents];
118120

119-
// Used only for the blur "detachedTarget" logic
120-
const rootEventTypes = ['blur'];
121+
const rootEventTypes = ['afterblur'];
121122

122123
function addWindowEventListener(types, callback, options) {
123124
types.forEach(type => {
@@ -192,10 +193,10 @@ function createFocusEvent(
192193
type: FocusEventType | FocusWithinEventType,
193194
target: Element | Document,
194195
pointerType: PointerType,
195-
isTargetAttached: boolean,
196+
relatedTarget: null | Element | Document,
196197
): FocusEvent {
197198
return {
198-
isTargetAttached,
199+
relatedTarget,
199200
target,
200201
type,
201202
pointerType,
@@ -297,7 +298,7 @@ function dispatchFocusEvents(
297298
'focus',
298299
target,
299300
pointerType,
300-
true,
301+
null,
301302
);
302303
context.dispatchEvent(syntheticEvent, onFocus, DiscreteEvent);
303304
}
@@ -321,7 +322,7 @@ function dispatchBlurEvents(
321322
'blur',
322323
target,
323324
pointerType,
324-
true,
325+
null,
325326
);
326327
context.dispatchEvent(syntheticEvent, onBlur, DiscreteEvent);
327328
}
@@ -346,7 +347,7 @@ function dispatchFocusWithinEvents(
346347
'focuswithin',
347348
target,
348349
pointerType,
349-
true,
350+
null,
350351
);
351352
context.dispatchEvent(syntheticEvent, onFocusWithin, DiscreteEvent);
352353
}
@@ -361,19 +362,40 @@ function dispatchBlurWithinEvents(
361362
const pointerType = state.pointerType;
362363
const target = ((state.focusTarget: any): Element | Document) || event.target;
363364
const onBlurWithin = (props.onBlurWithin: any);
364-
const isTargetAttached = state.detachedTarget === null;
365365
if (isFunction(onBlurWithin)) {
366366
const syntheticEvent = createFocusEvent(
367367
context,
368368
'blurwithin',
369369
target,
370370
pointerType,
371-
isTargetAttached,
371+
null,
372372
);
373373
context.dispatchEvent(syntheticEvent, onBlurWithin, DiscreteEvent);
374374
}
375375
}
376376

377+
function dispatchAfterBlurWithinEvents(
378+
context: ReactDOMResponderContext,
379+
event: ReactDOMResponderEvent,
380+
props: FocusWithinProps,
381+
state: FocusState,
382+
) {
383+
const pointerType = state.pointerType;
384+
const target = ((state.focusTarget: any): Element | Document) || event.target;
385+
const onAfterBlurWithin = (props.onAfterBlurWithin: any);
386+
const relatedTarget = state.detachedTarget;
387+
if (isFunction(onAfterBlurWithin)) {
388+
const syntheticEvent = createFocusEvent(
389+
context,
390+
'afterblurwithin',
391+
target,
392+
pointerType,
393+
relatedTarget,
394+
);
395+
context.dispatchEvent(syntheticEvent, onAfterBlurWithin, DiscreteEvent);
396+
}
397+
}
398+
377399
function dispatchFocusChange(
378400
context: ReactDOMResponderContext,
379401
props: FocusProps,
@@ -616,7 +638,7 @@ const focusWithinResponderImpl = {
616638
'beforeblurwithin',
617639
event.target,
618640
state.pointerType,
619-
true,
641+
null,
620642
);
621643
state.detachedTarget = event.target;
622644
context.dispatchEvent(
@@ -660,10 +682,10 @@ const focusWithinResponderImpl = {
660682
props: FocusWithinProps,
661683
state: FocusState,
662684
): void {
663-
if (event.type === 'blur') {
685+
if (event.type === 'afterblur') {
664686
const detachedTarget = state.detachedTarget;
665687
if (detachedTarget !== null && detachedTarget === event.target) {
666-
dispatchBlurWithinEvents(context, event, props, state);
688+
dispatchAfterBlurWithinEvents(context, event, props, state);
667689
state.detachedTarget = null;
668690
if (state.addedRootEvents) {
669691
state.addedRootEvents = false;

packages/react-interactions/events/src/dom/__tests__/FocusWithin-test.internal.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
290290
});
291291

292292
describe('onBeforeBlurWithin', () => {
293-
let onBeforeBlurWithin, onBlurWithin, ref, innerRef, innerRef2;
293+
let onBeforeBlurWithin, onAfterBlurWithin, ref, innerRef, innerRef2;
294294

295295
beforeEach(() => {
296296
onBeforeBlurWithin = jest.fn();
297-
onBlurWithin = jest.fn();
297+
onAfterBlurWithin = jest.fn();
298298
ref = React.createRef();
299299
innerRef = React.createRef();
300300
innerRef2 = React.createRef();
@@ -305,7 +305,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
305305
const Component = ({show}) => {
306306
const listener = useFocusWithin({
307307
onBeforeBlurWithin,
308-
onBlurWithin,
308+
onAfterBlurWithin,
309309
});
310310
return (
311311
<div ref={ref} DEPRECATED_flareListeners={listener}>
@@ -322,12 +322,12 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
322322
target.keydown({key: 'Tab'});
323323
target.focus();
324324
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
325-
expect(onBlurWithin).toHaveBeenCalledTimes(0);
325+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
326326
ReactDOM.render(<Component show={false} />, container);
327327
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
328-
expect(onBlurWithin).toHaveBeenCalledTimes(1);
329-
expect(onBlurWithin).toHaveBeenCalledWith(
330-
expect.objectContaining({isTargetAttached: false}),
328+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
329+
expect(onAfterBlurWithin).toHaveBeenCalledWith(
330+
expect.objectContaining({relatedTarget: inner}),
331331
);
332332
});
333333

@@ -336,7 +336,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
336336
const Component = ({show}) => {
337337
const listener = useFocusWithin({
338338
onBeforeBlurWithin,
339-
onBlurWithin,
339+
onAfterBlurWithin,
340340
});
341341
return (
342342
<div ref={ref} DEPRECATED_flareListeners={listener}>
@@ -357,12 +357,12 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
357357
target.keydown({key: 'Tab'});
358358
target.focus();
359359
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
360-
expect(onBlurWithin).toHaveBeenCalledTimes(0);
360+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
361361
ReactDOM.render(<Component show={false} />, container);
362362
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
363-
expect(onBlurWithin).toHaveBeenCalledTimes(1);
364-
expect(onBlurWithin).toHaveBeenCalledWith(
365-
expect.objectContaining({isTargetAttached: false}),
363+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
364+
expect(onAfterBlurWithin).toHaveBeenCalledWith(
365+
expect.objectContaining({relatedTarget: inner}),
366366
);
367367
});
368368

@@ -418,7 +418,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
418418
const Component = ({show}) => {
419419
const listener = useFocusWithin({
420420
onBeforeBlurWithin,
421-
onBlurWithin,
421+
onAfterBlurWithin,
422422
});
423423

424424
return (
@@ -444,7 +444,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
444444
target.keydown({key: 'Tab'});
445445
target.focus();
446446
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(0);
447-
expect(onBlurWithin).toHaveBeenCalledTimes(0);
447+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(0);
448448

449449
suspend = true;
450450
root.render(<Component />);
@@ -454,7 +454,7 @@ describe.each(table)('FocusWithin responder', hasPointerEvents => {
454454
'<div><input style="display: none;">Loading...</div>',
455455
);
456456
expect(onBeforeBlurWithin).toHaveBeenCalledTimes(1);
457-
expect(onBlurWithin).toHaveBeenCalledTimes(1);
457+
expect(onAfterBlurWithin).toHaveBeenCalledTimes(1);
458458
resolve();
459459

460460
document.body.removeChild(container2);

0 commit comments

Comments
 (0)