0
0
Svelteframework~20 mins

Common action patterns (click-outside, tooltip) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Action Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you click outside a Svelte component using a click-outside action?

Consider a Svelte component that uses a clickOutside action to close a dropdown menu when clicking outside it.

What is the expected behavior when the user clicks outside the dropdown?

Svelte
<script>
  import { clickOutside } from './actions.js';
  let open = true;
  function close() {
    open = false;
  }
</script>

<div use:clickOutside={close}>
  {#if open}
    <p>Dropdown content</p>
  {/if}
</div>
AThe dropdown toggles open and closed every time you click anywhere on the page.
BThe dropdown closes immediately when clicking anywhere outside the div using the action.
CThe dropdown closes only when clicking inside the div, not outside.
DThe dropdown stays open even when clicking outside because the action only listens inside the div.
Attempts:
2 left
💡 Hint

Think about what a click-outside action is designed to detect.

📝 Syntax
intermediate
1:30remaining
Which Svelte action syntax correctly applies a tooltip with a message?

You want to add a tooltip action that shows a message when hovering over a button.

Which syntax correctly passes the message 'Hello!' to the tooltip action?

A<button use:tooltip={'Hello!'}>Hover me</button>
B<button use:tooltip="Hello!">Hover me</button>
C<button use:tooltip(Hello!)>Hover me</button>
D<button use:tooltip={Hello!}>Hover me</button>
Attempts:
2 left
💡 Hint

Remember how to pass string parameters to Svelte actions.

🔧 Debug
advanced
2:30remaining
Why does this click-outside action not close the menu when clicking outside?

Examine the following Svelte action code for click-outside. Why does the menu not close when clicking outside?

Svelte
export function clickOutside(node, callback) {
  const handleClick = event => {
    if (!node.contains(event.target)) {
      callback();
    }
  };
  document.addEventListener('click', handleClick);
  return {
    destroy() {
      document.removeEventListener('click', handleClick);
    }
  };
}

<script>
  import { clickOutside } from './actions.js';
  let open = true;
  function close() {
    open = false;
  }
</script>

<div use:clickOutside={close}>
  {#if open}
    <p>Menu content</p>
  {/if}
</div>
AThe callback is called but the component does not update because the callback is not reactive.
BThe event listener is never added because the action is missing the 'node' parameter.
CThe callback is never called because the event listener is attached to the wrong element.
DThe callback is called but the menu stays open because the 'open' variable is not updated inside the callback.
Attempts:
2 left
💡 Hint

Consider how Svelte reactivity works with functions called outside component scope.

state_output
advanced
2:00remaining
What is the tooltip text shown after hovering over the button twice?

Given this Svelte component with a tooltip action that changes text on hover, what tooltip text is shown after the second hover?

Svelte
<script>
  import { tooltip } from './actions.js';
  let message = 'First';
  function update() {
    message = 'Second';
  }
</script>

<button use:tooltip={message} on:mouseenter={update}>Hover me</button>
AThe tooltip shows 'First' on the first hover and 'Second' on the second hover.
BThe tooltip shows 'Second' on both hovers.
CThe tooltip shows 'First' on both hovers.
DThe tooltip shows 'Second' on the first hover and 'First' on the second hover.
Attempts:
2 left
💡 Hint

Think about when the action receives updated parameters.

🧠 Conceptual
expert
3:00remaining
Why is it important to remove event listeners in Svelte actions like click-outside?

In a Svelte action that adds a global click event listener to detect clicks outside an element, why must you remove the listener when the element is destroyed?

ABecause event listeners cannot be removed once added, so this is a trick question.
BBecause Svelte automatically removes all event listeners, so manual removal is redundant.
CTo make the action run faster by disabling the listener temporarily.
DTo prevent memory leaks and avoid multiple listeners stacking up causing unexpected behavior.
Attempts:
2 left
💡 Hint

Think about what happens if event listeners accumulate over time.