|
1 | 1 | # svelte-toasts |
2 | 2 |
|
3 | 3 | **svelte-toasts** is yet another toast library for [Svelte](https://svelte.dev/), written with [Typescript](https://www.typescriptlang.org/), [TailwindCSS](https://tailwindcss.com/), and formatted with [Prettier](https://prettier.io/). |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +TODO: Installation |
| 8 | + |
| 9 | +Import the `ToastWrapper` component, with optional `position`, `toastDefaults` and `animationDefaults`. |
| 10 | + |
| 11 | +```svelte |
| 12 | +<script lang="ts"> |
| 13 | + import { AnimationOptions, Position, ToastOptions, ToastWrapper } from ''; |
| 14 | +
|
| 15 | + const toastDefaults: ToastOptions = { |
| 16 | + ... |
| 17 | + }; |
| 18 | +
|
| 19 | + const animationDefaults: AnimationOptions = { |
| 20 | + ... |
| 21 | + }; |
| 22 | +</script> |
| 23 | +
|
| 24 | +... |
| 25 | +
|
| 26 | +<ToastWrapper position="end" {toastDefaults} {animationDefaults} /> |
| 27 | +``` |
| 28 | + |
| 29 | +Import the `toasts` store and push as much toast as you desire. |
| 30 | + |
| 31 | +```svelte |
| 32 | +<script lang="ts"> |
| 33 | +import { toasts } from ''; |
| 34 | +
|
| 35 | +toasts.push('Toast Me!'); |
| 36 | +</script> |
| 37 | +``` |
| 38 | + |
| 39 | +## Functions |
| 40 | + |
| 41 | +```typescript |
| 42 | +/** |
| 43 | + * Remove all toasts from the store. Sets the fade transition duration to 0 beforehand, to ensure toasts |
| 44 | + * are cleared instantly after awaiting 'tick'. |
| 45 | + */ |
| 46 | +const clear = async () => { ... } |
| 47 | + |
| 48 | +/** |
| 49 | + * Removes a toast from the store based on the given {@link id}. This can relate to the id number of |
| 50 | + * a toast or {@link ToastPop}. 'new' removes the latest toast, whereas 'old' removes the oldest. |
| 51 | + * @param {number | ToastPop} id The id of the toast to remove, or either 'new' or 'old'. |
| 52 | + */ |
| 53 | +const pop = (id: number | ToastPop) => { ... } |
| 54 | + |
| 55 | +/** |
| 56 | + * Push a new toast to the store. |
| 57 | + * @param {string} message The message for the toast to display. |
| 58 | + * @param {ToastOptions} opts (optional) |
| 59 | + * @returns {number} Id of the toast. |
| 60 | + */ |
| 61 | +const push = (message: string, opts?: ToastOptions): number => { ... } |
| 62 | +``` |
| 63 | + |
| 64 | +## Options |
| 65 | + |
| 66 | +### Toast Options |
| 67 | + |
| 68 | +```typescript |
| 69 | +toasts.push('...', { |
| 70 | +auto: boolean; |
| 71 | +border: 'bottom' | 'end' | 'start' | 'top' | 'all'; |
| 72 | +duration: number; |
| 73 | +icon: boolean, |
| 74 | +pausable: boolean, |
| 75 | +type: 'error' | 'info' | 'success' | 'warning'; |
| 76 | +}); |
| 77 | +``` |
| 78 | + |
| 79 | +#### ToastOptions |
| 80 | + |
| 81 | +- **auto**: Whether or not the toast is automatically dismissed after `duration` has elapsed. |
| 82 | + - _default_: `true` |
| 83 | +- **border**: Position of the toasts border. |
| 84 | + - _default_: `start` |
| 85 | +- **duration**: Amount of time in ms to elapse before dismissing, in conjunction with `auto`. |
| 86 | + - _default_: `3000` |
| 87 | +- **icon**: Whether or not to display an icon related to `type`. |
| 88 | + - _default_: `true` |
| 89 | +- **pausable**: Whether or not `duration` is able to be paused by mouse hover. |
| 90 | + - _default_: `true` |
| 91 | +- **type**: The type of the toast, which affects `icon` and `border` colour. |
| 92 | + - _default_: `info` |
| 93 | + |
| 94 | +### Default Options |
| 95 | + |
| 96 | +If you want to stick to a style and would prefer to not give `ToastOptions` every time you push a toast, defaults can instead be set on the `ToastWrapper`. |
| 97 | + |
| 98 | +```typescript |
| 99 | +const toastDefaults: ToastOptions = { |
| 100 | + ... |
| 101 | +}; |
| 102 | + |
| 103 | +const animationDefaults: AnimationOptions = { |
| 104 | + fade: {}, // FadeParams |
| 105 | + flip: {}, // FlipParams |
| 106 | + fly: {} // FlyParams |
| 107 | +}; |
| 108 | +``` |
| 109 | + |
| 110 | +```svelte typescript |
| 111 | +<ToastWrapper position={'bottom' | 'end' | 'start' | 'top'} {animationDefaults} {toastDefaults} /> |
| 112 | +``` |
| 113 | + |
| 114 | +#### ToastOptions |
| 115 | + |
| 116 | +as [above](#ToastOptions). |
| 117 | + |
| 118 | +#### AnimationOptions |
| 119 | + |
| 120 | +- **fade**: Fade transition parameters. **OUT** transition. |
| 121 | + - _default_: `{ duration: 400 }` |
| 122 | +- **flip**: Flip animation parameters. |
| 123 | + - _default_: `{ duration: 400 }` |
| 124 | +- **fly**: Fly transition parameters. **IN** transition. |
| 125 | + - _default_: `{ duration: 400 }` |
0 commit comments