<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Widget Composer</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=Space+Grotesk:wght@400;500;600;700&family=DM+Sans:ital,wght@0,400;0,500;0,600;1,400&display=swap" rel="stylesheet">
<script>
tailwind.config = {
theme: {
extend: {
fontFamily: {
display: ['Space Grotesk', 'sans-serif'],
ui: ['DM Sans', 'sans-serif'],
},
}
}
}
</script>
<style>
:root {
--bg-dark: #0f1117;
--bg-surface: #1a1d27;
--bg-elevated: #242833;
--fg: #f4f4f6;
--fg-muted: #8b8fa3;
--accent: #ff6b4a;
--accent-hover: #ff8266;
--success: #34d399;
--warning: #fbbf24;
--error: #f87171;
--border: #2e3341;
--canvas-bg: #f8f9fb;
--brand-primary: #2563eb;
--brand-secondary: #7c3aed;
--brand-accent: #ff6b4a;
}
* {
font-family: 'DM Sans', sans-serif;
}
body {
background: var(--bg-dark);
color: var(--fg);
}
.font-display {
font-family: 'Space Grotesk', sans-serif;
}
/* Custom scrollbar */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: var(--bg-surface);
}
::-webkit-scrollbar-thumb {
background: var(--border);
border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--fg-muted);
}
/* Validation states */
.validation-error {
box-shadow: inset 0 0 0 2px var(--error);
}
.validation-warning {
box-shadow: inset 0 0 0 2px var(--warning);
}
.validation-success {
box-shadow: inset 0 0 0 2px var(--success);
}
/* Animations */
@keyframes slideIn {
from { opacity: 0; transform: translateY(8px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.6; }
}
.animate-slide-in {
animation: slideIn 0.3s ease-out forwards;
}
.animate-fade-in {
animation: fadeIn 0.2s ease-out forwards;
}
.animate-pulse-slow {
animation: pulse 2s ease-in-out infinite;
}
/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}
/* Component hover states */
.component-item {
transition: all 0.15s ease;
}
.component-item:hover {
background: var(--bg-elevated);
transform: translateX(4px);
}
/* Canvas widget styles */
.widget-preview {
transition: all 0.3s ease;
}
/* Brand color swatch */
.color-swatch {
transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.color-swatch:hover {
transform: scale(1.1);
}
.color-swatch.selected {
box-shadow: 0 0 0 3px var(--bg-dark), 0 0 0 5px var(--accent);
}
/* Breakpoint buttons */
.breakpoint-btn {
transition: all 0.15s ease;
}
.breakpoint-btn.active {
background: var(--accent);
color: white;
}
/* Validation badge */
.validation-badge {
transition: all 0.2s ease;
}
/* Export button */
.export-btn {
background: linear-gradient(135deg, var(--accent) 0%, #ff8266 100%);
transition: all 0.2s ease;
}
.export-btn:hover {
transform: translateY(-1px);
box-shadow: 0 8px 24px rgba(255, 107, 74, 0.3);
}
.export-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
/* Toast notification */
.toast {
animation: slideIn 0.3s ease-out;
}
/* Focus states */
input:focus, textarea:focus, button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
</style>
</head>
<body class="min-h-screen overflow-hidden">
<div class="flex h-screen">
<!-- Left Sidebar - Components & Brand -->
<aside class="w-64 bg-[var(--bg-surface)] border-r border-[var(--border)] flex flex-col">
<!-- Logo -->
<div class="p-4 border-b border-[var(--border)]">
<h1 class="font-display font-bold text-lg tracking-tight">Widget Composer</h1>
<p class="text-xs text-[var(--fg-muted)] mt-1">Brand-safe marketing tools</p>
</div>
<!-- Components Section -->
<div class="flex-1 overflow-y-auto">
<div class="p-4">
<h2 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Components</h2>
<div id="componentList" class="space-y-1">
<!-- Components rendered by JS -->
</div>
</div>
<!-- Brand Colors -->
<div class="p-4 border-t border-[var(--border)]">
<h2 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Brand Colors</h2>
<div id="brandColors" class="grid grid-cols-4 gap-2">
<!-- Colors rendered by JS -->
</div>
</div>
<!-- Typography -->
<div class="p-4 border-t border-[var(--border)]">
<h2 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Typography</h2>
<div id="typographyOptions" class="space-y-2">
<!-- Typography rendered by JS -->
</div>
</div>
</div>
</aside>
<!-- Main Canvas Area -->
<main class="flex-1 flex flex-col bg-[var(--bg-dark)]">
<!-- Top Toolbar -->
<header class="h-14 border-b border-[var(--border)] flex items-center justify-between px-4 bg-[var(--bg-surface)]">
<div class="flex items-center gap-4">
<!-- Widget Name -->
<input
type="text"
id="widgetName"
value="Summer Campaign Banner"
class="bg-transparent font-display font-semibold text-lg border-none focus:outline-none focus:ring-2 focus:ring-[var(--accent)] rounded px-2 py-1 -ml-2"
>
<!-- Validation Status -->
<div id="validationStatus" class="flex items-center gap-2 px-3 py-1 rounded-full bg-[var(--bg-elevated)]">
<span class="w-2 h-2 rounded-full bg-[var(--success)]"></span>
<span class="text-xs text-[var(--fg-muted)]">All validations passed</span>
</div>
</div>
<div class="flex items-center gap-3">
<!-- Breakpoint Toggle -->
<div class="flex items-center gap-1 bg-[var(--bg-elevated)] rounded-lg p-1">
<button class="breakpoint-btn px-3 py-1.5 rounded text-xs font-medium" data-breakpoint="mobile" aria-label="Mobile view">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<rect x="5" y="2" width="14" height="20" rx="2" stroke-width="2"/>
</svg>
</button>
<button class="breakpoint-btn px-3 py-1.5 rounded text-xs font-medium active" data-breakpoint="tablet" aria-label="Tablet view">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<rect x="4" y="2" width="16" height="20" rx="2" stroke-width="2"/>
</svg>
</button>
<button class="breakpoint-btn px-3 py-1.5 rounded text-xs font-medium" data-breakpoint="desktop" aria-label="Desktop view">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<rect x="2" y="3" width="20" height="14" rx="2" stroke-width="2"/>
<path d="M8 21h8M12 17v4" stroke-width="2" stroke-linecap="round"/>
</svg>
</button>
</div>
<!-- Export Button -->
<button id="exportBtn" class="export-btn px-4 py-2 rounded-lg font-semibold text-sm text-white">
Export Widget
</button>
</div>
</header>
<!-- Canvas -->
<div class="flex-1 overflow-auto p-8 bg-[var(--canvas-bg)]">
<div class="flex justify-center">
<div id="canvasContainer" class="bg-white rounded-2xl shadow-2xl overflow-hidden transition-all duration-300" style="width: 768px;">
<!-- Widget Preview -->
<div id="widgetPreview" class="widget-preview">
<!-- Rendered by JS -->
</div>
</div>
</div>
</div>
</main>
<!-- Right Panel - Properties & Validation -->
<aside class="w-80 bg-[var(--bg-surface)] border-l border-[var(--border)] flex flex-col">
<!-- Panel Tabs -->
<div class="flex border-b border-[var(--border)]">
<button class="panel-tab flex-1 py-3 text-sm font-medium text-center border-b-2 border-[var(--accent)] text-[var(--fg)]" data-panel="properties">
Properties
</button>
<button class="panel-tab flex-1 py-3 text-sm font-medium text-center border-b-2 border-transparent text-[var(--fg-muted)] hover:text-[var(--fg)]" data-panel="validation">
Validation
</button>
</div>
<!-- Properties Panel -->
<div id="propertiesPanel" class="flex-1 overflow-y-auto p-4 space-y-4">
<div class="animate-slide-in">
<!-- Properties content rendered by JS -->
</div>
</div>
<!-- Validation Panel -->
<div id="validationPanel" class="flex-1 overflow-y-auto p-4 hidden">
<div class="space-y-4">
<!-- Validation content rendered by JS -->
</div>
</div>
</aside>
</div>
<!-- Toast Container -->
<div id="toastContainer" class="fixed bottom-4 right-4 z-50 space-y-2"></div>
<script>
// ============================================
// DATA & STATE
// ============================================
// Brand Rules Configuration
const brandRules = {
colors: [
{ name: 'Primary', value: '#2563eb', allowed: true },
{ name: 'Secondary', value: '#7c3aed', allowed: true },
{ name: 'Accent', value: '#ff6b4a', allowed: true },
{ name: 'Dark', value: '#1e293b', allowed: true },
{ name: 'Light', value: '#f8fafc', allowed: true },
{ name: 'Muted', value: '#64748b', allowed: true },
{ name: 'Warning', value: '#f59e0b', allowed: false, reason: 'Use only for alerts' },
{ name: 'Error', value: '#ef4444', allowed: false, reason: 'Use only for errors' },
],
typography: {
headings: { font: 'Space Grotesk', minSize: 18, maxSize: 48 },
body: { font: 'DM Sans', minSize: 14, maxSize: 18 },
},
spacing: { min: 8, max: 64, increment: 4 },
};
// Copy Rules
const copyRules = {
headline: { maxLength: 60, required: true },
subheadline: { maxLength: 120, required: false },
cta: { maxLength: 20, required: true },
prohibitedWords: ['free', 'guarantee', 'best', 'cheapest', 'amazing'],
requiredDisclaimer: 'Terms apply',
};
// Available Components
const componentTypes = [
{ id: 'hero', name: 'Hero Banner', icon: 'hero' },
{ id: 'headline', name: 'Headline', icon: 'text' },
{ id: 'body', name: 'Body Text', icon: 'paragraph' },
{ id: 'cta', name: 'CTA Button', icon: 'button' },
{ id: 'image', name: 'Image Block', icon: 'image' },
{ id: 'divider', name: 'Divider', icon: 'divider' },
];
// Current Widget State
let widgetState = {
name: 'Summer Campaign Banner',
breakpoint: 'tablet',
selectedComponent: null,
components: [
{ id: 1, type: 'hero', props: { headline: 'Summer Sale Event', subheadline: 'Save up to 40% on selected items', ctaText: 'Shop Now', bgColor: '#2563eb', textColor: '#ffffff' } },
{ id: 2, type: 'body', props: { text: 'Discover our curated collection of premium products. Limited time offer valid through August 31st.', fontSize: 16 } },
{ id: 3, type: 'cta', props: { text: 'Get Started', bgColor: '#ff6b4a', textColor: '#ffffff', size: 'large' } },
],
validationIssues: [],
};
// Breakpoint widths
const breakpointWidths = {
mobile: 375,
tablet: 768,
desktop: 1200,
};
// ============================================
// INITIALIZATION
// ============================================
document.addEventListener('DOMContentLoaded', () => {
renderComponentList();
renderBrandColors();
renderTypographyOptions();
renderWidgetPreview();
renderPropertiesPanel();
renderValidationPanel();
updateValidationStatus();
setupEventListeners();
});
// ============================================
// RENDER FUNCTIONS
// ============================================
function renderComponentList() {
const container = document.getElementById('componentList');
container.innerHTML = componentTypes.map(comp => `
<button
class="component-item w-full flex items-center gap-3 px-3 py-2 rounded-lg text-left"
data-component-type="${comp.id}"
aria-label="Add ${comp.name} component"
>
${getComponentIcon(comp.icon)}
<span class="text-sm">${comp.name}</span>
</button>
`).join('');
// Add click listeners
container.querySelectorAll('button').forEach(btn => {
btn.addEventListener('click', () => addComponent(btn.dataset.componentType));
});
}
function renderBrandColors() {
const container = document.getElementById('brandColors');
container.innerHTML = brandRules.colors.map(color => `
<button
class="color-swatch w-10 h-10 rounded-lg ${!color.allowed ? 'opacity-50' : ''}"
style="background-color: ${color.value};"
data-color="${color.value}"
data-allowed="${color.allowed}"
title="${color.name}${!color.allowed ? ' (Restricted)' : ''}"
aria-label="Select ${color.name} color"
></button>
`).join('');
container.querySelectorAll('button').forEach(btn => {
btn.addEventListener('click', () => {
if (btn.dataset.allowed === 'true') {
applyColorToSelected(btn.dataset.color);
} else {
showToast(`${brandRules.colors.find(c => c.value === btn.dataset.color)?.reason || 'Color restricted'}`, 'warning');
}
});
});
}
function renderTypographyOptions() {
const container = document.getElementById('typographyOptions');
container.innerHTML = `
<div class="space-y-2">
<div class="flex items-center justify-between">
<span class="text-xs text-[var(--fg-muted)]">Heading Font</span>
<span class="text-xs font-medium">${brandRules.typography.headings.font}</span>
</div>
<div class="flex items-center justify-between">
<span class="text-xs text-[var(--fg-muted)]">Body Font</span>
<span class="text-xs font-medium">${brandRules.typography.body.font}</span>
</div>
<div class="flex items-center justify-between">
<span class="text-xs text-[var(--fg-muted)]">Size Range</span>
<span class="text-xs font-medium">${brandRules.typography.body.minSize}-${brandRules.typography.headings.maxSize}px</span>
</div>
</div>
`;
}
function renderWidgetPreview() {
const container = document.getElementById('widgetPreview');
const canvasContainer = document.getElementById('canvasContainer');
// Update canvas width
canvasContainer.style.width = `${breakpointWidths[widgetState.breakpoint]}px`;
canvasContainer.style.maxWidth = '100%';
container.innerHTML = widgetState.components.map((comp, index) => {
const isSelected = widgetState.selectedComponent === comp.id;
return `
<div
class="relative cursor-pointer transition-all ${isSelected ? 'ring-2 ring-[var(--accent)] ring-offset-2' : ''}"
data-component-id="${comp.id}"
style="animation-delay: ${index * 50}ms"
>
${renderComponent(comp)}
${isSelected ? `
<div class="absolute -top-3 -right-3 flex gap-1">
<button
class="w-6 h-6 rounded-full bg-[var(--error)] text-white flex items-center justify-center hover:bg-red-600 transition-colors"
onclick="event.stopPropagation(); removeComponent(${comp.id})"
aria-label="Remove component"
>
<svg class="w-3 h-3" 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>
</div>
` : ''}
</div>
`;
}).join('');
// Add click listeners for selection
container.querySelectorAll('[data-component-id]').forEach(el => {
el.addEventListener('click', (e) => {
e.stopPropagation();
selectComponent(parseInt(el.dataset.componentId));
});
});
}
function renderComponent(comp) {
switch (comp.type) {
case 'hero':
return `
<div class="p-8 text-center" style="background-color: ${comp.props.bgColor}; color: ${comp.props.textColor};">
<h2 class="font-display font-bold text-3xl mb-3" style="color: ${comp.props.textColor};">${comp.props.headline}</h2>
<p class="text-lg opacity-90 mb-6" style="color: ${comp.props.textColor};">${comp.props.subheadline}</p>
<button class="px-6 py-3 rounded-lg font-semibold text-sm transition-transform hover:scale-105" style="background-color: ${comp.props.textColor}; color: ${comp.props.bgColor};">
${comp.props.ctaText}
</button>
</div>
`;
case 'headline':
return `
<div class="p-6">
<h2 class="font-display font-bold text-2xl" style="color: ${comp.props.color || '#1e293b'};">${comp.props.text || 'Enter headline'}</h2>
</div>
`;
case 'body':
return `
<div class="p-6">
<p class="text-gray-600 leading-relaxed" style="font-size: ${comp.props.fontSize}px;">${comp.props.text}</p>
</div>
`;
case 'cta':
return `
<div class="p-6 flex justify-center">
<button class="px-8 py-4 rounded-xl font-semibold transition-all hover:shadow-lg ${comp.props.size === 'large' ? 'text-lg' : 'text-base'}"
style="background-color: ${comp.props.bgColor}; color: ${comp.props.textColor};">
${comp.props.text}
</button>
</div>
`;
case 'image':
return `
<div class="aspect-video bg-gradient-to-br from-gray-100 to-gray-200 flex items-center justify-center">
<svg class="w-12 h-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<rect x="3" y="3" width="18" height="18" rx="2" stroke-width="2"/>
<circle cx="8.5" cy="8.5" r="1.5" fill="currentColor"/>
<path d="M21 15l-5-5L5 21" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
</div>
`;
case 'divider':
return `
<div class="px-6 py-4">
<hr class="border-gray-200">
</div>
`;
default:
return '<div class="p-4 text-gray-400">Unknown component</div>';
}
}
function renderPropertiesPanel() {
const container = document.getElementById('propertiesPanel');
const selectedComp = widgetState.components.find(c => c.id === widgetState.selectedComponent);
if (!selectedComp) {
container.innerHTML = `
<div class="text-center py-12 text-[var(--fg-muted)]">
<svg class="w-12 h-12 mx-auto mb-3 opacity-50" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M15 15l-2 5L9 9l11 4-5 2zm0 0l5 5M7.188 2.239l.777 2.897M5.136 7.965l-2.898-.777M13.95 4.05l-2.122 2.122m-5.657 5.656l-2.12 2.122"/>
</svg>
<p class="text-sm">Select a component to edit its properties</p>
</div>
`;
return;
}
let propsHTML = '';
switch (selectedComp.type) {
case 'hero':
propsHTML = `
<div class="space-y-4">
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Headline</label>
<input
type="text"
class="w-full px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm"
value="${selectedComp.props.headline}"
data-prop="headline"
maxlength="${copyRules.headline.maxLength}"
>
<div class="flex justify-between mt-1">
<span class="text-xs text-[var(--fg-muted)] character-count">${selectedComp.props.headline.length}/${copyRules.headline.maxLength}</span>
${getCopyValidationBadge('headline', selectedComp.props.headline)}
</div>
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Subheadline</label>
<input
type="text"
class="w-full px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm"
value="${selectedComp.props.subheadline}"
data-prop="subheadline"
maxlength="${copyRules.subheadline.maxLength}"
>
<div class="flex justify-between mt-1">
<span class="text-xs text-[var(--fg-muted)]">${selectedComp.props.subheadline.length}/${copyRules.subheadline.maxLength}</span>
</div>
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">CTA Text</label>
<input
type="text"
class="w-full px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm"
value="${selectedComp.props.ctaText}"
data-prop="ctaText"
maxlength="${copyRules.cta.maxLength}"
>
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Background Color</label>
<div class="flex items-center gap-2">
<input
type="color"
class="w-10 h-10 rounded cursor-pointer"
value="${selectedComp.props.bgColor}"
data-prop="bgColor"
>
<input
type="text"
class="flex-1 px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm font-mono"
value="${selectedComp.props.bgColor}"
data-prop="bgColor"
>
</div>
${getColorValidationBadge(selectedComp.props.bgColor)}
</div>
</div>
`;
break;
case 'body':
propsHTML = `
<div class="space-y-4">
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Text Content</label>
<textarea
class="w-full px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm resize-none"
rows="4"
data-prop="text"
>${selectedComp.props.text}</textarea>
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">
Font Size: ${selectedComp.props.fontSize}px
</label>
<input
type="range"
class="w-full"
min="${brandRules.typography.body.minSize}"
max="${brandRules.typography.body.maxSize}"
value="${selectedComp.props.fontSize}"
data-prop="fontSize"
>
<div class="flex justify-between text-xs text-[var(--fg-muted)] mt-1">
<span>${brandRules.typography.body.minSize}px</span>
<span>${brandRules.typography.body.maxSize}px</span>
</div>
</div>
</div>
`;
break;
case 'cta':
propsHTML = `
<div class="space-y-4">
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Button Text</label>
<input
type="text"
class="w-full px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm"
value="${selectedComp.props.text}"
data-prop="text"
maxlength="${copyRules.cta.maxLength}"
>
${getCopyValidationBadge('cta', selectedComp.props.text)}
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Background Color</label>
<div class="flex items-center gap-2">
<input
type="color"
class="w-10 h-10 rounded cursor-pointer"
value="${selectedComp.props.bgColor}"
data-prop="bgColor"
>
<input
type="text"
class="flex-1 px-3 py-2 bg-[var(--bg-elevated)] border border-[var(--border)] rounded-lg text-sm font-mono"
value="${selectedComp.props.bgColor}"
data-prop="bgColor"
>
</div>
${getColorValidationBadge(selectedComp.props.bgColor)}
</div>
<div>
<label class="block text-xs font-medium text-[var(--fg-muted)] mb-2">Size</label>
<div class="flex gap-2">
<button class="flex-1 px-3 py-2 rounded-lg text-sm ${selectedComp.props.size === 'medium' ? 'bg-[var(--accent)] text-white' : 'bg-[var(--bg-elevated)]'}" data-prop="size" data-value="medium">Medium</button>
<button class="flex-1 px-3 py-2 rounded-lg text-sm ${selectedComp.props.size === 'large' ? 'bg-[var(--accent)] text-white' : 'bg-[var(--bg-elevated)]'}" data-prop="size" data-value="large">Large</button>
</div>
</div>
</div>
`;
break;
default:
propsHTML = '<p class="text-sm text-[var(--fg-muted)]">No editable properties</p>';
}
container.innerHTML = `
<div class="animate-slide-in">
<div class="flex items-center justify-between mb-4">
<h3 class="font-display font-semibold">${componentTypes.find(c => c.id === selectedComp.type)?.name || 'Component'}</h3>
<span class="text-xs text-[var(--fg-muted)] bg-[var(--bg-elevated)] px-2 py-1 rounded">ID: ${selectedComp.id}</span>
</div>
${propsHTML}
</div>
`;
// Add input listeners
container.querySelectorAll('input, textarea').forEach(input => {
input.addEventListener('input', (e) => {
updateComponentProperty(selectedComp.id, e.target.dataset.prop, e.target.value);
});
input.addEventListener('change', (e) => {
updateComponentProperty(selectedComp.id, e.target.dataset.prop, e.target.value);
});
});
// Add size button listeners for CTA
container.querySelectorAll('button[data-prop="size"]').forEach(btn => {
btn.addEventListener('click', () => {
updateComponentProperty(selectedComp.id, 'size', btn.dataset.value);
});
});
}
function renderValidationPanel() {
const container = document.getElementById('validationPanel');
const issues = validateAll();
container.innerHTML = `
<div class="space-y-4">
<!-- Summary -->
<div class="p-4 rounded-xl ${issues.length === 0 ? 'bg-green-500/10' : issues.some(i => i.severity === 'error') ? 'bg-red-500/10' : 'bg-yellow-500/10'}">
<div class="flex items-center gap-3">
<div class="w-10 h-10 rounded-full ${issues.length === 0 ? 'bg-green-500/20' : issues.some(i => i.severity === 'error') ? 'bg-red-500/20' : 'bg-yellow-500/20'} flex items-center justify-center">
${issues.length === 0 ? `
<svg class="w-5 h-5 text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
</svg>
` : `
<svg class="w-5 h-5 ${issues.some(i => i.severity === 'error') ? 'text-red-400' : 'text-yellow-400'}" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
</svg>
`}
</div>
<div>
<p class="font-medium ${issues.length === 0 ? 'text-green-400' : issues.some(i => i.severity === 'error') ? 'text-red-400' : 'text-yellow-400'}">
${issues.length === 0 ? 'All validations passed' : `${issues.length} issue${issues.length > 1 ? 's' : ''} found`}
</p>
<p class="text-xs text-[var(--fg-muted)]">
${issues.filter(i => i.severity === 'error').length} errors, ${issues.filter(i => i.severity === 'warning').length} warnings
</p>
</div>
</div>
</div>
<!-- Brand Rules -->
<div>
<h4 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Brand Compliance</h4>
<div class="space-y-2">
${renderValidationCategory(issues.filter(i => i.category === 'brand'))}
</div>
</div>
<!-- Copy Rules -->
<div>
<h4 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Copy Validation</h4>
<div class="space-y-2">
${renderValidationCategory(issues.filter(i => i.category === 'copy'))}
</div>
</div>
<!-- Responsive -->
<div>
<h4 class="text-xs font-semibold text-[var(--fg-muted)] uppercase tracking-wider mb-3">Responsive Integrity</h4>
<div class="space-y-2">
${renderValidationCategory(issues.filter(i => i.category === 'responsive'))}
</div>
</div>
</div>
`;
}
function renderValidationCategory(issues) {
if (issues.length === 0) {
return `
<div class="flex items-center gap-2 p-3 rounded-lg bg-[var(--bg-elevated)]">
<svg class="w-4 h-4 text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
</svg>
<span class="text-sm text-[var(--fg-muted)]">No issues</span>
</div>
`;
}
return issues.map(issue => `
<div class="flex items-start gap-2 p-3 rounded-lg bg-[var(--bg-elevated)]">
<svg class="w-4 h-4 mt-0.5 ${issue.severity === 'error' ? 'text-red-400' : 'text-yellow-400'}" fill="none" stroke="currentColor" viewBox="0 0 24 24">
${issue.severity === 'error' ? `
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
` : `
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
`}
</svg>
<div>
<p class="text-sm">${issue.message}</p>
<p class="text-xs text-[var(--fg-muted)] mt-1">${issue.suggestion}</p>
</div>
</div>
`).join('');
}
// ============================================
// VALIDATION LOGIC
// ============================================
function validateAll() {
const issues = [];
widgetState.components.forEach(comp => {
// Copy validation
if (comp.type === 'hero') {
// Check headline length
if (comp.props.headline.length > copyRules.headline.maxLength) {
issues.push({
category: 'copy',
severity: 'error',
message: `Headline exceeds ${copyRules.headline.maxLength} characters`,
suggestion: 'Shorten the headline for better impact',
componentId: comp.id,
});
}
// Check prohibited words
const prohibitedFound = copyRules.prohibitedWords.filter(word =>
comp.props.headline.toLowerCase().includes(word) ||
comp.props.subheadline.toLowerCase().includes(word)
);
if (prohibitedFound.length > 0) {
issues.push({
category: 'copy',
severity: 'warning',
message: `Prohibited words found: ${prohibitedFound.join(', ')}`,
suggestion: 'Remove superlatives for brand compliance',
componentId: comp.id,
});
}
}
if (comp.type === 'cta') {
if (comp.props.text.length > copyRules.cta.maxLength) {
issues.push({
category: 'copy',
severity: 'error',
message: `CTA text exceeds ${copyRules.cta.maxLength} characters`,
suggestion: 'Keep CTAs short and actionable',
componentId: comp.id,
});
}
}
// Brand color validation
const colorProps = ['bgColor', 'color'];
colorProps.forEach(prop => {
if (comp.props[prop]) {
const colorRule = brandRules.colors.find(c => c.value.toLowerCase() === comp.props[prop].toLowerCase());
if (colorRule && !colorRule.allowed) {
issues.push({
category: 'brand',
severity: 'warning',
message: `"${colorRule.name}" color is restricted`,
suggestion: colorRule.reason,
componentId: comp.id,
});
}
// Check if color is in brand palette at all
if (!colorRule && !isValidHexColor(comp.props[prop])) {
issues.push({
category: 'brand',
severity: 'warning',
message: 'Color not in brand palette',
suggestion: 'Use approved brand colors for consistency',
componentId: comp.id,
});
}
}
});
// Typography validation
if (comp.type === 'body' && comp.props.fontSize) {
if (comp.props.fontSize < brandRules.typography.body.minSize ||
comp.props.fontSize > brandRules.typography.body.maxSize) {
issues.push({
category: 'brand',
severity: 'error',
message: `Font size ${comp.props.fontSize}px outside allowed range`,
suggestion: `Use ${brandRules.typography.body.minSize}-${brandRules.typography.body.maxSize}px`,
componentId: comp.id,
});
}
}
});
// Responsive validation
if (widgetState.breakpoint === 'mobile') {
const hasLongText = widgetState.components.some(c =>
(c.props.headline && c.props.headline.length > 40) ||
(c.props.text && c.props.text.length > 200)
);
if (hasLongText) {
issues.push({
category: 'responsive',
severity: 'warning',
message: 'Long text may not display well on mobile',
suggestion: 'Consider shorter copy for mobile breakpoint',
});
}
}
widgetState.validationIssues = issues;
return issues;
}
function isValidHexColor(color) {
return /^#[0-9A-Fa-f]{6}$/.test(color);
}
function getCopyValidationBadge(field, value) {
const rule = copyRules[field];
if (!rule) return '';
const issues = [];
if (rule.maxLength && value.length > rule.maxLength) {
issues.push('Too long');
}
if (rule === copyRules.cta || field === 'headline') {
const prohibitedFound = copyRules.prohibitedWords.filter(word =>
value.toLowerCase().includes(word)
);
if (prohibitedFound.length > 0) {
issues.push('Prohibited words');
}
}
if (issues.length === 0) {
return `<span class="text-xs text-green-400">Valid</span>`;
}
return `<span class="text-xs text-yellow-400">${issues.join(', ')}</span>`;
}
function getColorValidationBadge(color) {
const colorRule = brandRules.colors.find(c => c.value.toLowerCase() === color.toLowerCase());
if (colorRule) {
if (colorRule.allowed) {
return `<span class="text-xs text-green-400 mt-1 block">Brand color: ${colorRule.name}</span>`;
} else {
return `<span class="text-xs text-yellow-400 mt-1 block">Restricted: ${colorRule.reason}</span>`;
}
}
return `<span class="text-xs text-yellow-400 mt-1 block">Not in brand palette</span>`;
}
function updateValidationStatus() {
const container = document.getElementById('validationStatus');
const issues = validateAll();
const hasErrors = issues.some(i => i.severity === 'error');
const hasWarnings = issues.some(i => i.severity === 'warning');
let statusColor = 'bg-[var(--success)]';
let statusText = 'All validations passed';
if (hasErrors) {
statusColor = 'bg-[var(--error)]';
statusText = `${issues.filter(i => i.severity === 'error').length} error(s)`;
} else if (hasWarnings) {
statusColor = 'bg-[var(--warning)]';
statusText = `${issues.filter(i => i.severity === 'warning').length} warning(s)`;
}
container.innerHTML = `
<span class="w-2 h-2 rounded-full ${statusColor}"></span>
<span class="text-xs text-[var(--fg-muted)]">${statusText}</span>
`;
// Update export button state
document.getElementById('exportBtn').disabled = hasErrors;
}
// ============================================
// ACTIONS
// ============================================
function addComponent(type) {
const newId = Math.max(...widgetState.components.map(c => c.id), 0) + 1;
const defaultProps = {
hero: { headline: 'New Headline', subheadline: 'Add your subheadline here', ctaText: 'Click Here', bgColor: '#2563eb', textColor: '#ffffff' },
headline: { text: 'New Headline', color: '#1e293b' },
body: { text: 'Add your body text here.', fontSize: 16 },
cta: { text: 'Button', bgColor: '#ff6b4a', textColor: '#ffffff', size: 'medium' },
image: {},
divider: {},
};
widgetState.components.push({
id: newId,
type: type,
props: defaultProps[type] || {},
});
renderWidgetPreview();
selectComponent(newId);
showToast(`${componentTypes.find(c => c.id === type)?.name} added`, 'success');
}
function removeComponent(id) {
widgetState.components = widgetState.components.filter(c => c.id !== id);
widgetState.selectedComponent = null;
renderWidgetPreview();
renderPropertiesPanel();
updateValidationStatus();
renderValidationPanel();
showToast('Component removed', 'info');
}
function selectComponent(id) {
widgetState.selectedComponent = id;
renderWidgetPreview();
renderPropertiesPanel();
}
function updateComponentProperty(id, prop, value) {
const comp = widgetState.components.find(c => c.id === id);
if (comp) {
// Handle font size as number
if (prop === 'fontSize') {
value = parseInt(value);
}
comp.props[prop] = value;
renderWidgetPreview();
updateValidationStatus();
renderValidationPanel();
// Re-render properties panel for range inputs
if (prop === 'fontSize') {
renderPropertiesPanel();
}
}
}
function applyColorToSelected(color) {
if (!widgetState.selectedComponent) {
showToast('Select a component first', 'warning');
return;
}
const comp = widgetState.components.find(c => c.id === widgetState.selectedComponent);
if (comp) {
if (comp.props.bgColor !== undefined) {
comp.props.bgColor = color;
} else if (comp.props.color !== undefined) {
comp.props.color = color;
}
renderWidgetPreview();
renderPropertiesPanel();
updateValidationStatus();
renderValidationPanel();
showToast('Color applied', 'success');
}
}
function setBreakpoint(breakpoint) {
widgetState.breakpoint = breakpoint;
document.querySelectorAll('.breakpoint-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.breakpoint === breakpoint);
});
renderWidgetPreview();
updateValidationStatus();
renderValidationPanel();
}
function exportWidget() {
const issues = validateAll();
const hasErrors = issues.some(i => i.severity === 'error');
if (hasErrors) {
showToast('Fix validation errors before exporting', 'error');
return;
}
// Generate export code
const exportCode = generateExportCode();
// Copy to clipboard
navigator.clipboard.writeText(exportCode).then(() => {
showToast('Widget code copied to clipboard!', 'success');
}).catch(() => {
showToast('Export ready! Check console.', 'info');
console.log('Widget Export:\n', exportCode);
});
}
function generateExportCode() {
return `<!-- Widget: ${widgetState.name} -->
<!-- Generated by Widget Composer -->
<div class="widget-container" style="max-width: ${breakpointWidths[widgetState.breakpoint]}px;">
${widgetState.components.map(comp => {
if (comp.type === 'hero') {
return ` <div style="background: ${comp.props.bgColor}; color: ${comp.props.textColor}; padding: 2rem; text-align: center;">
<h2 style="margin: 0 0 0.75rem; font-size: 1.875rem; font-weight: 700;">${comp.props.headline}</h2>
<p style="margin: 0 0 1.5rem; opacity: 0.9;">${comp.props.subheadline}</p>
<button style="background: ${comp.props.textColor}; color: ${comp.props.bgColor}; padding: 0.75rem 1.5rem; border: none; border-radius: 0.5rem; font-weight: 600;">${comp.props.ctaText}</button>
</div>`;
}
if (comp.type === 'body') {
return ` <div style="padding: 1.5rem;">
<p style="margin: 0; color: #4b5563; line-height: 1.6; font-size: ${comp.props.fontSize}px;">${comp.props.text}</p>
</div>`;
}
if (comp.type === 'cta') {
return ` <div style="padding: 1.5rem; text-align: center;">
<button style="background: ${comp.props.bgColor}; color: ${comp.props.textColor}; padding: 1rem 2rem; border: none; border-radius: 0.75rem; font-weight: 600; font-size: ${comp.props.size === 'large' ? '1.125rem' : '1rem'};">${comp.props.text}</button>
</div>`;
}
return '';
}).filter(Boolean).join('\n')}
</div>`;
}
function showToast(message, type = 'info') {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
const colors = {
success: 'bg-green-500',
error: 'bg-red-500',
warning: 'bg-yellow-500',
info: 'bg-blue-500',
};
toast.className = `toast px-4 py-3 rounded-lg ${colors[type]} text-white text-sm font-medium shadow-lg`;
toast.textContent = message;
container.appendChild(toast);
setTimeout(() => {
toast.style.opacity = '0';
toast.style.transform = 'translateY(8px)';
toast.style.transition = 'all 0.2s ease';
setTimeout(() => toast.remove(), 200);
}, 3000);
}
// ============================================
// EVENT LISTENERS
// ============================================
function setupEventListeners() {
// Breakpoint buttons
document.querySelectorAll('.breakpoint-btn').forEach(btn => {
btn.addEventListener('click', () => setBreakpoint(btn.dataset.breakpoint));
});
// Panel tabs
document.querySelectorAll('.panel-tab').forEach(tab => {
tab.addEventListener('click', () => {
document.querySelectorAll('.panel-tab').forEach(t => {
t.classList.remove('border-[var(--accent)]', 'text-[var(--fg)]');
t.classList.add('border-transparent', 'text-[var(--fg-muted)]');
});
tab.classList.remove('border-transparent', 'text-[var(--fg-muted)]');
tab.classList.add('border-[var(--accent)]', 'text-[var(--fg)]');
const panel = tab.dataset.panel;
document.getElementById('propertiesPanel').classList.toggle('hidden', panel !== 'properties');
document.getElementById('validationPanel').classList.toggle('hidden', panel !== 'validation');
});
});
// Export button
document.getElementById('exportBtn').addEventListener('click', exportWidget);
// Widget name input
document.getElementById('widgetName').addEventListener('input', (e) => {
widgetState.name = e.target.value;
});
// Click outside to deselect
document.getElementById('widgetPreview').addEventListener('click', (e) => {
if (e.target.id === 'widgetPreview') {
widgetState.selectedComponent = null;
renderWidgetPreview();
renderPropertiesPanel();
}
});
}
// ============================================
// UTILITY FUNCTIONS
// ============================================
function getComponentIcon(icon) {
const icons = {
hero: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2" stroke-width="2"/><path d="M3 9h18" stroke-width="2"/></svg>`,
text: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-width="2" d="M4 6h16M4 12h10"/></svg>`,
paragraph: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h10"/></svg>`,
button: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="8" width="18" height="8" rx="2" stroke-width="2"/></svg>`,
image: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2" stroke-width="2"/><circle cx="8.5" cy="8.5" r="1.5" fill="currentColor"/><path d="M21 15l-5-5L5 21" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
divider: `<svg class="w-4 h-4 text-[var(--fg-muted)]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-width="2" d="M4 12h16"/></svg>`,
};
return icons[icon] || icons.text;
}
</script>
</body>
</html>