Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Real-World Svelte

You're reading from  Real-World Svelte

Product type Book
Published in Dec 2023
Publisher Packt
ISBN-13 9781804616031
Pages 282 pages
Edition 1st Edition
Languages
Author (1):
Tan Li Hau Tan Li Hau
Profile icon Tan Li Hau

Table of Contents (22) Chapters

Preface 1. Part 1: Writing Svelte Components
2. Chapter 1: Lifecycles in Svelte 3. Chapter 2: Implementing Styling and Theming 4. Chapter 3: Managing Props and State 5. Chapter 4: Composing Components 6. Part 2: Actions
7. Chapter 5: Custom Events with Actions 8. Chapter 6: Integrating Libraries with Actions 9. Chapter 7: Progressive Enhancement with Actions 10. Part 3: Context and Stores
11. Chapter 8: Context versus Stores 12. Chapter 9: Implementing Custom Stores 13. Chapter 10: State Management with Svelte Stores 14. Chapter 11: Renderless Components 15. Chapter 12: Stores and Animations 16. Part 4: Transitions
17. Chapter 13: Using Transitions 18. Chapter 14: Exploring Custom Transitions 19. Chapter 15: Accessibility with Transitions 20. Index 21. Other Books You May Enjoy

Using Transitions

Transitions are essential in creating smooth and engaging user experiences. By defining how elements appear, disappear, or change within a user interface, transitions can turn ordinary interactions into captivating experiences that leave lasting impressions on users.

Over the next three chapters, we will explore the topic of transitions in Svelte, beginning with a comprehensive understanding of how to use transitions in Svelte.

In this chapter, we will start by learning how to add transitions to elements in Svelte. We will explore the different transition directives and learn how to customize the transitions.

After that, we will discuss when and how the transitions are being played. We will look at different scenarios, such as where there’s a mix of elements with and without transitions, or when the elements are within nested logical blocks.

To truly master transitions, it’s important to understand the inner workings of the Svelte transition...

Technical requirements

You can find the code used in this chapter on GitHub: https://github.com/PacktPublishing/Real-World-Svelte/tree/main/Chapter13.

Adding transitions to elements

Svelte provides a simple and powerful way to add transitions to your application elements. The framework offers built-in transition functions that can be easily applied to elements, allowing for smooth animations and seamless user experiences. You can also define your own custom transitions, which we will learn about in the next chapter.

Transitions in Svelte are applied to elements when the elements are mounted or unmounted from the DOM. This ensures that elements appear and disappear gracefully, rather than just abruptly popping in and out of view.

To add a transition to an element in Svelte, you can use the transition: directive with the desired transition function. Here’s an example of adding a transition to an element:

<script>
  import { fade } from 'svelte/transition';
</script>
<div transition:fade>some text here</div>

In the preceding code snippet, we imported fade from svelte/transition...

When are the transitions played?

The transitions in Svelte are played when elements are added or removed from the DOM.

in: transitions are executed when an element is added to the DOM. This usually occurs when a component is initialized or when a condition that controls the element’s rendering becomes true.

For example, in an {#if} block, when the if condition turns from falsy to truthy, the elements inside the {#if} block are added to the DOM. All the in: transitions applied to these elements will be played simultaneously as soon as the elements are inserted into the DOM:

{#if condition}
  <div in:fade>some content</div>
  <div transition:blur>more content</div>
{/if}

In the preceding code snippet, as condition turns to true, both <div> elements will be inserted into the DOM. As soon as both <div> elements are inserted, both the fade and blur transitions will start playing simultaneously. Whether both the fade...

How Svelte transition works under the hood

Before we delve into the inner workings of Svelte transitions, let us first briefly discuss the general methods for creating animations on the web. Understanding these fundamental concepts provides a solid foundation for grasping how Svelte transitions work.

In general, you can create animations using either CSS or JavaScript.

Creating animations with CSS

To create animations using CSS, you can use the CSS animation property along with the @keyframes rules. The @keyframes rule is used to define a sequence of styles, specifying the CSS styles at each keyframe (from 0% to 100%) during the animation.

See this, for example:

@keyframes example {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  100% {
    opacity: 0;
    transform: scale(1.75);
  }
}

In the preceding code snippet, we defined an animation keyframe...

Summary

In this chapter, we learned how to add a transition onto an element. We explored the transition:, in:, and out: directives, and how to customize them.

Following that, we looked at when and how the transitions are played. We discussed how the transitions are played when we have a mix of elements with and without transitions, and also how the transitions are played when used inside elements within nested logical blocks.

Last but not least, we dug deeper into how the transition animations are played by Svelte.

With this knowledge, you can now confidently apply transitions into elements when working with Svelte. This will allow you to enhance the interactivity and visual appeal of your applications, thereby providing a more engaging user experience.

In the next chapter, we will look beyond the built-in transitions and will explore the creation of custom transitions.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Real-World Svelte
Published in: Dec 2023 Publisher: Packt ISBN-13: 9781804616031
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}