Skip to content

Commit

Permalink
Add more non-React events to the priority list
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Feb 9, 2021
1 parent 212b1c7 commit 3d593a1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,37 @@ describe('ReactDOMNativeEventHeuristic-test', () => {
expect(container.textContent).toEqual('hovered');
});
});

// @gate experimental
// @gate enableDiscreteEventMicroTasks && enableNativeEventPriorityInference
it('mouse enter should be user-blocking but not discrete', async () => {
const root = ReactDOM.unstable_createRoot(container);

const target = React.createRef(null);
function Foo() {
const [isHover, setHover] = React.useState(false);
React.useLayoutEffect(() => {
target.current.onmouseenter = () => setHover(true);
});
return <div ref={target}>{isHover ? 'hovered' : 'not hovered'}</div>;
}

await act(async () => {
root.render(<Foo />);
});
expect(container.textContent).toEqual('not hovered');

await act(async () => {
// Note: React does not use native mouseenter/mouseleave events
// but we should still correctly determine their priority.
const mouseEnterEvent = document.createEvent('MouseEvents');
mouseEnterEvent.initEvent('mouseenter', true, true);
dispatchAndSetCurrentEvent(target.current, mouseEnterEvent);

// 3s should be enough to expire the updates
Scheduler.unstable_advanceTime(3000);
expect(Scheduler).toFlushExpired([]);
expect(container.textContent).toEqual('hovered');
});
});
});
14 changes: 14 additions & 0 deletions packages/react-dom/src/events/ReactDOMEventListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,16 @@ export function getEventPriority(domEventName: DOMEventName): * {
// eslint-disable-next-line no-fallthrough
case 'beforeblur':
case 'afterblur':
// Not used by React but could be by user code:
// eslint-disable-next-line no-fallthrough
case 'beforeinput':
case 'blur':
case 'fullscreenchange':
case 'focus':
case 'hashchange':
case 'popstate':
case 'select':
case 'selectstart':
return InputDiscreteLanePriority;
case 'drag':
case 'dragenter':
Expand All @@ -413,6 +423,10 @@ export function getEventPriority(domEventName: DOMEventName): * {
case 'toggle':
case 'touchmove':
case 'wheel':
// Not used by React but could be by user code:
// eslint-disable-next-line no-fallthrough
case 'mouseenter':
case 'mouseleave':
return InputContinuousLanePriority;
default:
return DefaultLanePriority;
Expand Down

0 comments on commit 3d593a1

Please sign in to comment.