-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from barbaraperic/pr/slider
STYLE: reduced slider indicators and added a hook for reduced motion preference
- Loading branch information
Showing
2 changed files
with
102 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useState, useEffect } from "react"; | ||
|
||
const QUERY = "(prefers-reduced-motion: no-preference)"; | ||
|
||
export default function usePrefersReducedMotion() { | ||
// Default to prefers animations, since we don't know what the | ||
// user's preference is on the server. | ||
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false); | ||
useEffect(() => { | ||
const mediaQueryList = window.matchMedia(QUERY); | ||
|
||
// Set the true initial value, now that we're on the client: | ||
setPrefersReducedMotion(!window.matchMedia(QUERY).matches); | ||
|
||
// Register our event listener | ||
const listener = (event) => { | ||
setPrefersReducedMotion(!event.matches); | ||
}; | ||
|
||
mediaQueryList.addEventListener("change", listener); | ||
|
||
return () => { | ||
mediaQueryList.removeEventListener("change", listener); | ||
}; | ||
}, []); | ||
|
||
return prefersReducedMotion; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters