📖 Scenario: You are building a simple Svelte app that shows and hides a message when a button is clicked. You want to make the message appear and disappear smoothly to make the app feel friendly and polished.
🎯 Goal: Build a Svelte component that toggles a message with a smooth fade transition when the button is clicked.
📋 What You'll Learn
Create a boolean variable showMessage to control message visibility
Add a button that toggles showMessage when clicked
Use Svelte's built-in fade transition on the message
Ensure the message smoothly fades in and out when toggled
💡 Why This Matters
🌍 Real World
Transitions are used in websites and apps to make showing and hiding content feel smooth and natural, like opening a door gently instead of slamming it.
💼 Career
Understanding how to use transitions in frameworks like Svelte is important for front-end developers to create polished, user-friendly interfaces.
Progress0 / 4 steps
1
DATA SETUP: Create a boolean variable to control message visibility
Create a variable called showMessage and set it to false.
Svelte
Hint
Use let to declare a variable in Svelte script.
2
CONFIGURATION: Add a button to toggle the message visibility
Add a <button> element with an on:click event that toggles the showMessage variable.
Svelte
Hint
Use on:click to handle button clicks in Svelte.
3
CORE LOGIC: Import and apply the fade transition to the message
Import the fade transition from 'svelte/transition' and apply it to a <p> element that shows the text "Hello, welcome!" only when showMessage is true.
Svelte
Hint
Use Svelte's {#if} block to conditionally show the message with transition:fade.
4
COMPLETION: Add a duration to the fade transition for smooth effect
Modify the fade transition on the <p> element to have a duration of 500 milliseconds.
Svelte
Hint
Pass an object with duration: 500 to the fade transition.
Practice
(1/5)
1. Why do transitions improve user experience in Svelte applications?
easy
A. They make UI changes feel smooth and clear to users.
B. They increase the app's loading speed significantly.
C. They automatically fix bugs in the code.
D. They reduce the app's file size drastically.
Solution
Step 1: Understand the purpose of transitions
Transitions help make changes in the user interface feel smooth and natural, improving clarity.
Step 2: Compare other options
Options about loading speed, bug fixing, or file size are unrelated to transitions' main role.
Final Answer:
They make UI changes feel smooth and clear to users. -> Option A
Quick Check:
Transitions enhance clarity and smoothness = D [OK]
4. Identify the error in this Svelte code that prevents the transition from working:
<script>
import { fade } from 'svelte/transition';
let show = true;
</script>
{#if show}
<div transition=fade>Content</div>
{/if}
<button on:click={() => show = !show}>Toggle</button>
medium
A. The transition directive syntax is incorrect; it should be transition:fade.
B. The variable 'show' is not initialized properly.
C. The import statement for fade is missing.
D. The button click handler syntax is invalid.
Solution
Step 1: Check transition directive syntax
The correct syntax uses a colon: transition:fade, not transition=fade.
Step 2: Verify other parts
The variable 'show' is initialized, fade is imported, and button syntax is valid.
Final Answer:
The transition directive syntax is incorrect; it should be transition:fade. -> Option A
Quick Check:
Correct transition syntax uses colon = B [OK]
Hint: Use colon in transition directive: transition:fade [OK]
Common Mistakes:
Using '=' instead of ':' in transition directive
Assuming variable initialization is wrong
Missing import statement
Incorrect button event syntax
5. You want to add a slide transition that lasts 500ms when a component appears or disappears. Which Svelte code snippet correctly applies this with accessibility in mind?