Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -3232,8 +3232,11 @@ FragmentInstance.prototype.blur = function (this: FragmentInstanceType): void {
const parentInstanceOrContainer = getInstanceFromHostFiber<
Instance | Container,
>(parentHostFiber);
// TODO: Handle parentInstanceOrContainer being a document
const activeElement = parentInstanceOrContainer.ownerDocument.activeElement;
// Instance is included in the Container type for DOM.
const ownerDocument = getOwnerDocumentFromRootContainer(
parentInstanceOrContainer,
);
const activeElement = ownerDocument.activeElement;
if (
activeElement === null ||
!parentInstanceOrContainer.contains(activeElement)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails reactcore
* @jest-environment node
*/

'use strict';

let JSDOM;
let React;
let ReactDOMClient;
let act;
let document;
let Fragment;

describe('FragmentRefs', () => {
beforeEach(() => {
jest.resetModules();
JSDOM = require('jsdom');
React = require('react');
Fragment = React.Fragment;
ReactDOMClient = require('react-dom/client');
act = require('internal-test-utils').act;

const jsdom = new JSDOM.JSDOM('');
document = jsdom.window.document;
global.window = jsdom.window;
global.document = global.window.document;
});

describe('focus methods', () => {
describe('blur()', () => {
// @gate enableFragmentRefs
it('throws when the nearest host parent is a Document container', async () => {
const fragmentRef = React.createRef();
const root = ReactDOMClient.createRoot(document);

await act(() => {
root.render(
<Fragment ref={fragmentRef}>
<html>
<body>
<a id="child-a" href="/">
A
</a>
</body>
</html>
</Fragment>,
);
});

await act(() => {
fragmentRef.current.focus();
});
expect(document.activeElement.id).toEqual('child-a');

await act(() => {
fragmentRef.current.blur();
});
expect(document.activeElement).toEqual(document.body);
});
});
});
});
Loading