Skip to content

Commit fd8d143

Browse files
committed
Add deferred transitions
1 parent 2771f3e commit fd8d143

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

src/App.svelte

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
import TodoItem from './TodoItem.svelte'
55
import { todoItems } from './store'
66
import { todoStats } from './todoStatsStore'
7+
import { quintOut } from 'svelte/easing';
8+
import { crossfade } from 'svelte/transition';
9+
10+
const [send, receive] = crossfade({
11+
duration: d => Math.sqrt(d * 200),
12+
13+
fallback(node, params) {
14+
const style = getComputedStyle(node);
15+
const transform = style.transform === 'none' ? '' : style.transform;
16+
17+
return {
18+
duration: 600,
19+
easing: quintOut,
20+
css: t => `
21+
transform: ${transform} scale(${t});
22+
opacity: ${t}
23+
`
24+
};
25+
}
26+
});
727
828
let mounted = false
929
onMount(() => {
@@ -31,30 +51,59 @@
3151
/>
3252
</div>
3353

34-
{#if mounted}
3554
{#if $todoStats.totalCount === 0}
3655
No items yet
3756
{:else}
38-
<div class="todo-items-container">
39-
{#each $todoItems as { id, text, checked }, index (id)}
40-
<div class="todo-item-container">
41-
<TodoItem
42-
text={`${index + 1}: ${text}`}
43-
{checked}
44-
on:checked={event => handleItemChecked(id, event.detail)}
45-
on:remove={() => handleItemRemove(id)}
46-
/>
47-
</div>
48-
{/each}
57+
<div class="todos-container">
58+
<div class="todo-items-container">
59+
{#each $todoStats.notDoneItems as { id, text, checked }, index (id)}
60+
<div
61+
class="todo-item-container"
62+
in:receive="{{key: id}}"
63+
out:send="{{key: id}}"
64+
>
65+
<TodoItem
66+
text={`${index + 1}: ${text}`}
67+
{checked}
68+
on:checked={event => handleItemChecked(id, event.detail)}
69+
on:remove={() => handleItemRemove(id)}
70+
/>
71+
</div>
72+
{/each}
73+
</div>
74+
<div class="todo-items-container">
75+
{#each $todoStats.doneItems as { id, text, checked }, index (id)}
76+
<div
77+
class="todo-item-container"
78+
in:receive="{{key: id}}"
79+
out:send="{{key: id}}"
80+
>
81+
<TodoItem
82+
text={`${index + 1}: ${text}`}
83+
{checked}
84+
on:checked={event => handleItemChecked(id, event.detail)}
85+
on:remove={() => handleItemRemove(id)}
86+
/>
87+
</div>
88+
{/each}
89+
</div>
4990
</div>
5091
{/if}
51-
{/if}
5292

5393
<style>
5494
.add-todo-item-container {
5595
margin-bottom: 5px;
5696
}
57-
.todo-items-container:not(:last-child) > .todo-item-container {
97+
.todo-items-container {
98+
flex: 1;
99+
padding: 5px;
100+
box-sizing: border-box;
101+
}
102+
.todo-items-container > .todo-item-container:not(:last-child) {
58103
margin-bottom: 5px;
59104
}
105+
.todos-container {
106+
width: 100%;
107+
display: flex;
108+
}
60109
</style>

src/TodoItem.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import { createEventDispatcher } from 'svelte'
33
import { tweened } from 'svelte/motion';
44
import { cubicOut } from 'svelte/easing';
5-
import { fly, fade } from 'svelte/transition';
65
import { interpolateLab } from 'd3-interpolate';
76
87
const dispatch = createEventDispatcher()
@@ -31,11 +30,7 @@
3130
}
3231
</script>
3332

34-
<div
35-
in:fly|local="{{ y: 200, duration: 500 }}"
36-
out:fade
37-
class="main-container"
38-
>
33+
<div class="main-container">
3934
<div
4035
class="inner-container"
4136
style="background-color: {$checkedMotion};"

src/todoStatsStore.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { todoItems } from './store'
44
export const todoStats = derived(
55
[todoItems],
66
([todoItems]) => {
7-
const doneCount = todoItems.filter(item => item.checked).length
7+
const doneItems = todoItems.filter(item => item.checked)
8+
const doneCount = doneItems.length
89
const totalCount = todoItems.length
910
return {
1011
doneCount,
1112
totalCount,
12-
leftTodoCount: totalCount - doneCount
13+
leftTodoCount: totalCount - doneCount,
14+
doneItems,
15+
notDoneItems: todoItems.filter(item => !item.checked)
1316
}
1417
}
1518
)

0 commit comments

Comments
 (0)