Skip to content

feat(Modal/Slideover): add close method in slots #4219

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

Merged
merged 3 commits into from
May 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const open = ref(false)
<Placeholder class="h-48" />
</template>

<template #footer>
<UButton label="Cancel" color="neutral" variant="outline" @click="open = false" />
<template #footer="{ close }">
<UButton label="Cancel" color="neutral" variant="outline" @click="close" />
<UButton label="Submit" color="neutral" />
</template>
</UModal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const open = ref(false)
<Placeholder class="h-full" />
</template>

<template #footer>
<UButton label="Cancel" color="neutral" variant="outline" @click="open = false" />
<template #footer="{ close }">
<UButton label="Cancel" color="neutral" variant="outline" @click="close" />
<UButton label="Submit" color="neutral" />
</template>
</USlideover>
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.components/modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ slots:

### Programmatic usage

You can use the [`useOverlay`](/composables/use-overlay) composable to open a Modal programatically.
You can use the [`useOverlay`](/composables/use-overlay) composable to open a Modal programmatically.

::warning
Make sure to wrap your app with the [`App`](/components/app) component which uses the [`OverlayProvider`](https://github.com/nuxt/ui/blob/v3/src/runtime/components/OverlayProvider.vue) component.
::

First, create a modal component that will be opened programatically:
First, create a modal component that will be opened programmatically:

::component-example
---
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.components/slideover.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ slots:

### Programmatic usage

You can use the [`useOverlay`](/composables/use-overlay) composable to open a Slideover programatically.
You can use the [`useOverlay`](/composables/use-overlay) composable to open a Slideover programmatically.

::warning
Make sure to wrap your app with the [`App`](/components/app) component which uses the [`OverlayProvider`](https://github.com/nuxt/ui/blob/v3/src/runtime/components/OverlayProvider.vue) component.
::

First, create a slideover component that will be opened programatically:
First, create a slideover component that will be opened programmatically:

::component-example
---
Expand Down
8 changes: 8 additions & 0 deletions playground/app/pages/components/modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,13 @@ function openModal() {
</UModal>

<UButton label="Open programmatically" color="neutral" variant="outline" @click="openModal" />

<UModal title="First modal">
<UButton color="neutral" variant="outline" label="Close with scoped slot close" />

<template #footer="{ close }">
<UButton label="Close with scoped slot close" @click="close" />
</template>
</UModal>
</div>
</template>
16 changes: 16 additions & 0 deletions playground/app/pages/components/slideover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,21 @@ function openSlideover() {
</USlideover>

<UButton label="Open programmatically" color="neutral" variant="outline" @click="openSlideover" />

<USlideover title="Slideover with scoped slot close" description="This slideover has a scoped slot close that can be used to close the slideover from within the content.">
<UButton color="neutral" variant="subtle" label="Open with scoped slot close" />

<template #header="{ close }">
<UButton label="Close with scoped slot close" @click="close" />
</template>

<template #body="{ close }">
<UButton label="Close with scoped slot close" @click="close" />
</template>

<template #footer="{ close }">
<UButton label="Close with scoped slot close" @click="close" />
</template>
</USlideover>
</div>
</template>
31 changes: 16 additions & 15 deletions src/runtime/components/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export interface ModalEmits extends DialogRootEmits {

export interface ModalSlots {
default(props: { open: boolean }): any
content(props?: {}): any
header(props?: {}): any
content(props: { close: () => void }): any
header(props: { close: () => void }): any
title(props?: {}): any
description(props?: {}): any
close(props: { ui: { [K in keyof Required<Modal['slots']>]: (props?: Record<string, any>) => string } }): any
body(props?: {}): any
footer(props?: {}): any
close(props: { close: () => void, ui: { [K in keyof Required<Modal['slots']>]: (props?: Record<string, any>) => string } }): any
body(props: { close: () => void }): any
footer(props: { close: () => void }): any
}
</script>

Expand Down Expand Up @@ -124,8 +124,9 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
}))
</script>

<!-- eslint-disable vue/no-template-shadow -->
<template>
<DialogRoot v-slot="{ open }" v-bind="rootProps">
<DialogRoot v-slot="{ open, close }" v-bind="rootProps">
<DialogTrigger v-if="!!slots.default" as-child :class="props.class">
<slot :open="open" />
</DialogTrigger>
Expand All @@ -148,9 +149,9 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
</DialogDescription>
</VisuallyHidden>

<slot name="content">
<div v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || (close || !!slots.close)" :class="ui.header({ class: props.ui?.header })">
<slot name="header">
<slot name="content" :close="close">
<div v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || (props.close || !!slots.close)" :class="ui.header({ class: props.ui?.header })">
<slot name="header" :close="close">
<div :class="ui.wrapper({ class: props.ui?.wrapper })">
<DialogTitle v-if="title || !!slots.title" :class="ui.title({ class: props.ui?.title })">
<slot name="title">
Expand All @@ -165,16 +166,16 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
</DialogDescription>
</div>

<DialogClose v-if="close || !!slots.close" as-child>
<slot name="close" :ui="ui">
<DialogClose v-if="props.close || !!slots.close" as-child>
<slot name="close" :close="close" :ui="ui">
<UButton
v-if="close"
v-if="props.close"
:icon="closeIcon || appConfig.ui.icons.close"
size="md"
color="neutral"
variant="ghost"
:aria-label="t('modal.close')"
v-bind="(typeof close === 'object' ? close as Partial<ButtonProps> : {})"
v-bind="(typeof props.close === 'object' ? props.close as Partial<ButtonProps> : {})"
:class="ui.close({ class: props.ui?.close })"
/>
</slot>
Expand All @@ -183,11 +184,11 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.modal || {})
</div>

<div v-if="!!slots.body" :class="ui.body({ class: props.ui?.body })">
<slot name="body" />
<slot name="body" :close="close" />
</div>

<div v-if="!!slots.footer" :class="ui.footer({ class: props.ui?.footer })">
<slot name="footer" />
<slot name="footer" :close="close" />
</div>
</slot>
</DialogContent>
Expand Down
31 changes: 16 additions & 15 deletions src/runtime/components/Slideover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ export interface SlideoverEmits extends DialogRootEmits {

export interface SlideoverSlots {
default(props: { open: boolean }): any
content(props?: {}): any
header(props?: {}): any
content(props: { close: () => void }): any
header(props: { close: () => void }): any
title(props?: {}): any
description(props?: {}): any
close(props: { ui: { [K in keyof Required<Slideover['slots']>]: (props?: Record<string, any>) => string } }): any
body(props?: {}): any
footer(props?: {}): any
close(props: { close: () => void, ui: { [K in keyof Required<Slideover['slots']>]: (props?: Record<string, any>) => string } }): any
body(props: { close: () => void }): any
footer(props: { close: () => void }): any
}
</script>

Expand Down Expand Up @@ -124,8 +124,9 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
}))
</script>

<!-- eslint-disable vue/no-template-shadow -->
<template>
<DialogRoot v-slot="{ open }" v-bind="rootProps">
<DialogRoot v-slot="{ open, close }" v-bind="rootProps">
<DialogTrigger v-if="!!slots.default" as-child :class="props.class">
<slot :open="open" />
</DialogTrigger>
Expand Down Expand Up @@ -155,9 +156,9 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
</DialogDescription>
</VisuallyHidden>

<slot name="content">
<div v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || (close || !!slots.close)" :class="ui.header({ class: props.ui?.header })">
<slot name="header">
<slot name="content" :close="close">
<div v-if="!!slots.header || (title || !!slots.title) || (description || !!slots.description) || (props.close || !!slots.close)" :class="ui.header({ class: props.ui?.header })">
<slot name="header" :close="close">
<div :class="ui.wrapper({ class: props.ui?.wrapper })">
<DialogTitle v-if="title || !!slots.title" :class="ui.title({ class: props.ui?.title })">
<slot name="title">
Expand All @@ -172,16 +173,16 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
</DialogDescription>
</div>

<DialogClose v-if="close || !!slots.close" as-child>
<slot name="close" :ui="ui">
<DialogClose v-if="props.close || !!slots.close" as-child>
<slot name="close" :close="close" :ui="ui">
<UButton
v-if="close"
v-if="props.close"
:icon="closeIcon || appConfig.ui.icons.close"
size="md"
color="neutral"
variant="ghost"
:aria-label="t('slideover.close')"
v-bind="(typeof close === 'object' ? close as Partial<ButtonProps> : {})"
v-bind="(typeof props.close === 'object' ? props.close as Partial<ButtonProps> : {})"
:class="ui.close({ class: props.ui?.close })"
/>
</slot>
Expand All @@ -190,11 +191,11 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.slideover ||
</div>

<div :class="ui.body({ class: props.ui?.body })">
<slot name="body" />
<slot name="body" :close="close" />
</div>

<div v-if="!!slots.footer" :class="ui.footer({ class: props.ui?.footer })">
<slot name="footer" />
<slot name="footer" :close="close" />
</div>
</slot>
</DialogContent>
Expand Down