1+ import BrowserOnly from '@docusaurus/BrowserOnly' ;
12import StackBlitzSDK , { EmbedOptions } from '@stackblitz/sdk' ;
23import { useCallback } from 'react' ;
34import React from 'react' ;
@@ -8,19 +9,26 @@ interface Props {
89
910const cache : Record < string , HTMLDivElement > = { } ;
1011
11- const elRoot = document . createElement ( 'div' ) ;
12- elRoot . style . visibility = 'hidden' ;
13- elRoot . style . pointerEvents = 'none' ;
14- elRoot . style . width = '0px' ;
15- elRoot . style . height = '0px' ;
16- elRoot . style . position = 'absolute' ;
12+ let elRoot : HTMLDivElement | null = null ;
1713
18- document . body . appendChild ( elRoot ) ;
14+ function getRootElement ( ) {
15+ const elRoot = document . createElement ( 'div' ) ;
16+ elRoot . style . visibility = 'hidden' ;
17+ elRoot . style . pointerEvents = 'none' ;
18+ elRoot . style . width = '0px' ;
19+ elRoot . style . height = '0px' ;
20+ elRoot . style . position = 'absolute' ;
21+
22+ document . body . appendChild ( elRoot ) ;
23+ return elRoot ;
24+ }
1925
2026function getStackblitzEl ( projectId : string ) {
2127 const existing = cache [ projectId ] ;
2228 if ( existing ) return existing ;
2329
30+ elRoot = elRoot || getRootElement ( ) ;
31+
2432 const elParent = document . createElement ( 'div' ) ;
2533 elParent . style . display = 'contents' ;
2634 elRoot . appendChild ( elParent ) ;
@@ -50,7 +58,11 @@ function getStackblitzEl(projectId: string) {
5058 return elParent ;
5159}
5260
53- export function Stackblitz ( { embedId } : Props ) {
61+ export function Stackblitz ( props : Props ) {
62+ return < BrowserOnly > { ( ) => < StackblitzCore { ...props } /> } </ BrowserOnly > ;
63+ }
64+
65+ function StackblitzCore ( { embedId } : Props ) {
5466 const el = getStackblitzEl ( embedId ) ;
5567
5668 const ref = useCallback (
0 commit comments