Code Playground
The Code Playground component provides an interactive development environment for writing and executing code directly in the browser. It supports JavaScript, HTML, and CSS with live preview and console output.
Featuresβ
- Live code execution in the browser
- Split-view editor and output panes
- Console output for JavaScript logging
- HTML/CSS live preview
- Syntax highlighting and code formatting
- Tab indentation support (press Tab key)
- Quick actions: Run, Copy, Reset
- Responsive mobile design
- Keyboard shortcuts (Ctrl+Enter to run)
Usageβ
JavaScript Playgroundβ
<CodePlayground language="javascript" />
HTML Playgroundβ
<CodePlayground language="html" />
CSS Playgroundβ
<CodePlayground language="css" />
With Initial Codeβ
<CodePlayground
language="javascript"
initialCode={`console.log('Hello, World!');\nconst name = 'CodeHarborHub';\nconsole.log(name);`}
/>
JavaScript Playground Exampleβ
JAVASCRIPT Playground
Console Output
Run code to see output here
HTML Playground Exampleβ
HTML Playground
Preview
CSS Playground Exampleβ
CSS Playground
Preview
Keyboard Shortcutsβ
| Shortcut | Action |
|---|---|
Ctrl+Enter or Cmd+Enter | Run code |
Tab | Insert indentation |
Ctrl+A | Select all code |
Supported Languagesβ
JavaScriptβ
- Full ES6+ support with arrow functions, async/await
- Console logging (console.log, console.error, console.warn)
- Error handling and reporting
- Access to standard JavaScript APIs
HTMLβ
- Full HTML5 support
- Inline styles and embedded scripts
- Live rendering preview
- Safe sandboxed execution
CSSβ
- Modern CSS3 features
- Gradients, animations, transforms
- Flexbox and Grid support
- Live style preview with sample HTML
Featuresβ
Live Execution: Code runs immediately when you click Run or press Ctrl+Enter
Console Output: JavaScript console logs appear in real-time with color-coded message types
Error Reporting: Syntax and runtime errors are displayed with helpful messages
Copy to Clipboard: Quickly copy your code with the Copy button
Reset: Restore default example code with the Reset button
Mobile Responsive: Stacks vertically on smaller screens
Sandbox Security: HTML and CSS execute in an isolated iframe
Console Output Typesβ
- log - Standard console.log output
- error - Errors and exceptions in red
- warn - Warning messages in yellow
- info - Informational messages in blue
Accessibilityβ
- Keyboard navigation support
- ARIA labels for all buttons
- Clear error messages
- Screen reader friendly
Tipsβ
- Use
console.log()to debug JavaScript code - Press Tab to indent code and Shift+Tab to outdent
- HTML preview supports onclick handlers and event listeners
- CSS examples include sample HTML for styling demonstration
- Use
console.error()for intentional error logging
Limitationsβ
- JavaScript runs in the browser without Node.js APIs
- No access to external APIs (use mock data instead)
- HTML/CSS execute in a sandboxed iframe for security
- Console only shows text and simple JSON serialized objects