Skip to content

Commit

Permalink
fix: add fallback for viewport height on mobile (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Oct 7, 2022
1 parent 8de6023 commit 1e3acd5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 8 additions & 7 deletions packages/web/src/javascripts/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ const getKey = () => {
return keyCount++
}

const setViewportHeight = () => {
export const setViewportHeightWithFallback = () => {
const currentValue = parseInt(document.documentElement.style.getPropertyValue('--viewport-height'))
const newValue = visualViewport && visualViewport.height > 0 ? visualViewport.height : window.innerHeight
const supportsSVHUnit = CSS.supports('height', '1svh')

if (currentValue && !newValue) {
return
}

if (!currentValue && !newValue) {
document.documentElement.style.setProperty('--viewport-height', '100vh')
document.documentElement.style.setProperty('--viewport-height', supportsSVHUnit ? '100svh' : '100vh')
return
}

Expand Down Expand Up @@ -79,9 +80,9 @@ const startApplication: StartApplication = async function startApplication(

const onDestroy = () => {
if (device.environment === Environment.Desktop) {
window.removeEventListener('resize', setViewportHeight)
window.removeEventListener('resize', setViewportHeightWithFallback)
}
window.removeEventListener('orientationchange', setViewportHeight)
window.removeEventListener('orientationchange', setViewportHeightWithFallback)
const rootElement = document.getElementById(ElementIds.RootId) as HTMLElement
root.unmount()
rootElement.remove()
Expand All @@ -96,10 +97,10 @@ const startApplication: StartApplication = async function startApplication(

disableIosTextFieldZoom()

setViewportHeight()
window.addEventListener('orientationchange', setViewportHeight)
setViewportHeightWithFallback()
window.addEventListener('orientationchange', setViewportHeightWithFallback)
if (device.environment === Environment.Desktop) {
window.addEventListener('resize', setViewportHeight)
window.addEventListener('resize', setViewportHeightWithFallback)
}

setDefaultMonospaceFont(device.platform)
Expand Down
8 changes: 6 additions & 2 deletions packages/web/src/javascripts/Application/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ArchiveManager, AutolockService, IOService, WebAlertService, ThemeManag
import { MobileWebReceiver } from './MobileWebReceiver'
import { AndroidBackHandler } from '@/NativeMobileWeb/AndroidBackHandler'
import { PrefDefaults } from '@/Constants/PrefDefaults'
import { setViewportHeightWithFallback } from '@/App'

type WebServices = {
viewControllerManager: ViewControllerManager
Expand Down Expand Up @@ -233,8 +234,9 @@ export class WebApplication extends SNApplication implements WebApplicationInter
await this.lockApplicationAfterMobileEventIfApplicable()
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
async handleMobileGainingFocusEvent(): Promise<void> {}
async handleMobileGainingFocusEvent(): Promise<void> {
setViewportHeightWithFallback()
}

async handleMobileLosingFocusEvent(): Promise<void> {
if (this.protections.getMobileScreenshotPrivacyEnabled()) {
Expand All @@ -248,6 +250,8 @@ export class WebApplication extends SNApplication implements WebApplicationInter
if (this.protections.getMobileScreenshotPrivacyEnabled()) {
this.mobileDevice().hideMobileInterfaceFromScreenshots()
}

setViewportHeightWithFallback()
}

private async lockApplicationAfterMobileEventIfApplicable(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/stylesheets/_main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
@media screen and (max-width: 768px) {
--sn-stylekit-font-size-editor: 1rem;
}

--viewport-height: 100vh;
}

html {
Expand Down

0 comments on commit 1e3acd5

Please sign in to comment.