--- name: ink-component-patterns description: Use when building terminal UIs with Ink component patterns for React-based CLI applications. allowed-tools: [] --- # Ink Component Patterns You are an expert in building terminal UIs with Ink, React for the terminal. ## Core Principles - Use functional components with TypeScript for type safety - Leverage Ink's built-in components (Box, Text, Newline, Spacer) - Keep components focused and composable - Use proper prop types and interfaces - Handle terminal resizing gracefully ## Component Structure ### Basic Component ```tsx import { Box, Text } from 'ink'; import React from 'react'; interface MyComponentProps { title: string; items: string[]; } export const MyComponent: React.FC = ({ title, items }) => { return ( {title} {items.map((item, index) => ( • {item} ))} ); }; ``` ### Layout Patterns #### Vertical Stack ```tsx First item Second item ``` #### Horizontal Row ```tsx Left Right ``` #### Centered Content ```tsx Centered content ``` #### Padded Container ```tsx Content with border and padding ``` ## Common Patterns ### List with Icons ```tsx interface ListItem { icon: string; label: string; value: string; } const List: React.FC<{ items: ListItem[] }> = ({ items }) => ( {items.map((item, i) => ( {item.icon} {item.label}: {item.value} ))} ); ``` ### Status Messages ```tsx const StatusMessage: React.FC<{ type: 'success' | 'error' | 'warning'; message: string }> = ({ type, message, }) => { const config = { success: { icon: '✅', color: 'green' }, error: { icon: '❌', color: 'red' }, warning: { icon: '⚠️', color: 'yellow' }, }; const { icon, color } = config[type]; return ( {icon} {message} ); }; ``` ### Progress Indicator ```tsx const ProgressIndicator: React.FC<{ current: number; total: number; label: string }> = ({ current, total, label, }) => ( {label}: {current}/{total} ); ``` ### Collapsible Section ```tsx const CollapsibleSection: React.FC<{ title: string; isOpen: boolean; children: React.ReactNode }> = ({ title, isOpen, children, }) => ( {isOpen ? '▼' : '▶'} {title} {isOpen && {children}} ); ``` ## Best Practices 1. **Type Safety**: Always define prop interfaces 2. **Performance**: Use React.memo for expensive renders 3. **Accessibility**: Use semantic structure and clear labels 4. **Error Boundaries**: Wrap components in error boundaries 5. **Testing**: Test components with ink-testing-library ## Anti-Patterns to Avoid - Don't use DOM-specific APIs (document, window) - Don't use CSS classes or inline styles (use Ink props) - Don't mutate terminal state directly - Don't forget to handle edge cases (empty arrays, null values) - Don't create deeply nested component trees (keep it flat)