-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…4377) * feat(VaToast #4373): add bottom-center, top-center position options * fix: correctly handle multiple toasts offset * chore(toast): fix paddings * chore: remove redundant * fix(toast): improve appear and close animations --------- Co-authored-by: Parsons <[email protected]> Co-authored-by: Maksim Nedoshev <[email protected]>
- Loading branch information
1 parent
280534b
commit 5b53670
Showing
9 changed files
with
160 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/ui/src/components/va-toast/hooks/useToastService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { computed, getCurrentInstance, onBeforeUnmount, onMounted, Ref, ref, VNode } from 'vue' | ||
import { ToastOptions } from '../types' | ||
|
||
const GAP = 5 | ||
|
||
// Expect as client-side used only | ||
const toastInstances = ref([]) as Ref<VNode[]> | ||
|
||
type OptionKeys = keyof ToastOptions; | ||
|
||
const getNodeProps = (vNode: VNode): Record<OptionKeys, any> => { | ||
return (vNode.component?.props as Record<OptionKeys, any>) || {} | ||
} | ||
|
||
const getTranslateValue = (item: VNode) => { | ||
if (item.el) { | ||
return (item.el.offsetHeight + GAP) | ||
} | ||
return 0 | ||
} | ||
|
||
export const useToastService = (props: { | ||
position: NonNullable<ToastOptions['position']>, | ||
}) => { | ||
const currentInstance = getCurrentInstance()! | ||
|
||
const yOffset = computed(() => { | ||
const currentIndex = toastInstances.value.findIndex((instance) => instance === currentInstance.vnode) | ||
|
||
if (currentIndex === -1) { return 0 } | ||
|
||
return toastInstances.value.slice(currentIndex + 1).reduce((acc, instance) => { | ||
const { | ||
position: itemPosition, | ||
} = getNodeProps(instance) | ||
|
||
const { position } = props | ||
|
||
if (position === itemPosition) { | ||
return getTranslateValue(instance) + acc | ||
} | ||
|
||
return acc | ||
}, 0) | ||
}) | ||
|
||
onMounted(() => { | ||
toastInstances.value.unshift(currentInstance.vnode) | ||
}) | ||
|
||
onBeforeUnmount(() => { | ||
toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode) | ||
}) | ||
|
||
return { | ||
yOffset, | ||
updateYOffset: () => { | ||
toastInstances.value = toastInstances.value.filter((item) => item !== currentInstance.vnode) | ||
}, | ||
} | ||
} |
Oops, something went wrong.