Build me todo app in Svelte ruins
- User
Build me todo app in Svelte ruins
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Build me todo app in Svelte ruins
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Sure! Let's build a To-Do App in Svelte using the SvelteKit framework, which is the recommended way to build apps with Svelte.
I assume you meant "Svelte + Vite" or "SvelteKit" instead of "Svelte ruins". 😄
If not, let me know and I can adjust accordingly.
Make sure you have:
npm here)npm create svelte@latest svelte-todo-app
cd svelte-todo-app
Choose the following options when prompted:
Then install dependencies:
npm install
npm run dev
Open http://localhost:5173 in your browser.
src/routes/+page.svelte with a simple To-Do AppHere’s a basic but functional To-Do app in one file:
<!-- src/routes/+page.svelte -->
<script lang="ts">
let tasks = ['Learn Svelte', 'Build a To-Do App'];
let newTask = '';
function addTask() {
if (newTask.trim()) {
tasks = [...tasks, newTask.trim()];
newTask = '';
}
}
function removeTask(index: number) {
tasks = tasks.filter((_, i) => i !== index);
}
</script>
<main class="container">
<h1>📝 Svelte To-Do App</h1>
<div class="input-group">
<input bind:value={newTask} placeholder="Add a new task" on:keydown={(e) => e.key === 'Enter' && addTask()} />
<button on:click={addTask}>Add</button>
</div>
<ul>
{#each tasks as task, i}
<li>
<span>{task}</span>
<button on:click={() => removeTask(i)}>❌</button>
</li>
{/each}
</ul>
</main>
<style>
.container {
max-width: 500px;
margin: 2rem auto;
text-align: center;
font-family: sans-serif;
}
.input-group input {
padding: 0.5rem;
width: 70%;
}
.input-group button {
padding: 0.5rem;
margin-left: 0.5rem;
cursor: pointer;
}
ul {
list-style: none;
padding: 0;
margin-top: 1rem;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
background: #f4f4f4;
margin-bottom: 0.5rem;
padding: 0.5rem;
border-radius: 4px;
}
button {
background: none;
border: none;
color: red;
cursor: pointer;
}
</style>
Want to persist tasks across reloads? Here’s how:
Update the <script> block like this:
let tasks = $:
JSON.parse(localStorage.getItem('tasks') || '[]');
$: localStorage.setItem('tasks', JSON.stringify(tasks));
let newTask = '';
function addTask() {
if (newTask.trim()) {
tasks = [...tasks, newTask.trim()];
newTask = '';
}
}
function removeTask(index: number) {
tasks = tasks.filter((_, i) => i !== index);
}
Now tasks will be saved even after refreshing the page!
You can deploy your SvelteKit app to platforms like:
Build command:
npm run build
You now have a working To-Do app built with Svelte + SvelteKit.
Let me know if you'd like to expand it with features like:
Happy coding! 💻✨
Give me something I can run in browser
Sure! Here's a fully self-contained Svelte To-Do app that you can run directly in the browser using the Svelte REPL — no installation required!
<script>
let tasks = ['Learn Svelte', 'Build a To-Do App'];
let newTask = '';
function addTask() {
if (newTask.trim()) {
tasks = [...tasks, newTask.trim()];
newTask = '';
}
}
function removeTask(index) {
tasks = tasks.filter((_, i) => i !== index);
}
</script>
<main>
<h1>📝 Svelte To-Do App</h1>
<div style="margin-bottom: 1rem;">
<input bind:value={newTask} placeholder="Add a new task" on:keydown={(e) => e.key === 'Enter' && addTask()} />
<button on:click={addTask}>Add</button>
</div>
<ul>
{#each tasks as task, i}
<li>
<span>{task}</span>
<button on:click={() => removeTask(i)}>❌</button>
</li>
{/each}
</ul>
</main>
<style>
main {
max-width: 500px;
margin: auto;
padding: 2rem;
font-family: sans-serif;
text-align: center;
}
input {
padding: 0.5rem;
width: 70%;
}
button {
padding: 0.5rem;
margin-left: 0.5rem;
cursor: pointer;
background-color: #ff4b5c;
color: white;
border: none;
border-radius: 4px;
}
ul {
list-style: none;
padding: 0;
margin-top: 1rem;
}
li {
display: flex;
justify-content: space-between;
align-items: center;
background: #f4f4f4;
margin-bottom: 0.5rem;
padding: 0.5rem;
border-radius: 4px;
}
</style>
Let me know if you'd like a version with features like:
Happy coding! 🚀