Skip to content

Integration Guide

Ved Gupta edited this page Nov 13, 2026 · 9 revisions

Integration Guide

CodeLab works cleanly inside React, Next.js, Vite, Astro, and plain HTML websites. Because the workbench manages its own DOM lifecycle and Web Workers, wrapper components simply mount an instance into a container ref and dispose it on unmount.


React Workbench Component

Wrap the workbench in a client component and destroy the instance during cleanup so workers and event listeners are released cleanly:

import React, { useEffect, useRef } from 'react';
import CodeLab, { CodeLabWorkbench, CodeLabOptions } from '@innovatorved/code-lab';
import { typescriptExtension } from '@innovatorved/code-lab/extensions/typescript';
import { cppExtension } from '@innovatorved/code-lab/extensions/cpp';
import { pythonExtension } from '@innovatorved/code-lab/extensions/python';
import { guidedLabExtension } from '@innovatorved/code-lab/extensions/guided-lab';
import '@innovatorved/code-lab/code-lab.css';

interface CodeLabProps extends CodeLabOptions {
  enableTypeScript?: boolean;
  enableCpp?: boolean;
  enablePython?: boolean;
  className?: string;
  style?: React.CSSProperties;
}

export const CodeLabEditor: React.FC<CodeLabProps> = ({
  enableTypeScript = true,
  enableCpp = false,
  enablePython = false,
  className,
  style,
  ...options
}) => {
  const containerRef = useRef<HTMLDivElement>(null);
  const workbenchRef = useRef<CodeLabWorkbench | null>(null);

  useEffect(() => {
    if (!containerRef.current) return;

    const wb = CodeLab.mount(containerRef.current, {
      ...options,
      persistState: options.persistState ?? true
    });

    if (enableTypeScript) wb.use(typescriptExtension);
    if (enableCpp) wb.use((inst) => cppExtension(inst, { std: 'c++20' }));
    if (enablePython) wb.use((inst) => pythonExtension(inst, { cacheInIndexedDB: true }));
    if (options.steps && options.steps.length > 0) {
      wb.use((inst) => guidedLabExtension(inst, options));
    }

    workbenchRef.current = wb;
    return () => {
      wb.destroy();
      workbenchRef.current = null;
    };
  }, [options.id]);

  return (
    <div
      ref={containerRef}
      className={className}
      style={{ width: '100%', height: '100%', minHeight: '600px', ...style }}
    />
  );
};

React Inline SQL Component

Use the lightweight SQL entrypoint when rendering inline queries inside lesson or documentation components:

import React, { useEffect, useRef } from 'react';
import { createSqlEditor, SqlEditorOptions } from '@innovatorved/code-lab/sql';
import '@innovatorved/code-lab/sql.css';

export const SqlBlock: React.FC<SqlEditorOptions> = (props) => {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (!ref.current) return;
    const editor = createSqlEditor(ref.current, props);
    return () => editor.destroy();
  }, []);

  return <div ref={ref} />;
};

Next.js App Router

Monaco Editor and Web Workers require browser globals, so load the React wrapper dynamically on the client with server-side rendering disabled:

'use client';

import dynamic from 'next/dynamic';

const CodeLabEditor = dynamic(
  () => import('./CodeLabEditor').then((mod) => mod.CodeLabEditor),
  { ssr: false }
);

export default function LabPage({ params }: { params: { slug: string } }) {
  return (
    <main style={{ height: '100vh' }}>
      <CodeLabEditor
        id={`lab-${params.slug}`}
        title={`Lab: ${params.slug}`}
        enableTypeScript
        language="ts"
        files={{
          'index.ts': 'console.log("Running inside Next.js!");\n'
        }}
      />
    </main>
  );
}

Vite & Popped-Out Web Previews

In Vite or static apps, mount CodeLab directly in your client script. If you want users to open running loopback servers in a separate browser tab, serve the included preview Service Worker from your public root directory:

import CodeLab from '@innovatorved/code-lab';
import { typescriptExtension } from '@innovatorved/code-lab/extensions/typescript';
import '@innovatorved/code-lab/code-lab.css';

CodeLab.mount(document.getElementById('app')!, {
  id: 'vite-sandbox',
  title: 'Vite Embedded IDE',
  language: 'ts',
  files: {
    'index.ts': 'console.log("Running inside Vite!");\n'
  }
}).use(typescriptExtension);

Clone this wiki locally