Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add useIsChromium hook

Richie McIlroy8edb619b9081
diff --git a/apps/web/hooks/useIsChromium.ts b/apps/web/hooks/useIsChromium.ts
new file mode 100644
index 000000000..dde226242
--- /dev/null
+++ b/apps/web/hooks/useIsChromium.ts
@@ -0,0 +1,34 @@
+import { useEffect, useState } from "react";
+
+/**
+ * Detects whether the current browser is Chromium-based (Chrome, Edge, Brave,
+ * Arc, Opera, Vivaldi, …), i.e. one that can install the Cap Chrome extension.
+ *
+ * Defaults to `true` during SSR and on the first client render — Chromium is
+ * the overwhelming majority, so this avoids a layout flash for most visitors
+ * while still resolving the real value after mount (non-Chromium browsers see a
+ * single reflow). Detection runs in an effect, so there's no hydration mismatch.
+ */
+export const useIsChromium = (): boolean => {
+ const [isChromium, setIsChromium] = useState(true);
+
+ useEffect(() => {
+ if (typeof navigator === "undefined") return;
+
+ const uaData = (navigator as any).userAgentData;
+ if (Array.isArray(uaData?.brands)) {
+ setIsChromium(
+ uaData.brands.some((b: { brand: string }) => /chromium/i.test(b.brand)),
+ );
+ return;
+ }
+
+ // Fallback for browsers without userAgentData: every Chromium-based
+ // engine reports "Chrome" in its UA, while Safari and Firefox do not.
+ // Exclude CriOS (Chrome on iOS) since extensions aren't supported there.
+ const ua = navigator.userAgent;
+ setIsChromium(/chrome|chromium/i.test(ua) && !/crios/i.test(ua));
+ }, []);
+
+ return isChromium;
+};