Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix window bindings to store #3835

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/compile/render_dom/wrappers/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default class WindowWrapper extends Wrapper {

component.partly_hoisted.push(b`
function ${id}() {
${props.map(prop => b`$$invalidate('${prop.name}', ${prop.name} = @_window.${prop.value});`)}
${props.map(prop => component.invalidate(prop.name, x`${prop.name} = @_window.${prop.value}`))}
}
`);

Expand Down Expand Up @@ -167,7 +167,7 @@ export default class WindowWrapper extends Wrapper {

component.partly_hoisted.push(b`
function ${id}() {
$$invalidate('${name}', ${name} = @_navigator.onLine);
${component.invalidate(name, x`${name} = @_navigator.onLine`)}
}
`);

Expand Down
1 change: 1 addition & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ global.document = window.document;
global.navigator = window.navigator;
global.getComputedStyle = window.getComputedStyle;
global.requestAnimationFrame = null; // placeholder, filled in using set_raf
global.window = window;

// add missing ecmascript globals to window
for (const key of Object.getOwnPropertyNames(global)) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/window-binding-scroll/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function instance($$self, $$props, $$invalidate) {
let { y } = $$props;

function onwindowscroll() {
$$invalidate("y", y = window.pageYOffset);
$$invalidate("y", y = window.pageYOffset)
}

$$self.$set = $$props => {
Expand Down
31 changes: 31 additions & 0 deletions test/runtime/samples/window-binding-scroll-store/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default {
skip_if_ssr: true,

async test({ assert, component, target, window }) {
assert.equal(window.pageYOffset, 0);

const event = new window.Event('scroll');
Object.defineProperties(window, {
pageYOffset: {
value: 234,
configurable: true,
},
});

await window.dispatchEvent(event);

assert.htmlEqual(
target.innerHTML,
`<p style="position: fixed; top: 1em; left: 1em;">scroll\ny\nis\n234.\n234\n*\n234\n=\n54756</p><div style="height: 9999px;"></div>`
);
},

after_test() {
Object.defineProperties(window, {
pageYOffset: {
value: 0,
configurable: true,
},
});
},
};
15 changes: 15 additions & 0 deletions test/runtime/samples/window-binding-scroll-store/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import { writable, derived } from 'svelte/store';
const y = writable(0);
const y_squared = derived(y, $y => $y * $y);
</script>

<svelte:window bind:scrollY={$y}/>

<p style="position: fixed; top: 1em; left: 1em;">
scroll y is {$y}. {$y} * {$y} = {$y_squared}
</p>

<div style="height: 9999px">

</div>