Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: React cannot render in ShadowRoot #15894

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 17 additions & 13 deletions packages/react-dom/src/client/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
DOCUMENT_NODE,
ELEMENT_NODE,
COMMENT_NODE,
DOCUMENT_FRAGMENT_NODE,
} from '../shared/HTMLNodeType';
import isCustomComponent from '../shared/isCustomComponent';
import possibleStandardNames from '../shared/possibleStandardNames';
Expand Down Expand Up @@ -256,7 +257,7 @@ if (__DEV__) {
}

export function ensureListeningTo(
rootContainerInstance: Element | Node,
rootContainerInstance: Element | Node | ShadowRoot,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary. ShadowRoot is already a Node. (So is element, fwiw)

reactPropEvent: string,
targetElement: Element | null,
): void {
Expand All @@ -271,7 +272,10 @@ export function ensureListeningTo(
// with the modern plugin event system.
invariant(
rootContainerElement != null &&
rootContainerElement.nodeType === ELEMENT_NODE,
(rootContainerElement.nodeType === ELEMENT_NODE ||
(rootContainerElement.nodeType === DOCUMENT_FRAGMENT_NODE &&
// $FlowFixMe GH issue #8457
((rootContainerElement: any): ShadowRoot).mode)),
'ensureListeningTo(): received a container that was not an element node. ' +
'This is likely a bug in React.',
);
Expand All @@ -283,7 +287,7 @@ export function ensureListeningTo(
}

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

export function createTextNode(
text: string,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | ShadowRoot,
): Text {
return getOwnerDocumentFromRootContainer(rootContainerElement).createTextNode(
text,
Expand All @@ -510,7 +514,7 @@ export function setInitialProperties(
domElement: Element,
tag: string,
rawProps: Object,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | ShadowRoot,
): void {
const isCustomComponentTag = isCustomComponent(tag, rawProps);
if (__DEV__) {
Expand Down Expand Up @@ -645,7 +649,7 @@ export function diffProperties(
tag: string,
lastRawProps: Object,
nextRawProps: Object,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | ShadowRoot,
): null | Array<mixed> {
if (__DEV__) {
validatePropertiesInDevelopment(tag, nextRawProps);
Expand Down Expand Up @@ -910,7 +914,7 @@ export function diffHydratedProperties(
tag: string,
rawProps: Object,
parentNamespace: string,
rootContainerElement: Element | Document,
rootContainerElement: Element | Document | ShadowRoot,
): null | Array<mixed> {
let isCustomComponentTag;
let extraAttributeNames: Set<string>;
Expand Down Expand Up @@ -1209,7 +1213,7 @@ export function warnForUnmatchedText(textNode: Text, text: string) {
}

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

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

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

export function warnForInsertedHydratedText(
parentNode: Element | Document,
parentNode: Element | Document | ShadowRoot,
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 @@ -104,7 +104,8 @@ export type EventTargetChildElement = {
};
export type Container =
| (Element & {_reactRootContainer?: RootType, ...})
| (Document & {_reactRootContainer?: RootType, ...});
| (Document & {_reactRootContainer?: RootType, ...})
| (ShadowRoot & {_reactRootContainer?: RootType, ...});
export type Instance = Element;
export type TextInstance = Text;
export type SuspenseInstance = Comment & {_reactRetry?: () => void, ...};
Expand Down