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

svelte:window bind:devicePixelRatio #8285

Closed
ion1 opened this issue Feb 15, 2023 · 1 comment · Fixed by #8534
Closed

svelte:window bind:devicePixelRatio #8285

ion1 opened this issue Feb 15, 2023 · 1 comment · Fixed by #8534

Comments

@ion1
Copy link

ion1 commented Feb 15, 2023

Describe the problem

window.devicePixelRatio can change, for example when the user zooms or drags the window to another display. I am currently using the following (based on boilerplate code from MDN) to track the changes:

import { readable } from "svelte/store";

const devicePixelRatio = readable<number | null>(
  typeof window !== "undefined" ? window.devicePixelRatio : null,
  function start(set) {
    if (typeof window === "undefined") return;

    let removeListener = () => {};

    function updatePixelRatio() {
      removeListener();

      const media = matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
      media.addEventListener("change", updatePixelRatio);
      removeListener = () => {
        media.removeEventListener("change", updatePixelRatio);
      };

      set(window.devicePixelRatio);
    }

    updatePixelRatio();

    return function stop() {
      removeListener();
    };
  }
);

It would be convenient if Svelte provided this functionality through svelte:window.

Describe the proposed solution

<svelte:window bind:devicePixelRatio={r}/>

Alternatives considered

The alternative is just to use the boilerplate code.

Importance

nice to have

@ion1 ion1 changed the title <svelte:window bind:devicePixelRatio={r}/> svelte:window bind:devicePixelRatio Feb 15, 2023
dummdidumm added a commit that referenced this issue Apr 27, 2023
closes: #8285

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

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
@Conduitry
Copy link
Member

This binding is now supported in 3.59.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants