These patterns help make your app interactive and user-friendly. Click-outside closes menus when clicking outside them. Tooltip shows helpful info when hovering or focusing on elements.
Common action patterns (click-outside, tooltip) in Svelte
function clickOutside(node) { const handleClick = event => { if (!node.contains(event.target)) { node.dispatchEvent(new CustomEvent('outclick')); } }; document.addEventListener('click', handleClick, true); return { destroy() { document.removeEventListener('click', handleClick, true); } }; } function tooltip(node, text) { let tooltipDiv; function mouseOver() { if (tooltipDiv) { tooltipDiv.remove(); } tooltipDiv = document.createElement('div'); tooltipDiv.textContent = text; tooltipDiv.style.position = 'absolute'; tooltipDiv.style.background = 'black'; tooltipDiv.style.color = 'white'; tooltipDiv.style.padding = '0.25rem 0.5rem'; tooltipDiv.style.borderRadius = '0.25rem'; tooltipDiv.style.fontSize = '0.75rem'; tooltipDiv.style.zIndex = '1000'; document.body.appendChild(tooltipDiv); const rect = node.getBoundingClientRect(); tooltipDiv.style.top = `${rect.bottom + 5}px`; tooltipDiv.style.left = `${rect.left}px`; } function mouseOut() { if (tooltipDiv) { tooltipDiv.remove(); tooltipDiv = null; } } node.addEventListener('mouseover', mouseOver); node.addEventListener('mouseout', mouseOut); node.addEventListener('focus', mouseOver); node.addEventListener('blur', mouseOut); return { destroy() { if (tooltipDiv) { tooltipDiv.remove(); tooltipDiv = null; } node.removeEventListener('mouseover', mouseOver); node.removeEventListener('mouseout', mouseOut); node.removeEventListener('focus', mouseOver); node.removeEventListener('blur', mouseOut); } }; }
The clickOutside action listens for clicks outside the element and triggers a custom event.
The tooltip action creates a small popup near the element on hover or focus, improving accessibility.
clickOutside action.<script> import { clickOutside } from './actions.js'; let open = false; function toggle() { open = !open; } function close() { open = false; } </script> <button on:click={toggle}>Toggle Menu</button> {#if open} <div use:clickOutside on:outclick={close} tabindex="0"> <p>This menu closes when clicking outside.</p> </div> {/if}
<script> import { tooltip } from './actions.js'; </script> <button use:tooltip={'Click me for info'} aria-describedby="tip1">Hover or focus me</button>
This Svelte component shows a button with a tooltip. When clicked, it opens a menu. Clicking outside the menu closes it. The menu and tooltip improve user experience and accessibility.
<script> // Actions defined inside the component for simplicity function clickOutside(node) { const handleClick = event => { if (!node.contains(event.target)) { node.dispatchEvent(new CustomEvent('outclick')); } }; document.addEventListener('click', handleClick, true); return { destroy() { document.removeEventListener('click', handleClick, true); } }; } function tooltip(node, text) { let tooltipDiv; function mouseOver() { if (tooltipDiv) { tooltipDiv.remove(); } tooltipDiv = document.createElement('div'); tooltipDiv.textContent = text; tooltipDiv.style.position = 'absolute'; tooltipDiv.style.background = 'black'; tooltipDiv.style.color = 'white'; tooltipDiv.style.padding = '0.25rem 0.5rem'; tooltipDiv.style.borderRadius = '0.25rem'; tooltipDiv.style.fontSize = '0.75rem'; tooltipDiv.style.zIndex = '1000'; document.body.appendChild(tooltipDiv); const rect = node.getBoundingClientRect(); tooltipDiv.style.top = `${rect.bottom + 5}px`; tooltipDiv.style.left = `${rect.left}px`; } function mouseOut() { if (tooltipDiv) { tooltipDiv.remove(); tooltipDiv = null; } } node.addEventListener('mouseover', mouseOver); node.addEventListener('mouseout', mouseOut); node.addEventListener('focus', mouseOver); node.addEventListener('blur', mouseOut); return { destroy() { if (tooltipDiv) { tooltipDiv.remove(); tooltipDiv = null; } node.removeEventListener('mouseover', mouseOver); node.removeEventListener('mouseout', mouseOut); node.removeEventListener('focus', mouseOver); node.removeEventListener('blur', mouseOut); } }; } let menuOpen = false; function toggleMenu() { menuOpen = !menuOpen; } function closeMenu() { menuOpen = false; } </script> <button on:click={toggleMenu} use:tooltip={'Toggle menu'} aria-haspopup="true" aria-expanded={menuOpen}>Menu</button> {#if menuOpen} <div use:clickOutside on:outclick={closeMenu} tabindex="0" style="border: 1px solid #ccc; padding: 1rem; margin-top: 0.5rem; width: 200px; background: #f9f9f9;"> <p>This menu closes when clicking outside.</p> <button on:click={closeMenu}>Close</button> </div> {/if}
Use tabindex="0" on elements with clickOutside to make them focusable for keyboard users.
Tooltips should appear on keyboard focus as well as mouse hover for accessibility.
Remember to clean up event listeners in the destroy method to avoid memory leaks.
Click-outside helps close menus or popups when clicking outside them.
Tooltip shows helpful info on hover or focus, improving user experience.
Both patterns improve accessibility and interactivity in your Svelte apps.
Practice
click-outside action pattern in Svelte?Solution
Step 1: Understand the click-outside pattern
This pattern detects clicks outside a specific element to trigger an action, usually closing menus or popups.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.Final Answer:
To close a menu or popup when the user clicks outside of it -> Option CQuick Check:
click-outside closes menus on outside click = A [OK]
- Confusing click-outside with tooltip behavior
- Thinking it toggles element visibility on inside clicks
- Assuming it triggers API calls
clickOutside action in a Svelte component?Solution
Step 1: Recall Svelte action syntax
Svelte actions are used with theuse:directive followed by the action name and optional parameters.Step 2: Identify correct syntax
usesuse:clickOutside, which is the correct way to apply an action. Other options misuse event or binding syntax.Final Answer:
<div use:clickOutside={handleClickOutside}></div> -> Option DQuick Check:
Actions use 'use:' directive = C [OK]
- Using event syntax 'on:' instead of 'use:' for actions
- Trying to bind actions with 'bind:'
- Passing actions as plain attributes
<button use:tooltip={{ text: 'Save changes' }}>Save</button>What will happen when the user hovers over the button?
Solution
Step 1: Understand tooltip action usage
Thetooltipaction shows a small info box with the provided text when hovering or focusing the element.Step 2: Analyze the code behavior
The button usesuse:tooltipwith text 'Save changes', so hovering triggers the tooltip display near the button.Final Answer:
A tooltip with the text 'Save changes' appears near the button -> Option AQuick Check:
Tooltip shows text on hover = A [OK]
- Thinking tooltip changes button text
- Assuming tooltip disables the button
- Believing the syntax is invalid
<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?
Solution
Step 1: Check action usage requirements
Foruse:clickOutsideto work, the action must be imported or defined in the component.Step 2: Identify missing import or definition
The snippet does not show any import or definition ofclickOutside, so the action won't run, preventing menu close.Final Answer:
The clickOutside action is not imported or defined -> Option AQuick Check:
Missing action import = B [OK]
- Assuming variable reactivity is the issue
- Confusing function logic with action setup
- Ignoring the need to import or define actions
Solution
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.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.Final Answer:
Use a tooltip action on the button and a clickOutside action on the tooltip container to hide it -> Option BQuick Check:
Tooltip + clickOutside combo controls show/hide = D [OK]
- Using only clickOutside to show tooltip
- Relying solely on CSS without accessibility
- Manually toggling tooltip without outside click detection
Ready to prove it?
Pass the test and this pattern moves unknown
