@@ -5,6 +5,7 @@ import React from 'react';
55
66interface Props {
77 embedId : 'string' ;
8+ exampleId ?: string ;
89}
910
1011const cache : Record < string , HTMLDivElement > = { } ;
@@ -23,7 +24,7 @@ function getRootElement() {
2324 return elRoot ;
2425}
2526
26- function getStackblitzEl ( projectId : string ) {
27+ function getStackblitzEl ( projectId : string , exampleId ?: string ) {
2728 const existing = cache [ projectId ] ;
2829 if ( existing ) return existing ;
2930
@@ -40,12 +41,15 @@ function getStackblitzEl(projectId: string) {
4041 forceEmbedLayout : true ,
4142 view : 'preview' ,
4243 height : '100%' ,
43- openFile : ' src/App .tsx' ,
44+ openFile : exampleId ? ` src/${ exampleId } /example .tsx` : undefined ,
4445 } ;
4546
4647 const isGithub = projectId . startsWith ( 'KurtGokhan' ) ;
47- if ( isGithub ) StackBlitzSDK . embedGithubProject ( el , projectId , opts ) ;
48- else StackBlitzSDK . embedProjectId ( el , projectId , opts ) ;
48+
49+ const embedFn = isGithub ? StackBlitzSDK . embedGithubProject : StackBlitzSDK . embedProjectId ;
50+ const embedPromise = embedFn ( el , projectId , opts ) ;
51+
52+ embedPromise . then ( ( p ) => p . preview . setUrl ( `/${ exampleId || '' } ` ) ) ;
4953
5054 cache [ projectId ] = elParent ;
5155 return elParent ;
@@ -55,8 +59,8 @@ export function Stackblitz(props: Props) {
5559 return < BrowserOnly > { ( ) => < StackblitzCore { ...props } /> } </ BrowserOnly > ;
5660}
5761
58- function StackblitzCore ( { embedId } : Props ) {
59- const el = getStackblitzEl ( embedId ) ;
62+ function StackblitzCore ( { embedId, exampleId } : Props ) {
63+ const el = getStackblitzEl ( embedId , exampleId ) ;
6064
6165 const ref = useCallback ( ( node ) => node ?. appendChild ( el ) , [ el ] ) ;
6266
0 commit comments