-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathpatch-solid-toast.ts
38 lines (33 loc) · 1.12 KB
/
patch-solid-toast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import clsx from 'clsx';
import {createUniqueId} from 'solid-js';
import {Message, toast, ToastHandler, ToastOptions} from 'solid-toast';
import * as styles from './Snackbar.css';
interface AugmentedOptions extends ToastOptions {
theme?: string;
}
export type AugmentedToastHandler = (
message: Message,
options?: AugmentedOptions,
) => string;
export function createPatch(_toast: typeof toast, key: keyof typeof toast) {
const old = _toast[key] as ToastHandler;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(toast as any)[key] = (message: Message, options?: AugmentedOptions) => {
if (options?.theme) {
const theme = options.theme;
const id = createUniqueId();
options.id = id;
options.className = clsx(options.className, options.id);
queueMicrotask(() => {
document
.querySelector(`.${id}`)
?.setAttribute('data-codeimage-theme', theme);
});
}
if (options?.className) {
options.className = clsx(options.className, styles.snackbar);
}
return old(message, options);
};
return toast[key] as AugmentedToastHandler;
}