Skip to content

Commit

Permalink
feat(next): update IsMobile to use Svelte's MediaQuery class (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wombosvideo authored Jan 19, 2025
1 parent ee58aa3 commit 6659ad9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
24 changes: 3 additions & 21 deletions sites/docs/src/lib/registry/default/hook/is-mobile.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import { untrack } from "svelte";
import { MediaQuery } from "svelte/reactivity";

const MOBILE_BREAKPOINT = 768;

export class IsMobile {
#current = $state<boolean>(false);

export class IsMobile extends MediaQuery {
constructor() {
$effect(() => {
return untrack(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
this.#current = window.innerWidth < MOBILE_BREAKPOINT;
};
mql.addEventListener("change", onChange);
onChange();
return () => {
mql.removeEventListener("change", onChange);
};
});
});
}

get current() {
return this.#current;
super(`max-width: ${MOBILE_BREAKPOINT - 1}px`);
}
}
24 changes: 3 additions & 21 deletions sites/docs/src/lib/registry/new-york/hook/is-mobile.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import { untrack } from "svelte";
import { MediaQuery } from "svelte/reactivity";

const MOBILE_BREAKPOINT = 768;

export class IsMobile {
#current = $state<boolean>(false);

export class IsMobile extends MediaQuery {
constructor() {
$effect(() => {
return untrack(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
this.#current = window.innerWidth < MOBILE_BREAKPOINT;
};
mql.addEventListener("change", onChange);
onChange();
return () => {
mql.removeEventListener("change", onChange);
};
});
});
}

get current() {
return this.#current;
super(`max-width: ${MOBILE_BREAKPOINT - 1}px`);
}
}

0 comments on commit 6659ad9

Please sign in to comment.