Skip to content

Commit 9b3e57b

Browse files
Merge pull request #24 from vue-final/feature/lock-scroll
#23, Fixed Background shifts when modal is opened
2 parents a9380d5 + 6e5c12e commit 9b3e57b

9 files changed

+48
-17
lines changed

dist/VueFinalModal.esm.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.esm.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/VueFinalModal.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { bindPrototype, registComponent } from './PluginCore'
22
import { DUPLICATE_PLUGIN_COMPONENT, DUPLICATE_COMPONENT } from './utils/errors'
3+
import mobile from 'is-mobile'
34

45
const defaultOptions = {
56
componentName: 'VueFinalModal',
6-
key: '$vfm'
7+
key: '$vfm',
8+
isMobile: mobile()
79
}
810

911
const Plugin = () => ({

lib/PluginCore.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import VueFinalModal from './VueFinalModal.vue'
2+
import { setStyle, removeStyle } from './utils/dom.js'
23

3-
function createVfm() {
4+
function createVfm(options) {
45
let vfm
56

67
return function() {
78
vfm = {
9+
isScrollLocked: false,
810
openedModals: [],
911
modals: [],
1012
show(name) {
@@ -23,24 +25,44 @@ function createVfm() {
2325
if (modal !== undefined) {
2426
modal.$emit('input', show === undefined ? !modal.value : show)
2527
}
28+
},
29+
lockScroll() {
30+
if (this.isScrollLocked) return
31+
if (options.isMobile) {
32+
setStyle(document.body, 'overflow', 'hidden')
33+
} else {
34+
window.addEventListener('wheel', this.lockScrollListener, { passive: false })
35+
}
36+
this.isScrollLocked = true
37+
},
38+
unlockScroll() {
39+
if (options.isMobile) {
40+
removeStyle(document.body, 'overflow')
41+
} else {
42+
window.removeEventListener('wheel', this.lockScrollListener)
43+
}
44+
this.isScrollLocked = false
45+
},
46+
lockScrollListener(e) {
47+
e.preventDefault()
2648
}
2749
}
2850
return vfm
2951
}
3052
}
3153

32-
export function bindPrototype(Vue, { key }) {
33-
const vfm = createVfm()()
34-
Object.defineProperty(Vue.prototype, key, {
54+
export function bindPrototype(Vue, options) {
55+
const vfm = createVfm(options)()
56+
Object.defineProperty(Vue.prototype, options.key, {
3557
get() {
3658
return vfm
3759
}
3860
})
3961
}
4062

41-
export function registComponent(Vue, { componentName, key }) {
42-
Vue.component(componentName, {
63+
export function registComponent(Vue, options) {
64+
Vue.component(options.componentName, {
4365
...VueFinalModal,
44-
props: { ...VueFinalModal.props, $_key: { type: String, default: key } }
66+
props: { ...VueFinalModal.props, $_options: { type: Object, default: () => options } }
4567
})
4668
}

lib/VueFinalModal.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
<script>
5858
import FocusTrap from './utils/focusTrap.js'
59-
import { setStyle, removeStyle } from './utils/dom.js'
6059
6160
const TransitionState = {
6261
Enter: 'enter',
@@ -115,7 +114,7 @@ export default {
115114
}),
116115
computed: {
117116
api() {
118-
return this[this.$_key]
117+
return this[this.$_options.key]
119118
},
120119
isComponentReadyToBeDestroyed() {
121120
return (
@@ -231,7 +230,7 @@ export default {
231230
},
232231
handleLockScroll() {
233232
if (this.value) {
234-
this.lockScroll ? setStyle(document.body, 'overflow', 'hidden') : removeStyle(document.body, 'overflow')
233+
this.lockScroll ? this.api.lockScroll() : this.api.unlockScroll()
235234
}
236235
},
237236
getAttachElement() {
@@ -290,7 +289,7 @@ export default {
290289
this.modalStackIndex = null
291290
292291
if (this.api.openedModals.length === 0) {
293-
this.lockScroll && removeStyle(document.body, 'overflow')
292+
this.lockScroll && this.api.unlockScroll()
294293
}
295294
this.$emit('closed')
296295
},

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,8 @@
7070
"vue-modal",
7171
"vue-dialog",
7272
"vue-component"
73-
]
73+
],
74+
"dependencies": {
75+
"is-mobile": "^2.2.2"
76+
}
7477
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5040,6 +5040,11 @@ is-glob@^4.0.0, is-glob@^4.0.1, is-glob@~4.0.1:
50405040
dependencies:
50415041
is-extglob "^2.1.1"
50425042

5043+
is-mobile@^2.2.2:
5044+
version "2.2.2"
5045+
resolved "https://registry.yarnpkg.com/is-mobile/-/is-mobile-2.2.2.tgz#f6c9c5d50ee01254ce05e739bdd835f1ed4e9954"
5046+
integrity sha512-wW/SXnYJkTjs++tVK5b6kVITZpAZPtUrt9SF80vvxGiF/Oywal+COk1jlRkiVq15RFNEQKQY31TkV24/1T5cVg==
5047+
50435048
is-module@^1.0.0:
50445049
version "1.0.0"
50455050
resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz#3258fb69f78c14d5b815d664336b4cffb6441591"

0 commit comments

Comments
 (0)