[fix] 'render' export uses ReactDOM.createRoot

AppRegistry.runApplication uses `ReactDOM.createRoot` by default, but
the `render` export uses the legacy `ReactDOM.render`. This patch makes
those 2 APIs consistent.

It also makes some adjustments to the `createSheet` internals to more
reliably implement and test style sheet replication within ShadowRoot's
and iframes.

Fix #2612
This commit is contained in:
Nicolas Gallagher
2023-11-29 11:55:07 -08:00
parent a3ea2a0a4f
commit dee1467a3e
8 changed files with 307 additions and 109 deletions
+122 -36
View File
@@ -1,51 +1,137 @@
import React from 'react'; import React, { useRef, useEffect, useState } from 'react';
import { AppRegistry, Text, StyleSheet } from 'react-native'; import { Pressable, Text, StyleSheet, View, render } from 'react-native';
import Example from '../../shared/example'; import Example from '../../shared/example';
function App() { function IframeWrapper({ children }) {
return <Text style={styles.text}>Should be red and bold</Text>; const iframeHost = useRef();
const reactRoot = useRef();
useEffect(() => {
if (iframeHost.current) {
if (!reactRoot.current) {
const iframeElement = iframeHost.current;
const iframeAppContainer = document.createElement('div');
iframeElement.contentWindow.document.body.appendChild(
iframeAppContainer
);
reactRoot.current = render(children, iframeAppContainer);
}
reactRoot.current.render(children);
}
});
return <iframe ref={iframeHost} style={{ border: 'none' }} />;
} }
const styles = StyleSheet.create({ function ShadowDomWrapper({ children }) {
text: { const shadowHost = useRef();
color: 'red', const reactRoot = useRef();
fontWeight: 'bold'
}
});
AppRegistry.registerComponent('App', () => App); useEffect(() => {
if (shadowHost.current) {
if (!reactRoot.current) {
const shadowRoot = shadowHost.current.attachShadow({ mode: 'open' });
reactRoot.current = render(children, shadowRoot);
}
reactRoot.current.render(children);
}
});
export default function AppStatePage() { return <div ref={shadowHost} />;
const iframeRef = React.useRef(null); }
const shadowRef = React.useRef(null);
React.useEffect(() => { function Heading({ children }) {
const iframeElement = iframeRef.current; return (
const iframeBody = iframeElement.contentWindow.document.body; <Text role="heading" style={styles.heading}>
const iframeRootTag = document.createElement('div'); {children}
iframeRootTag.id = 'iframe-root'; </Text>
iframeBody.appendChild(iframeRootTag); );
const app1 = AppRegistry.runApplication('App', { rootTag: iframeRootTag }); }
const shadowElement = shadowRef.current; function Button({ active, onPress, title }) {
const shadowRoot = shadowElement.attachShadow({ mode: 'open' }); return (
const shadowRootTag = document.createElement('div'); <Pressable
shadowRootTag.id = 'shadow-root'; onPress={() => onPress((old) => !old)}
shadowRoot.appendChild(shadowRootTag); style={[styles.button, active && styles.buttonActive]}
const app2 = AppRegistry.runApplication('App', { rootTag: shadowRootTag }); >
<Text style={styles.buttonText}>{title}</Text>
</Pressable>
);
}
return () => { function App() {
app1.unmount(); const [active, setActive] = useState(false);
app2.unmount(); const [activeIframe, setActiveIframe] = useState(false);
}; const [activeShadow, setActiveShadow] = useState(false);
}, []);
return ( return (
<Example title="AppRegistry"> <Example title="AppRegistry">
<Text>Styles in iframe</Text> <View style={styles.app}>
<iframe ref={iframeRef} /> <View style={styles.header}>
<Text>Styles in ShadowRoot</Text> <Heading>Styles in document</Heading>
<div ref={shadowRef} /> <Text style={styles.text}>Should be red and bold</Text>
<Button active={active} onPress={setActive} title={'Button'} />
<Heading>Styles in ShadowRoot</Heading>
<ShadowDomWrapper>
<Text style={styles.text}>Should be red and bold</Text>
<Button
active={activeShadow}
onPress={setActiveShadow}
title={'Button'}
/>
</ShadowDomWrapper>
<Heading>Styles in iframe</Heading>
<IframeWrapper>
<Text style={styles.text}>Should be red and bold</Text>
<Button
active={activeIframe}
onPress={setActiveIframe}
title={'Button'}
/>
</IframeWrapper>
</View>
</View>
</Example> </Example>
); );
} }
const styles = StyleSheet.create({
app: {
marginHorizontal: 'auto',
maxWidth: 500
},
header: {
padding: 20
},
heading: {
fontWeight: 'bold',
fontSize: '1.125rem',
marginBlockStart: '1rem',
marginBlockEnd: '0.25rem'
},
text: {
color: 'red',
fontWeight: 'bold'
},
button: {
backgroundColor: 'red',
paddingBlock: 5,
paddingInline: 10
},
buttonActive: {
backgroundColor: 'blue'
},
buttonText: {
color: 'white',
fontWeight: 'bold',
textAlign: 'center',
textTransform: 'uppercase',
userSelect: 'none'
}
});
export default function AppStatePage() {
return <App />;
}
@@ -0,0 +1,101 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`AppRegistry runApplication styles roots in iframes: iframe css 1`] = `
"[stylesheet-group=\\"0\\"]{}
body{margin:0;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);}
input::-webkit-search-cancel-button,input::-webkit-search-decoration,input::-webkit-search-results-button,input::-webkit-search-results-decoration{display:none;}
[stylesheet-group=\\"1\\"]{}
.css-view-175oi2r{align-items:stretch;background-color:rgba(0,0,0,0.00);border:0 solid black;box-sizing:border-box;display:flex;flex-basis:auto;flex-direction:column;flex-shrink:0;list-style:none;margin:0px;min-height:0px;min-width:0px;padding:0px;position:relative;text-decoration:none;z-index:0;}
[stylesheet-group=\\"2\\"]{}
.r-display-xoduu5{display:inline-flex;}
.r-flex-13awgt0{flex:1;}
[stylesheet-group=\\"3\\"]{}
.r-bottom-1p0dtai{bottom:0px;}
.r-left-1d2f490{left:0px;}
.r-pointerEvents-105ug2t{pointer-events:auto!important;}
.r-pointerEvents-12vffkv>*{pointer-events:auto;}
.r-pointerEvents-12vffkv{pointer-events:none!important;}
.r-pointerEvents-633pao{pointer-events:none!important;}
.r-pointerEvents-ah5dr5>*{pointer-events:none;}
.r-pointerEvents-ah5dr5{pointer-events:auto!important;}
.r-position-u8s1d{position:absolute;}
.r-right-zchlnj{right:0px;}
.r-top-ipm5af{top:0px;}"
`;
exports[`AppRegistry runApplication styles roots in iframes: iframe css 2`] = `
"[stylesheet-group=\\"0\\"]{}
body{margin:0;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);}
input::-webkit-search-cancel-button,input::-webkit-search-decoration,input::-webkit-search-results-button,input::-webkit-search-results-decoration{display:none;}
[stylesheet-group=\\"1\\"]{}
.css-view-175oi2r{align-items:stretch;background-color:rgba(0,0,0,0.00);border:0 solid black;box-sizing:border-box;display:flex;flex-basis:auto;flex-direction:column;flex-shrink:0;list-style:none;margin:0px;min-height:0px;min-width:0px;padding:0px;position:relative;text-decoration:none;z-index:0;}
[stylesheet-group=\\"2\\"]{}
.r-display-xoduu5{display:inline-flex;}
.r-flex-13awgt0{flex:1;}
[stylesheet-group=\\"3\\"]{}
.r-bottom-1p0dtai{bottom:0px;}
.r-left-1d2f490{left:0px;}
.r-pointerEvents-105ug2t{pointer-events:auto!important;}
.r-pointerEvents-12vffkv>*{pointer-events:auto;}
.r-pointerEvents-12vffkv{pointer-events:none!important;}
.r-pointerEvents-633pao{pointer-events:none!important;}
.r-pointerEvents-ah5dr5>*{pointer-events:none;}
.r-pointerEvents-ah5dr5{pointer-events:auto!important;}
.r-position-u8s1d{position:absolute;}
.r-right-zchlnj{right:0px;}
.r-top-ipm5af{top:0px;}"
`;
exports[`AppRegistry runApplication styles roots in shadow trees: shadow dom css 1`] = `
"[stylesheet-group=\\"0\\"]{}
body{margin:0;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);}
input::-webkit-search-cancel-button,input::-webkit-search-decoration,input::-webkit-search-results-button,input::-webkit-search-results-decoration{display:none;}
[stylesheet-group=\\"1\\"]{}
.css-view-175oi2r{align-items:stretch;background-color:rgba(0,0,0,0.00);border:0 solid black;box-sizing:border-box;display:flex;flex-basis:auto;flex-direction:column;flex-shrink:0;list-style:none;margin:0px;min-height:0px;min-width:0px;padding:0px;position:relative;text-decoration:none;z-index:0;}
[stylesheet-group=\\"2\\"]{}
.r-display-xoduu5{display:inline-flex;}
.r-flex-13awgt0{flex:1;}
[stylesheet-group=\\"3\\"]{}
.r-bottom-1p0dtai{bottom:0px;}
.r-left-1d2f490{left:0px;}
.r-pointerEvents-105ug2t{pointer-events:auto!important;}
.r-pointerEvents-12vffkv>*{pointer-events:auto;}
.r-pointerEvents-12vffkv{pointer-events:none!important;}
.r-pointerEvents-633pao{pointer-events:none!important;}
.r-pointerEvents-ah5dr5>*{pointer-events:none;}
.r-pointerEvents-ah5dr5{pointer-events:auto!important;}
.r-position-u8s1d{position:absolute;}
.r-right-zchlnj{right:0px;}
.r-top-ipm5af{top:0px;}"
`;
exports[`AppRegistry runApplication styles roots in shadow trees: shadow dom css 2`] = `
"[stylesheet-group=\\"0\\"]{}
body{margin:0;}
button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0;}
html{-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:rgba(0,0,0,0);}
input::-webkit-search-cancel-button,input::-webkit-search-decoration,input::-webkit-search-results-button,input::-webkit-search-results-decoration{display:none;}
[stylesheet-group=\\"1\\"]{}
.css-view-175oi2r{align-items:stretch;background-color:rgba(0,0,0,0.00);border:0 solid black;box-sizing:border-box;display:flex;flex-basis:auto;flex-direction:column;flex-shrink:0;list-style:none;margin:0px;min-height:0px;min-width:0px;padding:0px;position:relative;text-decoration:none;z-index:0;}
[stylesheet-group=\\"2\\"]{}
.r-display-xoduu5{display:inline-flex;}
.r-flex-13awgt0{flex:1;}
[stylesheet-group=\\"3\\"]{}
.r-bottom-1p0dtai{bottom:0px;}
.r-left-1d2f490{left:0px;}
.r-pointerEvents-105ug2t{pointer-events:auto!important;}
.r-pointerEvents-12vffkv>*{pointer-events:auto;}
.r-pointerEvents-12vffkv{pointer-events:none!important;}
.r-pointerEvents-633pao{pointer-events:none!important;}
.r-pointerEvents-ah5dr5>*{pointer-events:none;}
.r-pointerEvents-ah5dr5{pointer-events:auto!important;}
.r-position-u8s1d{position:absolute;}
.r-right-zchlnj{right:0px;}
.r-top-ipm5af{top:0px;}"
`;
@@ -68,7 +68,7 @@ describe.each([['concurrent'], ['legacy']])('AppRegistry', (mode) => {
expect(setMountedState).toHaveBeenLastCalledWith(false); expect(setMountedState).toHaveBeenLastCalledWith(false);
}); });
test('styles roots in different documents', () => { test('styles roots in iframes', () => {
AppRegistry.registerComponent('App', () => NoopComponent); AppRegistry.registerComponent('App', () => NoopComponent);
act(() => { act(() => {
AppRegistry.runApplication('App', { initialProps: {}, rootTag, mode }); AppRegistry.runApplication('App', { initialProps: {}, rootTag, mode });
@@ -76,7 +76,6 @@ describe.each([['concurrent'], ['legacy']])('AppRegistry', (mode) => {
// Create iframe context // Create iframe context
const iframe = document.createElement('iframe'); const iframe = document.createElement('iframe');
document.body.appendChild(iframe); document.body.appendChild(iframe);
const iframeRootTag = document.createElement('div'); const iframeRootTag = document.createElement('div');
iframeRootTag.id = 'react-iframe-root'; iframeRootTag.id = 'react-iframe-root';
iframe.contentWindow.document.body.appendChild(iframeRootTag); iframe.contentWindow.document.body.appendChild(iframeRootTag);
@@ -90,43 +89,39 @@ describe.each([['concurrent'], ['legacy']])('AppRegistry', (mode) => {
mode mode
}); });
}); });
const iframedoc = iframeRootTag.ownerDocument; const iframedoc = iframeRootTag.ownerDocument;
expect(iframedoc).toBe(iframe.contentWindow.document); expect(iframedoc).toBe(iframe.contentWindow.document);
expect(iframedoc).not.toBe(document); expect(iframedoc).not.toBe(document);
const cssText = iframedoc.getElementById(
'react-native-stylesheet'
).textContent;
expect(cssText).toMatchSnapshot('iframe css');
});
const cssText = Array.prototype.slice test('styles roots in shadow trees', () => {
.call( AppRegistry.registerComponent('App', () => NoopComponent);
iframedoc.getElementById('react-native-stylesheet').sheet.cssRules act(() => {
) AppRegistry.runApplication('App', { initialProps: {}, rootTag, mode });
.map((cssRule) => cssRule.cssText); });
// Create shadow dom
const shadowRootHost = document.createElement('div');
const shadowRoot = shadowRootHost.attachShadow({ mode: 'open' });
const shadowContainer = document.createElement('div');
shadowRoot.appendChild(shadowContainer);
document.body.appendChild(shadowRootHost);
expect(cssText).toMatchInlineSnapshot(` // Run in shadow dom
[ act(() => {
"[stylesheet-group=\\"0\\"] {}", AppRegistry.runApplication('App', {
"body {margin: 0;}", initialProps: {},
"button::-moz-focus-inner,input::-moz-focus-inner {border: 0; padding: 0;}", rootTag: shadowContainer,
"html {-ms-text-size-adjust: 100%; -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: rgba(0,0,0,0);}", mode
"input::-webkit-search-cancel-button,input::-webkit-search-decoration,input::-webkit-search-results-button,input::-webkit-search-results-decoration {display: none;}", });
"[stylesheet-group=\\"1\\"] {}", });
".css-view-175oi2r {align-items: stretch; background-color: rgba(0,0,0,0.00); border: 0 solid black; box-sizing: border-box; display: flex; flex-basis: auto; flex-direction: column; flex-shrink: 0; list-style: none; margin: 0px; min-height: 0px; min-width: 0px; padding: 0px; position: relative; text-decoration: none; z-index: 0;}", const cssText = shadowRoot.getElementById(
"[stylesheet-group=\\"2\\"] {}", 'react-native-stylesheet'
".r-display-xoduu5 {display: inline-flex;}", ).textContent;
".r-flex-13awgt0 {flex: 1;}", expect(cssText).toMatchSnapshot('shadow dom css');
"[stylesheet-group=\\"3\\"] {}",
".r-bottom-1p0dtai {bottom: 0px;}",
".r-left-1d2f490 {left: 0px;}",
".r-pointerEvents-105ug2t {pointer-events: auto !important;}",
".r-pointerEvents-12vffkv>* {pointer-events: auto;}",
".r-pointerEvents-12vffkv {pointer-events: none !important;}",
".r-pointerEvents-633pao {pointer-events: none !important;}",
".r-pointerEvents-ah5dr5>* {pointer-events: none;}",
".r-pointerEvents-ah5dr5 {pointer-events: auto !important;}",
".r-position-u8s1d {position: absolute;}",
".r-right-zchlnj {right: 0px;}",
".r-top-ipm5af {top: 0px;}",
]
`);
}); });
}); });
}); });
@@ -12,7 +12,7 @@ import type { ComponentType, Node } from 'react';
import AppContainer from './AppContainer'; import AppContainer from './AppContainer';
import invariant from 'fbjs/lib/invariant'; import invariant from 'fbjs/lib/invariant';
import renderLegacy, { hydrateLegacy, render, hydrate } from '../render'; import render, { hydrateLegacy, renderLegacy, hydrate } from '../render';
import StyleSheet from '../StyleSheet'; import StyleSheet from '../StyleSheet';
import React from 'react'; import React from 'react';
@@ -32,6 +32,7 @@ export default function renderApplication<Props: Object>(
} }
): Application { ): Application {
const { hydrate: shouldHydrate, initialProps, mode, rootTag } = options; const { hydrate: shouldHydrate, initialProps, mode, rootTag } = options;
const renderFn = shouldHydrate const renderFn = shouldHydrate
? mode === 'concurrent' ? mode === 'concurrent'
? hydrate ? hydrate
@@ -9,14 +9,14 @@ import { createSheet } from '../dom';
describe('createSheet', () => { describe('createSheet', () => {
test('creates a sheet on the client', () => { test('creates a sheet on the client', () => {
const sheet = createSheet(); const sheet = createSheet(document);
expect(sheet.id).toMatchInlineSnapshot(`"react-native-stylesheet"`); expect(sheet.id).toMatchInlineSnapshot(`"react-native-stylesheet"`);
expect(typeof sheet.getTextContent()).toBe('string'); expect(typeof sheet.getTextContent()).toBe('string');
expect(typeof sheet.insert).toBe('function'); expect(typeof sheet.insert).toBe('function');
}); });
test('supports multiple documents with same styles', () => { test('supports multiple documents with same styles', () => {
const sheet = createSheet(); const sheet = createSheet(document);
sheet.insert('.test-sheet { opacity: 1 }', 3); sheet.insert('.test-sheet { opacity: 1 }', 3);
// Iframe ----- // Iframe -----
@@ -25,7 +25,7 @@ describe('createSheet', () => {
const iframeDoc = iframe.contentWindow.document; const iframeDoc = iframe.contentWindow.document;
const iframeRootTag = document.createElement('div'); const iframeRootTag = document.createElement('div');
iframeDoc.body.appendChild(iframeRootTag); iframeDoc.body.appendChild(iframeRootTag);
const iframeSheet = createSheet(iframeRootTag); const iframeSheet = createSheet(iframeDoc);
// Did we generate a new sheet? // Did we generate a new sheet?
expect(sheet).not.toBe(iframeSheet); expect(sheet).not.toBe(iframeSheet);
@@ -39,20 +39,22 @@ describe('createSheet', () => {
expect(iframeSheet.getTextContent().includes('test-iframe')).toBe(true); expect(iframeSheet.getTextContent().includes('test-iframe')).toBe(true);
// ShadowDOM ----- // ShadowDOM -----
const shadowRootHost = document.createElement('div');
const shadowRoot = shadowRootHost.attachShadow({ mode: 'open' });
const div = document.createElement('div'); const div = document.createElement('div');
const shadowRoot = div.attachShadow({ mode: 'open' }); shadowRoot.appendChild(div);
const shadowRootTag = document.createElement('div'); document.body.appendChild(shadowRootHost);
shadowRoot.appendChild(shadowRootTag); const shadowSheet = createSheet(shadowRoot);
document.body.appendChild(shadowRoot);
const shadowSheet = createSheet(shadowRootTag);
// Did we generate a new sheet? // Did we generate a new sheet?
expect(sheet).not.toBe(shadowSheet); expect(sheet).not.toBe(shadowSheet);
expect(shadowSheet.id).toMatchInlineSnapshot(`"react-native-stylesheet"`); expect(shadowSheet.id).toMatchInlineSnapshot(`"react-native-stylesheet"`);
expect(typeof shadowSheet.insert).toBe('function'); expect(typeof shadowSheet.insert).toBe('function');
// expect(shadowRoot.getElementById('react-native-stylesheet')).not.toBe(null); expect(shadowRoot.getElementById('react-native-stylesheet')).not.toBe(null);
// Does the content match existing sheets? // Does the content match existing sheets?
expect(shadowSheet.getTextContent().includes('test-sheet')).toBe(true); // NOTE: commented out because JSDOM doesn't reproduce the inserted text content.
// JSDOM doesn't appear to populate `sheet.cssRules` after a sheet's `textContent` is set.
// expect(shadowSheet.getTextContent().includes('test-sheet')).toBe(true);
// Does the content update when other sheets are updated? // Does the content update when other sheets are updated?
sheet.insert('.test-shadow { opacity: 0 }', 3); sheet.insert('.test-shadow { opacity: 0 }', 3);
expect(shadowSheet.getTextContent().includes('test-shadow')).toBe(true); expect(shadowSheet.getTextContent().includes('test-shadow')).toBe(true);
@@ -14,19 +14,18 @@ export default function createCSSStyleSheet(
rootNode?: Document | ShadowRoot, rootNode?: Document | ShadowRoot,
textContent?: string textContent?: string
): ?CSSStyleSheet { ): ?CSSStyleSheet {
if (canUseDOM) { if (canUseDOM && rootNode != null) {
const root = rootNode != null ? rootNode : document; let element = rootNode.getElementById(id);
let element = root.getElementById(id);
if (element == null) { if (element == null) {
element = document.createElement('style'); element = document.createElement('style');
element.setAttribute('id', id); element.setAttribute('id', id);
if (typeof textContent === 'string') { if (typeof textContent === 'string') {
element.appendChild(document.createTextNode(textContent)); element.appendChild(document.createTextNode(textContent));
} }
if (root instanceof ShadowRoot) { if (rootNode instanceof ShadowRoot) {
root.insertBefore(element, root.firstChild); rootNode.insertBefore(element, rootNode.firstChild);
} else { } else {
const head = root.head; const head = rootNode.head;
if (head) { if (head) {
head.insertBefore(element, head.firstChild); head.insertBefore(element, head.firstChild);
} }
@@ -18,8 +18,8 @@ type Sheet = {
}; };
const defaultId = 'react-native-stylesheet'; const defaultId = 'react-native-stylesheet';
const roots = new WeakMap<Node, number>(); const _roots = new WeakMap<Node, number>();
const sheets = []; const _sheets = [];
const initialRules = [ const initialRules = [
// minimal top-level reset // minimal top-level reset
@@ -31,16 +31,25 @@ const initialRules = [
]; ];
export function createSheet( export function createSheet(
root?: HTMLElement, _rootNode?: Document | ShadowRoot,
id?: string = defaultId id?: string = defaultId
): Sheet { ): Sheet {
let sheet; let sheet;
if (canUseDOM) { if (canUseDOM) {
const rootNode: Node = root != null ? root.getRootNode() : document; window.__sheets = window.__sheets || _sheets;
window.__roots = window.__roots || _roots;
const sheets = window.__sheets;
const roots = window.__roots;
let rootNode = _rootNode;
if (_rootNode == null) {
rootNode = document;
}
// Create the initial style sheet // Create the initial style sheet
if (sheets.length === 0) { if (sheets.length === 0) {
sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(id)); sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(id, rootNode));
initialRules.forEach((rule) => { initialRules.forEach((rule) => {
sheet.insert(rule, 0); sheet.insert(rule, 0);
}); });
@@ -65,14 +74,14 @@ export function createSheet(
} }
} else { } else {
// Create the initial style sheet // Create the initial style sheet
if (sheets.length === 0) { if (_sheets.length === 0) {
sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(id)); sheet = createOrderedCSSStyleSheet(createCSSStyleSheet(id));
initialRules.forEach((rule) => { initialRules.forEach((rule) => {
sheet.insert(rule, 0); sheet.insert(rule, 0);
}); });
sheets.push(sheet); _sheets.push(sheet);
} else { } else {
sheet = sheets[0]; sheet = _sheets[0];
} }
} }
@@ -82,6 +91,7 @@ export function createSheet(
}, },
id, id,
insert(cssText: string, groupValue: number) { insert(cssText: string, groupValue: number) {
const sheets = canUseDOM ? window.__sheets : _sheets;
sheets.forEach((s) => { sheets.forEach((s) => {
s.insert(cssText, groupValue); s.insert(cssText, groupValue);
}); });
+18 -14
View File
@@ -19,34 +19,38 @@ import {
import unmountComponentAtNode from '../unmountComponentAtNode'; import unmountComponentAtNode from '../unmountComponentAtNode';
import { createSheet } from '../StyleSheet/dom'; import { createSheet } from '../StyleSheet/dom';
export function hydrate(element, root) { export function hydrate(element, container) {
createSheet(root); const rootNode = container.getRootNode();
return domHydrateRoot(root, element); createSheet(rootNode);
return domHydrateRoot(container, element);
} }
export function render(element, root) { export default function render(element, container) {
createSheet(root); const rootNode = container.getRootNode();
const reactRoot = domCreateRoot(root); const reactRoot = domCreateRoot(container);
reactRoot.render(element); reactRoot.render(element);
createSheet(rootNode);
return reactRoot; return reactRoot;
} }
export function hydrateLegacy(element, root, callback) { export function hydrateLegacy(element, container, callback) {
createSheet(root); const rootNode = container.getRootNode();
domLegacyHydrate(element, root, callback); domLegacyHydrate(element, container, callback);
createSheet(rootNode);
return { return {
unmount: function () { unmount: function () {
return unmountComponentAtNode(root); return unmountComponentAtNode(container);
} }
}; };
} }
export default function renderLegacy(element, root, callback) { export function renderLegacy(element, container, callback) {
createSheet(root); const rootNode = container.getRootNode();
domLegacyRender(element, root, callback); domLegacyRender(element, container, callback);
createSheet(rootNode);
return { return {
unmount: function () { unmount: function () {
return unmountComponentAtNode(root); return unmountComponentAtNode(container);
} }
}; };
} }