Make useIsDesktop Tailwind 4 compatible

This commit is contained in:
Sam Becker 2025-02-08 22:04:07 -06:00
parent 6eab91025b
commit a8a880e748

View File

@ -1,15 +1,18 @@
import { useLayoutEffect, useState } from 'react';
import { useEffect, useState } from 'react';
export default function useIsDesktop() {
const [isDesktop, setIsDesktop] = useState(false);
const [isDesktop, setIsDesktop] = useState<boolean>();
useLayoutEffect(() => {
useEffect(() => {
if (typeof window !== 'undefined') {
const mql = window.matchMedia('(min-width: var(--breakpoint-md))');
const breakpointMd = getComputedStyle(document.documentElement)
.getPropertyValue('--breakpoint-md');
const mql = window.matchMedia(`(min-width: ${breakpointMd})`);
setIsDesktop(mql.matches);
const eventHandler = (event: MediaQueryListEvent) => {
const eventHandler = (event: MediaQueryListEvent) =>
setIsDesktop(event.matches);
};
mql.addEventListener('change', eventHandler);
return () => mql.removeEventListener('change', eventHandler);
}