Using <code>svelte:window</code> to Track Window Resize
📖 Scenario: You are building a simple Svelte app that shows the current width of the browser window. This helps users see how wide their screen is in real time.
🎯 Goal: Create a Svelte component that listens to window resize events using svelte:window and updates the displayed window width dynamically.
📋 What You'll Learn
Create a variable width to store the window width
Use svelte:window with the on:resize event
Update width when the window resizes
Display the current width in the component
💡 Why This Matters
🌍 Real World
Tracking window size is common in responsive web design to adjust layouts or content dynamically.
💼 Career
Understanding how to handle window events in Svelte is useful for building interactive, accessible web apps that respond to user environment changes.
Progress0 / 4 steps
1
Set up the width variable
Create a variable called width and set it to window.innerWidth to get the initial window width.
Svelte
Hint
Use let width = window.innerWidth to get the current width of the browser window.
2
Add svelte:window with on:resize event
Add a <svelte:window on:resize={handleResize}></svelte:window> tag to listen for window resize events. Define a function called handleResize that updates width to window.innerWidth.
Svelte
Hint
Define handleResize to update width. Use <svelte:window on:resize={handleResize}> to listen for resize.
3
Display the current window width
Add a <p> tag that shows the text Window width: {width}px so users can see the current width.
Svelte
Hint
Use <p>Window width: {width}px</p> to show the width.
4
Make the component accessible and responsive
Add an aria-live="polite" attribute to the <p> tag so screen readers announce width changes politely.
Svelte
Hint
Add aria-live="polite" to the paragraph to improve accessibility.
Practice
(1/5)
1. What is the main purpose of using svelte:window in a Svelte component?
easy
A. To style the window element with CSS
B. To create a new browser window
C. To listen to events on the browser window like resize or scroll
D. To manage component state internally
Solution
Step 1: Understand svelte:window usage
svelte:window is a special element in Svelte used to listen to events on the browser window object.
Step 2: Identify the correct purpose
It does not create windows or style elements but listens to events like resize or scroll on the window.
Final Answer:
To listen to events on the browser window like resize or scroll -> Option C
Quick Check:
svelte:window listens to window events [OK]
Hint: Remember: svelte:window listens to window events only [OK]
Common Mistakes:
Thinking it creates or styles windows
Confusing with component state management
Using it to listen to DOM element events
2. Which of the following is the correct syntax to listen for the window's resize event using svelte:window?
easy
A. <div on:resize={handleResize}></div>
B. <svelte:window on:resize={handleResize} />
C. <window onresize={handleResize}></window>
D. <svelte:window bind:resize={handleResize} />
Solution
Step 1: Recall correct event binding syntax in Svelte
In Svelte, event listeners use on:eventName={handler} syntax.
Step 2: Apply to svelte:window
The correct way to listen to window resize is <svelte:window on:resize={handleResize} />.
Final Answer:
<svelte:window on:resize={handleResize} /> -> Option B
Quick Check:
Use on:event on svelte:window [OK]
Hint: Use on:event syntax on svelte:window [OK]
Common Mistakes:
Using HTML window tag which doesn't exist
Trying to bind event with bind: instead of on:
Attaching events to div instead of svelte:window
3. Given this Svelte code snippet, what will be the value of width after resizing the browser window?
<script>
let width = 0;
function updateWidth(event) {
width = event.currentTarget.innerWidth;
}
</script>
<svelte:window on:resize={updateWidth} />
<p>Window width: {width}</p>
medium
A. It updates to the current window width on every resize
B. It stays 0 because width is never updated
C. It throws an error because event.currentTarget is undefined
D. It updates only once when the component loads
Solution
Step 1: Understand the event handler logic
The updateWidth function sets width to event.currentTarget.innerWidth, which is the window's width.
Step 2: Recognize svelte:window event behavior
On every resize event, updateWidth runs, updating width reactively.
Final Answer:
It updates to the current window width on every resize -> Option A
Assuming width never changes without explicit initialization
Confusing event.currentTarget with event.target
Thinking event.currentTarget is undefined in this context
4. Identify the error in this Svelte code that tries to listen for the window scroll event:
<script>
let scrollY = 0;
function onScroll() {
scrollY = window.scrollY;
}
</script>
<svelte:window on:scroll={onScroll}></svelte:window>
<p>Scroll position: {scrollY}</p>
medium
A. Not binding scrollY with bind:scrollY
B. Missing event parameter in onScroll function
C. Accessing window.scrollY inside the handler is invalid
D. Using <svelte:window> with a closing tag instead of self-closing
Solution
Step 1: Check svelte:window tag usage
svelte:window is a special element that must be self-closing when used to listen to events.
Step 2: Identify the tag error
The code uses an opening and closing tag <svelte:window></svelte:window>, which is invalid syntax for this element.
Final Answer:
Using <svelte:window> with a closing tag instead of self-closing -> Option D
Quick Check:
svelte:window must be self-closing [OK]
Hint: Always self-close svelte:window when listening to events [OK]
Common Mistakes:
Trying to use opening and closing tags for svelte:window
Thinking bind: is needed for event values
Assuming window.scrollY is inaccessible in handler
5. You want to track both the window's width and height reactively using svelte:window. Which code snippet correctly updates both values on resize events?
hard
A. <script>
let width = 0, height = 0;
function updateSize() {
width = window.innerWidth;
height = window.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} />
B. <script>
let width = 0, height = 0;
function updateSize(event) {
width = event.currentTarget.innerWidth;
height = event.currentTarget.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} />
C. <script>
let width = 0, height = 0;
<svelte:window bind:innerWidth={width} bind:innerHeight={height} />
</script>
D. <script>
let width = 0, height = 0;
function updateSize(event) {
width = event.target.innerWidth;
height = event.target.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} />
Solution
Step 1: Understand event object properties
In window resize events, event.currentTarget and event.target may not reliably provide innerWidth and innerHeight. Using window.innerWidth and window.innerHeight is safer.
Step 2: Check each option
<script>
let width = 0, height = 0;
function updateSize(event) {
width = event.currentTarget.innerWidth;
height = event.currentTarget.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} /> uses event.currentTarget which may not always be the window. <script>
let width = 0, height = 0;
function updateSize() {
width = window.innerWidth;
height = window.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} /> uses window.innerWidth and window.innerHeight directly, which is correct. <script>
let width = 0, height = 0;
<svelte:window bind:innerWidth={width} bind:innerHeight={height} />
</script> tries to bind non-bindable properties. <script>
let width = 0, height = 0;
function updateSize(event) {
width = event.target.innerWidth;
height = event.target.innerHeight;
}
</script>
<svelte:window on:resize={updateSize} /> uses event.target which is unreliable.
Final Answer:
Use window.innerWidth and window.innerHeight inside the handler -> Option A
Quick Check:
Access window size via window.innerWidth in handler [OK]
Hint: Use window.innerWidth inside handler, not event properties [OK]
Common Mistakes:
Using event.currentTarget or event.target for window size
Trying to bind window properties directly
Assuming event object always has innerWidth/innerHeight
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.