utils.ts 809 B

12345678910111213141516171819202122232425
  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. export const hasFinePointer = () =>
  9. typeof window !== "undefined" &&
  10. window.matchMedia("(hover: hover) and (pointer: fine)").matches;
  11. /** Run `cb` when the browser is idle (or soon, where idle callbacks aren't supported). */
  12. export function onIdle(cb: () => void, timeout = 2000) {
  13. if (typeof window.requestIdleCallback === "function") {
  14. const id = window.requestIdleCallback(cb, { timeout });
  15. return () => window.cancelIdleCallback(id);
  16. }
  17. const id = setTimeout(cb, Math.min(timeout, 700));
  18. return () => clearTimeout(id);
  19. }