-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<script setup lang="ts"> | ||
import { onUpdated } from 'vue' | ||
import { Toast } from 'bootstrap' | ||
import { isMobile } from '@/utils/device' | ||
import type { ToastItem } from '@/utils/toast' | ||
const props = defineProps<{ | ||
toasts: ToastItem[] | ||
}>() | ||
onUpdated(() => { | ||
props.toasts | ||
.filter((obj) => !obj.shown) | ||
.forEach((item: ToastItem) => { | ||
var toast_element = document.getElementById(item.id)! | ||
var toast = new Toast(toast_element) | ||
toast.show() | ||
item.shown = true | ||
}) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div | ||
ref="container" | ||
:style="{ | ||
'z-index': 999, | ||
'max-width': '350px', | ||
width: '100%', | ||
margin: '0 auto', | ||
position: 'fixed', | ||
padding: '1rem', | ||
bottom: 0, | ||
left: isMobile() ? '50%' : '0', | ||
transform: isMobile() ? 'translateX(-50%)' : 'none' | ||
}" | ||
> | ||
<div | ||
v-for="item in toasts" | ||
:key="item.id" | ||
:id="item.id" | ||
class="toast fade opacity-90" | ||
role="alert" | ||
aria-live="assertive" | ||
aria-atomic="true" | ||
data-bs-delay="2000" | ||
@[`hidden.bs.toast`]="$emit('remove-toast', item.id)" | ||
> | ||
<div | ||
class="toast show" | ||
role="alert" | ||
aria-live="assertive" | ||
aria-atomic="true" | ||
data-bs-autohide="false" | ||
data-bs-toggle="toast" | ||
> | ||
<div class="toast-header"> | ||
<strong class="me-auto">{{ item.title }}</strong> | ||
<button | ||
type="button" | ||
class="ms-2 btn-close" | ||
data-bs-dismiss="toast" | ||
aria-label="Close" | ||
></button> | ||
</div> | ||
<div class="toast-body" v-if="item.detail">{{ item.detail }}</div> | ||
</div> | ||
</div> | ||
</div> | ||
</template> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { ref } from 'vue' | ||
|
||
export type ToastItem = { | ||
id: string | ||
title: string | ||
detail?: string | ||
shown: boolean | ||
} | ||
|
||
export const toasts = ref<ToastItem[]>([]) | ||
|
||
const TOASTS_MAX = 5 | ||
const toast_id = ref(0) | ||
|
||
export function showToast(title: string, detail?: string) { | ||
if (toasts.value.length === TOASTS_MAX) { | ||
toasts.value.shift() | ||
} | ||
toasts.value.push({ | ||
id: toast_id.value.toString(), | ||
title: title, | ||
detail: detail, | ||
shown: false | ||
} as ToastItem) | ||
toast_id.value++ | ||
} | ||
|
||
export function removeToast(id: string) { | ||
toasts.value = toasts.value.filter((obj) => { | ||
return obj.id != id | ||
}) | ||
} |