Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Update useIsTouchDevice when the primary pointer changes

Rafi2f30c306ce4e
diff --git a/packages/ui/src/hooks/use-is-touch-device.ts b/packages/ui/src/hooks/use-is-touch-device.ts
index b9cbe14c0f..e5460cd7b4 100644
--- a/packages/ui/src/hooks/use-is-touch-device.ts
+++ b/packages/ui/src/hooks/use-is-touch-device.ts
@@ -1,11 +1,18 @@
import { useEffect, useState } from "react";
-// true when the primary pointer is a finger, for example on a phone or tablet
+// true when the primary pointer is a finger, for example on a phone or tablet,
+// and it updates when that changes, for example when a mouse is connected
export function useIsTouchDevice() {
const [isTouchDevice, setIsTouchDevice] = useState(false);
useEffect(() => {
- setIsTouchDevice(window.matchMedia("(pointer: coarse)").matches);
+ const mediaQuery = window.matchMedia("(pointer: coarse)");
+ const update = () => setIsTouchDevice(mediaQuery.matches);
+
+ update();
+ mediaQuery.addEventListener("change", update);
+
+ return () => mediaQuery.removeEventListener("change", update);
}, []);
return isTouchDevice;