What is Codespark
A lightweight React playground for live code editing and instant preview in your documentation.
Codespark is a React component playground for documentation sites, component libraries, and interactive tutorials.
Why Codespark
Traditional documentation has limitations:
- Static code blocks - Users can only read, not interact
- External sandboxes - Popular playground like CodeSandbox requires context switching
- Manual setup - Each demo needs boilerplate configuration
- Stale examples - Code snippets become outdated without validation
Codespark solves these by providing:
- Live editing - Modify code and see results instantly
- Inline preview - Code and preview side-by-side
- Auto dependency resolution - No manual configuration needed
- Multiple integrations - Works with React, MDX, and markdown
Features
- Modular architecture with highly customizable component styles
- Components using popular TailwindCSS and shadcn/ui as the design system
- Real-time preview with hot reloading
- Automatic dependency resolution via esm.sh
- Monaco Editor with syntax highlighting and IntelliSense
- MDX and remark plugin integration
- Rollup/Vite plugin for build-time processing
How it Works
Codespark transforms your code into a live preview through a streamlined pipeline:
-
User Input - Code changes in the Monaco editor trigger a debounced update
-
Workspace Management - The
Workspaceclass tracks all files and notifies listeners of changes -
Dependency Analysis -
framework.analyze()parses the AST to extract:- Internal imports (other files in the workspace)
- External imports (npm packages resolved via esm.sh)
-
Code Compilation -
framework.compile()transforms the code:- Converts internal imports to blob URLs
- Appends React render boilerplate
- Generates executable ES module code
-
Iframe Communication -
PreviewProxysends compiled code to a sandboxed iframe viapostMessage -
Live Rendering - The iframe executes the code as an ES module and renders the React component
Live Demo
Try editing the code below to see the pipeline in action:
Last updated on