Skip to content

Commit 2b1ef0c

Browse files
feildmasterclaude
andcommitted
feat: adapt toasts to SimpleToast 3.0
Work in progress, not releasable until SimpleToast 3.0.0 is tagged and the @require and package.json are bumped. - toastCss applies the legacy css option to the element 3.0 returns - UnderScript's default toast look is now classes in toast.css - toast buttons still close the toast after their handler - errorToast forwards timeout - plugin toast warns once per plugin when css is used - tests run against the real library and skip until it is installed Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent a8df834 commit 2b1ef0c

8 files changed

Lines changed: 542 additions & 29 deletions

File tree

‎src/base/plugin/toast.api.d.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type UnderScriptToastClass = string | readonly string[];
2020
type UnderScriptToastCloseReason = 'timeout' | 'dismissed' | 'unknown' | (string & {});
2121

2222
type UnderScriptToast = {
23+
readonly element: HTMLElement;
2324
exists(): boolean;
2425
setText(text: string): void;
2526
close(reason?: string): void;
@@ -28,13 +29,15 @@ type UnderScriptToast = {
2829
type UnderScriptToastButton = {
2930
text: string;
3031
className?: UnderScriptToastClass;
32+
/** @deprecated Use `className` instead */
3133
css?: UnderScriptToastButtonStyle;
3234
onclick?(this: UnderScriptToast, event: MouseEvent, toast: UnderScriptToast): unknown;
3335
};
3436

3537
type UnderScriptToastOptions = {
3638
text: string;
3739
title?: string;
40+
/** @deprecated Use `className` instead */
3841
css?: UnderScriptToastCSS;
3942
className?: UnderScriptToastClass | { toast?: UnderScriptToastClass; button?: UnderScriptToastClass };
4043
buttons?: UnderScriptToastButton | readonly UnderScriptToastButton[];

‎src/base/plugin/toast.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import wrap from 'src/utils/2.pokemon.js';
22
import { registerModule } from 'src/utils/plugin.js';
33
import { toast as basicToast, errorToast } from 'src/utils/2.toasts.js';
4+
import toArray from 'src/utils/toArray.js';
45

56
wrap(() => {
67
const name = 'toast';
7-
function pluginToast(plugin, data) {
8-
const toast = typeof data === 'object' ? { ...data } : { text: data };
8+
function pluginToast(plugin, toast) {
99
toast.footer = `${plugin.name} • via UnderScript`;
1010
if (toast.error) return errorToast(toast);
1111
return basicToast(toast);
1212
}
1313

1414
function mod(plugin) {
15-
return (data) => pluginToast(plugin, data);
15+
let warned = false;
16+
return (data) => {
17+
const toast = typeof data === 'object' ? { ...data } : { text: data };
18+
if (!warned && (toast.css || toArray(toast.buttons).some((button) => button?.css))) {
19+
warned = true;
20+
plugin.logger.warn('toast(): `css` is deprecated, please use `className` instead.');
21+
}
22+
return pluginToast(plugin, toast);
23+
};
1624
}
1725

18-
registerModule(name, mod);
26+
registerModule(name, mod, 'logger');
1927
});

‎src/base/styles/toast.css‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
#AlertToast {
22
height: 0;
33

4+
.underscript-toast {
5+
background-color: rgba(0, 5, 20, 60%);
6+
font-family: monospace;
7+
text-shadow: none;
8+
9+
.simpletoast-footer {
10+
text-align: end;
11+
}
12+
}
13+
14+
.underscript-toast-error {
15+
background-color: rgba(200, 0, 0, 60%);
16+
}
17+
18+
.underscript-toast-info {
19+
font-family: inherit;
20+
}
21+
22+
.dismiss {
23+
border: revert;
24+
border-radius: revert;
25+
background: revert;
26+
font-size: inherit;
27+
height: revert;
28+
margin: revert;
29+
}
30+
431
.dismissable {
532
> span {
633
display: block;

‎src/utils/2.toasts.js‎

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
import Translation from 'src/structures/constants/translation.js';
2-
import { buttonCSS } from './1.variables.js';
32
import merge from './merge.js';
43
import eventManager from './eventManager.js';
54
import toArray from './toArray.js';
5+
import applyToastCss from './toastCss.js';
66

77
let ready = false;
88

99
eventManager.on('underscript:ready', () => ready = true);
1010

11+
function addToastClass(className, name) {
12+
if (className && typeof className === 'object' && !Array.isArray(className)) {
13+
return { ...className, toast: [name, ...toArray(className.toast)] };
14+
}
15+
return [name, ...toArray(className)];
16+
}
17+
18+
function dismissing(button) {
19+
if (!button) return button;
20+
return {
21+
...button,
22+
onclick(event, instance) {
23+
try {
24+
if (typeof button.onclick === 'function') button.onclick.call(this, event, instance);
25+
} finally {
26+
if (!event.cancelBubble) instance.close('dismissed');
27+
}
28+
},
29+
};
30+
}
31+
1132
export function blankToast() {
1233
return new SimpleToast();
1334
}
@@ -26,21 +47,16 @@ export function toast(arg) {
2647
].some((obj) => obj instanceof Translation);
2748
const defaults = {
2849
footer: 'via UnderScript',
29-
css: {
30-
'background-color': 'rgba(0,5,20,0.6)',
31-
'text-shadow': '',
32-
'font-family': 'monospace',
33-
footer: {
34-
'text-align': 'end',
35-
},
36-
},
3750
};
3851
if (ready && isTranslation) preprocess(arg);
39-
const slice = new SimpleToast(merge(defaults, arg));
52+
const { css, ...options } = merge(defaults, arg);
53+
options.className = addToastClass(options.className, 'underscript-toast');
54+
if (options.buttons) options.buttons = toArray(options.buttons).map(dismissing);
55+
const slice = new SimpleToast(options);
56+
if (slice.exists()) applyToastCss(slice, css, options.buttons);
4057
if (!ready && isTranslation && slice.exists()) {
41-
const el = $('#AlertToast > div:last');
4258
eventManager.on('underscript:ready', () => {
43-
process(slice, el, arg);
59+
process(slice, arg);
4460
});
4561
}
4662
return slice;
@@ -58,13 +74,11 @@ export function errorToast(error) {
5874
const lToast = {
5975
title: error.name || error.title || Translation.ERROR,
6076
text: error.message || error.text || getStack(error.error || error) || error,
61-
css: {
62-
'background-color': 'rgba(200,0,0,0.6)',
63-
},
64-
className: error.className,
77+
className: addToastClass(error.className, 'underscript-toast-error'),
6578
onClose: error.onClose,
6679
footer: error.footer,
6780
buttons: error.buttons,
81+
timeout: error.timeout,
6882
};
6983
return toast(lToast);
7084
}
@@ -77,6 +91,7 @@ export function infoToast(arg, key, val = '1') {
7791
};
7892
} else if (typeof arg !== 'object') return null;
7993
const override = {
94+
className: addToastClass(arg.className, 'underscript-toast-info'),
8095
onClose: (...args) => {
8196
if (typeof arg.onClose === 'function') {
8297
if (arg.onClose(...args)) {
@@ -88,9 +103,6 @@ export function infoToast(arg, key, val = '1') {
88103
};
89104
const defaults = {
90105
title: Translation.INFO,
91-
css: {
92-
'font-family': 'inherit',
93-
},
94106
};
95107
return toast(merge(defaults, arg, override));
96108
}
@@ -100,7 +112,6 @@ export function dismissable({ title, text, key, value = 'true', css = {} }) {
100112
const buttons = {
101113
text: Translation.DISMISS,
102114
className: 'dismiss',
103-
css: buttonCSS,
104115
onclick: (e) => {
105116
localStorage.setItem(key, value);
106117
},
@@ -123,11 +134,12 @@ function preprocess(arg) {
123134
});
124135
}
125136

126-
function process(instance, el, { text, title, buttons }) {
137+
function process(instance, { text, title, buttons }) {
138+
const el = instance.element;
127139
if (text instanceof Translation) instance.setText(`${text}`);
128-
if (title instanceof Translation) el.find('> span:first').text(title);
129-
const $buttons = el.find('> button');
130-
toArray(buttons).forEach((button, i) => {
131-
if (button.text instanceof Translation) $buttons[i].textContent = button.text;
140+
if (title instanceof Translation) el.querySelector('.simpletoast-title').textContent = `${title}`;
141+
const rendered = el.querySelectorAll('.simpletoast-button');
142+
toArray(buttons).filter((button) => button?.text).forEach((button, i) => {
143+
if (button.text instanceof Translation) rendered[i].textContent = `${button.text}`;
132144
});
133145
}

‎src/utils/toastCss.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import toArray from './toArray.js';
2+
3+
function apply(element, style) {
4+
const previous = {};
5+
if (!element || !style) return previous;
6+
Object.keys(style).forEach((key) => {
7+
const value = style[key];
8+
if (typeof value === 'object') return;
9+
previous[key] = element.style[key];
10+
element.style[key] = value;
11+
});
12+
return previous;
13+
}
14+
15+
function hover(element, style) {
16+
if (!Object.keys(style).length) return;
17+
let previous = {};
18+
element.addEventListener('mouseenter', () => {
19+
previous = apply(element, style);
20+
});
21+
element.addEventListener('mouseleave', () => {
22+
apply(element, previous);
23+
previous = {};
24+
});
25+
}
26+
27+
export default function applyToastCss({ element }, css, buttons) {
28+
const style = css || {};
29+
apply(element, style.toast || style);
30+
31+
const title = element.querySelector('.simpletoast-title');
32+
if (title.hasChildNodes()) apply(title, style.title);
33+
const footer = element.querySelector('.simpletoast-footer');
34+
if (footer.hasChildNodes()) apply(footer, style.footer);
35+
36+
const rendered = element.querySelectorAll('.simpletoast-button');
37+
toArray(buttons).filter((button) => button?.text).forEach((button, index) => {
38+
const target = rendered[index];
39+
if (!target) return;
40+
apply(target, style.button);
41+
apply(target, button.css);
42+
hover(target, { ...style.button?.mouseOver, ...button.css?.mouseOver });
43+
});
44+
}

0 commit comments

Comments
 (0)