1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 17:06:37 -04:00

fix prefers-reduced-motion detection

This commit is contained in:
2022-05-03 14:57:32 -04:00
parent bb4115a529
commit 2ea6495fd8
7 changed files with 57 additions and 117 deletions

View File

@ -1,22 +0,0 @@
import { useState, useEffect } from "react";
export const useLocalStorage = (key: string, allowNull = false) => {
const [value, setValue] = useState(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let currentValue: any;
try {
currentValue = window.localStorage.getItem(key);
} catch (error) {} // eslint-disable-line no-empty
return currentValue;
});
useEffect(() => {
// don't write null values to storage unless specified
if (value !== null || allowNull) {
window.localStorage?.setItem(key, value);
}
}, [key, value, allowNull]);
return [value, setValue];
};

View File

@ -1,31 +0,0 @@
// SSR-safe reduced motion hook:
// https://www.joshwcomeau.com/react/prefers-reduced-motion/#ssr-safety
import { useEffect, useState } from "react";
const QUERY = "(prefers-reduced-motion: no-preference)";
export const usePrefersReducedMotion = (): boolean => {
// default to no animations on server-side
const [prefersReducedMotion, setPrefersReducedMotion] = useState(true);
useEffect(() => {
// this can now be safely set for the first time on the client-side
setPrefersReducedMotion(!window.matchMedia(QUERY).matches);
// register a listener for changes
const listener = (event: MediaQueryListEvent) => {
setPrefersReducedMotion(!event.matches);
};
const mediaQueryList = window.matchMedia(QUERY);
mediaQueryList.addEventListener("change", listener);
// clean up the event listener
return () => {
mediaQueryList.removeEventListener("change", listener);
};
}, [setPrefersReducedMotion]);
return prefersReducedMotion;
};