|
| 1 | +# useModal |
| 2 | + |
| 3 | +<ClientOnly> |
| 4 | + <Teleport to=".bd-toc"> |
| 5 | + |
| 6 | +[[toc]] |
| 7 | + |
| 8 | + </Teleport> |
| 9 | +</ClientOnly> |
| 10 | + |
| 11 | +<div class="lead mb-5"> |
| 12 | + |
| 13 | +`useToast` is used to create `Toasts` on demand. You must have initialized the `BToaster` component once in your application. This is covered mostly under the [components docs](/docs/components/toast) section. So, this will be more in depth as to what the composable does. The following examples are shown in the component documentation |
| 14 | + |
| 15 | +</div> |
| 16 | + |
| 17 | +## Showing a Toast |
| 18 | + |
| 19 | +Showing a toast is done through the show method |
| 20 | + |
| 21 | +<HighlightCard> |
| 22 | + <BButton @click="show('Hello World!')">Show</BButton> |
| 23 | + <template #html> |
| 24 | + |
| 25 | +```vue |
| 26 | +<template> |
| 27 | + <BButton @click="show('Hello World!')">Show</BButton> |
| 28 | +</template> |
| 29 | +
|
| 30 | +<script setup lang="ts"> |
| 31 | +const {show} = useToast() |
| 32 | +</script> |
| 33 | +``` |
| 34 | + |
| 35 | + </template> |
| 36 | +</HighlightCard> |
| 37 | + |
| 38 | +The show method returns a symbol. This symbol is a unique id. Since `Toasts` are fluid and can move around a lot, returning the index at a given point in time is not ideal for obvious reasons. So, for use with the hide method, we need to give you a unique identifier |
| 39 | + |
| 40 | +## Hiding a Toast |
| 41 | + |
| 42 | +We then use that returned id to hide a `Toast` |
| 43 | + |
| 44 | +<HighlightCard> |
| 45 | + <BButtonGroup> |
| 46 | + <BButton @click="showMe" variant="success"> |
| 47 | + Show the Toast |
| 48 | + </BButton> |
| 49 | + <BButton @click="hideMe" variant="danger"> |
| 50 | + Hide the Toast |
| 51 | + </BButton> |
| 52 | + </BButtonGroup> |
| 53 | + <template #html> |
| 54 | + |
| 55 | +```vue |
| 56 | +<template> |
| 57 | + <BButtonGroup> |
| 58 | + <BButton @click="showMe" variant="success"> Show the Toast </BButton> |
| 59 | + <BButton @click="hideMe" variant="danger"> Hide the Toast </BButton> |
| 60 | + </BButtonGroup> |
| 61 | +</template> |
| 62 | +
|
| 63 | +<script setup lang="ts"> |
| 64 | +const {show, hide} = useToast() |
| 65 | +
|
| 66 | +let showValue: undefined | symbol |
| 67 | +
|
| 68 | +const showMe = () => { |
| 69 | + if (typeof showValue === 'symbol') return |
| 70 | + // `show` returns a symbol |
| 71 | + showValue = show('Showing', {value: true, variant: 'success', pos: 'bottom-center'}) |
| 72 | +} |
| 73 | +
|
| 74 | +const hideMe = () => { |
| 75 | + if (showValue === undefined) return |
| 76 | + hide(showValue) |
| 77 | + showValue = undefined |
| 78 | +} |
| 79 | +</script> |
| 80 | +``` |
| 81 | + |
| 82 | + </template> |
| 83 | + |
| 84 | +</HighlightCard> |
| 85 | + |
| 86 | +## The `toasts` Value |
| 87 | + |
| 88 | +Not covered in the component docs is the value `toasts` returned by the composable. This is mostly an internal value used by the `BToaster` component for actually rendering your `BToast` component. This is actually how the entire thing functions! It is simply the array of `Toasts`. Its not a very useful property externally. Keep in mind, this is all global. If you add a Toast from one place, it will always be added from to this array. There is no separation. Functionally it is quite similar to `useBreadcrumb` |
| 89 | + |
| 90 | +<HighlightCard> |
| 91 | + <BButton @click="show('Hello World!')">Show</BButton> |
| 92 | + {{ toasts }} |
| 93 | + <template #html> |
| 94 | + |
| 95 | +```vue |
| 96 | +<template> |
| 97 | + <BButton @click="show('Hello World!')">Show</BButton> |
| 98 | + {{ toasts }} |
| 99 | +</template> |
| 100 | +
|
| 101 | +<script setup lang="ts"> |
| 102 | +const {show, toasts} = useToast() |
| 103 | +</script> |
| 104 | +``` |
| 105 | + |
| 106 | + </template> |
| 107 | +</HighlightCard> |
| 108 | + |
| 109 | +<script setup lang="ts"> |
| 110 | +import {data} from '../../data/components/toast.data' |
| 111 | +import {BButton, useToast} from 'bootstrap-vue-next' |
| 112 | +import HighlightCard from '../../components/HighlightCard.vue' |
| 113 | + |
| 114 | +const {show, hide, toasts} = useToast() |
| 115 | + |
| 116 | +let showValue: undefined | symbol |
| 117 | + |
| 118 | +const showMe = () => { |
| 119 | + if (typeof showValue === 'symbol') return |
| 120 | + showValue = show('Showing', {value: true, variant: 'success', pos: 'bottom-center'}) |
| 121 | +} |
| 122 | + |
| 123 | +const hideMe = () => { |
| 124 | + if (showValue === undefined) return |
| 125 | + hide(showValue) |
| 126 | + showValue = undefined |
| 127 | +} |
| 128 | + |
| 129 | +</script> |
0 commit comments