0
0
Svelteframework~20 mins

In and out transitions in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Transitions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Svelte component toggles visibility?

Consider this Svelte component that uses the fade transition on a paragraph. What will you see when you click the button to toggle the paragraph?

Svelte
  <script>
    import { fade } from 'svelte/transition';
    let visible = true;
  </script>

  <button on:click={() => visible = !visible} aria-pressed={visible} aria-label="Toggle paragraph">Toggle</button>

  {#if visible}
    <p transition:fade>This paragraph fades in and out.</p>
  {/if}
AThe paragraph blinks on and off without fading.
BThe paragraph smoothly fades in when shown and fades out when hidden.
CThe paragraph instantly appears and disappears without any animation.
DThe paragraph slides in from the left and slides out to the right.
Attempts:
2 left
💡 Hint

Think about what the fade transition does in Svelte.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies a custom duration to an out transition in Svelte?

You want to make a paragraph fade out over 2 seconds when it is removed. Which code snippet correctly sets the duration for the fade out transition?

Svelte
  <script>
    import { fade } from 'svelte/transition';
    let show = true;
  </script>

  <button on:click={() => show = !show} aria-label="Toggle paragraph">Toggle</button>

  {#if show}
    <p ???>This paragraph fades out slowly.</p>
  {/if}
Atransition:fade(duration=2000)
Btransition:fade={{ duration: 2000 }}
Cfade:out={{ duration: 2000 }}
Dout:fade={{ duration: 2000 }}
Attempts:
2 left
💡 Hint

Remember how to specify only the out transition with custom parameters.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component's out transition not run?

Look at this Svelte code. The paragraph should fade out when hidden, but it disappears instantly. Why?

Svelte
  <script>
    import { fade } from 'svelte/transition';
    let visible = true;
  </script>

  <button on:click={() => visible = !visible} aria-label="Toggle">Toggle</button>

  {#if visible}
    <p transition:fade={{ duration: 1000 }} out:fade={{ duration: 2000 }}>Hello!</p>
  {/if}
AThe paragraph is removed immediately because the <code>fade</code> import is missing.
BThe <code>out:fade</code> is ignored because it has a longer duration than the in transition.
CThe <code>transition:fade</code> overrides the <code>out:fade</code>, so only the in transition runs.
DThe <code>out:fade</code> must be placed before <code>transition:fade</code> to work.
Attempts:
2 left
💡 Hint

Check how Svelte handles multiple transition directives on the same element.

state_output
advanced
2:00remaining
What is the final value of count after this Svelte component runs?

This Svelte component increments count each time the paragraph finishes its out transition. What is the value of count after clicking the button twice?

Svelte
  <script>
    import { fade } from 'svelte/transition';
    let visible = true;
    let count = 0;
    function handleOutroEnd() {
      count += 1;
    }
  </script>

  <button on:click={() => visible = !visible} aria-label="Toggle">Toggle</button>

  {#if visible}
    <p transition:fade on:outroend={handleOutroEnd}>Count: {count}</p>
  {/if}
A1
B0
C2
D3
Attempts:
2 left
💡 Hint

Remember when the outroend event fires in Svelte transitions.

🧠 Conceptual
expert
2:00remaining
Which statement about Svelte's in and out transitions is true?

Choose the correct statement about how Svelte handles in and out transitions on elements.

AAn element can have both <code>in</code> and <code>out</code> transitions, and they run independently when the element appears or disappears.
BIf an element has an <code>in</code> transition, it must also have an <code>out</code> transition defined.
COnly one transition type (<code>in</code> or <code>out</code>) can be used on an element at a time.
DUsing <code>transition</code> shorthand disables both <code>in</code> and <code>out</code> transitions.
Attempts:
2 left
💡 Hint

Think about how Svelte triggers transitions when elements enter or leave the DOM.