-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useAnimations): flag to enable sleep wake scenarios (#1435)
- Loading branch information
1 parent
53ba6fb
commit edc2faf
Showing
3 changed files
with
40 additions
and
16 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
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
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,29 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Syncs the wake event with the visibility change event. | ||
* @param onWake The function to call when the wake event is triggered. | ||
* @param enabled Whether the sync should be enabled. | ||
*/ | ||
export const useSleepWakeSync = (onWake: () => void, enabled: boolean) => { | ||
React.useEffect(() => { | ||
const handleVisibilityChange = () => { | ||
if (document.visibilityState === 'visible') { | ||
onWake(); | ||
} | ||
}; | ||
|
||
if (enabled) { | ||
document.addEventListener('visibilitychange', handleVisibilityChange); | ||
} | ||
|
||
return () => { | ||
if (enabled) { | ||
document.removeEventListener( | ||
'visibilitychange', | ||
handleVisibilityChange | ||
); | ||
} | ||
}; | ||
}, [onWake, enabled]); | ||
}; |