svelte-playground/src/lib/apps/todoApp.svelte

231 lines
5.1 KiB
Svelte
Raw Normal View History

2021-08-01 21:33:31 +02:00
<script>
2021-08-01 22:22:34 +02:00
import {todos} from "$lib/stores/todos.js";
let todo_text = "";
2021-08-02 07:59:31 +02:00
let todo_type = "active";
let filteredTodo = [];
2021-08-01 21:33:31 +02:00
let addTodo = () => {
if (todo_text.length > 0) {
const todo = {
text: todo_text,
date: Date.now,
done: false
}
2021-08-03 09:43:26 +02:00
todo_text = "";
if (todo_type == "done") todo_type = "all";
2021-08-01 21:33:31 +02:00
$todos = [todo, ...$todos];
}
}
let clearTodo = (todo) => {
if(todo.done) {
2021-08-01 21:33:31 +02:00
$todos = $todos.filter(x => x != todo)
}
else {
2021-08-01 21:33:31 +02:00
todo.done = true;
$todos = $todos
}
2021-08-01 21:33:31 +02:00
}
2021-08-01 22:22:34 +02:00
let filterTodo = (type) => {
todo_type = type;
2021-08-01 22:22:34 +02:00
switch (type) {
case 'active':
filteredTodo = $todos.filter(todo => !todo.done);
break;
case 'done':
filteredTodo = $todos.filter(todo => todo.done);
break;
case 'all':
filteredTodo = $todos;
break;
}
}
$: {
filteredTodo = $todos;
filterTodo(todo_type)
}
2021-08-01 21:33:31 +02:00
</script>
<div class = "panel">
<h2 class= "title">todo</h2>
2021-08-01 22:22:34 +02:00
<hr/>
2021-08-03 09:43:26 +02:00
<div class="todo-input">
<input class="todo-text" bind:value="{todo_text}" placeholder="title"/>
<button on:click="{addTodo}" disabled = {todo_text.length == 0} class = "todo-add {todo_text.length == 0 ? 'disabled' : ''}">add</button>
</div>
2021-08-01 22:22:34 +02:00
<div class="todo-filter">
2021-08-02 07:59:31 +02:00
<button on:click="{() => filterTodo('active')}" class = {todo_type == 'active' ? 'disabled' : ''}>active</button>
<button on:click="{() => filterTodo('done')}" class = {todo_type == 'done' ? 'disabled' : ''}>done</button>
<button on:click="{() => filterTodo('all')}" class = {todo_type == 'all' ? 'disabled' : ''}>all</button>
2021-08-01 21:33:31 +02:00
</div>
2021-08-01 22:22:34 +02:00
<ul class = "todo-list">
{#each filteredTodo as todo}
<li class = "todo {todo.done ? 'done' : ''}">
<p>{todo.text}</p>
<button on:click="{() => clearTodo(todo)}">{todo.done ? '✗' : '✓'}</button>
</li>
{/each}
</ul>
2021-08-01 21:33:31 +02:00
</div>
<style>
2021-08-03 09:43:26 +02:00
* {
--cream: #FEF9EF;
--blue: #227c9d;
--blue-disabled: #aabac0;
2021-08-03 09:43:26 +02:00
--green: #17C3B2;
--yellow: #FFCB77;
--red: #FE6D73;
}
.panel {
padding-top: 1rem;
padding-bottom: 1rem;
2021-08-03 09:43:26 +02:00
background-color: var(--cream);
font-size: 1rem;
font-family: 'Atkinson Hyperlegible', sans-serif;
2021-08-03 09:43:26 +02:00
}
@media only screen and (min-width: 500px) {
.panel {
margin-left: auto;
margin-right: auto;
width: 500px;
border: solid var(--cream) 0px;
border-radius: 10px;
2021-08-03 09:43:26 +02:00
}
}
.title {
margin-left: 2rem;
font-weight: 400;
font-size: 3rem;
color: var(--yellow);
}
2021-08-03 09:43:26 +02:00
hr {
margin-left: 1rem;
margin-bottom: 1rem;
border: 2px solid var(--yellow);
2021-08-01 21:33:31 +02:00
}
button {
padding: 5px;
margin: 0px;
2021-08-03 09:43:26 +02:00
background-color: var(--blue);
border: solid var(--blue) 1px;
2021-08-01 21:33:31 +02:00
border-radius: 5px;
color: white;
font-family: 'Atkinson Hyperlegible', sans-serif;
font-size: 1rem;
2021-08-01 21:33:31 +02:00
}
2021-08-03 09:43:26 +02:00
.todo-input {
font-size: 0px;
margin: 1rem;
2021-08-03 09:43:26 +02:00
}
.todo-text {
padding: 4px;
2021-08-03 09:43:26 +02:00
margin: 0px;
border: solid white 2px;
2021-08-03 09:43:26 +02:00
border-right: 0px;
border-radius: 0px;
border-bottom-left-radius: 5px;
border-top-left-radius: 5px;
font-family: 'Atkinson Hyperlegible', sans-serif;
font-size: 1rem;
2021-08-02 07:59:31 +02:00
}
2021-08-03 09:43:26 +02:00
.todo-text:focus {
outline: none;
border-bottom-color: var(--blue);
2021-08-02 07:59:31 +02:00
}
2021-08-03 09:43:26 +02:00
.todo-add {
border-bottom-left-radius: 0px;
border-top-left-radius: 0px;
2021-08-01 22:22:34 +02:00
}
2021-08-03 09:43:26 +02:00
.disabled {
background-color: var(--blue-disabled);
border-color: var(--blue-disabled);
2021-08-03 09:43:26 +02:00
}
button:active {
background-color: var(--yellow);
border-color: var(--yellow);
2021-08-03 09:43:26 +02:00
color: black;
2021-08-01 21:33:31 +02:00
}
button:active.disabled {
background-color: var(--blue-disabled);
border-color: var(--blue-disabled);
color: white;
2021-08-01 21:33:31 +02:00
}
2021-08-01 22:22:34 +02:00
.todo-filter {
margin-top: 1rem;
2021-08-01 22:22:34 +02:00
display: flex;
align-items: center;
justify-content: center;
}
.todo-filter button {
margin-right: 0.5rem;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
2021-08-01 22:22:34 +02:00
}
2021-08-02 07:59:44 +02:00
.todo-list {
padding: 1rem;
padding-bottom: 0.5rem;
background-color: var(--blue-disabled);
/* list
* padding 1.5rem
todo
* content 1.0rem
* padding 1.0rem
* marging 0.5rem
button
* padding 6px
* border 6px
total
(1.5 + 2.5*3)rem (9rem)
+ (12*3)px (36px)
*/
min-height: calc(11.5rem + 48px);
2021-08-02 07:59:44 +02:00
}
2021-08-01 21:33:31 +02:00
.todo {
padding: 0.5rem;
margin-bottom: 0.5rem;
2021-08-03 09:43:26 +02:00
background-color: var(--yellow);
2021-08-01 21:33:31 +02:00
border-radius: 5px;
display: flex;
align-items: center;
}
2021-08-03 09:43:26 +02:00
.todo.done {
background-color: var(--cream);
2021-08-01 21:33:31 +02:00
}
.todo button {
padding: 3px;
2021-08-01 21:33:31 +02:00
margin-left: auto;
2021-08-03 09:43:26 +02:00
background-color: var(--green);
border: solid 3px var(--green);
2021-08-03 09:43:26 +02:00
color: var(--cream)
}
.todo.done button {
background-color: var(--red);
border-color: var(--red);
color: var(--cream);
}
.todo button:active {
background-color: var(--yellow);
color: black;
2021-08-01 21:33:31 +02:00
}
</style>