Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Modal/Slideover): emit close-prevented event #1207

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/6.overlays/1.modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Set the `transition` prop to `false` to disable it.

### Prevent close

Use the `prevent-close` prop to disable the outside click alongside the `esc` keyboard shortcut.
Use the `prevent-close` prop to disable the outside click alongside the `esc` keyboard shortcut. A `close-prevented` event will be emitted when the user tries to close the modal.

:component-example{component="modal-example-prevent-close"}

Expand Down
2 changes: 1 addition & 1 deletion docs/content/6.overlays/2.slideover.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Set the `transition` prop to `false` to disable it.

### Prevent close

Use the `prevent-close` prop to disable the outside click alongside the `esc` keyboard shortcut.
Use the `prevent-close` prop to disable the outside click alongside the `esc` keyboard shortcut. A `close-prevented` event will be emitted when the user tries to close the modal.

:component-example{component="slideover-example-prevent-close"}

Expand Down
10 changes: 8 additions & 2 deletions src/runtime/components/overlays/Modal.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<TransitionRoot :appear="appear" :show="isOpen" as="template">
<HDialog :class="ui.wrapper" v-bind="attrs" @close="(e) => !preventClose && close(e)">
<HDialog :class="ui.wrapper" v-bind="attrs" @close="close">
<TransitionChild v-if="overlay" as="template" :appear="appear" v-bind="ui.overlay.transition">
<div :class="[ui.overlay.base, ui.overlay.background]" />
</TransitionChild>
Expand Down Expand Up @@ -81,7 +81,7 @@ export default defineComponent({
default: () => ({})
}
},
emits: ['update:modelValue', 'close'],
emits: ['update:modelValue', 'close', 'close-prevented'],
setup (props, { emit }) {
const { ui, attrs } = useUI('modal', toRef(props, 'ui'), config, toRef(props, 'class'))

Expand All @@ -105,6 +105,12 @@ export default defineComponent({
})

function close (value: boolean) {
if (props.preventClose) {
emit('close-prevented')

return
}

isOpen.value = value

emit('close')
Expand Down
10 changes: 8 additions & 2 deletions src/runtime/components/overlays/Slideover.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<TransitionRoot as="template" :appear="appear" :show="isOpen">
<HDialog :class="[ui.wrapper, { 'justify-end': side === 'right' }]" v-bind="attrs" @close="(e) => !preventClose && close(e)">
<HDialog :class="[ui.wrapper, { 'justify-end': side === 'right' }]" v-bind="attrs" @close="close">
<TransitionChild v-if="overlay" as="template" :appear="appear" v-bind="ui.overlay.transition">
<div :class="[ui.overlay.base, ui.overlay.background]" />
</TransitionChild>
Expand Down Expand Up @@ -70,7 +70,7 @@ export default defineComponent({
default: () => ({})
}
},
emits: ['update:modelValue', 'close'],
emits: ['update:modelValue', 'close', 'close-prevented'],
setup (props, { emit }) {
const { ui, attrs } = useUI('slideover', toRef(props, 'ui'), config, toRef(props, 'class'))

Expand Down Expand Up @@ -98,6 +98,12 @@ export default defineComponent({
})

function close (value: boolean) {
if (props.preventClose) {
emit('close-prevented')

return
}

isOpen.value = value
emit('close')
}
Expand Down