From 9ea33a06b67537ee75d36ea920c5a5549549d2a0 Mon Sep 17 00:00:00 2001 From: NiallJP Date: Mon, 5 Jul 2021 14:14:57 +0100 Subject: [PATCH 1/4] fix crash on safari --- src/targets/web/index.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/targets/web/index.js b/src/targets/web/index.js index 97545af..2156cda 100644 --- a/src/targets/web/index.js +++ b/src/targets/web/index.js @@ -23,10 +23,22 @@ export function useReduceMotion() { setMatch(mq.matches); }; handleChange(); - mq.addEventListener('change', handleChange); - return () => { - mq.removeEventListener('change', handleChange); - }; - }, []); + // If only has depreciated api use that, otherwise use current api + // Safari 14 + has both, Safari <= 13 only has depreciated + // fixes https://github.com/infiniteluke/react-reduce-motion/issues/8 + const usesDepreciatedApi = mq.addListener && !mq.addEventListener; + if (usesDepreciatedApi) { + mq.addListener(handleChange); + } else { + mq.addEventListener('change', handleChange); + } + return () => { + if (usesDepreciatedApi) { + mq.removeListener(handleChange); + } else { + mq.addEventListener('change', handleChange); + } + }; + }, []); return matches; } From f9818abe71eb9a45ab41c4301dfaba167c5bc4f9 Mon Sep 17 00:00:00 2001 From: NiallJP Date: Mon, 5 Jul 2021 14:18:47 +0100 Subject: [PATCH 2/4] spacing is consistent --- src/targets/web/index.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/targets/web/index.js b/src/targets/web/index.js index 2156cda..6ff9313 100644 --- a/src/targets/web/index.js +++ b/src/targets/web/index.js @@ -24,21 +24,21 @@ export function useReduceMotion() { }; handleChange(); // If only has depreciated api use that, otherwise use current api - // Safari 14 + has both, Safari <= 13 only has depreciated - // fixes https://github.com/infiniteluke/react-reduce-motion/issues/8 + // Safari 14 + has both, Safari <= 13 only has depreciated + // fixes https://github.com/infiniteluke/react-reduce-motion/issues/8 const usesDepreciatedApi = mq.addListener && !mq.addEventListener; - if (usesDepreciatedApi) { - mq.addListener(handleChange); - } else { - mq.addEventListener('change', handleChange); - } - return () => { - if (usesDepreciatedApi) { - mq.removeListener(handleChange); - } else { - mq.addEventListener('change', handleChange); - } - }; - }, []); + if (usesDepreciatedApi) { + mq.addListener(handleChange); + } else { + mq.addEventListener('change', handleChange); + } + return () => { + if (usesDepreciatedApi) { + mq.removeListener(handleChange); + } else { + mq.addEventListener('change', handleChange); + } + }; + }, []); return matches; } From 4ec1d8016354aecde38b4879b1e85384e1d34702 Mon Sep 17 00:00:00 2001 From: NiallJP Date: Mon, 5 Jul 2021 14:46:16 +0100 Subject: [PATCH 3/4] fix typo (oops) --- src/targets/web/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/targets/web/index.js b/src/targets/web/index.js index 6ff9313..56d7a04 100644 --- a/src/targets/web/index.js +++ b/src/targets/web/index.js @@ -36,7 +36,7 @@ export function useReduceMotion() { if (usesDepreciatedApi) { mq.removeListener(handleChange); } else { - mq.addEventListener('change', handleChange); + mq.removeEventListener('change', handleChange); } }; }, []); From b3d55340b4c8a5335573c88bf377e27864b3e6d3 Mon Sep 17 00:00:00 2001 From: NiallJP Date: Fri, 9 Jul 2021 16:27:35 +0100 Subject: [PATCH 4/4] fix another typo (oops again) --- src/targets/web/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/targets/web/index.js b/src/targets/web/index.js index 56d7a04..1752969 100644 --- a/src/targets/web/index.js +++ b/src/targets/web/index.js @@ -23,17 +23,17 @@ export function useReduceMotion() { setMatch(mq.matches); }; handleChange(); - // If only has depreciated api use that, otherwise use current api - // Safari 14 + has both, Safari <= 13 only has depreciated + // If only has deprecated api use that, otherwise use current api + // Safari 14 + has both, Safari <= 13 only has deprecated // fixes https://github.com/infiniteluke/react-reduce-motion/issues/8 - const usesDepreciatedApi = mq.addListener && !mq.addEventListener; - if (usesDepreciatedApi) { + const usesDeprecatedApi = mq.addListener && !mq.addEventListener; + if (usesDeprecatedApi) { mq.addListener(handleChange); } else { mq.addEventListener('change', handleChange); } return () => { - if (usesDepreciatedApi) { + if (usesDeprecatedApi) { mq.removeListener(handleChange); } else { mq.removeEventListener('change', handleChange);