This commit is contained in:
jarek committed 2026-03-02 07:59:58 +01:00
1 parent 4b430340db
commit 9c451aedf9
120 files changed
+7777 -6046

No files matched your search

+17 -5
View File
@@ -5,6 +5,9 @@ const DEFAULT_MOBILE_BREAKPOINT = 768;
export class IsMobile {
#breakpoint: number;
#current = $state(false);
#handleResize: (() => void) | null = null;
#handleMediaChange: ((e: MediaQueryListEvent) => void) | null = null;
#mql: MediaQueryList | null = null;
constructor(breakpoint: number = DEFAULT_MOBILE_BREAKPOINT) {
this.#breakpoint = breakpoint;
@@ -14,22 +17,31 @@ export class IsMobile {
this.#current = window.innerWidth < this.#breakpoint;
// Listen for resize events
const handleResize = () => {
this.#handleResize = () => {
this.#current = window.innerWidth < this.#breakpoint;
};
window.addEventListener('resize', handleResize);
window.addEventListener('resize', this.#handleResize);
// Also use matchMedia for more reliable detection
const mql = window.matchMedia(`(max-width: ${this.#breakpoint - 1}px)`);
const handleMediaChange = (e: MediaQueryListEvent) => {
this.#mql = window.matchMedia(`(max-width: ${this.#breakpoint - 1}px)`);
this.#handleMediaChange = (e: MediaQueryListEvent) => {
this.#current = e.matches;
};
mql.addEventListener('change', handleMediaChange);
this.#mql.addEventListener('change', this.#handleMediaChange);
}
}
get current() {
return this.#current;
}
destroy() {
if (this.#handleResize) {
window.removeEventListener('resize', this.#handleResize);
}
if (this.#mql && this.#handleMediaChange) {
this.#mql.removeEventListener('change', this.#handleMediaChange);
}
}
}