Skip to content

Commit ab430b8

Browse files
committed
ability to add example id to examples
1 parent 0c8e26a commit ab430b8

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

website/src/components/Stackblitz/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React from 'react';
55

66
interface Props {
77
embedId: 'string';
8+
exampleId?: string;
89
}
910

1011
const 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

Comments
 (0)