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: multiple action support #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
57 changes: 34 additions & 23 deletions packages/Toast.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,29 @@
{{ toast.cancel.label }}
</button>
</template>

<template v-if="toast.action">
<button
:class="cn(classes?.actionButton, toast.classes?.actionButton)"
data-button
@click="
(event) => {
toast.action?.onClick(event)
if (event.defaultPrevented) return
deleteToast()
}
"
<template
v-for="(action, index) in Array.isArray(toast.action)
? toast.action
: [toast.action]"
:key="index"
>
{{ toast.action.label }}
</button>
<button
:class="cn(classes?.actionButton, action?.classes)"
data-button
@click="
(event) => {
action.onClick(event, {
deleteToast
})
if (event.defaultPrevented) return
}
"
>
{{ action.label }}
</button>
</template>
</template>
</template>
</li>
Expand Down Expand Up @@ -265,18 +274,20 @@ onMounted(() => {
emit('update:heights', newHeightArr as HeightT[])
})

const deleteToast = () => {
// Save the offset for the exit swipe animation
removed.value = true
offsetBeforeRemove.value = offset.value
const newHeights = props.heights.filter(
(height) => height.toastId !== props.toast.id
)
emit('update:heights', newHeights)

const deleteToast = (delay = 0) => {
setTimeout(() => {
emit('removeToast', props.toast)
}, TIME_BEFORE_UNMOUNT)
// Save the offset for the exit swipe animation
removed.value = true
offsetBeforeRemove.value = offset.value
const newHeights = props.heights.filter(
(height) => height.toastId !== props.toast.id
)
emit('update:heights', newHeights)

setTimeout(() => {
emit('removeToast', props.toast)
}, TIME_BEFORE_UNMOUNT)
}, delay)
}

const handleCloseToast = () => {
Expand Down
16 changes: 12 additions & 4 deletions packages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export interface ToastIcons {
loading?: Component
}

export type ToastAction = {
label: string | Component
onClick: (
event: MouseEvent,
toast: {
deleteToast: (delay?: number) => void
}
) => void
classes?: string
}

export type ToastT<T extends Component = Component> = {
id: number | string
title?: string | Component
Expand All @@ -62,10 +73,7 @@ export type ToastT<T extends Component = Component> = {
duration?: number
delete?: boolean
important?: boolean
action?: {
label: string | Component
onClick: (event: MouseEvent) => void
}
action?: ToastAction | ToastAction[]
cancel?: {
label: string | Component
onClick?: () => void
Expand Down
29 changes: 29 additions & 0 deletions src/components/Types.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ const allTypes = [
duration: 10000000
})
},
{
name: 'Multi Action',
snippet: `toast('Event has been created', {
action: [
{
label: 'Undo',
onClick: () => console.log('Undo')
},
{
label: 'Redo',
onClick: () => console.log('Redo')
}
],
})`,
action: () =>
toast.message('Event has been created', {
action: [
{
label: 'Cancel',
onClick: (e, toast) => toast.deleteToast(200)
},
{
label: 'Confirm',
onClick: () => console.log('Confirm')
}
],
duration: 10000000
})
},
{
name: 'Promise',
snippet: `const promise = () => new Promise((resolve) => setTimeout(resolve, 2000));
Expand Down