Skip to content

Commit

Permalink
fix(Toaster): teleport to body
Browse files Browse the repository at this point in the history
Resolves #2404
  • Loading branch information
benjamincanac committed Nov 10, 2024
1 parent 36ea3e4 commit b0be26d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
34 changes: 21 additions & 13 deletions src/runtime/components/Toaster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface ToasterProps extends Omit<ToastProviderProps, 'swipeDirection'>
* @defaultValue true
*/
expand?: boolean
/**
* Render the toaster in a portal.
* @defaultValue true
*/
portal?: boolean
class?: any
ui?: Partial<typeof toaster.slots>
}
Expand All @@ -44,6 +49,7 @@ import UToast from './Toast.vue'
const props = withDefaults(defineProps<ToasterProps>(), {
expand: true,
portal: true,
duration: 5000
})
defineSlots<ToasterSlots>()
Expand Down Expand Up @@ -120,18 +126,20 @@ function getOffset(index: number) {
@click="toast.click && toast.click(toast)"
/>

<ToastViewport
:data-expanded="expanded"
:class="ui.viewport({ class: [props.class, props.ui?.viewport] })"
:style="{
'--scale-factor': '0.05',
'--translate-factor': position?.startsWith('top') ? '1px' : '-1px',
'--gap': position?.startsWith('top') ? '16px' : '-16px',
'--front-height': `${frontHeight}px`,
'--height': `${height}px`
}"
@mouseenter="hovered = true"
@mouseleave="hovered = false"
/>
<Teleport to="body" :disabled="!portal">
<ToastViewport
:data-expanded="expanded"
:class="ui.viewport({ class: [props.class, props.ui?.viewport] })"
:style="{
'--scale-factor': '0.05',
'--translate-factor': position?.startsWith('top') ? '1px' : '-1px',
'--gap': position?.startsWith('top') ? '16px' : '-16px',
'--front-height': `${frontHeight}px`,
'--height': `${height}px`
}"
@mouseenter="hovered = true"
@mouseleave="hovered = false"
/>
</Teleport>
</ToastProvider>
</template>
2 changes: 1 addition & 1 deletion test/components/Toast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ToastWrapper = defineComponent({
ClientOnly
},
inheritAttrs: false,
template: `<UToaster>
template: `<UToaster :portal="false">
<ClientOnly>
<UToast v-bind="$attrs">
<template v-for="(_, name) in $slots" #[name]="slotData">
Expand Down
Loading

1 comment on commit b0be26d

@AaronDewes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: It looks like Toast now has teleport support upstream: unovue/radix-vue#1417. Not sure if it's worth using that, but maybe you want to have a look.

Please sign in to comment.