The user wants a web sandbox for product experimentation that isolates CSS, tokens, and runtime dependencies per idea. This is a development tool concept - essentially a sandbox environment where different product ideas/experiments can be tested in isolation.
Let me think about what this should include:
-
Core Concept: A sandbox manager where each "idea" or experiment has its own isolated environment with:
- Isolated CSS (scoped styles)
- Isolated tokens (design tokens like colors, spacing, typography)
- Isolated runtime dependencies (each sandbox can have its own scripts)
-
Key Features:
- Multiple sandbox panels/frames
- Token editor per sandbox
- CSS editor per sandbox
- Dependency manager
- Live preview
- Isolation visualization
-
Visual Design:
- Dark theme (developer tool aesthetic)
- Technical/industrial feel
- Clear visual separation between sandboxes
- Code editor-like interfaces
- Status indicators
-
Interactive Elements:
- Create/delete sandboxes
- Edit tokens with live preview
- Edit CSS with live preview
- Toggle between sandboxes
- Visualize isolation status
- Maybe a "run" button for each sandbox
-
Dynamic Elements (per requirements):
- Particle effects or grid animations in background
- Smooth transitions between sandboxes
- Hover effects on controls
- Real-time token updates with visual feedback
Let me design this as a sophisticated developer tool with:
- A sidebar showing all sandboxes
- Main area with split view: editor (tokens/CSS) and preview iframe
- Visual indicators for isolation status
- Clean, modern dark UI with accent colors
I'll use:
- CSS custom properties for theming
- iframe for true isolation (using srcdoc)
- Shadow DOM concept visualization
- Real-time editing capabilities
Let me create this as a single HTML file with all the features.
Product Experimentation Sandbox
A sophisticated isolated environment for testing product ideas with scoped CSS, design tokens, and runtime dependencies.
Design Approach
- True Isolation - Each sandbox runs in an isolated iframe with its own CSS scope and tokens
- Token System - Editable design tokens (colors, spacing, typography) per sandbox
- Live Preview - Real-time rendering of experiments with isolated dependencies
- Technical Aesthetic - Dark theme with a grid pattern background, glowing accents
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sandbox Lab — Product Experimentation</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0a0a0f;
--bg-elevated: #12121a;
--bg-card: #1a1a24;
--fg: #e8e8ed;
--fg-muted: #6b6b7a;
--accent: #00ff88;
--accent-dim: rgba(0, 255, 136, 0.15);
--warning: #ff6b35;
--info: #3b82f6;
--border: #2a2a36;
--border-active: #3a3a4a;
}
* {
box-sizing: border-box;
}
body {
font-family: 'Space Grotesk', sans-serif;
background: var(--bg);
color: var(--fg);
margin: 0;
min-height: 100vh;
overflow-x: hidden;
}
.font-mono {
font-family: 'JetBrains Mono', monospace;
}
/* Grid Background */
.grid-bg {
position: fixed;
inset: 0;
background-image:
linear-gradient(rgba(0, 255, 136, 0.03) 1px, transparent 1px),
linear-gradient(90deg, rgba(0, 255, 136, 0.03) 1px, transparent 1px);
background-size: 40px 40px;
pointer-events: none;
z-index: 0;
}
.grid-bg::after {
content: '';
position: absolute;
inset: 0;
background: radial-gradient(ellipse at 50% 0%, rgba(0, 255, 136, 0.08) 0%, transparent 60%);
}
/* Scrollbar */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: var(--bg-elevated);
}
::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--border-active);
}
/* Sandbox Item */
.sandbox-item {
transition: all 0.2s ease;
border-left: 3px solid transparent;
}
.sandbox-item:hover {
background: var(--bg-card);
}
.sandbox-item.active {
background: var(--accent-dim);
border-left-color: var(--accent);
}
/* Token Input */
.token-input {
background: var(--bg);
border: 1px solid var(--border);
color: var(--fg);
padding: 0.5rem 0.75rem;
border-radius: 6px;
font-family: 'JetBrains Mono', monospace;
font-size: 13px;
transition: border-color 0.2s, box-shadow 0.2s;
width: 100%;
}
.token-input:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-dim);
}
/* Code Editor */
.code-editor {
background: var(--bg);
border: 1px solid var(--border);
border-radius: 8px;
font-family: 'JetBrains Mono', monospace;
font-size: 13px;
line-height: 1.6;
color: var(--fg);
resize: none;
padding: 1rem;
width: 100%;
min-height: 200px;
}
.code-editor:focus {
outline: none;
border-color: var(--accent);
}
/* Button */
.btn {
padding: 0.5rem 1rem;
border-radius: 6px;
font-weight: 500;
font-size: 14px;
transition: all 0.2s;
cursor: pointer;
border: none;
display: inline-flex;
align-items: center;
gap: 0.5rem;
}
.btn-primary {
background: var(--accent);
color: var(--bg);
}
.btn-primary:hover {
filter: brightness(1.1);
transform: translateY(-1px);
}
.btn-ghost {
background: transparent;
color: var(--fg-muted);
border: 1px solid var(--border);
}
.btn-ghost:hover {
background: var(--bg-card);
color: var(--fg);
border-color: var(--border-active);
}
.btn-danger {
background: rgba(255, 107, 53, 0.15);
color: var(--warning);
border: 1px solid rgba(255, 107, 53, 0.3);
}
.btn-danger:hover {
background: rgba(255, 107, 53, 0.25);
}
/* Status Badge */
.status-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 4px 10px;
border-radius: 20px;
font-size: 12px;
font-weight: 500;
}
.status-isolated {
background: var(--accent-dim);
color: var(--accent);
}
.status-warning {
background: rgba(255, 107, 53, 0.15);
color: var(--warning);
}
/* Preview Frame */
.preview-frame {
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}
/* Tab */
.tab {
padding: 0.75rem 1.25rem;
font-size: 14px;
font-weight: 500;
color: var(--fg-muted);
cursor: pointer;
border-bottom: 2px solid transparent;
transition: all 0.2s;
}
.tab:hover {
color: var(--fg);
}
.tab.active {
color: var(--accent);
border-bottom-color: var(--accent);
}
/* Dependency Tag */
.dep-tag {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 12px;
background: var(--bg);
border: 1px solid var(--border);
border-radius: 6px;
font-size: 12px;
font-family: 'JetBrains Mono', monospace;
}
.dep-tag button {
background: none;
border: none;
color: var(--fg-muted);
cursor: pointer;
padding: 0;
line-height: 1;
}
.dep-tag button:hover {
color: var(--warning);
}
/* Animation */
@keyframes fadeSlideIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate-in {
animation: fadeSlideIn 0.3s ease forwards;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.pulse {
animation: pulse 2s ease-in-out infinite;
}
/* Focus visible */
:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
/* Responsive */
@media (max-width: 1024px) {
.sidebar {
position: fixed;
left: -280px;
z-index: 50;
transition: left 0.3s ease;
}
.sidebar.open {
left: 0;
}
.mobile-toggle {
display: flex !important;
}
}
</style>
</head>
<body>
<div class="grid-bg"></div>
<div class="relative z-10 flex min-h-screen">
<!-- Sidebar -->
<aside class="sidebar w-72 bg-[var(--bg-elevated)] border-r border-[var(--border)] flex flex-col">
<!-- Logo -->
<div class="p-5 border-b border-[var(--border)]">
<div class="flex items-center gap-3">
<div class="w-9 h-9 rounded-lg bg-[var(--accent-dim)] flex items-center justify-center">
<svg class="w-5 h-5 text-[var(--accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19.428 15.428a2 2 0 00-1.022-.547l-2.387-.477a6 6 0 00-3.86.517l-.318.158a6 6 0 01-3.86.517L6.05 15.21a2 2 0 00-1.806.547M8 4h8l-1 1v5.172a2 2 0 00.586 1.414l5 5c1.26 1.26.367 3.414-1.415 3.414H4.828c-1.782 0-2.674-2.154-1.414-3.414l5-5A2 2 0 009 10.172V5L8 4z"/>
</svg>
</div>
<div>
<h1 class="font-semibold text-lg leading-tight">Sandbox Lab</h1>
<p class="text-xs text-[var(--fg-muted)]">Experimentation Engine</p>
</div>
</div>
</div>
<!-- New Sandbox Button -->
<div class="p-4">
<button onclick="createNewSandbox()" class="btn btn-primary w-full justify-center">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
</svg>
New Experiment
</button>
</div>
<!-- Sandbox List -->
<div class="flex-1 overflow-y-auto px-2">
<p class="px-3 py-2 text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider">Active Experiments</p>
<div id="sandbox-list" class="space-y-1">
<!-- Populated by JS -->
</div>
</div>
<!-- Footer Stats -->
<div class="p-4 border-t border-[var(--border)]">
<div class="grid grid-cols-2 gap-3 text-center">
<div class="bg-[var(--bg)] rounded-lg p-3">
<p id="total-sandboxes" class="text-xl font-semibold text-[var(--accent)]">0</p>
<p class="text-xs text-[var(--fg-muted)]">Sandboxes</p>
</div>
<div class="bg-[var(--bg)] rounded-lg p-3">
<p id="isolated-count" class="text-xl font-semibold text-[var(--info)]">0</p>
<p class="text-xs text-[var(--fg-muted)]">Isolated</p>
</div>
</div>
</div>
</aside>
<!-- Main Content -->
<main class="flex-1 flex flex-col min-w-0">
<!-- Mobile Toggle -->
<button
class="mobile-toggle hidden fixed top-4 left-4 z-50 btn btn-ghost"
onclick="toggleSidebar()"
aria-label="Toggle sidebar"
>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"/>
</svg>
</button>
<!-- Header -->
<header class="h-16 border-b border-[var(--border)] bg-[var(--bg-elevated)] flex items-center justify-between px-6">
<div class="flex items-center gap-4">
<h2 id="current-sandbox-name" class="font-semibold text-lg">Select an Experiment</h2>
<span id="isolation-badge" class="status-badge status-isolated hidden">
<span class="w-2 h-2 rounded-full bg-[var(--accent)] pulse"></span>
Isolated
</span>
</div>
<div class="flex items-center gap-3">
<button onclick="duplicateSandbox()" class="btn btn-ghost" id="dup-btn" disabled>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>
Duplicate
</button>
<button onclick="deleteSandbox()" class="btn btn-danger" id="del-btn" disabled>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
</svg>
Delete
</button>
</div>
</header>
<!-- Editor Area -->
<div class="flex-1 flex min-h-0" id="editor-area">
<!-- Empty State -->
<div id="empty-state" class="flex-1 flex items-center justify-center">
<div class="text-center max-w-md">
<div class="w-20 h-20 rounded-2xl bg-[var(--bg-card)] border border-[var(--border)] flex items-center justify-center mx-auto mb-6">
<svg class="w-10 h-10 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M19.428 15.428a2 2 0 00-1.022-.547l-2.387-.477a6 6 0 00-3.86.517l-.318.158a6 6 0 01-3.86.517L6.05 15.21a2 2 0 00-1.806.547M8 4h8l-1 1v5.172a2 2 0 00.586 1.414l5 5c1.26 1.26.367 3.414-1.415 3.414H4.828c-1.782 0-2.674-2.154-1.414-3.414l5-5A2 2 0 009 10.172V5L8 4z"/>
</svg>
</div>
<h3 class="text-xl font-semibold mb-2">No Experiment Selected</h3>
<p class="text-[var(--fg-muted)] mb-6">Create a new sandbox or select an existing one to start experimenting with isolated CSS, tokens, and dependencies.</p>
<button onclick="createNewSandbox()" class="btn btn-primary">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
</svg>
Create First Experiment
</button>
</div>
</div>
<!-- Editor Panels -->
<div id="editor-panels" class="hidden flex-1 flex min-h-0">
<!-- Left: Code Editors -->
<div class="w-1/2 flex flex-col border-r border-[var(--border)]">
<!-- Tabs -->
<div class="flex border-b border-[var(--border)] bg-[var(--bg-elevated)]">
<button class="tab active" data-tab="tokens" onclick="switchTab('tokens')">Design Tokens</button>
<button class="tab" data-tab="css" onclick="switchTab('css')">CSS</button>
<button class="tab" data-tab="html" onclick="switchTab('html')">HTML</button>
<button class="tab" data-tab="deps" onclick="switchTab('deps')">Dependencies</button>
</div>
<!-- Token Editor -->
<div id="tab-tokens" class="flex-1 overflow-y-auto p-5">
<div class="space-y-5">
<div>
<label class="block text-sm font-medium mb-3 text-[var(--fg-muted)]">Colors</label>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--primary</label>
<div class="flex gap-2">
<input type="color" id="token-primary" value="#3b82f6" class="w-10 h-10 rounded cursor-pointer border border-[var(--border)]" onchange="updateToken('primary', this.value)">
<input type="text" class="token-input flex-1" id="token-primary-text" value="#3b82f6" onchange="updateTokenFromText('primary', this.value)">
</div>
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--secondary</label>
<div class="flex gap-2">
<input type="color" id="token-secondary" value="#10b981" class="w-10 h-10 rounded cursor-pointer border border-[var(--border)]" onchange="updateToken('secondary', this.value)">
<input type="text" class="token-input flex-1" id="token-secondary-text" value="#10b981" onchange="updateTokenFromText('secondary', this.value)">
</div>
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--background</label>
<div class="flex gap-2">
<input type="color" id="token-background" value="#ffffff" class="w-10 h-10 rounded cursor-pointer border border-[var(--border)]" onchange="updateToken('background', this.value)">
<input type="text" class="token-input flex-1" id="token-background-text" value="#ffffff" onchange="updateTokenFromText('background', this.value)">
</div>
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--surface</label>
<div class="flex gap-2">
<input type="color" id="token-surface" value="#f3f4f6" class="w-10 h-10 rounded cursor-pointer border border-[var(--border)]" onchange="updateToken('surface', this.value)">
<input type="text" class="token-input flex-1" id="token-surface-text" value="#f3f4f6" onchange="updateTokenFromText('surface', this.value)">
</div>
</div>
</div>
</div>
<div>
<label class="block text-sm font-medium mb-3 text-[var(--fg-muted)]">Typography</label>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--font-size-base</label>
<input type="text" class="token-input" id="token-font-size" value="16px" onchange="updateToken('fontSize', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--font-family</label>
<input type="text" class="token-input" id="token-font-family" value="system-ui, sans-serif" onchange="updateToken('fontFamily', this.value)">
</div>
</div>
</div>
<div>
<label class="block text-sm font-medium mb-3 text-[var(--fg-muted)]">Spacing Scale</label>
<div class="grid grid-cols-4 gap-3">
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--space-xs</label>
<input type="text" class="token-input" id="token-space-xs" value="4px" onchange="updateToken('spaceXs', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--space-sm</label>
<input type="text" class="token-input" id="token-space-sm" value="8px" onchange="updateToken('spaceSm', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--space-md</label>
<input type="text" class="token-input" id="token-space-md" value="16px" onchange="updateToken('spaceMd', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--space-lg</label>
<input type="text" class="token-input" id="token-space-lg" value="24px" onchange="updateToken('spaceLg', this.value)">
</div>
</div>
</div>
<div>
<label class="block text-sm font-medium mb-3 text-[var(--fg-muted)]">Border Radius</label>
<div class="grid grid-cols-3 gap-3">
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--radius-sm</label>
<input type="text" class="token-input" id="token-radius-sm" value="4px" onchange="updateToken('radiusSm', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--radius-md</label>
<input type="text" class="token-input" id="token-radius-md" value="8px" onchange="updateToken('radiusMd', this.value)">
</div>
<div>
<label class="block text-xs mb-1.5 font-mono text-[var(--fg-muted)]">--radius-lg</label>
<input type="text" class="token-input" id="token-radius-lg" value="12px" onchange="updateToken('radiusLg', this.value)">
</div>
</div>
</div>
</div>
</div>
<!-- CSS Editor -->
<div id="tab-css" class="hidden flex-1 p-5">
<textarea id="css-editor" class="code-editor h-full" placeholder="/* Enter custom CSS styles */" oninput="updatePreview()"></textarea>
</div>
<!-- HTML Editor -->
<div id="tab-html" class="hidden flex-1 p-5">
<textarea id="html-editor" class="code-editor h-full" placeholder="<!-- Enter HTML content -->" oninput="updatePreview()"></textarea>
</div>
<!-- Dependencies -->
<div id="tab-deps" class="hidden flex-1 p-5 overflow-y-auto">
<div class="space-y-4">
<div>
<label class="block text-sm font-medium mb-3 text-[var(--fg-muted)]">External Scripts</label>
<div class="flex gap-2 mb-3">
<input type="text" id="new-dep-input" class="token-input flex-1" placeholder="https://cdn.example.com/lib.js">
<button onclick="addDependency()" class="btn btn-ghost">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
</svg>
Add
</button>
</div>
<div id="deps-list" class="flex flex-wrap gap-2">
<!-- Populated by JS -->
</div>
</div>
<div class="bg-[var(--bg)] rounded-lg p-4 border border-[var(--border)]">
<p class="text-sm text-[var(--fg-muted)]">
<strong class="text-[var(--fg)]">Isolation Note:</strong> Each dependency is loaded within the sandbox iframe, preventing global scope pollution and style leakage.
</p>
</div>
</div>
</div>
</div>
<!-- Right: Preview -->
<div class="w-1/2 flex flex-col">
<div class="h-14 border-b border-[var(--border)] bg-[var(--bg-elevated)] flex items-center justify-between px-5">
<span class="font-medium text-sm text-[var(--fg-muted)]">Live Preview</span>
<div class="flex items-center gap-3">
<button onclick="setViewport('mobile')" class="btn btn-ghost py-1.5 px-3 text-xs" id="vp-mobile">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"/>
</svg>
Mobile
</button>
<button onclick="setViewport('tablet')" class="btn btn-ghost py-1.5 px-3 text-xs" id="vp-tablet">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M7 21h10a2 2 0 002-2V5a2 2 0 00-2-2H7a2 2 0 00-2 2v14a2 2 0 002 2z"/>
</svg>
Tablet
</button>
<button onclick="setViewport('desktop')" class="btn btn-ghost py-1.5 px-3 text-xs active" id="vp-desktop">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9.75 17L9 20l-1 1h8l-1-1-.75-3M3 13h18M5 17h14a2 2 0 002-2V5a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/>
</svg>
Desktop
</button>
</div>
</div>
<div class="flex-1 p-5 bg-[var(--bg)] overflow-auto flex justify-center items-start">
<div id="preview-container" class="preview-frame w-full h-full transition-all duration-300">
<iframe id="preview-frame" class="w-full h-full border-0" sandbox="allow-scripts allow-same-origin"></iframe>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
<!-- Overlay for mobile sidebar -->
<div id="sidebar-overlay" class="fixed inset-0 bg-black/50 z-40 hidden lg:hidden" onclick="toggleSidebar()"></div>
<script>
// ============================================
// Data Store
// ============================================
let sandboxes = [];
let activeSandboxId = null;
let sandboxIdCounter = 0;
// Default templates
const defaultTokens = {
primary: '#3b82f6',
secondary: '#10b981',
background: '#ffffff',
surface: '#f3f4f6',
fontSize: '16px',
fontFamily: 'system-ui, sans-serif',
spaceXs: '4px',
spaceSm: '8px',
spaceMd: '16px',
spaceLg: '24px',
radiusSm: '4px',
radiusMd: '8px',
radiusLg: '12px'
};
const defaultCSS = `/* Custom styles for your experiment */
.container {
padding: var(--space-md);
background: var(--background);
}
.card {
background: var(--surface);
border-radius: var(--radius-md);
padding: var(--space-lg);
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.button {
background: var(--primary);
color: white;
padding: var(--space-sm) var(--space-md);
border-radius: var(--radius-sm);
border: none;
cursor: pointer;
font-size: var(--font-size-base);
}
.button:hover {
opacity: 0.9;
}`;
const defaultHTML = `<div class="container">
<h1>Experiment Preview</h1>
<p>Start editing to see your changes live.</p>
<div class="card">
<h2>Card Component</h2>
<p>This card uses your design tokens for styling.</p>
<button class="button">Interactive Button</button>
</div>
</div>`;
// ============================================
// Sandbox Management
// ============================================
function createNewSandbox() {
const id = ++sandboxIdCounter;
const sandbox = {
id,
name: `Experiment ${id}`,
tokens: { ...defaultTokens },
css: defaultCSS,
html: defaultHTML,
dependencies: [],
createdAt: Date.now()
};
sandboxes.push(sandbox);
renderSandboxList();
selectSandbox(id);
updateStats();
}
function selectSandbox(id) {
activeSandboxId = id;
const sandbox = sandboxes.find(s => s.id === id);
if (!sandbox) return;
// Update UI
document.getElementById('current-sandbox-name').textContent = sandbox.name;
document.getElementById('isolation-badge').classList.remove('hidden');
document.getElementById('empty-state').classList.add('hidden');
document.getElementById('editor-panels').classList.remove('hidden');
document.getElementById('dup-btn').disabled = false;
document.getElementById('del-btn').disabled = false;
// Update active state in list
document.querySelectorAll('.sandbox-item').forEach(el => {
el.classList.toggle('active', parseInt(el.dataset.id) === id);
});
// Load sandbox data into editors
loadSandboxToEditors(sandbox);
updatePreview();
// Close sidebar on mobile
if (window.innerWidth < 1024) {
toggleSidebar(false);
}
}
function loadSandboxToEditors(sandbox) {
// Tokens
document.getElementById('token-primary').value = sandbox.tokens.primary;
document.getElementById('token-primary-text').value = sandbox.tokens.primary;
document.getElementById('token-secondary').value = sandbox.tokens.secondary;
document.getElementById('token-secondary-text').value = sandbox.tokens.secondary;
document.getElementById('token-background').value = sandbox.tokens.background;
document.getElementById('token-background-text').value = sandbox.tokens.background;
document.getElementById('token-surface').value = sandbox.tokens.surface;
document.getElementById('token-surface-text').value = sandbox.tokens.surface;
document.getElementById('token-font-size').value = sandbox.tokens.fontSize;
document.getElementById('token-font-family').value = sandbox.tokens.fontFamily;
document.getElementById('token-space-xs').value = sandbox.tokens.spaceXs;
document.getElementById('token-space-sm').value = sandbox.tokens.spaceSm;
document.getElementById('token-space-md').value = sandbox.tokens.spaceMd;
document.getElementById('token-space-lg').value = sandbox.tokens.spaceLg;
document.getElementById('token-radius-sm').value = sandbox.tokens.radiusSm;
document.getElementById('token-radius-md').value = sandbox.tokens.radiusMd;
document.getElementById('token-radius-lg').value = sandbox.tokens.radiusLg;
// CSS & HTML
document.getElementById('css-editor').value = sandbox.css;
document.getElementById('html-editor').value = sandbox.html;
// Dependencies
renderDependencies(sandbox);
}
function duplicateSandbox() {
const original = sandboxes.find(s => s.id === activeSandboxId);
if (!original) return;
const id = ++sandboxIdCounter;
const sandbox = {
...original,
id,
name: `${original.name} (copy)`,
tokens: { ...original.tokens },
dependencies: [...original.dependencies],
createdAt: Date.now()
};
sandboxes.push(sandbox);
renderSandboxList();
selectSandbox(id);
updateStats();
}
function deleteSandbox() {
if (!activeSandboxId) return;
sandboxes = sandboxes.filter(s => s.id !== activeSandboxId);
activeSandboxId = null;
// Reset UI
document.getElementById('current-sandbox-name').textContent = 'Select an Experiment';
document.getElementById('isolation-badge').classList.add('hidden');
document.getElementById('empty-state').classList.remove('hidden');
document.getElementById('editor-panels').classList.add('hidden');
document.getElementById('dup-btn').disabled = true;
document.getElementById('del-btn').disabled = true;
renderSandboxList();
updateStats();
// Select first available
if (sandboxes.length > 0) {
selectSandbox(sandboxes[0].id);
}
}
function renderSandboxList() {
const list = document.getElementById('sandbox-list');
list.innerHTML = sandboxes.map(sandbox => `
<button
class="sandbox-item w-full text-left px-3 py-3 rounded-lg ${sandbox.id === activeSandboxId ? 'active' : ''}"
data-id="${sandbox.id}"
onclick="selectSandbox(${sandbox.id})"
>
<div class="flex items-center gap-3">
<div class="w-8 h-8 rounded-md flex items-center justify-center" style="background: ${sandbox.tokens.primary}20; color: ${sandbox.tokens.primary}">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z"/>
</svg>
</div>
<div class="min-w-0">
<p class="font-medium text-sm truncate">${sandbox.name}</p>
<p class="text-xs text-[var(--fg-muted)]">${sandbox.dependencies.length} deps</p>
</div>
</div>
</button>
`).join('');
}
function updateStats() {
document.getElementById('total-sandboxes').textContent = sandboxes.length;
document.getElementById('isolated-count').textContent = sandboxes.length;
}
// ============================================
// Token Management
// ============================================
function updateToken(key, value) {
const sandbox = sandboxes.find(s => s.id === activeSandboxId);
if (!sandbox) return;
sandbox.tokens[key] = value;
// Sync text input
const textInput = document.getElementById(`token-${key}-text`);
if (textInput) textInput.value = value;
updatePreview();
}
function updateTokenFromText(key, value) {
const sandbox = sandboxes.find(s => s.id === activeSandboxId);
if (!sandbox) return;
sandbox.tokens[key] = value;
// Sync color input if valid color
const colorInput = document.getElementById(`token-${key}`);
if (colorInput && /^#[0-9A-Fa-f]{6}$/.test(value)) {
colorInput.value = value;
}
updatePreview();
}
// ============================================
// Tab Management
// ============================================
function switchTab(tabId) {
document.querySelectorAll('.tab').forEach(tab => {
tab.classList.toggle('active', tab.dataset.tab === tabId);
});
document.querySelectorAll('[id^="tab-"]').forEach(panel => {
panel.classList.toggle('hidden', panel.id !== `tab-${tabId}`);
});
}
// ============================================
// Dependencies Management
// ============================================
function addDependency() {
const input = document.getElementById('new-dep-input');
const url = input.value.trim();
if (!url) return;
const sandbox = sandboxes.find(s => s.id === activeSandboxId);
if (!sandbox) return;
if (!sandbox.dependencies.includes(url)) {
sandbox.dependencies.push(url);
renderDependencies(sandbox);
updatePreview();
}
input.value = '';
}
function removeDependency(url) {
const sandbox = sandboxes.find(s => s.id === activeSandboxId);
if (!sandbox) return;
sandbox.dependencies = sandbox.dependencies.filter(d => d !== url);
renderDependencies(sandbox);
updatePreview();
}
function renderDependencies(sandbox) {
const list = document.getElementById('deps-list');
if (sandbox.dependencies.length === 0) {
list.innerHTML = '<p class="text-sm text-[var(--fg-muted)]">No external dependencies</p>';
return;
}
list.innerHTML = sandbox.dependencies.map(url => `
<span class="dep-tag animate-in">
<svg class="w-3 h-3 text-[var(--accent)]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1"/>
</svg>
<span class="truncate max-w-[150px]">${url.split('/').pop()}</span>
<button onclick="removeDependency('${url}')" aria-label="Remove dependency">
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
</svg>
</button>
</span>
`).join('');
}
// ============================================
// Preview Management
// ============================================
function updatePreview() {
const sandbox = sandboxes.find(s => s.id === activeSandboxId);
if (!sandbox) return;
// Update sandbox data from editors
sandbox.css = document.getElementById('css-editor').value;
sandbox.html = document.getElementById('html-editor').value;
const tokens = sandbox.tokens;
const tokenCSS = `
:root {
--primary: ${tokens.primary};
--secondary: ${tokens.secondary};
--background: ${tokens.background};
--surface: ${tokens.surface};
--font-size-base: ${tokens.fontSize};
--font-family: ${tokens.fontFamily};
--space-xs: ${tokens.spaceXs};
--space-sm: ${tokens.spaceSm};
--space-md: ${tokens.spaceMd};
--space-lg: ${tokens.spaceLg};
--radius-sm: ${tokens.radiusSm};
--radius-md: ${tokens.radiusMd};
--radius-lg: ${tokens.radiusLg};
}
`;
const scripts = sandbox.dependencies
.map(url => `<script src="${url}"><\/script>`)
.join('\n');
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
${tokenCSS}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: var(--font-family); font-size: var(--font-size-base); background: var(--background); }
h1 { font-size: 1.5rem; margin-bottom: var(--space-sm); }
h2 { font-size: 1.25rem; margin-bottom: var(--space-sm); }
p { margin-bottom: var(--space-sm); line-height: 1.6; }
${sandbox.css}
</style>
${scripts}
</head>
<body>
${sandbox.html}
</body>
</html>
`;
const iframe = document.getElementById('preview-frame');
iframe.srcdoc = html;
}
// ============================================
// Viewport Management
// ============================================
function setViewport(size) {
const container = document.getElementById('preview-container');
const buttons = ['mobile', 'tablet', 'desktop'];
buttons.forEach(s => {
const btn = document.getElementById(`vp-${s}`);
btn.classList.toggle('active', s === size);
if (s === size) {
btn.style.background = 'var(--accent-dim)';
btn.style.borderColor = 'var(--accent)';
btn.style.color = 'var(--accent)';
} else {
btn.style.background = '';
btn.style.borderColor = '';
btn.style.color = '';
}
});
switch (size) {
case 'mobile':
container.style.maxWidth = '375px';
container.style.height = '667px';
break;
case 'tablet':
container.style.maxWidth = '768px';
container.style.height = '100%';
break;
case 'desktop':
container.style.maxWidth = '100%';
container.style.height = '100%';
break;
}
}
// ============================================
// Mobile Sidebar
// ============================================
function toggleSidebar(forceState) {
const sidebar = document.querySelector('.sidebar');
const overlay = document.getElementById('sidebar-overlay');
const isOpen = sidebar.classList.contains('open');
const shouldOpen = forceState !== undefined ? forceState : !isOpen;
sidebar.classList.toggle('open', shouldOpen);
overlay.classList.toggle('hidden', !shouldOpen);
}
// ============================================
// Initialize
// ============================================
document.addEventListener('DOMContentLoaded', () => {
// Create initial demo sandbox
createNewSandbox();
// Set initial viewport
setViewport('desktop');
// Enter key for dependency input
document.getElementById('new-dep-input').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addDependency();
}
});
});
</script>
</body>
</html>
Key Features
1. True Isolation Architecture
- Each experiment runs in its own iframe with
srcdoc, ensuring complete CSS and JavaScript isolation
- No global style leakage between experiments
- Sandboxed iframes with controlled permissions
2. Design Token System
- Color tokens: Primary, secondary, background, surface
- Typography tokens: Font size, font family
- Spacing scale: xs, sm, md, lg
- Border radius: sm, md, lg
- Real-time preview updates as you edit
3. Dependency Management
- Add external scripts per experiment
- Dependencies are isolated to each sandbox
- Visual tags for easy management
4. Responsive Preview
- Mobile (375px), Tablet (768px), Desktop (100%) viewports
- Live switching between breakpoints
5. Interactions
- Create/duplicate/delete experiments
- Tab-based editor (Tokens, CSS, HTML, Dependencies)
- Color picker with text input sync
- Mobile-responsive sidebar