create a svelte 5 component for a shopping list form
- User
create a svelte 5 component for a shopping list form
Assistant
Here's a Svelte 5 shopping list form component with modern features:
<script> let newItem = $state('') let items = $state([]) function addItem() { if (newItem.trim()) { items = [...items, { id: crypto.randomUUID(), name: newItem.trim(), completed: false }] newItem = '' } } function removeItem(id) { items = items.filter(item => item.id !== id) } function toggleComplete(id) { items = items.map(item => item.id === id ? {...item, completed: !item.completed} : item ) } </script> <form on:submit|preventDefault={addItem} class="shopping-form"> <div class="input-group"> <input type="text" bind:value={newItem} placeholder="Add grocery item..." aria-label="Shopping item" class="item-input" > <button type="submit" class="add-btn" disabled={!newItem.trim()}> Add </button> </div> </form> <ul class="items-list"> {#each items as item (item.id)} <li class:completed={item.completed}> <div class="item-content"> <input type="checkbox" checked={item.completed} on:change={() => toggleComplete(item.id)} aria-label="Toggle completion" > <span>{item.name}</span> </div> <button on:click={() => removeItem(item.id)} class="remove-btn" aria-label="Remove item" > × </button> </li> {:else} <li class="empty-state">No items yet. Add your first grocery!</li> {/each} </ul> <style> .shopping-form { margin-bottom: 1.5rem; } .input-group { display: flex; gap: 0.5rem; } .item-input { flex-grow: 1; padding: 0.75rem; border: 1px solid #e0e0e0; border-radius: 4px; font-size: 1rem; } .add-btn { background: #4f46e5; color: white; border: none; border-radius: 4px; padding: 0 1.5rem; cursor: pointer; font-weight: 600; } .add-btn:disabled { opacity: 0.6; cursor: not-allowed; } .items-list { list-style: none; padding: 0; margin: 0; } .items-list li { display: flex; justify-content: space-between; align-items: center; padding: 0.75rem; border-bottom: 1px solid #f0f0f0; } .item-content { display: flex; align-items: center; gap: 0.75rem; } .completed span { text-decoration: line-through; color: #94a3b8; } .remove-btn { background: transparent; border: none; color: #ef4444; font-size: 1.5rem; cursor: pointer; line-height: 1; padding: 0 0.5rem; } .empty-state { color: #64748b; font-style: italic; padding: 1.5rem; text-align: center; border-bottom: none; } </style>Key features:
-
Modern Svelte 5 Syntax:
- Uses
$statefor reactive variables - Utilizes Svelte's enhanced reactivity system
- Uses
-
Core Functionality:
- Add items with Enter key or button click
- Mark items as completed
- Remove items from list
- Input validation (prevents empty items)
- Unique IDs for list items
-
UX Enhancements:
- Disabled "Add" button for empty input
- Visual feedback for completed items
- Placeholder text
- Accessible ARIA labels
- Empty state message
- Responsive design
-
Styling:
- Clean, modern interface
- Visual feedback for interactions
- Appropriate spacing and typography
- Hover effects on interactive elements
- Cross-browser compatible
To use this component:
- Create a new Svelte 5 project
- Place this code in
ShoppingList.svelte - Import and use it in your routes:
<script> import ShoppingList from './ShoppingList.svelte'; </script> <ShoppingList />The component includes:
- Form with input validation
- Dynamic list rendering
- Item completion tracking
- Modern UI transitions
- Mobile-friendly layout
- Accessible controls
- Clean visual hierarchy
You can customize the styling by modifying the CSS variables or adjusting the style block. The component uses semantic HTML and follows accessibility best practices.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.