Production Patterns
Production-ready patterns with live, editable code examples
Tabs Component
NavigationTab navigation with content panels
// Tabs Pattern
const { div, h3, button, p, span } = van.tags;
// Active tab
const activeTab = van.state('overview');
// Tab content
const tabs = {
overview: {
label: 'Overview',
icon: '📊',
content: 'This is the overview tab with general information and statistics.'
},
activity: {
label: 'Activity',
icon: '📈',
count: 12,
content: 'Recent activity feed showing latest updates and interactions.'
},
settings: {
label: 'Settings',
icon: '⚙️',
content: 'Configuration and preferences for your account.'
}
};
return div(
{ class: 'space-y-4' },
h3({ class: 'text-lg font-semibold mb-2' }, 'Tabs Navigation'),
// Tab bar
div({ class: 'flex gap-2 border-b border-mist' },
...Object.entries(tabs).map(([key, tab]) =>
button({
onclick: () => activeTab.val = key,
class: () => activeTab.val === key
? 'px-4 py-2 border-b-2 border-sky text-sky font-medium flex items-center gap-2'
: 'px-4 py-2 text-secondary hover:text-midnight flex items-center gap-2'
},
span(tab.icon),
span(tab.label),
tab.count ? Badge({ class: 'subtle vwfcolor-error vwfsize-xs' }, tab.count) : ''
)
)
),
// Tab content
Card({ class: 'p-6 bg-white' },
() => p(tabs[activeTab.val].content)
)
);▶️ Live Preview
Tabs Navigation
This is the overview tab with general information and statistics.
💡 Click "Edit Code" to modify the example
🔄 Click "Apply Changes" to see your modifications
↩️ Click "Reset" to restore original code
📋 Best Practices
State Management
• Use individual van.state() for each value
• Keep state close to where it's used
• Update arrays immutably with spread operator
• Use arrow functions for reactive values
Performance
• Debounce expensive operations
• Use keys for list items
• Avoid complex computations in render
• Clean up timers and listeners
Component Design
• Accept both props and children
• Use parseArgs for flexibility
• Provide sensible defaults
• Keep components focused
User Experience
• Show loading states
• Provide immediate feedback
• Handle errors gracefully
• Add smooth transitions