mirror of
https://github.com/nicotsx/zerobyte.git
synced 2026-04-19 06:18:12 -04:00
22 lines
525 B
TypeScript
22 lines
525 B
TypeScript
import * as React from "react";
|
|
|
|
const MOBILE_BREAKPOINT = 768;
|
|
|
|
function subscribe(callback: () => void) {
|
|
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
mql.addEventListener("change", callback);
|
|
return () => mql.removeEventListener("change", callback);
|
|
}
|
|
|
|
function getSnapshot() {
|
|
return window.innerWidth < MOBILE_BREAKPOINT;
|
|
}
|
|
|
|
function getServerSnapshot() {
|
|
return false;
|
|
}
|
|
|
|
export function useIsMobile() {
|
|
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
}
|