📖 Scenario: You are building a simple interactive box on a webpage. You want to change the box's background color when the user clicks on it. To do this, you will use a Svelte action that accepts a color parameter.
🎯 Goal: Create a Svelte component with a colored box. Use an action that changes the box's background color based on a parameter passed to the action.
📋 What You'll Learn
Create a Svelte action function named colorChange that accepts a parameter for the color.
Create a color variable with the value 'lightblue'.
Use the colorChange action on a <div> element, passing the color variable as a parameter.
The box should have a fixed size and show the background color from the action parameter.
💡 Why This Matters
🌍 Real World
Actions in Svelte let you add reusable behavior to HTML elements, like changing styles or handling events, which is common in interactive web apps.
💼 Career
Understanding how to use actions with parameters helps you build clean, modular, and accessible UI components in Svelte, a popular modern frontend framework.
Progress0 / 4 steps
1
Create the colorChange action function
Create a Svelte action function called colorChange that takes two parameters: node and color. Inside the function, set the node.style.backgroundColor to the color parameter. Return an object with a destroy method that resets the background color to an empty string.
Svelte
Hint
Remember, a Svelte action is a function that receives the DOM node and parameters. You can set styles directly on the node.
2
Create a color variable
Inside the <script> tag, create a variable called color and set it to the string 'lightblue'.
Svelte
Hint
Use let color = 'lightblue'; inside the script tag.
3
Use the colorChange action on a div
Add a <div> element below the <script> tag. Give it a fixed width and height of 150px using inline styles. Use the use:colorChange directive on the <div> and pass the color variable as the parameter.
Svelte
Hint
Use <div use:colorChange={color} style="width: 150px; height: 150px;"></div>.
4
Add a border and aria-label for accessibility
Add a border style of 2px solid black to the <div> element's inline styles. Also add an aria-label attribute with the value 'Color box' to the <div> for accessibility.
Svelte
Hint
Add border: 2px solid black; to the style and aria-label="Color box" to the div.
Practice
(1/5)
1. What is the purpose of passing parameters to a Svelte action using use:action={parameter}?
easy
A. To define a new component inside the action
B. To customize the behavior of the action with extra data
C. To import external libraries into the action
D. To style the element directly from the action
Solution
Step 1: Understand action parameters
Action parameters allow you to send extra data to a Svelte action to customize its behavior.
Step 2: Identify the purpose of use:action={parameter}
This syntax passes the parameter to the action so it can use it internally.
Final Answer:
To customize the behavior of the action with extra data -> Option B
Quick Check:
Action parameters customize behavior = A [OK]
Hint: Parameters customize actions with extra data [OK]
Common Mistakes:
Thinking parameters define components
Confusing parameters with styling
Assuming parameters import libraries
2. Which of the following is the correct syntax to pass a parameter named color with value 'red' to a Svelte action called highlight?
easy
A. use:highlight={{color: 'red'}}
B. use:highlight={'red'}
C. use:highlight={color} with let color = 'red'
D. use:highlight(color='red')
Solution
Step 1: Recall correct parameter passing syntax
Svelte actions receive parameters using use:action={parameter} where parameter can be any value or object.
Step 2: Identify correct object syntax for passing color
To pass an object with a key color and value 'red', use {color: 'red'} inside curly braces.
Final Answer:
use:highlight={{color: 'red'}} -> Option A
Quick Check:
Pass object with color key = D [OK]
Hint: Use curly braces with object for named parameters [OK]
Common Mistakes:
Using parentheses instead of curly braces
Passing string directly without braces
Passing variable without defining it
3. Given this Svelte action:
function border(node, { color }) {
node.style.border = `2px solid ${color}`;
return {
update({ color }) {
node.style.border = `2px solid ${color}`;
}
};
}
What will be the border color after this code runs?
<div use:border={{ color: 'blue' }}>Box</div>
medium
A. A 2px solid blue border
B. No border will appear
C. A 2px solid red border
D. A 2px solid black border
Solution
Step 1: Understand initial border style setting
The action sets the border style to '2px solid' plus the passed color, which is 'blue'.
Step 2: Confirm the border color after applying the action
The div will have a blue border because the parameter color is 'blue' and the style is applied immediately.
Final Answer:
A 2px solid blue border -> Option A
Quick Check:
Passed color 'blue' sets border color = B [OK]
Hint: Action sets style using passed color parameter [OK]
Which of the following will result in an invalid CSS transform value when used as use:scale={...}?
medium
A. use:scale={2}
B. use:scale={'2'}
C. use:scale={{ factor: 2 }}
D. use:scale={0.5}
Solution
Step 1: Analyze expected parameter type
The action expects a single number parameter factor, not an object.
Step 2: Identify which option passes an object instead of a number
use:scale={{ factor: 2 }} passes an object { factor: 2 }, which will cause scale(${newFactor}) to become scale([object Object]), an invalid CSS transform value.
Final Answer:
use:scale={{ factor: 2 }} -> Option C
Quick Check:
Passing object instead of number causes invalid transform = C [OK]
Hint: Match parameter type expected by action exactly [OK]
Common Mistakes:
Passing object when number expected
Passing string instead of number (allowed but may cause visual issues)
Passing objects when primitive expected
5. You want to create a Svelte action tooltip that shows a message passed as a parameter and updates the message if it changes. Which of these implementations correctly handles parameter updates?
hard
A.
function tooltip(node, message) {
const tip = document.createElement('div');
tip.textContent = message;
node.appendChild(tip);
}
B.
function tooltip(node, message) {
node.title = message;
return {};
}
C.
function tooltip(node, message) {
node.setAttribute('tooltip', message);
}