Skip to content

Commit 0c8cb44

Browse files
tanhauhauConduitry
authored andcommitted
fix window bindings to store (#3835)
Fixes #3832
1 parent b4c2226 commit 0c8cb44

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/compiler/compile/render_dom/wrappers/Window.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default class WindowWrapper extends Wrapper {
127127

128128
component.partly_hoisted.push(b`
129129
function ${id}() {
130-
${props.map(prop => b`$$invalidate('${prop.name}', ${prop.name} = @_window.${prop.value});`)}
130+
${props.map(prop => component.invalidate(prop.name, x`${prop.name} = @_window.${prop.value}`))}
131131
}
132132
`);
133133

@@ -167,7 +167,7 @@ export default class WindowWrapper extends Wrapper {
167167

168168
component.partly_hoisted.push(b`
169169
function ${id}() {
170-
$$invalidate('${name}', ${name} = @_navigator.onLine);
170+
${component.invalidate(name, x`${name} = @_navigator.onLine`)}
171171
}
172172
`);
173173

test/helpers.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ global.document = window.document;
5252
global.navigator = window.navigator;
5353
global.getComputedStyle = window.getComputedStyle;
5454
global.requestAnimationFrame = null; // placeholder, filled in using set_raf
55+
global.window = window;
5556

5657
// add missing ecmascript globals to window
5758
for (const key of Object.getOwnPropertyNames(global)) {

test/js/samples/window-binding-scroll/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function instance($$self, $$props, $$invalidate) {
6868
let { y } = $$props;
6969

7070
function onwindowscroll() {
71-
$$invalidate("y", y = window.pageYOffset);
71+
$$invalidate("y", y = window.pageYOffset)
7272
}
7373

7474
$$self.$set = $$props => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default {
2+
skip_if_ssr: true,
3+
4+
async test({ assert, component, target, window }) {
5+
assert.equal(window.pageYOffset, 0);
6+
7+
const event = new window.Event('scroll');
8+
Object.defineProperties(window, {
9+
pageYOffset: {
10+
value: 234,
11+
configurable: true,
12+
},
13+
});
14+
15+
await window.dispatchEvent(event);
16+
17+
assert.htmlEqual(
18+
target.innerHTML,
19+
`<p style="position: fixed; top: 1em; left: 1em;">scroll\ny\nis\n234.\n234\n*\n234\n=\n54756</p><div style="height: 9999px;"></div>`
20+
);
21+
},
22+
23+
after_test() {
24+
Object.defineProperties(window, {
25+
pageYOffset: {
26+
value: 0,
27+
configurable: true,
28+
},
29+
});
30+
},
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<script>
2+
import { writable, derived } from 'svelte/store';
3+
const y = writable(0);
4+
const y_squared = derived(y, $y => $y * $y);
5+
</script>
6+
7+
<svelte:window bind:scrollY={$y}/>
8+
9+
<p style="position: fixed; top: 1em; left: 1em;">
10+
scroll y is {$y}. {$y} * {$y} = {$y_squared}
11+
</p>
12+
13+
<div style="height: 9999px">
14+
15+
</div>

0 commit comments

Comments
 (0)