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:
@ -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];
|
||||
};
|
@ -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;
|
||||
};
|
Reference in New Issue
Block a user