feat: add window bind devicePixelRatio support (#8534)

closes: #8285

add window bind devicePixelRatio support, change devicePixelRatio on window resize.

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/8550/head
xxkl1 2 years ago committed by GitHub
parent f064c39d5f
commit b7359c8361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1083,6 +1083,7 @@ export interface SvelteWindowAttributes extends HTMLAttributes<Window> {
readonly 'bind:innerHeight'?: Window['innerHeight'] | undefined | null; readonly 'bind:innerHeight'?: Window['innerHeight'] | undefined | null;
readonly 'bind:outerWidth'?: Window['outerWidth'] | undefined | null; readonly 'bind:outerWidth'?: Window['outerWidth'] | undefined | null;
readonly 'bind:outerHeight'?: Window['outerHeight'] | undefined | null; readonly 'bind:outerHeight'?: Window['outerHeight'] | undefined | null;
readonly 'bind:devicePixelRatio'?: Window['devicePixelRatio'] | undefined | null;
'bind:scrollX'?: Window['scrollX'] | undefined | null; 'bind:scrollX'?: Window['scrollX'] | undefined | null;
'bind:scrollY'?: Window['scrollY'] | undefined | null; 'bind:scrollY'?: Window['scrollY'] | undefined | null;
readonly 'bind:online'?: Window['navigator']['onLine'] | undefined | null; readonly 'bind:online'?: Window['navigator']['onLine'] | undefined | null;

@ -1742,6 +1742,7 @@ You can also bind to the following properties:
* `scrollX` * `scrollX`
* `scrollY` * `scrollY`
* `online` — an alias for `window.navigator.onLine` * `online` — an alias for `window.navigator.onLine`
* `devicePixelRatio`
All except `scrollX` and `scrollY` are readonly. All except `scrollX` and `scrollY` are readonly.

@ -17,6 +17,7 @@ const valid_bindings = [
'outerHeight', 'outerHeight',
'scrollX', 'scrollX',
'scrollY', 'scrollY',
'devicePixelRatio',
'online' 'online'
]; ];

@ -14,6 +14,7 @@ const associated_events = {
innerHeight: 'resize', innerHeight: 'resize',
outerWidth: 'resize', outerWidth: 'resize',
outerHeight: 'resize', outerHeight: 'resize',
devicePixelRatio: 'resize',
scrollX: 'scroll', scrollX: 'scroll',
scrollY: 'scroll' scrollY: 'scroll'
@ -29,6 +30,7 @@ const readonly = new Set([
'innerHeight', 'innerHeight',
'outerWidth', 'outerWidth',
'outerHeight', 'outerHeight',
'devicePixelRatio',
'online' 'online'
]); ]);

@ -1,5 +1,5 @@
export default { export default {
html: '<div>1024x768</div>', html: '<div>1024x768</div><div>1</div>',
before_test() { before_test() {
Object.defineProperties(window, { Object.defineProperties(window, {
@ -10,6 +10,10 @@ export default {
innerHeight: { innerHeight: {
value: 768, value: 768,
configurable: true configurable: true
},
devicePixelRatio: {
value: 1,
configurable: true
} }
}); });
}, },
@ -27,13 +31,17 @@ export default {
innerHeight: { innerHeight: {
value: 456, value: 456,
configurable: true configurable: true
},
devicePixelRatio: {
value: 2,
configurable: true
} }
}); });
await window.dispatchEvent(event); await window.dispatchEvent(event);
assert.htmlEqual(target.innerHTML, ` assert.htmlEqual(target.innerHTML, `
<div>567x456</div> <div>567x456</div><div>2</div>
`); `);
} }
}; };

@ -1,8 +1,10 @@
<script> <script>
export let width; export let width;
export let height; export let height;
export let devicePixelRatio;
</script> </script>
<svelte:window bind:innerWidth={width} bind:innerHeight={height}/> <svelte:window bind:innerWidth={width} bind:innerHeight={height} bind:devicePixelRatio={devicePixelRatio}/>
<div>{width}x{height}</div> <div>{width}x{height}</div>
<div>{devicePixelRatio}</div>

@ -1,6 +1,6 @@
[{ [{
"code": "invalid-binding", "code": "invalid-binding",
"message": "'potato' is not a valid binding on <svelte:window> — valid bindings are innerWidth, innerHeight, outerWidth, outerHeight, scrollX, scrollY or online", "message": "'potato' is not a valid binding on <svelte:window> — valid bindings are innerWidth, innerHeight, outerWidth, outerHeight, scrollX, scrollY, devicePixelRatio or online",
"start": { "start": {
"line": 1, "line": 1,
"column": 15 "column": 15

Loading…
Cancel
Save