Skip to content

Commit 05307f6

Browse files
committed
add stackblitz example
1 parent 96108a4 commit 05307f6

File tree

7 files changed

+85
-7
lines changed

7 files changed

+85
-7
lines changed

package-lock.json

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"scripts": {
2020
"start": "npm run start --workspace=tanstack-query-builder-example-vite",
21+
"docs": "npm run start --workspace=tanstack-query-builder-website",
2122
"test": "vitest",
2223
"lint": "biome lint",
2324
"format": "biome format",

website/docs/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ title: Introduction
1010
```bash npm2yarn
1111
npm install tanstack-query-builder
1212
```
13+
14+
<Stackblitz embedId="KurtGokhan/tanstack-query-builder/tree/main/examples/vite" />

website/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "website",
2+
"name": "tanstack-query-builder-website",
33
"version": "0.9.2",
44
"private": true,
55
"scripts": {
@@ -19,6 +19,7 @@
1919
"@docusaurus/preset-classic": "3.7.0",
2020
"@docusaurus/remark-plugin-npm2yarn": "^3.7.0",
2121
"@mdx-js/react": "^3.1.0",
22+
"@stackblitz/sdk": "^1.11.0",
2223
"clsx": "^2.1.1",
2324
"prism-react-renderer": "^2.4.1",
2425
"react": "^19.0.0",

website/src/components/BrowserWindow/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Props {
1111
bodyStyle?: CSSProperties;
1212
}
1313

14-
export default function BrowserWindow({ children, minHeight, url = 'http://localhost:3000', style, bodyStyle }: Props) {
14+
export function BrowserWindow({ children, minHeight, url = 'http://localhost:3000', style, bodyStyle }: Props) {
1515
return (
1616
<div className={styles.browserWindow} style={{ ...style, minHeight }}>
1717
<div className={styles.browserWindowHeader}>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import StackBlitzSDK, { EmbedOptions } from '@stackblitz/sdk';
2+
import { useCallback } from 'react';
3+
import React from 'react';
4+
5+
interface Props {
6+
embedId: 'string';
7+
}
8+
9+
const cache: Record<string, HTMLDivElement> = {};
10+
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';
17+
18+
document.body.appendChild(elRoot);
19+
20+
function getStackblitzEl(projectId: string) {
21+
const existing = cache[projectId];
22+
if (existing) return existing;
23+
24+
const elParent = document.createElement('div');
25+
elParent.style.display = 'contents';
26+
elRoot.appendChild(elParent);
27+
28+
const el = document.createElement('div');
29+
elParent.appendChild(el);
30+
31+
const opts: EmbedOptions = {
32+
forceEmbedLayout: true,
33+
view: 'preview',
34+
height: '100%',
35+
hideNavigation: true,
36+
hideExplorer: true,
37+
};
38+
39+
const isGithub = projectId.startsWith('KurtGokhan');
40+
const promise = isGithub ? StackBlitzSDK.embedGithubProject(el, projectId, opts) : StackBlitzSDK.embedProjectId(el, projectId, opts);
41+
42+
promise.then(() => {
43+
const iframe = elParent.querySelector('iframe');
44+
// if (!iframe) return;
45+
// iframe.style.width = '100%';
46+
// iframe.style.height = '100%';
47+
});
48+
49+
cache[projectId] = elParent;
50+
return elParent;
51+
}
52+
53+
export function Stackblitz({ embedId }: Props) {
54+
const el = getStackblitzEl(embedId);
55+
56+
const ref = useCallback(
57+
(node) => {
58+
node?.appendChild(el);
59+
},
60+
[el],
61+
);
62+
63+
return <div ref={ref} key={embedId} style={{ height: 600 }} />;
64+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import BrowserWindow from '@site/src/components/BrowserWindow';
1+
import { BrowserWindow } from '@site/src/components/BrowserWindow';
2+
import { Stackblitz } from '@site/src/components/Stackblitz';
23
import MDXComponents from '@theme-original/MDXComponents';
34

45
export default {
56
...MDXComponents,
67
BrowserWindow,
8+
Stackblitz,
79
};

0 commit comments

Comments
 (0)