utils.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import clsx, { type ClassValue } from "clsx";
  2. export function cn(...inputs: ClassValue[]) {
  3. return clsx(inputs);
  4. }
  5. export const prefersReducedMotion = () =>
  6. typeof window !== "undefined" &&
  7. window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  8. /** WebKit proper: Safari, and every browser on iOS (Chrome there included). */
  9. export const isWebKit = () => {
  10. const ua = navigator.userAgent;
  11. return /AppleWebKit/.test(ua) && !/(Chrome|Chromium|Edg|OPR)\//.test(ua);
  12. };
  13. export const hasFinePointer = () =>
  14. typeof window !== "undefined" &&
  15. window.matchMedia("(hover: hover) and (pointer: fine)").matches;
  16. /** Run `cb` when the browser is idle (or soon, where idle callbacks aren't supported). */
  17. export function onIdle(cb: () => void, timeout = 2000) {
  18. if (typeof window.requestIdleCallback === "function") {
  19. const id = window.requestIdleCallback(cb, { timeout });
  20. return () => window.cancelIdleCallback(id);
  21. }
  22. const id = setTimeout(cb, Math.min(timeout, 700));
  23. return () => clearTimeout(id);
  24. }