Bird
Raised Fist0
Svelteframework~10 mins

Common action patterns (click-outside, tooltip) in Svelte - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a click-outside action that calls the close function.

Svelte
<script>
  import { onMount } from 'svelte';
  let open = true;
  function close() {
    open = false;
  }
  function clickOutside(node) {
    const handleClick = event => {
      if (!node.contains(event.target)) {
        [1]();
      }
    };
    document.addEventListener('click', handleClick);
    return {
      destroy() {
        document.removeEventListener('click', handleClick);
      }
    };
  }
</script>

<div use:clickOutside>
  {#if open}
    <p>Click outside me to close.</p>
  {/if}
</div>
Drag options to blanks, or click blank then click option'
Aclose
Bopen
ConMount
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Calling open() instead of close().
Using node() which is not a function.
Forgetting to call the function with parentheses.
2fill in blank
medium

Complete the code to show the tooltip text when hovering over the button.

Svelte
<script>
  let show = false;
</script>

<button on:mouseenter={() => [1] = true} on:mouseleave={() => show = false} aria-describedby="tip">
  Hover me
</button>
{#if show}
  <div id="tip" role="tooltip">
    Tooltip text
  </div>
{/if}
Drag options to blanks, or click blank then click option'
Afalse
Bshow
Copen
Dvisible
Attempts:
3 left
💡 Hint
Common Mistakes
Using open or visible which are not defined.
Setting show to false instead of true.
Forgetting to update show on mouse enter.
3fill in blank
hard

Fix the error in the click-outside action to properly remove the event listener.

Svelte
<script>
  function clickOutside(node) {
    const handleClick = event => {
      if (!node.contains(event.target)) {
        node.dispatchEvent(new CustomEvent('outclick'));
      }
    };
    document.addEventListener('click', handleClick);
    return {
      destroy() {
        document.removeEventListener('click', [1]);
      }
    };
  }
</script>
Drag options to blanks, or click blank then click option'
Aevent
BclickOutside
ChandleClick
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Passing clickOutside instead of handleClick.
Passing node or event which are not functions.
Omitting the argument in removeEventListener.
4fill in blank
hard

Fill both blanks to create a tooltip action that shows on mouseenter and hides on mouseleave.

Svelte
<script>
  export function tooltip(node) {
    function show() {
      node.setAttribute('aria-expanded', [1]);
    }
    function hide() {
      node.setAttribute('aria-expanded', [2]);
    }
    node.addEventListener('mouseenter', show);
    node.addEventListener('mouseleave', hide);
    return {
      destroy() {
        node.removeEventListener('mouseenter', show);
        node.removeEventListener('mouseleave', hide);
      }
    };
  }
</script>
Drag options to blanks, or click blank then click option'
A"true"
B"false"
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean true/false instead of strings.
Swapping the true and false values.
Forgetting to quote the values.
5fill in blank
hard

Fill all three blanks to create a Svelte action that toggles a CSS class visible on hover.

Svelte
<script>
  export function hoverVisible(node) {
    function onEnter() {
      node.classList.[1]('visible');
    }
    function onLeave() {
      node.classList.[2]('visible');
    }
    node.addEventListener('mouseenter', onEnter);
    node.addEventListener('mouseleave', onLeave);
    return {
      destroy() {
        node.removeEventListener('mouseenter', onEnter);
        node.removeEventListener('mouseleave', onLeave);
      }
    };
  }
</script>

<style>
  .visible {
    opacity: 1;
    transition: opacity 0.3s ease;
  }
  div {
    opacity: 0;
  }
</style>

<div use:hoverVisible>
  Hover to see me fade in.
</div>
Drag options to blanks, or click blank then click option'
Aadd
Bremove
Ctoggle
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using toggle which may cause flickering.
Using contains which only checks presence.
Removing the class on enter instead of adding.

Practice

(1/5)
1. What is the main purpose of the click-outside action pattern in Svelte?
easy
A. To toggle a checkbox state on click
B. To open a tooltip when hovering over an element
C. To close a menu or popup when the user clicks outside of it
D. To fetch data from an API when clicking a button

Solution

  1. Step 1: Understand the click-outside pattern

    This pattern detects clicks outside a specific element to trigger an action, usually closing menus or popups.
  2. Step 2: Match purpose with options

    To close a menu or popup when the user clicks outside of it correctly describes closing menus/popups on outside clicks, while others describe unrelated behaviors.
  3. Final Answer:

    To close a menu or popup when the user clicks outside of it -> Option C
  4. Quick Check:

    click-outside closes menus on outside click = A [OK]
Hint: Click-outside closes popups when clicking outside them [OK]
Common Mistakes:
  • Confusing click-outside with tooltip behavior
  • Thinking it toggles element visibility on inside clicks
  • Assuming it triggers API calls
2. Which of the following is the correct way to use a clickOutside action in a Svelte component?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall Svelte action syntax

    Svelte actions are used with the use: directive followed by the action name and optional parameters.
  2. Step 2: Identify correct syntax

    uses use:clickOutside, which is the correct way to apply an action. Other options misuse event or binding syntax.
  3. Final Answer:

    <div use:clickOutside={handleClickOutside}></div> -> Option D
  4. Quick Check:

    Actions use 'use:' directive = C [OK]
Hint: Use actions with 'use:' prefix in Svelte [OK]
Common Mistakes:
  • Using event syntax 'on:' instead of 'use:' for actions
  • Trying to bind actions with 'bind:'
  • Passing actions as plain attributes
3. Given this Svelte code snippet using a tooltip action:
  <button use:tooltip={{ text: 'Save changes' }}>Save</button>

What will happen when the user hovers over the button?
medium
A. A tooltip with the text 'Save changes' appears near the button
B. The button text changes to 'Save changes'
C. Nothing happens because the syntax is incorrect
D. The button is disabled

Solution

  1. Step 1: Understand tooltip action usage

    The tooltip action shows a small info box with the provided text when hovering or focusing the element.
  2. Step 2: Analyze the code behavior

    The button uses use:tooltip with text 'Save changes', so hovering triggers the tooltip display near the button.
  3. Final Answer:

    A tooltip with the text 'Save changes' appears near the button -> Option A
  4. Quick Check:

    Tooltip shows text on hover = A [OK]
Hint: Tooltip shows text on hover or focus [OK]
Common Mistakes:
  • Thinking tooltip changes button text
  • Assuming tooltip disables the button
  • Believing the syntax is invalid
4. Consider this Svelte snippet using a clickOutside action:
  <script>
    let open = true;
    function close() { open = false; }
  </script>
  <div use:clickOutside={close} >
    {#if open}
      <p>Menu content</p>
    {/if}
  </div>

Why might the menu not close when clicking outside?
medium
A. The clickOutside action is not imported or defined
B. The open variable is not reactive
C. The close function does not set open to true
D. The menu content is inside the div with the action

Solution

  1. Step 1: Check action usage requirements

    For use:clickOutside to work, the action must be imported or defined in the component.
  2. Step 2: Identify missing import or definition

    The snippet does not show any import or definition of clickOutside, so the action won't run, preventing menu close.
  3. Final Answer:

    The clickOutside action is not imported or defined -> Option A
  4. Quick Check:

    Missing action import = B [OK]
Hint: Always import or define actions before use [OK]
Common Mistakes:
  • Assuming variable reactivity is the issue
  • Confusing function logic with action setup
  • Ignoring the need to import or define actions
5. You want to create a tooltip that only appears when a button is focused or hovered, and disappears when clicking outside or losing focus. Which combination of Svelte patterns should you use?
hard
A. Use only CSS :hover and :focus styles without any actions
B. Use a tooltip action on the button and a clickOutside action on the tooltip container to hide it
C. Use a tooltip action on the button and toggle visibility with a manual click event
D. Use only a clickOutside action on the button to show and hide the tooltip

Solution

  1. Step 1: Understand tooltip and clickOutside roles

    The tooltip action shows info on hover/focus. The clickOutside action detects clicks outside to hide elements like tooltips.
  2. Step 2: Combine actions for desired behavior

    Using tooltip on the button shows the tooltip on hover/focus. Using clickOutside on the tooltip container hides it when clicking outside, ensuring it disappears properly.
  3. Final Answer:

    Use a tooltip action on the button and a clickOutside action on the tooltip container to hide it -> Option B
  4. Quick Check:

    Tooltip + clickOutside combo controls show/hide = D [OK]
Hint: Combine tooltip and clickOutside for show/hide control [OK]
Common Mistakes:
  • Using only clickOutside to show tooltip
  • Relying solely on CSS without accessibility
  • Manually toggling tooltip without outside click detection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard