-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useReducedMotion hook: guard the reference to window #17165
useReducedMotion hook: guard the reference to window #17165
Conversation
Fixes a bug where a naked reference to `window` will crash if used in a SSR context where there's no `window`.
Related: #16227 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsnajdr - can you think of a unit test which would prevent such issues in the future? I think by design, all unit tests use environment which simulates DOM with |
@@ -8,7 +8,9 @@ import useMediaQuery from '../use-media-query'; | |||
* | |||
* @type {boolean} | |||
*/ | |||
const IS_IE = window.navigator.userAgent.indexOf( 'Trident' ) >= 0; | |||
const IS_IE = | |||
typeof window !== 'undefined' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koke - this is another place where we could take advantage of some sort of platform detection. It would be useful not only for detecting native iOS or Android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case we could have a separate native implementation, since react native 0.60 exposes those APIs in AccessibilityInfo
I can't test this on 0.60 right now, but something like this should do it:
function useReducedMotion() {
const [ reducedMotion, setReducedMotion ] = useState( false );
useEffect( () => {
const updateReducedMotion = () => AccessibilityInfo.isReduceMotionEnabled().then( setReducedMotion );
updateReducedMotion();
AccessibilityInfo.addEventListener('reduceMotionChanged', updateReducedMotion);
return () => {
AccessibilityInfo.removeEventListener('reduceMotionChanged', updateReducedMotion);
};
} );
return reducedMotion;
}
In this case, the easiest TDD-style test would be to simply import Neither And the |
Filed new issue in #17273. |
Fixes a bug where a naked reference to
window
will crash if used in a SSR context where there's nowindow
.Discovered when trying to upgrade Calypso to the latest
@wordpress/compose
.