usePrefersReducedMotion

分类:

other

日期:

2022-3-02

标签:

chakra

usePrefersReducedMotion is a custom hook used to help detect the users motion preference.

Learn more about the API and its backgrounds.

Import#

import { usePrefersReducedMotion } from '@chakra-ui/react';

Return value#

The usePrefersReducedMotion hook returns a boolean, indicating whether the user prefers reduced motion.

Keep in mind this API relies on the users browser support of window.matchMedia and will always return false if it is not supported or does not exist (e.g. during serverside rendering).

Usage#

import { Image, keyframes, usePrefersReducedMotion } from '@chakra-ui/react';
import logo from './logo.svg';
const spin = keyframes`
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
`;
function Example() {
const prefersReducedMotion = usePrefersReducedMotion();
const animation = prefersReducedMotion
? undefined
: `${spin} infinite 20s linear`;
return <Image animation={animation} src={logo} {...props} />;
}
footer logo

© 2022 Designed with chakra ui. Powered by contentlayerjs and nextjs etc. All rights reserved

TwitterYouTubeGithub