Skip to content

Commit

Permalink
Share the root node via a singleton-like function, getRootNode(), and
Browse files Browse the repository at this point in the history
also extract `getRootDomElement()` to further reduce duplications.
  • Loading branch information
southp committed Oct 18, 2024
1 parent 5e3f084 commit f28e633
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 13 additions & 2 deletions client/boot/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ const unsavedFormsMiddleware = () => {
page.exit( '*', checkFormHandler );
};

export const getRootDomElement = () => document.getElementById( 'wpcom' );

const utils = () => {
debug( 'Executing Calypso utils.' );

Expand All @@ -217,7 +219,7 @@ const utils = () => {
accessibleFocus();

// Configure app element that React Modal will aria-hide when modal is open
Modal.setAppElement( document.getElementById( 'wpcom' ) );
Modal.setAppElement( getRootDomElement() );
};

const configureReduxStore = ( currentUser, reduxStore ) => {
Expand Down Expand Up @@ -402,8 +404,17 @@ const setupMiddlewares = ( currentUser, reduxStore, reactQueryClient ) => {
}
};

let wpcomRootNode;
export const getRootNode = () => {
if ( wpcomRootNode == null ) {
wpcomRootNode = createRoot( getRootDomElement() );
}

return wpcomRootNode;
};

function renderLayout( reduxStore, reactQueryClient ) {
createRoot( document.getElementById( 'wpcom' ) ).render(
getRootNode().render(
<ProviderWrappedLayout store={ reduxStore } queryClient={ reactQueryClient } />
);
}
Expand Down
7 changes: 4 additions & 3 deletions client/controller/web-util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createRoot, hydrateRoot } from 'react-dom/client';
import { hydrateRoot } from 'react-dom/client';
import { getRootNode, getRootDomElement } from 'calypso/boot/common';

export function render( context ) {
createRoot( document.getElementById( 'wpcom' ) ).render( context.layout );
getRootNode().render( context.layout );
}

export function hydrate( context ) {
hydrateRoot( document.getElementById( 'wpcom' ), context.layout );
hydrateRoot( getRootDomElement(), context.layout );
}

0 comments on commit f28e633

Please sign in to comment.