@carbon/ibm-products
Version:
Carbon for IBM Products
35 lines (33 loc) • 1.17 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { useIsomorphicEffect } from "./useIsomorphicEffect.js";
import { useState } from "react";
//#region src/global/js/hooks/usePrefersReducedMotion.js
/**
* Copyright IBM Corp. 2024, 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const usePrefersReducedMotion = () => {
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false);
useIsomorphicEffect(() => {
const mediaQueryList = window.matchMedia("(prefers-reduced-motion: no-preference)");
const { matches } = window.matchMedia("(prefers-reduced-motion: no-preference)");
setPrefersReducedMotion(!matches);
const listener = (event) => {
setPrefersReducedMotion(!event.matches);
};
mediaQueryList.addEventListener("change", listener);
return () => {
mediaQueryList.removeEventListener("change", listener);
};
}, []);
return prefersReducedMotion;
};
//#endregion
export { usePrefersReducedMotion };