Skip to content

Commit ad56c8f

Browse files
committed
Adapt examples environment to allow for React examples
1 parent 08c06e8 commit ad56c8f

11 files changed

Lines changed: 79 additions & 7 deletions

File tree

apps/typegpu-docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@typegpu/color": "workspace:*",
2828
"@typegpu/noise": "workspace:*",
2929
"@typegpu/sdf": "workspace:*",
30+
"@typegpu/react": "workspace:*",
3031
"@types/react": "^19.1.8",
3132
"@types/react-dom": "^19.1.6",
3233
"arktype": "catalog:",

apps/typegpu-docs/src/components/ExampleView.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,21 @@ function useExample(
6969
export function ExampleView({ example }: Props) {
7070
const { tsFiles, tsImport, htmlFile } = example;
7171

72+
const filePaths = tsFiles.map((file) => file.path);
73+
const entryFile = filePaths.find((path) =>
74+
path.startsWith('index.ts')
75+
) as string;
76+
7277
const [snackbarText, setSnackbarText] = useAtom(currentSnackbarAtom);
73-
const [currentFilePath, setCurrentFilePath] = useState<string>('index.ts');
78+
const [currentFilePath, setCurrentFilePath] = useState(entryFile);
7479

7580
const codeEditorShowing = useAtomValue(codeEditorShownAtom);
7681
const codeEditorMobileShowing = useAtomValue(codeEditorShownMobileAtom);
7782
const exampleHtmlRef = useRef<HTMLDivElement>(null);
7883

79-
const filePaths = tsFiles.map((file) => file.path);
8084
const editorTabsList = [
81-
'index.ts',
82-
...filePaths.filter((name) => name !== 'index.ts'),
85+
entryFile,
86+
...filePaths.filter((name) => name !== entryFile),
8387
'index.html',
8488
];
8589

apps/typegpu-docs/src/components/stackblitz/openInStackBlitz.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import typegpuNoisePackageJson from '@typegpu/noise/package.json' with {
1010
import typegpuSdfPackageJson from '@typegpu/sdf/package.json' with {
1111
type: 'json',
1212
};
13-
import typegpuPackageJson from 'typegpu/package.json' with { type: 'json' };
13+
import typegpuReactPackageJson from '@typegpu/react/package.json' with {
14+
type: 'json',
15+
};
16+
import typegpuPackageJson from 'typegpu/package.json' with {
17+
type: 'json',
18+
};
1419
import unpluginPackageJson from 'unplugin-typegpu/package.json' with {
1520
type: 'json',
1621
};
@@ -128,6 +133,7 @@ ${example.htmlFile.content}
128133
"@typegpu/noise": "${typegpuNoisePackageJson.version}",
129134
"@typegpu/color": "${typegpuColorPackageJson.version}",
130135
"@typegpu/sdf": "${typegpuSdfPackageJson.version}"
136+
"@typegpu/react": "${typegpuReactPackageJson.version}"
131137
}
132138
}`,
133139
'vite.config.js': `\
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id='example-app'></div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function App() {
2+
// TODO: Use useRender to draw a full-screen gradient
3+
// TODO: Provide a time variable to the shader with useUniformValue
4+
// TODO: Make the gradient shift colors over time using hsvToRgb from @typegpu/color
5+
return <main>Hello</main>;
6+
}
7+
8+
// #region Example controls and cleanup
9+
10+
import { createRoot } from 'react-dom/client';
11+
const reactRoot = createRoot(
12+
document.getElementById('example-app') as HTMLDivElement,
13+
);
14+
reactRoot.render(<App />);
15+
16+
export function onCleanup() {
17+
reactRoot.unmount();
18+
}
19+
20+
// #endregion
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"title": "React: Spinning Triangle",
3+
"category": "react",
4+
"tags": ["experimental"]
5+
}
181 KB
Loading

apps/typegpu-docs/src/utils/examples/exampleContent.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ const metaFiles = R.pipe(
8282
);
8383

8484
const readonlyTsFiles = R.pipe(
85-
import.meta.glob('../../content/examples/**/*.ts', {
85+
import.meta.glob([
86+
'../../content/examples/**/*.ts',
87+
'../../content/examples/**/*.tsx',
88+
], {
8689
query: 'raw',
8790
eager: true,
8891
import: 'default',
@@ -91,7 +94,10 @@ const readonlyTsFiles = R.pipe(
9194
);
9295

9396
const tsFilesImportFunctions = R.pipe(
94-
import.meta.glob('../../content/examples/**/index.ts') as Record<
97+
import.meta.glob([
98+
'../../content/examples/**/index.ts',
99+
'../../content/examples/**/index.tsx',
100+
]) as Record<
95101
string,
96102
() => Promise<unknown>
97103
>,

apps/typegpu-docs/src/utils/examples/sandboxModules.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ const allPackagesSrcFiles = pipe(
3636
fromEntries(),
3737
);
3838

39+
const reactModules = pipe(
40+
entries(
41+
import.meta.glob(
42+
'../../../node_modules/@types/react/**/*.d.ts',
43+
{
44+
query: 'raw',
45+
eager: true,
46+
import: 'default',
47+
},
48+
) as Record<string, string>,
49+
),
50+
map((dtsFile) => dtsFileToModule(dtsFile, '../../../node_modules/')),
51+
fromEntries(),
52+
);
53+
3954
const mediacaptureModules = pipe(
4055
entries(
4156
import.meta.glob(
@@ -54,7 +69,11 @@ const mediacaptureModules = pipe(
5469
export const SANDBOX_MODULES: Record<string, SandboxModuleDefinition> = {
5570
...allPackagesSrcFiles,
5671
...mediacaptureModules,
72+
...reactModules,
5773

74+
'react': {
75+
typeDef: { reroute: ['@types/react/index.d.ts'] },
76+
},
5877
'@webgpu/types': {
5978
typeDef: { content: dtsWebGPU },
6079
},
@@ -78,4 +97,10 @@ export const SANDBOX_MODULES: Record<string, SandboxModuleDefinition> = {
7897
'@typegpu/color': {
7998
typeDef: { reroute: ['typegpu-color/src/index.ts'] },
8099
},
100+
'@typegpu/sdf': {
101+
typeDef: { reroute: ['typegpu-sdf/src/index.ts'] },
102+
},
103+
'@typegpu/react': {
104+
typeDef: { reroute: ['typegpu-react/src/index.ts'] },
105+
},
81106
};

apps/typegpu-docs/src/utils/liveEditor/embeddedTypeScript.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const tsCompilerOptions: languages.typescript.CompilerOptions = {
1010
skipLibCheck: true,
1111
exactOptionalPropertyTypes: true,
1212
baseUrl: '.',
13+
jsx: languages.typescript.JsxEmit.React,
1314
lib: ['dom', 'es2021'],
1415
};

0 commit comments

Comments
 (0)