From 601f4681adee6e4dca54cd862cf093a9fe1cc1d5 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 24 Dec 2022 15:18:23 -0600 Subject: [PATCH] Make client hoverable methods SSR compatible for NextJS --- src/utils/helpers.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 1fa978d..cb0b41e 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -2,8 +2,16 @@ export function classNames(...classes: (string | null | undefined)[]) { return classes.filter(Boolean).join(" "); } -const hoverableQuery = window.matchMedia('(hover: hover) and (pointer: fine)'); +const isClient = (): boolean => { + return typeof window !== "undefined"; +} + +const isServer = (): boolean => { + return !isClient(); +} + +const hoverableQuery: MediaQueryList | null = isClient() ? window.matchMedia('(hover: hover) and (pointer: fine)') : null; export function isHoverable() { - return hoverableQuery.matches; + return hoverableQuery?.matches; } \ No newline at end of file