Skip to content

Commit ab8ba97

Browse files
committed
add escToClose
1 parent 9a050db commit ab8ba97

File tree

11 files changed

+60
-20
lines changed

11 files changed

+60
-20
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,14 @@ toggle modal by name.
277277
lockScroll: { type: Boolean, default: true },
278278
hideOverlay: { type: Boolean, default: false },
279279
clickToClose: { type: Boolean, default: true },
280+
escToClose: { type: Boolean, default: false },
280281
preventClick: { type: Boolean, default: false },
281282
attach: { type: null, default: false, validator: validateAttachTarget },
282283
transition: { type: String, default: 'vfm' },
283284
overlayTransition: { type: String, default: 'vfm' },
284285
zIndexBase: { type: [String, Number], default: 1000 },
285286
zIndex: { type: [Boolean, String, Number], default: false },
287+
focusRetain: { type: Boolean, default: true },
286288
focusTrap: { type: Boolean, default: false }
287289
}
288290
```
@@ -313,4 +315,4 @@ toggle modal by name.
313315

314316
## Contribution
315317

316-
If you have any ideas for optimization of `vue-final-modal`, feel free to open [issues](https://github.com/hunterliu1003/vue-final-modal/issues) or [pull requests](https://github.com/hunterliu1003/vue-final-modal/pulls).
318+
👋 Hi I'm Hunter. Author of `vue-final-modal`. There is no perfect library even the `final` of vue modal. If you have any ideas for optimization of `vue-final-modal`, feel free to open [issues](https://github.com/hunterliu1003/vue-final-modal/issues) or [pull requests](https://github.com/hunterliu1003/vue-final-modal/pulls).

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.

docs/content/en/index.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Introduction
33
description: 'Vue Final Modal is a renderless, stackable, detachable and lightweight modal component.'
44
position: 0
55
category: Getting Start
6-
version: 0.18
6+
version: 0.19
77
features:
88
- Support Vue 3 and Vue 2
99
- Tailwind CSS friendly
@@ -390,7 +390,16 @@ Hide the display of the overlay.
390390
- Type: `Boolean`
391391
- Default: `true`
392392

393-
Clicking outside of modal content element will close the modal.
393+
Clicking overlay of modal to close the modal.
394+
395+
### `escToClose`
396+
397+
<badge>v0.19+</badge>
398+
399+
- Type: `Boolean`
400+
- Default: `false`
401+
402+
Press `esc` to close the modal.
394403

395404
### `preventClick`
396405

@@ -425,12 +434,21 @@ Calculate `z-index` automatically with zIndexBase. If zIndex is set, `zIndexBase
425434

426435
Set specific `z-index` to root of the modal element. If zIndex is set, `zIndexBase` will become invalid.
427436

437+
### `focusRemain`
438+
439+
<badge>v0.19+</badge>
440+
441+
- Type: `Boolean`
442+
- Default: `true`
443+
444+
Focus the modal `vfm__container` after the modal enter.
445+
428446
### `focusTrap`
429447

430448
- Type: `Boolean`
431449
- Default: `false`
432450

433-
Enables focus trap meaning that only inputs/buttons that are withing the modal window can be focused by pressing Tab (plugin uses very naive implementation of the focus trap)
451+
Enables focus trap meaning that only inputs/buttons that are withing the modal window can be focused by pressing Tab (plugin uses very naive implementation of the focus trap).
434452

435453
## **Events**
436454

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"@nuxt/content-theme-docs": "^0.6.1",
1313
"nuxt": "^2.14.1",
14-
"vue-final-modal": "^0.18.0"
14+
"vue-final-modal": "^0.18.2"
1515
},
1616
"devDependencies": {
1717
"@nuxtjs/google-analytics": "^2.4.0",

docs/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9899,10 +9899,10 @@ vue-client-only@^2.0.0:
98999899
resolved "https://registry.yarnpkg.com/vue-client-only/-/vue-client-only-2.0.0.tgz#ddad8d675ee02c761a14229f0e440e219de1da1c"
99009900
integrity sha512-arhk1wtWAfLsJyxGMoEYhoBowM87/i6HLSG2LH/03Yog6i2d9JEN1peMP0Ceis+/n9DxdenGYZZTxbPPJyHciA==
99019901

9902-
vue-final-modal@^0.18.0:
9903-
version "0.18.0"
9904-
resolved "https://registry.yarnpkg.com/vue-final-modal/-/vue-final-modal-0.18.0.tgz#8525133d68b9f7e63af38610d1ee5f59deae4646"
9905-
integrity sha512-yeso4SfPXg0wpLNc42YwZnbG0BTNfDLPgmWE8OIsFBHtRq/+gdd+vj1kJNefJbiv+1f6r0xe6Ac54C+DlGo/Eg==
9902+
vue-final-modal@^0.18.2:
9903+
version "0.18.2"
9904+
resolved "https://registry.yarnpkg.com/vue-final-modal/-/vue-final-modal-0.18.2.tgz#6a6d4e62c42c0fc71707ff0bc75e71d894c23d1f"
9905+
integrity sha512-emMSYD09rbs0YPBB/V/rsAowUoiCmGeQD31RkX+699oj1hL8BBWCRqPQTsoBi/Y+n9mysm6YoW4weXXipeepMQ==
99069906

99079907
vue-hot-reload-api@^2.3.0:
99089908
version "2.3.4"

example/src/components/basic/BasicOptions.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
<span>clickToClose:</span>
4242
<input v-model="clickToClose" type="checkbox" />
4343
</label>
44+
<label class="flex items-center space-x-2">
45+
<span>escToClose:</span>
46+
<input v-model="escToClose" type="checkbox" />
47+
</label>
4448
<label class="flex items-center space-x-2">
4549
<span>preventClick:</span>
4650
<input v-model="preventClick" type="checkbox" />
@@ -79,6 +83,10 @@
7983
<span>attach:</span>
8084
<input v-model="attach" type="checkbox" />
8185
</label>
86+
<label class="flex items-center space-x-2">
87+
<span>focusRetain:</span>
88+
<input v-model="focusRetain" type="checkbox" />
89+
</label>
8290
<label class="flex items-center space-x-2">
8391
<span>focusTrap:</span>
8492
<input v-model="focusTrap" type="checkbox" />
@@ -109,13 +117,15 @@ const initData = () => ({
109117
lockScroll: true,
110118
hideOverlay: false,
111119
clickToClose: true,
120+
escToClose: false,
112121
preventClick: false,
113122
transition: true,
114123
overlayTransition: true,
115124
zIndexBase: 1000,
116125
allowZIndex: false,
117126
zIndex: 0,
118127
attach: false,
128+
focusRetain: true,
119129
focusTrap: false
120130
})
121131
@@ -128,12 +138,14 @@ export default {
128138
lockScroll: this.lockScroll,
129139
hideOverlay: this.hideOverlay,
130140
clickToClose: this.clickToClose,
141+
escToClose: this.escToClose,
131142
preventClick: this.preventClick,
132143
transition: this.transition ? 'vfm' : '',
133144
overlayTransition: this.overlayTransition ? 'vfm' : '',
134145
zIndexBase: this.zIndexBase,
135146
...(this.allowZIndex && { zIndex: this.zIndex }),
136147
attach: this.attach ? '#attach' : false,
148+
focusRetain: this.focusRetain,
137149
focusTrap: this.focusTrap
138150
}
139151
}

lib/VueFinalModal.vue

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:style="{ zIndex: calculateZIndex }"
66
class="vfm vfm--inset"
77
:class="[attach === false ? 'vfm--fixed' : 'vfm--absolute', { 'vfm--prevent-none': preventClick }]"
8+
@keydown="onEsc"
89
>
910
<transition
1011
:name="overlayTransition"
@@ -36,6 +37,7 @@
3637
:aria-expanded="visibility.modal.toString()"
3738
role="dialog"
3839
aria-modal="true"
40+
tabindex="-1"
3941
@click="onClickContainer"
4042
>
4143
<div
@@ -91,12 +93,14 @@ export default {
9193
lockScroll: { type: Boolean, default: true },
9294
hideOverlay: { type: Boolean, default: false },
9395
clickToClose: { type: Boolean, default: true },
96+
escToClose: { type: Boolean, default: false },
9497
preventClick: { type: Boolean, default: false },
9598
attach: { type: null, default: false, validator: validateAttachTarget },
9699
transition: { type: String, default: 'vfm' },
97100
overlayTransition: { type: String, default: 'vfm' },
98101
zIndexBase: { type: [String, Number], default: 1000 },
99102
zIndex: { type: [Boolean, String, Number], default: false },
103+
focusRetain: { type: Boolean, default: true },
100104
focusTrap: { type: Boolean, default: false }
101105
},
102106
data: () => ({
@@ -210,7 +214,9 @@ export default {
210214
// If there are still nested modals opened
211215
const $_vm = this.api.openedModals[this.api.openedModals.length - 1]
212216
$_vm.handleLockScroll()
213-
$_vm.focusTrap && $_vm.$focusTrap.firstElement().focus()
217+
if ($_vm.focusRetain || $_vm.focusTrap) {
218+
$_vm.$refs.vfmContainer.focus()
219+
}
214220
!$_vm.hideOverlay && ($_vm.visibility.overlay = true)
215221
}
216222
this.startTransitionLeave()
@@ -263,6 +269,9 @@ export default {
263269
},
264270
afterModalEnter() {
265271
this.modalTransitionState = TransitionState.Enter
272+
if (this.focusRetain || this.focusTrap) {
273+
this.$refs.vfmContainer.focus()
274+
}
266275
if (this.focusTrap) {
267276
this.$focusTrap.enable(this.$refs.vfmContainer)
268277
}
@@ -288,6 +297,11 @@ export default {
288297
onClickContainer() {
289298
this.$emit('click-outside')
290299
this.clickToClose && this.$emit('input', false)
300+
},
301+
onEsc(evt) {
302+
if (evt.keyCode === 27 && this.visible && this.escToClose) {
303+
this.$emit('input', false)
304+
}
291305
}
292306
}
293307
}

0 commit comments

Comments
 (0)