fix: viewport height-related issue on mobile
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
import { disableIosTextFieldZoom } from '@/Utils'
|
import { disableIosTextFieldZoom, isDev } from '@/Utils'
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
@@ -42,22 +42,27 @@ const getKey = () => {
|
|||||||
return keyCount++
|
return keyCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
let initialCorrectViewportHeight: number | null = null
|
const ViewportHeightKey = '--viewport-height'
|
||||||
|
|
||||||
export const setViewportHeightWithFallback = (isOrientationChange = false) => {
|
export const setViewportHeightWithFallback = () => {
|
||||||
|
const currentHeight = parseInt(document.documentElement.style.getPropertyValue(ViewportHeightKey))
|
||||||
const newValue = visualViewport && visualViewport.height > 0 ? visualViewport.height : window.innerHeight
|
const newValue = visualViewport && visualViewport.height > 0 ? visualViewport.height : window.innerHeight
|
||||||
|
|
||||||
if (initialCorrectViewportHeight && newValue < initialCorrectViewportHeight && !isOrientationChange) {
|
if (isDev) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`currentHeight: ${currentHeight}, newValue: ${newValue}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentHeight && newValue < currentHeight) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!newValue) {
|
if (!newValue) {
|
||||||
document.documentElement.style.setProperty('--viewport-height', '100vh')
|
document.documentElement.style.setProperty(ViewportHeightKey, '100vh')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
initialCorrectViewportHeight = newValue
|
document.documentElement.style.setProperty(ViewportHeightKey, `${newValue}px`)
|
||||||
document.documentElement.style.setProperty('--viewport-height', `${newValue}px`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const setDefaultMonospaceFont = (platform?: Platform) => {
|
const setDefaultMonospaceFont = (platform?: Platform) => {
|
||||||
@@ -84,26 +89,18 @@ const startApplication: StartApplication = async function startApplication(
|
|||||||
device.environment === Environment.Desktop ||
|
device.environment === Environment.Desktop ||
|
||||||
(matchMedia(MediaQueryBreakpoints.md).matches && matchMedia(MediaQueryBreakpoints.pointerFine))
|
(matchMedia(MediaQueryBreakpoints.md).matches && matchMedia(MediaQueryBreakpoints.pointerFine))
|
||||||
|
|
||||||
const orientationChangeHandler = () => {
|
|
||||||
setViewportHeightWithFallback(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
const resizeHandler = () => {
|
|
||||||
setViewportHeightWithFallback(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
const setupViewportHeightListeners = () => {
|
const setupViewportHeightListeners = () => {
|
||||||
if (!isDesktop) {
|
if (!isDesktop) {
|
||||||
setViewportHeightWithFallback()
|
setViewportHeightWithFallback()
|
||||||
window.addEventListener('orientationchange', orientationChangeHandler)
|
window.addEventListener('orientationchange', setViewportHeightWithFallback)
|
||||||
window.addEventListener('resize', resizeHandler)
|
window.addEventListener('resize', setViewportHeightWithFallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeViewportHeightListeners = () => {
|
const removeViewportHeightListeners = () => {
|
||||||
if (!isDesktop) {
|
if (!isDesktop) {
|
||||||
window.removeEventListener('orientationchange', orientationChangeHandler)
|
window.removeEventListener('orientationchange', setViewportHeightWithFallback)
|
||||||
window.removeEventListener('resize', resizeHandler)
|
window.removeEventListener('resize', setViewportHeightWithFallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,8 +35,6 @@
|
|||||||
@media screen and (max-width: 768px) {
|
@media screen and (max-width: 768px) {
|
||||||
--sn-stylekit-font-size-editor: 1rem;
|
--sn-stylekit-font-size-editor: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
--viewport-height: 100vh;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
html {
|
html {
|
||||||
|
|||||||
Reference in New Issue
Block a user