diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 4339b58df..a45c9cc5c 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,3 +1,4 @@ +import { ScreenOrientationLocker } from '@/components/Global/ScreenOrientationLocker' import { TranslationSafeWrapper } from '@/components/Global/TranslationSafeWrapper' import { PeanutProvider } from '@/config' import { ContextProvider } from '@/context' @@ -61,6 +62,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }) + diff --git a/src/components/Global/ScreenOrientationLocker.tsx b/src/components/Global/ScreenOrientationLocker.tsx new file mode 100644 index 000000000..e200813fa --- /dev/null +++ b/src/components/Global/ScreenOrientationLocker.tsx @@ -0,0 +1,39 @@ +'use client' + +import { useEffect } from 'react' +import { captureException } from '@sentry/nextjs' + +export function ScreenOrientationLocker() { + useEffect(() => { + const lockOrientation = async () => { + if (screen.orientation && (screen.orientation as any).lock) { + try { + await (screen.orientation as any).lock('portrait-primary') + } catch (error) { + console.error('Failed to lock screen orientation:', error) + captureException(error) + } + } + } + + lockOrientation() + + const handleOrientationChange = () => { + // if the orientation is no longer portrait, try to lock it back. + if (screen.orientation && !screen.orientation.type.startsWith('portrait')) { + lockOrientation() + } + } + + // some browsers might not support addEventListener on screen.orientation + if (screen.orientation && screen.orientation.addEventListener) { + screen.orientation.addEventListener('change', handleOrientationChange) + + return () => { + screen.orientation.removeEventListener('change', handleOrientationChange) + } + } + }, []) + + return null +}