Skip to content

Commit 710b32a

Browse files
committed
Updated README, added comments
1 parent 39a042c commit 710b32a

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
pnpm-lock.yaml
33
package-lock.json
44
yarn.lock
5+
README.md

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,125 @@
11
# svelte-toasts
22

33
**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 }`

src/lib/stores/toasts.store.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const createStore = () => {
99

1010
const { set, subscribe, update } = writable<(Required<ToastOptions> & Toast)[]>([]);
1111

12-
const pop = async (id: number | ToastPop) => {
12+
/**
13+
* Removes a toast from the store based on the given {@link id}. This can relate to the id number of
14+
* a toast or {@link ToastPop}. 'new' removes the latest toast, whereas 'old' removes the oldest.
15+
* @param {number | ToastPop} id The id of the toast to remove, or either 'new' or 'old'.
16+
*/
17+
const pop = (id: number | ToastPop) => {
1318
switch (id) {
1419
case 'new':
1520
update((toasts) => toasts.slice(1));
@@ -22,13 +27,25 @@ const createStore = () => {
2227
}
2328
};
2429

25-
const push = async (message: string, opts?: ToastOptions) => {
30+
/**
31+
* Push a new toast to the store.
32+
* @param {string} message The message for the toast to display.
33+
* @param {ToastOptions} opts (optional)
34+
* @returns {number} Id of the toast.
35+
*/
36+
const push = (message: string, opts?: ToastOptions): number => {
2637
const options = { ...get(defaultToastStore), ...opts } as Required<ToastOptions>;
2738
const id = toastId++;
2839

2940
update((toasts) => [{ ...options, ...{ id, message } }, ...toasts]);
41+
42+
return id;
3043
};
3144

45+
/**
46+
* Remove all toasts from the store. Sets the fade transition duration to 0 beforehand, to ensure toasts
47+
* are cleared instantly after awaiting 'tick'.
48+
*/
3249
const clear = async () => {
3350
animationStore.update((animations) => ({ ...animations, ...{ fade: { duration: 0 } } }));
3451
await tick();

0 commit comments

Comments
 (0)