-
-
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.
[CastIt.Server.ClientApp] Minor fixes
- Loading branch information
Showing
6 changed files
with
129 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
27 changes: 27 additions & 0 deletions
27
CastIt.Server/ClientApp/src/hooks/use_page_visibility.hook.ts
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,27 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { isSupportedLocal, isVisible, vendorEvent } from '../utils/visibility'; | ||
|
||
const usePageVisibility = () => { | ||
const initiallyVisible = isVisible(); | ||
const [state, setState] = useState(initiallyVisible); | ||
|
||
useEffect(() => { | ||
if (isSupportedLocal) { | ||
const handler = () => { | ||
const currentlyVisible = isVisible(); | ||
setState(currentlyVisible); | ||
}; | ||
|
||
document.addEventListener(vendorEvent!.event, handler); | ||
|
||
return () => { | ||
document.removeEventListener(vendorEvent!.event, handler); | ||
}; | ||
} | ||
}, []); | ||
|
||
return state; | ||
}; | ||
|
||
export default usePageVisibility; |
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,61 @@ | ||
interface IVenderEvents { | ||
hidden: string; | ||
event: string; | ||
state: string; | ||
} | ||
|
||
const vendorEvents: IVenderEvents[] = [ | ||
{ | ||
hidden: 'hidden', | ||
event: 'visibilitychange', | ||
state: 'visibilityState', | ||
}, | ||
{ | ||
hidden: 'webkitHidden', | ||
event: 'webkitvisibilitychange', | ||
state: 'webkitVisibilityState', | ||
}, | ||
{ | ||
hidden: 'mozHidden', | ||
event: 'mozvisibilitychange', | ||
state: 'mozVisibilityState', | ||
}, | ||
{ | ||
hidden: 'msHidden', | ||
event: 'msvisibilitychange', | ||
state: 'msVisibilityState', | ||
}, | ||
{ | ||
hidden: 'oHidden', | ||
event: 'ovisibilitychange', | ||
state: 'oVisibilityState', | ||
}, | ||
]; | ||
|
||
const hasDocument = typeof document !== 'undefined'; | ||
export const isSupported = hasDocument && Boolean(document.addEventListener); | ||
|
||
export const vendorEvent = ((): IVenderEvents | null => { | ||
if (!isSupported) { | ||
return null; | ||
} | ||
for (let event of vendorEvents) { | ||
if (event.hidden in document) { | ||
return event; | ||
} | ||
} | ||
// otherwise it's not supported | ||
return null; | ||
})(); | ||
|
||
export const isSupportedLocal = isSupported && vendorEvent; | ||
|
||
export const isVisible = () => { | ||
if (!vendorEvent) { | ||
return true; | ||
} | ||
|
||
const hidden = vendorEvent.hidden as keyof Document; | ||
const currentlyVisible = !document[hidden]; | ||
return currentlyVisible; | ||
}; |