This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a TypeScript-based web component library that provides a <code-playground> custom element for displaying editable and executable code in web pages. The component supports JavaScript, HTML, and CSS editing with optional CodeMirror integration for syntax highlighting.
npm run build- Development build (outputs tobuild/)npm run dist- Production build (outputs todist/)
npm run lint- Format code with Prettier using config/prettierrc.yaml
- Interactive Test Playground:
test-playground.html- Comprehensive testing environment - Manual Testing: Open
test-playground.htmlin browser after runningnpm run build - No formal test suite: No automated testing framework configured
npm run preversion- Runs production build before versioning
- Main file:
src/code-playground.ts- Single TypeScript file containing the entire web component - Custom Element:
CodePlaygroundElementextendsHTMLElementand implements the<code-playground>web component - Shadow DOM: Component uses Shadow DOM for encapsulation with styles and functionality
- Multi-language Support: Supports JavaScript, HTML, and CSS editing in separate slots
- CodeMirror Integration: Optional integration with CodeMirror for enhanced editing
- Live Execution: JavaScript code is executed in a sandboxed environment
- Console Emulation: Intercepts console calls and displays output within the component
- Module Mapping: Supports custom module resolution via
moduleMap - Theming: Built-in tomorrow-night theme with CSS custom properties
- Slots: Uses named slots for different code types (html, javascript, css, style)
- Button Bar: Run/Reset buttons with configurable visibility
- Output Area: Displays HTML output and console messages
- Pseudo Console: Custom console implementation that captures and displays output
- Rollup: Uses Rollup for bundling with TypeScript plugin
- Dual Output: Generates both minified and unminified versions
- Source Maps: Enabled for development builds
- Terser: Used for minification in production builds
tsconfig.json- TypeScript configuration targeting ES2019 with DOM types- Includes CodeMirror types, allows UMD global access
- Source maps enabled, strict null checks disabled
- Uses
@cortex-js/prettier-configpackage configuration - Ignores
/demoand/distdirectories (see.prettierignore)
rollup.config.js- Rollup configuration for bundlingscripts/build.sh- Build script that handles development/production builds- Automatically installs dependencies if
node_modulesmissing
The component processes JavaScript code by:
- Isolated Execution: Each script runs in its own isolated function scope
- DOM Scoping: Intercepting
document.querySelector*calls to scope them to the component's output area - Console Interception: Replacing
console.*calls with custom pseudo-console that displays within the component - Error Handling: Comprehensive error handling including async/uncaught errors
- Module Support: Handling ES module imports with optional module mapping
- Global Function Access: Functions can be made globally accessible via
window.functionNamefor onclick handlers
- Uses CSS custom properties for theming
- Tomorrow-night color scheme as default
- Extensive CodeMirror styling integration
- Responsive design with proper mobile support
layout: "stack" or "tabs" for display modeautorun: "never" or millisecond delay for auto-executionshow-line-numbers: Enable/disable line numbersbutton-bar-visibility: "visible", "hidden", or "auto"- Line marking attributes for highlighting specific lines
src/
code-playground.ts # Main component implementation
scripts/
build.sh # Build script
demo/
index.html # Basic demo page
test-playground.html # Interactive testing environment
build/ # Development build output
dist/ # Production build output (when built)
The test-playground.html file provides a comprehensive testing environment with:
- Console Debug Test: Tests console interception and output formatting
- Basic Console Output: Tests various console methods (log, warn, error, info)
- Interactive Elements: Tests button click handlers and DOM manipulation
- Multi-language Support: Tests HTML + CSS + JavaScript integration with tabs
- Manual Run Mode: Tests
autorun="never"functionality - Performance Testing: Tests timing functions and large operations
- Error Handling: Tests both caught and uncaught error display
# Build the component
npm run build
# Open test-playground.html in browser
open test-playground.htmlEach test section includes:
- Reset/Run buttons: Manual control over script execution
- Debug capabilities: Browser console debugging for component internals
- Visual feedback: Console output displayed within each playground
- Interactive elements: Working buttons and UI components
- Error isolation: Each playground handles its own errors independently
- Console Interception: All console output appears in pseudo-console, not browser console
- Error Isolation: Errors in one playground don't affect others
- Async Error Handling: Even
setTimeouterrors are captured correctly - Global Function Access: Functions can be made globally accessible for onclick handlers
- Monospace Fonts: Consistent typography for code and console output
- Use
console.log(),console.warn(),console.error()etc. - they will appear in the pseudo-console - Objects and arrays are properly formatted and displayed
- Timing functions (
console.time/console.timeEnd) work correctly
- Make functions globally accessible:
window.myFunction = function() { ... } - Use
onclick="myFunction()"in HTML for event handlers - Access DOM elements with
document.getElementById()(automatically scoped)
- Both synchronous and asynchronous errors are captured
- Errors appear in the correct playground's pseudo-console
- No cross-contamination between different playground instances
- No formal automated test suite
- CodeMirror integration is optional and loaded externally
- Browser compatibility depends on ES2019 features
- Some TypeScript strict mode options are disabled for flexibility