Skip to content

Commit b8e16d0

Browse files
committed
customize options like cmd, name
1 parent a6603e4 commit b8e16d0

13 files changed

+103
-78
lines changed

.prettierrc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# .prettierrc or .prettierrc.yaml
2-
# printWidth: 80
2+
printWidth: 120
33
tabWidth: 2
44
semi: false
55
singleQuote: true

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.

example/src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import components from '@/components/index.js'
66

77
import VueFinalModal from 'vue-final-modal'
88

9-
Vue.use(VueFinalModal)
9+
Vue.use(VueFinalModal())
1010

1111
Object.keys(components).forEach(name => {
1212
Vue.component(name, components[name])

lib/Plugin.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { bindPrototype, registComponent } from './PluginCore'
2+
import { DUPLICATE_PLUGIN_COMPONENT, DUPLICATE_COMPONENT } from './utils/errors'
3+
4+
const defaultOptions = {
5+
name: 'VueFinalModal',
6+
cmd: '$vfm'
7+
}
8+
9+
const Plugin = () => ({
10+
install(Vue, options) {
11+
const _options = Object.assign({}, defaultOptions, options)
12+
const isDuplicateCmd = Vue.prototype[_options.cmd]
13+
const isDuplicateComponent = Vue.options.components[_options.name]
14+
15+
if (isDuplicateComponent) {
16+
console.warn(isDuplicateCmd ? DUPLICATE_PLUGIN_COMPONENT : DUPLICATE_COMPONENT)
17+
} else {
18+
if (!isDuplicateCmd) {
19+
bindPrototype(Vue, _options)
20+
}
21+
registComponent(Vue, _options)
22+
}
23+
}
24+
})
25+
26+
export default Plugin

lib/PluginCore.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import VueFinalModal from './VueFinalModal.vue'
2+
3+
function createVfm() {
4+
let vfm
5+
6+
return function() {
7+
vfm = {
8+
openedModals: [],
9+
modals: [],
10+
show(name) {
11+
this.toggle(name, true)
12+
},
13+
hide(name) {
14+
this.toggle(name, false)
15+
},
16+
hideAll() {
17+
this.openedModals.forEach(modal => {
18+
modal.$emit('input', false)
19+
})
20+
},
21+
toggle(name, show) {
22+
const modal = this.modals.find(modal => modal.name === name)
23+
if (modal !== undefined) {
24+
modal.$emit('input', show === undefined ? !modal.value : show)
25+
}
26+
}
27+
}
28+
return vfm
29+
}
30+
}
31+
32+
export function bindPrototype(Vue, { cmd }) {
33+
const vfm = createVfm()()
34+
Object.defineProperty(Vue.prototype, cmd, {
35+
get() {
36+
return vfm
37+
}
38+
})
39+
}
40+
41+
export function registComponent(Vue, { name, cmd }) {
42+
Vue.component(name, {
43+
...VueFinalModal,
44+
props: { ...VueFinalModal.props, cmd: { type: String, default: cmd } }
45+
})
46+
}

lib/VueFinalModal.vue

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
v-show="!ssr || visible"
55
:style="{ zIndex: calculateZIndex }"
66
class="vfm vfm--inset"
7-
:class="[
8-
attach === false ? 'vfm--fixed' : 'vfm--absolute',
9-
{ 'vfm--prevent-none': preventClick }
10-
]"
7+
:class="[attach === false ? 'vfm--fixed' : 'vfm--absolute', { 'vfm--prevent-none': preventClick }]"
118
>
129
<transition
1310
:name="overlayTransition"
@@ -56,8 +53,8 @@
5653
</template>
5754

5855
<script>
59-
import FocusTrap from './focusTrap.js'
60-
import { setStyle, removeStyle } from './dom.js'
56+
import FocusTrap from './utils/focusTrap.js'
57+
import { setStyle, removeStyle } from './utils/dom.js'
6158
6259
const TransitionState = {
6360
Enter: 'enter',
@@ -113,10 +110,12 @@ export default {
113110
modalTransitionState: null
114111
}),
115112
computed: {
113+
api() {
114+
return this[this.cmd]
115+
},
116116
isComponentReadyToBeDestroyed() {
117117
return (
118-
(this.hideOverlay ||
119-
this.overlayTransitionState === TransitionState.Leave) &&
118+
(this.hideOverlay || this.overlayTransitionState === TransitionState.Leave) &&
120119
this.modalTransitionState === TransitionState.Leave
121120
)
122121
},
@@ -153,7 +152,7 @@ export default {
153152
}
154153
},
155154
created() {
156-
this.$vfm.modals.push(this)
155+
this.api.modals.push(this)
157156
},
158157
mounted() {
159158
this.$focusTrap = new FocusTrap()
@@ -163,26 +162,26 @@ export default {
163162
this.close()
164163
this.$el.remove()
165164
166-
let index = this.$vfm.modals.findIndex(vm => vm === this)
167-
this.$vfm.modals.splice(index, 1)
165+
let index = this.api.modals.findIndex(vm => vm === this)
166+
this.api.modals.splice(index, 1)
168167
},
169168
methods: {
170169
mounted() {
171170
if (this.value) {
172171
let target = this.getAttachElement()
173172
if (target || this.attach === false) {
174173
this.attach !== false && target.appendChild(this.$el)
175-
let index = this.$vfm.openedModals.findIndex(vm => vm === this)
174+
let index = this.api.openedModals.findIndex(vm => vm === this)
176175
if (index !== -1) {
177176
// if this is already exist in modalStack, delete it
178-
this.$vfm.openedModals.splice(index, 1)
177+
this.api.openedModals.splice(index, 1)
179178
}
180-
this.$vfm.openedModals.push(this)
179+
this.api.openedModals.push(this)
181180
182-
this.modalStackIndex = this.$vfm.openedModals.length - 1
181+
this.modalStackIndex = this.api.openedModals.length - 1
183182
184183
this.handleLockScroll()
185-
this.$vfm.openedModals
184+
this.api.openedModals
186185
.filter(vm => vm !== this)
187186
.forEach((vm, index) => {
188187
if (vm.getAttachElement() === target) {
@@ -202,14 +201,14 @@ export default {
202201
}
203202
},
204203
close() {
205-
let index = this.$vfm.openedModals.findIndex(vm => vm === this)
204+
let index = this.api.openedModals.findIndex(vm => vm === this)
206205
if (index !== -1) {
207206
// remove this in modalStack
208-
this.$vfm.openedModals.splice(index, 1)
207+
this.api.openedModals.splice(index, 1)
209208
}
210-
if (this.$vfm.openedModals.length > 0) {
209+
if (this.api.openedModals.length > 0) {
211210
// If there are still nested modals opened
212-
const $_vm = this.$vfm.openedModals[this.$vfm.openedModals.length - 1]
211+
const $_vm = this.api.openedModals[this.api.openedModals.length - 1]
213212
$_vm.handleLockScroll()
214213
$_vm.focusTrap && $_vm.$focusTrap.firstElement().focus()
215214
!$_vm.hideOverlay && ($_vm.visibility.overlay = true)
@@ -226,9 +225,7 @@ export default {
226225
},
227226
handleLockScroll() {
228227
if (this.value) {
229-
this.lockScroll
230-
? setStyle(document.body, 'overflow', 'hidden')
231-
: removeStyle(document.body, 'overflow')
228+
this.lockScroll ? setStyle(document.body, 'overflow', 'hidden') : removeStyle(document.body, 'overflow')
232229
}
233230
},
234231
getAttachElement() {

lib/index.js

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,3 @@
1-
import VueFinalModal from './VueFinalModal.vue'
2-
3-
const Plugin = {
4-
install(Vue) {
5-
if (Vue.prototype.$vfm) {
6-
return
7-
}
8-
const createVfm = (function() {
9-
let vfm
10-
11-
return function() {
12-
if (!vfm) {
13-
vfm = {
14-
openedModals: [],
15-
modals: [],
16-
show(name) {
17-
this.toggle(name, true)
18-
},
19-
hide(name) {
20-
this.toggle(name, false)
21-
},
22-
hideAll() {
23-
this.openedModals.forEach(modal => {
24-
modal.$emit('input', false)
25-
})
26-
},
27-
toggle(name, show) {
28-
const modal = this.modals.find(modal => modal.name === name)
29-
if (modal !== undefined) {
30-
modal.$emit('input', show)
31-
}
32-
}
33-
}
34-
}
35-
return vfm
36-
}
37-
})()
38-
39-
const vfm = createVfm()
40-
41-
Object.defineProperty(Vue.prototype, '$vfm', {
42-
get() {
43-
return vfm
44-
}
45-
})
46-
47-
Vue.component('VueFinalModal', VueFinalModal)
48-
}
49-
}
1+
import Plugin from './Plugin'
502

513
export default Plugin

0 commit comments

Comments
 (0)