Skip to content

Commit 745b2de

Browse files
committed
add escToClose
2 parents b032f9a + 1c8c08f commit 745b2de

File tree

11 files changed

+62
-19
lines changed

11 files changed

+62
-19
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:style="{ zIndex: calculateZIndex }"
77
class="vfm vfm--inset"
88
:class="[attach === false ? 'vfm--fixed' : 'vfm--absolute', { 'vfm--prevent-none': preventClick }]"
9+
@keydown="onEsc"
910
>
1011
<transition
1112
:name="overlayTransition"
@@ -37,6 +38,7 @@
3738
:aria-expanded="visibility.modal.toString()"
3839
role="dialog"
3940
aria-modal="true"
41+
tabindex="-1"
4042
@click="onClickContainer"
4143
>
4244
<div
@@ -80,6 +82,7 @@ export default {
8082
lockScroll: { type: Boolean, default: true },
8183
hideOverlay: { type: Boolean, default: false },
8284
clickToClose: { type: Boolean, default: true },
85+
escToClose: { type: Boolean, default: false },
8386
preventClick: { type: Boolean, default: false },
8487
attach: {
8588
type: null,
@@ -96,6 +99,7 @@ export default {
9699
overlayTransition: { type: String, default: 'vfm' },
97100
zIndexBase: { type: [String, Number], default: 1000 },
98101
zIndex: { type: [Boolean, String, Number], default: false },
102+
focusRetain: { type: Boolean, default: true },
99103
focusTrap: { type: Boolean, default: false }
100104
},
101105
emits: ['update:modelValue', 'click-outside', 'before-open', 'opened', 'before-close', 'closed']
@@ -186,12 +190,14 @@ function getModalInfo() {
186190
uid,
187191
name: props.name,
188192
emit,
193+
vfmContainer,
189194
vfmContent,
190195
getAttachElement,
191196
modalStackIndex,
192197
visibility,
193198
handleLockScroll,
194199
hideOverlay: props.hideOverlay,
200+
focusRetain: props.focusRetain,
195201
focusTrap: props.focusTrap,
196202
$focusTrap
197203
}
@@ -242,6 +248,9 @@ function close() {
242248
const $_vm = $vfm.openedModals[$vfm.openedModals.length - 1]
243249
$_vm.handleLockScroll()
244250
$_vm.focusTrap && $_vm.$focusTrap.firstElement().focus()
251+
if ($_vm.focusRetain || $_vm.focusTrap) {
252+
$_vm.vfmContainer.value.focus()
253+
}
245254
!$_vm.hideOverlay && ($_vm.visibility.overlay = true)
246255
}
247256
startTransitionLeave()
@@ -295,6 +304,9 @@ export function beforeModalEnter() {
295304
}
296305
export function afterModalEnter() {
297306
modalTransitionState.value = TransitionState.Enter
307+
if (props.focusRetain || props.focusTrap) {
308+
vfmContainer.value.focus()
309+
}
298310
if (props.focusTrap) {
299311
$focusTrap.enable(vfmContainer.value)
300312
}
@@ -320,6 +332,11 @@ export function onClickContainer() {
320332
emit('click-outside')
321333
props.clickToClose && emit('update:modelValue', false)
322334
}
335+
export function onEsc(evt) {
336+
if (evt.keyCode === 27 && visible.value && props.escToClose) {
337+
emit('update:modelValue', false)
338+
}
339+
}
323340
</script>
324341

325342
<style lang="css" scoped>

0 commit comments

Comments
 (0)