logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

body_scroll_lock.js (2600B)


  1. import * as bodyScrollLock from 'body-scroll-lock'
  2. let previousNavPaddingRight
  3. let previousAppBgWrapperRight
  4. const lockerEls = new Set([])
  5. const disableBodyScroll = (el) => {
  6. const scrollBarGap = window.innerWidth - document.documentElement.clientWidth
  7. bodyScrollLock.disableBodyScroll(el, {
  8. reserveScrollBarGap: true
  9. })
  10. lockerEls.add(el)
  11. setTimeout(() => {
  12. if (lockerEls.size <= 1) {
  13. // If previousNavPaddingRight is already set, don't set it again.
  14. if (previousNavPaddingRight === undefined) {
  15. const navEl = document.getElementById('nav')
  16. previousNavPaddingRight = window.getComputedStyle(navEl).getPropertyValue('padding-right')
  17. navEl.style.paddingRight = previousNavPaddingRight ? `calc(${previousNavPaddingRight} + ${scrollBarGap}px)` : `${scrollBarGap}px`
  18. }
  19. // If previousAppBgWrapeprRight is already set, don't set it again.
  20. if (previousAppBgWrapperRight === undefined) {
  21. const appBgWrapperEl = document.getElementById('app_bg_wrapper')
  22. previousAppBgWrapperRight = window.getComputedStyle(appBgWrapperEl).getPropertyValue('right')
  23. appBgWrapperEl.style.right = previousAppBgWrapperRight ? `calc(${previousAppBgWrapperRight} + ${scrollBarGap}px)` : `${scrollBarGap}px`
  24. }
  25. document.body.classList.add('scroll-locked')
  26. }
  27. })
  28. }
  29. const enableBodyScroll = (el) => {
  30. lockerEls.delete(el)
  31. setTimeout(() => {
  32. if (lockerEls.size === 0) {
  33. if (previousNavPaddingRight !== undefined) {
  34. document.getElementById('nav').style.paddingRight = previousNavPaddingRight
  35. // Restore previousNavPaddingRight to undefined so disableBodyScroll knows it can be set again.
  36. previousNavPaddingRight = undefined
  37. }
  38. if (previousAppBgWrapperRight !== undefined) {
  39. document.getElementById('app_bg_wrapper').style.right = previousAppBgWrapperRight
  40. // Restore previousAppBgWrapperRight to undefined so disableBodyScroll knows it can be set again.
  41. previousAppBgWrapperRight = undefined
  42. }
  43. document.body.classList.remove('scroll-locked')
  44. }
  45. })
  46. bodyScrollLock.enableBodyScroll(el)
  47. }
  48. const directive = {
  49. mounted: (el, binding) => {
  50. if (binding.value) {
  51. disableBodyScroll(el)
  52. }
  53. },
  54. updated: (el, binding) => {
  55. if (binding.oldValue === binding.value) {
  56. return
  57. }
  58. if (binding.value) {
  59. disableBodyScroll(el)
  60. } else {
  61. enableBodyScroll(el)
  62. }
  63. },
  64. unmounted: (el) => {
  65. enableBodyScroll(el)
  66. }
  67. }
  68. export default (Vue) => {
  69. Vue.directive('body-scroll-lock', directive)
  70. }