@@ -3,6 +3,7 @@ import React, { type ReactElement } from 'react';
33import { Body } from './Body' ;
44import { DocumentStructureContext } from './DocumentStructureContext' ;
55import { Head } from './Head' ;
6+ import { Root } from './Root' ;
67
78/**
89 * get the directly son element by name
@@ -21,6 +22,31 @@ function findTargetChildByComponent(
2122 return children . find ( item => item ?. type === component ) ;
2223}
2324
25+ /**
26+ * get the children(grandChild included) with target component reference
27+ * @param component the component reference
28+ * @param children son element
29+ * @returns target element
30+ */
31+ function findTargetElementByComponent (
32+ component : unknown ,
33+ children : ReactElement < any > [ ] ,
34+ ) : ReactElement < { children ?: ReactElement < any > } > | null {
35+ if ( children . length === 0 ) {
36+ return null ;
37+ }
38+ let nextChildren : ReactElement < { children : ReactElement < any > } > [ ] = [ ] ;
39+ for ( const item of children ) {
40+ if ( item ?. type === component ) {
41+ return item ;
42+ }
43+ if ( item ?. props ?. children ) {
44+ nextChildren = nextChildren . concat ( item . props . children ) ;
45+ }
46+ }
47+ return findTargetElementByComponent ( component , nextChildren ) ;
48+ }
49+
2450/**
2551 * get the type of react element
2652 */
@@ -70,7 +96,10 @@ export function Html(
7096 findTargetChildByComponent ( Body , children ) ||
7197 findTargetChildByName ( 'Body' , children ) ,
7298 ) ;
73- const hasSetRoot = Boolean ( findTargetElement ( 'Root' , children ) ) ;
99+ const hasSetRoot = Boolean (
100+ findTargetElementByComponent ( Root , children ) ||
101+ findTargetChildByName ( 'Root' , children ) ,
102+ ) ;
74103 const hasSetTitle = Boolean ( findTargetElement ( 'title' , children ) ) ;
75104 const notMissMustChild = [
76105 hasSetHead ,
0 commit comments