Skip to content

Commit

Permalink
fix: disable zoom on input focusing on ios (#1386)
Browse files Browse the repository at this point in the history
* fix: disable zoom when focusing input field on ios

* chore: better naming
  • Loading branch information
vardan-arm committed Aug 9, 2022
1 parent 1f3e741 commit d048800
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/web/src/javascripts/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

import { disableIosTextFieldZoom } from '@/Utils'

declare global {
interface Window {
dashboardUrl?: string
Expand All @@ -19,6 +21,7 @@ declare global {

application?: WebApplication
mainApplicationGroup?: ApplicationGroup
MSStream?: Record<string, unknown>
}
}

Expand Down Expand Up @@ -73,6 +76,8 @@ const startApplication: StartApplication = async function startApplication(
window.addEventListener('resize', setViewportHeight)
window.addEventListener('orientationchange', setViewportHeight)

disableIosTextFieldZoom()

root.render(
<ApplicationGroupView
key={getKey()}
Expand Down
31 changes: 31 additions & 0 deletions packages/web/src/javascripts/Utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Platform, platformFromString } from '@standardnotes/snjs'
import { IsDesktopPlatform, IsWebPlatform } from '@/Constants/Version'
import { EMAIL_REGEX } from '../Constants/Constants'

export { isMobile } from './IsMobile'

declare const process: {
Expand Down Expand Up @@ -170,3 +171,33 @@ export const openInNewTab = (url: string) => {
export const convertStringifiedBooleanToBoolean = (value: string) => {
return value !== 'false'
}

// https://stackoverflow.com/a/57527009/2504429
export const disableIosTextFieldZoom = () => {
const addMaximumScaleToMetaViewport = () => {
const viewportMetaElement = document.querySelector('meta[name=viewport]')

if (viewportMetaElement !== null) {
let content = viewportMetaElement.getAttribute('content')
if (!content) {
return
}
const maximumScaleRegex = /maximum-scale=[0-9.]+/g

if (maximumScaleRegex.test(content)) {
content = content.replace(maximumScaleRegex, 'maximum-scale=1.0')
} else {
content = [content, 'maximum-scale=1.0'].join(', ')
}

viewportMetaElement.setAttribute('content', content)
}
}

// https://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885#9039885
const checkIsIOS = () => /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream

if (checkIsIOS()) {
addMaximumScaleToMetaViewport()
}
}

0 comments on commit d048800

Please sign in to comment.