You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1aae5ce
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_12-transitions-and-animations.md
+46-32Lines changed: 46 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,56 +11,70 @@ weight: 3
11
11
youtube:
12
12
---
13
13
14
-
## Svelte Await Blocks
14
+
Svelte is a modern and powerful front-end framework that allows developers to create dynamic and reactive user interfaces with ease. One of the key features of Svelte is its ability to create transitions and animations that enhance the user experience.
15
15
16
-
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are similar to promises, but they have some key advantages.
16
+
In this blog post, we will explore how to use Svelte transitions and animations with code examples.
17
17
18
-
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
18
+
## Transitions in Svelte
19
19
20
-
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
20
+
Transitions in Svelte are used to create smooth animations when an element is added, removed, or updated. Svelte provides several built-in transition functions that can be used out of the box, such as `fade`, `slide`, `fly`, and `scale`.
21
21
22
-
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
22
+
Let's take a look at an example of using the `fade` transition:
23
23
24
-
## How to Use Svelte Await Blocks
24
+
```svelte
25
+
<script>
26
+
import { fade } from 'svelte/transition';
25
27
26
-
To use Svelte await blocks, you first need to define an asynchronous function. An asynchronous function is a function that returns a promise.
28
+
let show = true;
27
29
28
-
Once you have defined an asynchronous function, you can use it in an await block. To do this, simply surround the call to the asynchronous function with the `await` keyword.
30
+
function toggle() {
31
+
show = !show;
32
+
}
33
+
</script>
29
34
30
-
For example, the following code defines an asynchronous function that fetches a random number from a server:
35
+
<button on:click={toggle}>Toggle</button>
31
36
32
-
```svelte
33
-
async function getRandomNumber() {
34
-
const res = await fetch(`/tutorial/random-number`);
35
-
const text = await res.text();
36
-
return text;
37
-
}
37
+
{#if show}
38
+
<div transition:fade>Content that fades in and out</div>
39
+
{/if}
38
40
```
39
41
40
-
The following code uses the `getRandomNumber()` function in an await block:
42
+
In the above code, we import the `fade` transition from the `svelte/transition` module. We also create a boolean variable `show` and a function `toggle` that toggles the value of `show` when the button is clicked.
43
+
44
+
When `show` is true, the content inside the `if` block is rendered with the `fade` transition. When `show` is false, the content is removed with the `fade` transition.
45
+
46
+
### Animations in Svelte
47
+
48
+
Animations in Svelte are used to create more complex and dynamic animations. Svelte provides several built-in animation functions that can be used out of the box, such as `spring`, `tweened`, and `crossfade`.
49
+
50
+
Let's take a look at an example of using the `spring` animation:
41
51
42
52
```svelte
43
-
{#await getRandomNumber()}
44
-
<p>...waiting</p>
45
-
{:then number}
46
-
<p>The number is {number}</p>
47
-
{:catch error}
48
-
<p style="color: red">{error.message}</p>
49
-
{/await}
50
-
```
53
+
<script>
54
+
import { spring } from 'svelte/motion';
55
+
56
+
let x = 0;
51
57
52
-
This code will first fetch a random number from the server. If the request is successful, the number will be displayed on the screen. If the request fails, an error message will be displayed.
Svelte await blocks have several advantages over other ways of writing asynchronous code.
70
+
In the above code, we import the `spring` animation from the `svelte/motion` module. We also create a variable `x` and a function `move` that toggles the value of `x` between `0` and `100` when the button is clicked.
57
71
58
-
First, await blocks are much easier to read. They are written in a natural, declarative style, which makes it easy to understand what the code is doing.
72
+
The `div` element has a `style` attribute that positions it absolutely and sets its `left` property to the current value of `x`.
59
73
60
-
Second, await blocks are more efficient. They do not require the use of callbacks, which can make code more complex and less performant.
74
+
The `animate` directive on the `div` element uses the `spring` animation to animate the `left` property of the `div` element to the new value of `x` when it changes.
61
75
62
-
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
76
+
### Conclusion
63
77
64
-
## Conclusion
78
+
Svelte transitions and animations provide powerful tools for creating dynamic and engaging user interfaces. With the built-in transition and animation functions, it's easy to add smooth and fluid animations to your Svelte components.
65
79
66
-
Svelte await blocks are a powerful feature that allow you to write asynchronous code in a concise and easy-to-read way. They are a great way to improve the readability, efficiency, and flexibility of your code.
80
+
In this blog post, we've seen some examples of how to use transitions and animations in Svelte. However, there are many more functions and options available in the Svelte documentation, so be sure to check it out to learn more.
0 commit comments