|
1 |
| -# svelte-toast |
| 1 | +# svelte-toast |
| 2 | + |
| 3 | +> Simple elegant toast notifications. |
| 4 | +
|
| 5 | +Because a demo helps better than a thousand API docs: https://zerodevx.github.io/svelte-toast/ |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +Install the package: |
| 10 | + |
| 11 | +```bash |
| 12 | +$ npm i -D @zerodevx/svelte-toast |
| 13 | +``` |
| 14 | + |
| 15 | +The following are exported: |
| 16 | + |
| 17 | +* `SvelteToast` as the toast container; |
| 18 | +* `toast` as the toast emitter. |
| 19 | + |
| 20 | +### Svelte |
| 21 | + |
| 22 | +If you're using this in a Svelte app, import the toast container and place it in your app shell. |
| 23 | + |
| 24 | +`App.svelte`: |
| 25 | + |
| 26 | +```html |
| 27 | +<script> |
| 28 | +import { SvelteToast } from '@zerodevx/svelte-toast' |
| 29 | +
|
| 30 | +// Optionally set default options here |
| 31 | +const options = { |
| 32 | + ... |
| 33 | +} |
| 34 | +</script> |
| 35 | + |
| 36 | +... |
| 37 | +<SvelteToast {options} /> |
| 38 | + |
| 39 | +``` |
| 40 | + |
| 41 | +Use anywhere in your app - just import the toast emitter. |
| 42 | + |
| 43 | +`MyComponent.svelte` |
| 44 | + |
| 45 | +```html |
| 46 | +<script> |
| 47 | +import { toast } from '@zerodevx/svelte-toast' |
| 48 | +</script> |
| 49 | + |
| 50 | +<button on:click={() => toast.push('Hello world!')}>EMIT TOAST</button> |
| 51 | +``` |
| 52 | + |
| 53 | +### Vanilla JS |
| 54 | + |
| 55 | +For any other applications with a bundler, something like this should work. |
| 56 | + |
| 57 | +```js |
| 58 | +import { SvelteToast, toast } from `@zerodevx/svelte-toast' |
| 59 | +
|
| 60 | +const app = new SvelteToast({ |
| 61 | + // Set where the toast container should be appended into |
| 62 | + target: document.body, |
| 63 | + // Optionally set default options here |
| 64 | + props: { |
| 65 | + options: { |
| 66 | + ... |
| 67 | + } |
| 68 | + } |
| 69 | +}) |
| 70 | +
|
| 71 | +toast.push('Hello world!') |
| 72 | +``` |
| 73 | + |
| 74 | +### CDN |
| 75 | + |
| 76 | +Or if you prefer to go old-school javascript and a CDN: |
| 77 | + |
| 78 | +```html |
| 79 | +<html> |
| 80 | +<head> |
| 81 | + ... |
| 82 | + <script defer src="https://cdn.jsdelivr.net/npm/@zerodevx/svelte-toast@0"></script> |
| 83 | +</head> |
| 84 | +<body> |
| 85 | +<body> |
| 86 | +</html> |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +## Theming |
| 91 | + |
| 92 | +In general, use CSS variables - the following are exposed: |
| 93 | + |
| 94 | +```css |
| 95 | +ToastContainer { |
| 96 | + top: var(--toastContainerTop, 1.5rem); |
| 97 | + right: var(--toastContainerRight, 2rem); |
| 98 | + bottom: var(--toastContainerBottom, auto); |
| 99 | + left: var(--toastContainerLeft, auto); |
| 100 | +} |
| 101 | +
|
| 102 | +ToastItem { |
| 103 | + width: var(--toastWidth, 16rem); |
| 104 | + height: var(--toastHeight, 3.5rem); |
| 105 | + margin-bottom: var(--toastMarginBottom, 0.5rem); |
| 106 | + background: var(--toastBackground, rgba(74,85,104,0.98)); |
| 107 | + color: var(--toastColor, #FFF); |
| 108 | +} |
| 109 | +
|
| 110 | +ToastItemMessage { |
| 111 | + font-size: var(--toastFontSize, 1rem); |
| 112 | +} |
| 113 | +
|
| 114 | +ToastProgressBar { |
| 115 | + background: var(--toastProgressBackground, rgba(66,153,225,0.98)); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +So to apply your custom theme globally, do something like: |
| 120 | + |
| 121 | +```html |
| 122 | +<style> |
| 123 | +:root { |
| 124 | + --toastBackground: #ABCDEF; |
| 125 | + --toastColor: #123456; |
| 126 | + --toastHeight: 300px; |
| 127 | + ... |
| 128 | +} |
| 129 | +</style> |
| 130 | +``` |
| 131 | + |
| 132 | +To apply overrides to a particular Toast Item, emit the toast with options: |
| 133 | + |
| 134 | +```js |
| 135 | +toast.push('Yo!', { |
| 136 | + theme: { |
| 137 | + '--toastBackground': 'cyan', |
| 138 | + '--toastColor': 'black' |
| 139 | + } |
| 140 | +}) |
| 141 | +``` |
| 142 | + |
| 143 | +## Options |
| 144 | + |
| 145 | +```js |
| 146 | +// Default options |
| 147 | +const defaults = { |
| 148 | + duration: 4000, // duration of progress bar tween |
| 149 | + dismissable: true, // allow dismiss with close button |
| 150 | + initial: 1, // initial progress bar value |
| 151 | + progress: 0, // current progress |
| 152 | + reversed: false, // insert new toast to bottom of stack |
| 153 | + intro: { x: 256 }, // toast intro fly animation settings |
| 154 | + theme: {} // css var overrides |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## Toast API |
| 159 | + |
| 160 | +```js |
| 161 | +const id = toast.push(message, { options }) |
| 162 | +toast.pop(id) |
| 163 | +toast.set(id, { object }) |
| 164 | +``` |
| 165 | + |
| 166 | +## To-do |
| 167 | + |
| 168 | +- [ ] Definitely improve the docs |
| 169 | +- [ ] Create some option presets |
| 170 | + |
0 commit comments