Skip to content

Commit 1aae5ce

Browse files
committed
update transitions post
1 parent 0068aca commit 1aae5ce

1 file changed

Lines changed: 46 additions & 32 deletions

File tree

‎apps/codingcatdev/src/content/course/sveltekit-firebase/lesson/2_12-transitions-and-animations.md‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,70 @@ weight: 3
1111
youtube:
1212
---
1313

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.
1515

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.
1717

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
1919

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`.
2121

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:
2323

24-
## How to Use Svelte Await Blocks
24+
```svelte
25+
<script>
26+
import { fade } from 'svelte/transition';
2527
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;
2729
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>
2934
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>
3136
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}
3840
```
3941

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:
4151

4252
```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;
5157
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.
58+
function move() {
59+
x = x === 0 ? 100 : 0;
60+
}
61+
</script>
5362
54-
## Advantages of Svelte Await Blocks
63+
<button on:click={move}>Move</button>
64+
65+
<div style="position: absolute; left: {x}px;" animate:{{ left: spring(x) }}>
66+
Content that moves left and right
67+
</div>
68+
```
5569

56-
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.
5771

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`.
5973

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.
6175

62-
Third, await blocks are more flexible. They can be used with any asynchronous function, not just those that return promises.
76+
### Conclusion
6377

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.
6579

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

Comments
 (0)