Created
April 13, 2021 15:24
-
-
Save jickoo/333f39c1221d9333cb3006b09c1963e0 to your computer and use it in GitHub Desktop.
useOrientation #react #hooks
This file contains hidden or 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
function WindowAddEventListener(props: WindowAddEventListenerProps) { | |
const {handleFullScreen} = props; | |
const orientationType = useOrientationFullScreen(handleFullScreen); | |
useEffect(() => { | |
console.debug('orientation type', orientationType); | |
}, [orientationType]); | |
} | |
----- | |
// type def : "landscape-primary" | "landscape-secondary" | "portrait-primary" | "portrait-secondary" | |
const getOrientationType = (): ORIENTATION_TYPE => { | |
if (screen.orientation.type.startsWith("portrait")) { | |
return ORIENTATION_TYPE.PORTRAIT; | |
} else { | |
return ORIENTATION_TYPE.LANDSCAPE; | |
} | |
} | |
function useOrientationFullScreen(handleFullScreen: FullScreenHandle): ORIENTATION_TYPE { | |
const corePlayer = useRecoilValue(corePlayerState); | |
const [orientationType, setOrientationType] = useState<ORIENTATION_TYPE>(getOrientationType()); | |
const [isFullViewButtonActive, setIsFullViewButtonActive] = useRecoilState(FullViewButton.isActive); | |
useEffect(() => { | |
const handleOrientationChange = () => { | |
setOrientationType(getOrientationType()); | |
if (getOrientationType() === ORIENTATION_TYPE.LANDSCAPE) { | |
if (corePlayer.paused) { | |
return; | |
} | |
enterFullScreen(handleFullScreen) | |
.then(() => { | |
}) | |
.catch(e => { | |
console.error('enter error', e); | |
}); | |
} else { | |
exitFullScreen(handleFullScreen) | |
.then(() => { | |
}) | |
.catch(e => { | |
console.error('exit error', e); | |
}); | |
} | |
} | |
if (screen.orientation) { | |
screen.orientation.addEventListener('change', handleOrientationChange); | |
} | |
return () => { | |
if (screen.orientation) { | |
screen.orientation.removeEventListener('change', handleOrientationChange); | |
} | |
} | |
}, []); | |
return orientationType; | |
} | |
export {useOrientationFullScreen}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment