Skip to content

Commit

Permalink
add fixTma and unfixTma methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Jun 17, 2024
1 parent eb15c71 commit 20fe2cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
56 changes: 56 additions & 0 deletions lib/helpers/fixTma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const options = { passive: false }
let ts: any
let scrollableEl: HTMLElement | null

function ignore(e: TouchEvent) {
e.preventDefault()
}

function onTouchStart(e: TouchEvent) {
ts = e.touches[0].clientY
}

function onTouchMove(e: TouchEvent) {
if (scrollableEl) {
const scroll = scrollableEl.scrollTop
const te = e.changedTouches[0].clientY
if (scroll <= 0 && ts < te) {
e.preventDefault()
}
} else {
e.preventDefault()
}
}

function onScroll() {
// @ts-ignore
if (window.scrollY !== window.overflowPadding) {
// @ts-ignore
window.scrollTo(0, window.overflowPadding)
}
}

export function fixTma(el: HTMLElement | null) {
scrollableEl = el

// @ts-ignore
window.overflowPadding = 100
// @ts-ignore
document.body.style.height = window.innerHeight + window.overflowPadding + 'px'
// @ts-ignore
window.scrollTo(0, window.overflowPadding)

window.addEventListener('scroll', onScroll)
document.documentElement.addEventListener('touchstart', onTouchStart, options)
document.documentElement.addEventListener('touchmove', onTouchMove, options)
document.body.addEventListener('touchcancel', ignore, options)
document.body.addEventListener('touchend', ignore, options)
}

export function unfixTma() {
window.removeEventListener('scroll', onScroll)
document.documentElement.removeEventListener('touchstart', onTouchStart)
document.documentElement.removeEventListener('touchmove', onTouchMove)
document.body.removeEventListener('touchcancel', ignore)
document.body.removeEventListener('touchend', ignore)
}
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './actions'
export { fixTma, unfixTma } from './helpers/fixTma'
export { createTelegramMiniApp } from './plugin'
export { state, updateState } from './state'
export { EventType } from './types'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kolirt/vue-telegram-mini-app",
"version": "0.0.3",
"version": "0.0.4",
"type": "module",
"description": "Vue 3 telegram mini app package",
"author": "kolirt",
Expand Down

0 comments on commit 20fe2cb

Please sign in to comment.