Svelte is a framework that helps developers to work on the front-end of webpages. It is an open-source interactive language that allows development of front-end pages in JavaScript.
A to-do app allows us to maintain the records of one's daily tasks digitally. It stores all the information in the form of a list with the most important task at the top and the least important at the bottom.
A to-do app in Svelte would have the following functionalities:
For creating a To-Do app in Svelte, we need a CSS
, JSON package
, and npm install
CSS
file for the front-end to style different components such as buttons, drop downs, logos, texts etc.<!DOCTYPE html><html lang="en"><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'><title>Svelte app</title><link rel='icon' type='image/png' href='/favicon.png'><link rel='stylesheet' href='/global.css'><link rel='stylesheet' href='/build/bundle.css'><script defer src='/build/bundle.js'></script></head><body></body></html>
JSON package
will contain all the information regarding the ports
, plugins
, server
, and hosts
.{"name": "svelte-app","version": "1.0.0","devDependencies": {"npm-run-all": "^4.1.5","rollup": "^1.10.1","rollup-plugin-commonjs": "^9.3.4","rollup-plugin-livereload": "^1.0.0","rollup-plugin-node-resolve": "^4.2.3","rollup-plugin-svelte": "^5.0.3","rollup-plugin-terser": "^4.0.4","svelte": "^3.0.0"},"dependencies": {"sirv-cli": "^0.4.4"},"scripts": {"build": "rollup -c","autobuild": "rollup -c -w","dev": "run-p start:dev autobuild","start": "sirv public --single --host 0.0.0.0","start:dev": "sirv public --single --dev --host 0.0.0.0 --port 5000"}}
The source includes the basic functionalities of the application.
<script>let todoItems = [];let newItem = "";function addTodoItem() {newItem = newItem.trim();if (newItem.length == 0) {return;}const todo = {text: newItem,checked: false,id: Date.now(),};todoItems = [...todoItems,{text: newItem,checked: false,id: Date.now(),},];newItem = "";}function toggleItemDone(id) {todoItems = todoItems.map((item) => {if (item.id === id) {item.checked = !item.checked;}return item;});}function deleteTodoItem(id) {todoItems = todoItems.filter((item) => item.id !== Number(id));}</script><main><div class="container"><h1 class="title">todos</h1><ul class="todo-list">{#each todoItems as todo (todo.id)}<li class="todo-item {todo.checked? 'done': ''}"><inputid={todo.id}type="checkbox"on:click={() => toggleItemDone(todo.id)}/><label for={todo.id} /><span class="todo-text">{todo.text}</span><buttonclass="delete-button"on:click={() => deleteTodoItem(todo.id)}><svg class="icon"><use href="#delete-icon" /></svg></button></li>{/each}</ul><form on:submit|preventDefault={addTodoItem}><inputclass="todo-input"type="text"aria-label="Enter a new todo item"placeholder="E.g. Build a web app"bind:value={newItem}/><button class="add-button" type="submit">Add</button></form></div></main>Footer
newItem
would add todo item, represents the input field text.addTodoItem()
is a function that adds a new to-do item to the existing list. it would take the input field from the user.const todo
create a new todo item object with the text from the input field.function toggleItemDone()
will toggle a to-do item from the to-do list.function deleteTodoItem()
will remove a to-do item from the to-do list.We needed to add all these files to a folder and enter the following scripts.
NPM
needed to be installed first so that we would enter the npm install
command.npm
so that we will enter npm start
.5000
and the local host.The application would be executed as follow:
<!DOCTYPE html> <html lang="en"> <head> <meta charset='utf-8'> <meta name='viewport' content='width=device-width,initial-scale=1'> <title>Svelte app</title> <link rel='icon' type='image/png' href='/favicon.png'> <link rel='stylesheet' href='/global.css'> <link rel='stylesheet' href='/build/bundle.css'> <script defer src='/build/bundle.js'></script> </head> <body> </body> </html>
Free Resources