Skip to content

Commit 2955fdd

Browse files
authored
Merge pull request #460 from aceofwings/main
fix client side toasts
2 parents f9275eb + 40ef206 commit 2955fdd

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

docs/.vuepress/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default defineClientConfig({
99
if (!__VUEPRESS_SSR__) {
1010
const {BootstrapVue3, BToastPlugin} = await import('../../dist/bootstrap-vue-3.es')
1111
app.use(BootstrapVue3)
12-
app.use(BToastPlugin)
12+
app.use(BToastPlugin, {BToast: {injectkey: 'toast'}})
1313
app.component('doc-reference', DocReference)
1414
}
1515
},

docs/components/Toast.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,13 @@ Toasts can be displayed as variants thru various helper methods or be set in `To
135135
</ClientOnly>
136136

137137
<script lang='ts' setup>
138-
import {ref, computed} from 'vue';
138+
import {ref, computed, inject} from 'vue';
139139
import {useToast} from 'bootstrap-vue-3';
140+
let toast = null
141+
142+
if (!__VUEPRESS_SSR__) {
143+
toast = inject("toast").useToast()
140144

141-
// let toast = useToast()
142145

143146
let show1 = () => {toast.show({title: 'example title'})};
144147
let show2 = () => {toast.show({title: 'example title', body: "This is a toast"}, {variant: 'info'})};
@@ -147,4 +150,5 @@ Toasts can be displayed as variants thru various helper methods or be set in `To
147150
let variantshow2 = () => {toast.show({title: 'New Message', body: "This is a toast"}, {pos: 'bottom-right', variant: 'info'})};
148151
let variantshow3 = () => {toast.show({title: 'Warning for Item', body: "Please check list"},{pos: 'bottom-right', variant: 'warning'})};
149152
let variantshow4 = () => {toast.show({title: 'Event Created!', body: "Bootstrap Event"},{pos: 'bottom-right', variant: 'success'})};
153+
}
150154
</script>

src/components/BToast/plugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,20 @@ export class ToastController {
179179

180180
// default global inject key to fetch the controller
181181
const injectkey = Symbol()
182+
const fetchKey = Symbol()
183+
182184
const rootkey = 'root' // TODO: I guess this variable is not used in any place...
183185

184186
const defaults = {
185187
container: undefined,
186188
toasts: [],
187189
root: false,
188190
}
191+
192+
export function getKey(): any {
193+
return inject(fetchKey)
194+
}
195+
189196
export function useToast(): ToastInstance | undefined
190197
export function useToast(vm: {id: symbol}, key?: symbol): ToastInstance | undefined
191198
export function useToast(
@@ -195,7 +202,7 @@ export function useToast(
195202

196203
export function useToast(vm?: any, key: symbol = injectkey): ToastInstance | undefined {
197204
//let's get our controller to fetch the toast instance
198-
const controller = inject(key !== null ? key : injectkey) as ToastController
205+
const controller = inject(getKey()) as ToastController
199206

200207
// not parameters passed, use root if defined
201208
if (!vm) {
@@ -211,6 +218,7 @@ export function useToast(vm?: any, key: symbol = injectkey): ToastInstance | und
211218

212219
export const BToastPlugin: Plugin = {
213220
install: (app: App, options: BootstrapVueOptions = {}) => {
221+
app.provide(fetchKey, options?.BToast?.injectkey ?? injectkey)
214222
app.provide(options?.BToast?.injectkey ?? injectkey, new ToastController())
215223
},
216224
}

0 commit comments

Comments
 (0)