Skip to content

Commit

Permalink
Merge pull request #49 from wobsoriano/dynamic-position
Browse files Browse the repository at this point in the history
feat: allow setting position when creating a toast
  • Loading branch information
xiaoluoboding authored Feb 29, 2024
2 parents 311ecc8 + 0326cea commit 06d9e39
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions packages/Toaster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<!-- Remove item from normal navigation flow, only available via hotkey -->
<section :aria-label="`Notifications ${hotkeyLabel}`" :tabIndex="-1">
<ol
v-for="(position, index) in possiblePositions"
:key="index"
ref="listRef"
data-sonner-toaster
:dir="dir === 'auto' ? getDocumentDirection() : dir"
:tabIndex="-1"
:data-theme="theme"
:data-rich-colors="richColors"
:data-y-position="coords[0]"
:data-x-position="coords[1]"
:data-y-position="position.split('-')[0]"
:data-x-position="position.split('-')[1]"
:style="
{
'--front-toast-height': `${heights[0]?.height}px`,
Expand All @@ -32,9 +34,9 @@
@pointerdown="interacting = false"
@pointerup="interacting = false"
>
<template v-for="(toast, index) in toasts" :key="toast.id">
<template v-for="(toast, idx) in toasts.filter(toast => (!toast.position && index === 0) || toast.position === position)" :key="toast.id">
<Toast
:index="index"
:index="idx"
:toast="toast"
:duration="toastOptions?.duration ?? duration"
:className="toastOptions?.className"
Expand Down Expand Up @@ -142,6 +144,9 @@ const props = withDefaults(defineProps<ToasterProps>(), {
const attrs = useAttrs()
const toasts = ref<ToastT[]>([])
const possiblePositions = computed(() => Array.from(
new Set([props.position].concat(toasts.value.filter((toast) => toast.position).map((toast) => toast.position as Position))),
))
const heights = ref<HeightT[]>([])
const expanded = ref(false)
const interacting = ref(false)
Expand Down

0 comments on commit 06d9e39

Please sign in to comment.