Skip to content

Commit

Permalink
Fix createRoot container signature (#24110)
Browse files Browse the repository at this point in the history
The internal Container type represents the types of containers that React
can support in its internals that deal with containers.

This didn't include DocumentFragment which we support specifically for
rendering into shadow roots.

However, not all types makes sense to pass into the createRoot API.

One of those is comment nodes that is deprecated and we don't really fully
support. It really only exists for FB legacy.

For createRoot it doesn't make sense to pass a Document since that will try
to empty the document which removes the HTML tag which doesn't work.
Documents can only be passed to hydrateRoot.

Conversely I'm not sure we actually support hydrating a shadow root properly
so I excluded DocumentFragment from hydrateRoot.
  • Loading branch information
sebmarkbage authored Mar 17, 2022
1 parent b075f97 commit 1c44437
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
5 changes: 2 additions & 3 deletions packages/react-dom/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

'use strict';

import type {Container} from './src/client/ReactDOMHostConfig';
import type {ReactNodeList} from 'shared/ReactTypes';
import type {
RootType,
Expand All @@ -24,7 +23,7 @@ import {
} from './';

export function createRoot(
container: Container,
container: Element | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Expand All @@ -40,7 +39,7 @@ export function createRoot(
}

export function hydrateRoot(
container: Container,
container: Document | Element,
children: ReactNodeList,
options?: HydrateRootOptions,
): RootType {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const Internals = {
};

function createRoot(
container: Container,
container: Element | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (__DEV__) {
Expand All @@ -163,7 +163,7 @@ function createRoot(
}

function hydrateRoot(
container: Container,
container: Document | Element,
initialChildren: ReactNodeList,
options?: HydrateRootOptions,
): RootType {
Expand Down
22 changes: 11 additions & 11 deletions packages/react-dom/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export function checkForUnmatchedText(
}

function getOwnerDocumentFromRootContainer(
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
): Document {
return rootContainerElement.nodeType === DOCUMENT_NODE
? (rootContainerElement: any)
Expand All @@ -284,7 +284,7 @@ export function trapClickOnNonInteractiveElement(node: HTMLElement) {
function setInitialDOMProperties(
tag: string,
domElement: Element,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
nextProps: Object,
isCustomComponentTag: boolean,
): void {
Expand Down Expand Up @@ -371,7 +371,7 @@ function updateDOMProperties(
export function createElement(
type: string,
props: Object,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
parentNamespace: string,
): Element {
let isCustomComponentTag;
Expand Down Expand Up @@ -477,7 +477,7 @@ export function createElement(

export function createTextNode(
text: string,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
): Text {
return getOwnerDocumentFromRootContainer(rootContainerElement).createTextNode(
text,
Expand All @@ -488,7 +488,7 @@ export function setInitialProperties(
domElement: Element,
tag: string,
rawProps: Object,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
): void {
const isCustomComponentTag = isCustomComponent(tag, rawProps);
if (__DEV__) {
Expand Down Expand Up @@ -614,7 +614,7 @@ export function diffProperties(
tag: string,
lastRawProps: Object,
nextRawProps: Object,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
): null | Array<mixed> {
if (__DEV__) {
validatePropertiesInDevelopment(tag, nextRawProps);
Expand Down Expand Up @@ -867,7 +867,7 @@ export function diffHydratedProperties(
tag: string,
rawProps: Object,
parentNamespace: string,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | DocumentFragment,
isConcurrentMode: boolean,
shouldWarnDev: boolean,
): null | Array<mixed> {
Expand Down Expand Up @@ -1200,7 +1200,7 @@ export function diffHydratedText(
}

export function warnForDeletedHydratableElement(
parentNode: Element | Document,
parentNode: Element | Document | DocumentFragment,
child: Element,
) {
if (__DEV__) {
Expand All @@ -1217,7 +1217,7 @@ export function warnForDeletedHydratableElement(
}

export function warnForDeletedHydratableText(
parentNode: Element | Document,
parentNode: Element | Document | DocumentFragment,
child: Text,
) {
if (__DEV__) {
Expand All @@ -1234,7 +1234,7 @@ export function warnForDeletedHydratableText(
}

export function warnForInsertedHydratedElement(
parentNode: Element | Document,
parentNode: Element | Document | DocumentFragment,
tag: string,
props: Object,
) {
Expand All @@ -1252,7 +1252,7 @@ export function warnForInsertedHydratedElement(
}

export function warnForInsertedHydratedText(
parentNode: Element | Document,
parentNode: Element | Document | DocumentFragment,
text: string,
) {
if (__DEV__) {
Expand Down
3 changes: 2 additions & 1 deletion packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export type EventTargetChildElement = {
};
export type Container =
| (Element & {_reactRootContainer?: FiberRoot, ...})
| (Document & {_reactRootContainer?: FiberRoot, ...});
| (Document & {_reactRootContainer?: FiberRoot, ...})
| (DocumentFragment & {_reactRootContainer?: FiberRoot, ...});
export type Instance = Element;
export type TextInstance = Text;
export type SuspenseInstance = Comment & {_reactRetry?: () => void, ...};
Expand Down
13 changes: 7 additions & 6 deletions packages/react-dom/src/client/ReactDOMRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @flow
*/

import type {Container} from './ReactDOMHostConfig';
import type {MutableSource, ReactNodeList} from 'shared/ReactTypes';
import type {
FiberRoot,
Expand Down Expand Up @@ -165,7 +164,7 @@ ReactDOMHydrationRoot.prototype.unmount = ReactDOMRoot.prototype.unmount = funct
};

export function createRoot(
container: Container,
container: Element | DocumentFragment,
options?: CreateRootOptions,
): RootType {
if (!isValidContainer(container)) {
Expand Down Expand Up @@ -235,8 +234,10 @@ export function createRoot(
);
markContainerAsRoot(root.current, container);

const rootContainerElement =
container.nodeType === COMMENT_NODE ? container.parentNode : container;
const rootContainerElement: Document | Element | DocumentFragment =
container.nodeType === COMMENT_NODE
? (container.parentNode: any)
: container;
listenToAllSupportedEvents(rootContainerElement);

return new ReactDOMRoot(root);
Expand All @@ -253,7 +254,7 @@ function scheduleHydration(target: Node) {
ReactDOMHydrationRoot.prototype.unstable_scheduleHydration = scheduleHydration;

export function hydrateRoot(
container: Container,
container: Document | Element,
initialChildren: ReactNodeList,
options?: HydrateRootOptions,
): RootType {
Expand Down Expand Up @@ -351,7 +352,7 @@ export function isValidContainerLegacy(node: any): boolean {
);
}

function warnIfReactDOMContainerInDEV(container) {
function warnIfReactDOMContainerInDEV(container: any) {
if (__DEV__) {
if (
container.nodeType === ELEMENT_NODE &&
Expand Down

0 comments on commit 1c44437

Please sign in to comment.