fix resizing bugs (conflicting handlers)

This commit is contained in:
Michael Freno
2025-12-21 19:16:20 -05:00
parent 2a852f43b2
commit c6ff41b0cf
6 changed files with 150 additions and 110 deletions

57
src/lib/resize-utils.ts Normal file
View File

@@ -0,0 +1,57 @@
import { createSignal, onMount, onCleanup, Accessor } from "solid-js";
export const MOBILE_BREAKPOINT = 768;
/**
* Creates a reactive window width signal that updates on resize
* @param debounceMs Optional debounce delay in milliseconds
* @returns Accessor for current window width
*/
export function createWindowWidth(debounceMs?: number): Accessor<number> {
const initialWidth = typeof window !== "undefined" ? window.innerWidth : 1024;
const [width, setWidth] = createSignal(initialWidth);
onMount(() => {
let timeoutId: ReturnType<typeof setTimeout> | undefined;
const handleResize = () => {
if (debounceMs) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
setWidth(window.innerWidth);
}, debounceMs);
} else {
setWidth(window.innerWidth);
}
};
window.addEventListener("resize", handleResize);
onCleanup(() => {
clearTimeout(timeoutId);
window.removeEventListener("resize", handleResize);
});
});
return width;
}
/**
* Checks if the current window width is in mobile viewport
* @param width Current window width
* @returns true if mobile viewport
*/
export function isMobile(width: number): boolean {
return width < MOBILE_BREAKPOINT;
}
/**
* Creates a derived signal for mobile state
* @param windowWidth Window width accessor
* @returns Accessor for mobile state
*/
export function createIsMobile(
windowWidth: Accessor<number>
): Accessor<boolean> {
return () => isMobile(windowWidth());
}