Skip to content

Commit 73f062a

Browse files
feildmasterclaude
andcommitted
feat: dismiss option for toast buttons
A toast button with dismiss: false no longer closes the toast after its handler runs, and event.preventDefault() keeps it open as well, next to the existing stopPropagation() opt-out. The default is unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent b926871 commit 73f062a

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type UnderScriptToastButton = {
3131
className?: UnderScriptToastClass;
3232
/** @deprecated Use `className` instead */
3333
css?: UnderScriptToastButtonStyle;
34+
dismiss?: boolean;
3435
onclick?(this: UnderScriptToast, event: MouseEvent, toast: UnderScriptToast): unknown;
3536
};
3637

‎src/utils/2.toasts.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ let ready = false;
1010
eventManager.on('underscript:ready', () => ready = true);
1111

1212
function dismissing(button) {
13-
if (!button) return button;
13+
if (!button || button.dismiss === false) return button;
1414
return {
1515
...button,
1616
onclick(event, instance) {
1717
try {
1818
if (typeof button.onclick === 'function') button.onclick.call(this, event, instance);
1919
} finally {
20-
if (!event.cancelBubble) instance.close('dismissed');
20+
if (!event.cancelBubble && !event.defaultPrevented) instance.close('dismissed');
2121
}
2222
},
2323
};

‎test/types/plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ plain.toast({
4343
title: 'Title',
4444
text: 'Text',
4545
className: { toast: 'dismissable', button: 'dismiss' },
46-
buttons: [{ text: 'OK', className: 'dismiss', onclick: () => {} }],
46+
buttons: [{ text: 'OK', className: 'dismiss', onclick: () => {} }, { text: 'Stay', dismiss: false }],
4747
css: { 'background-color': 'red', footer: { 'text-align': 'end' } },
4848
error: true,
4949
}).exists() satisfies boolean;

‎test/utils/toasts.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,55 @@ describe('toasts', function toastTests() {
150150
expect(instance.exists()).to.equal(true);
151151
});
152152

153+
it('keep the toast open when the handler prevents default', () => {
154+
const instance = toast({ text: 'a', buttons: { text: 'b', onclick(event) { event.preventDefault(); } } });
155+
buttons(instance)[0].click();
156+
expect(instance.exists()).to.equal(true);
157+
});
158+
159+
it('keep the toast open when a button sets dismiss to false', () => {
160+
const calls = [];
161+
const instance = toast({ text: 'a', buttons: { text: 'b', dismiss: false, onclick(event, self) { calls.push([this, self]); } } });
162+
buttons(instance)[0].click();
163+
expect(instance.exists()).to.equal(true);
164+
expect(calls).to.have.length(1);
165+
expect(calls[0][0]).to.equal(instance);
166+
expect(calls[0][1]).to.equal(instance);
167+
});
168+
169+
it('keep the toast open for a dismiss false button without a handler', () => {
170+
const instance = toast({ text: 'a', buttons: { text: 'b', dismiss: false } });
171+
buttons(instance)[0].click();
172+
expect(instance.exists()).to.equal(true);
173+
});
174+
175+
it('still close when a button sets dismiss to true', () => {
176+
const instance = toast({ text: 'a', buttons: { text: 'b', dismiss: true } });
177+
buttons(instance)[0].click();
178+
expect(instance.exists()).to.equal(false);
179+
});
180+
181+
it('let a dismiss false handler close the toast itself', () => {
182+
let reason;
183+
const instance = toast({
184+
text: 'a',
185+
buttons: { text: 'b', dismiss: false, onclick(event, self) { self.close('done'); } },
186+
onClose: (why) => { reason = why; },
187+
});
188+
buttons(instance)[0].click();
189+
expect(instance.exists()).to.equal(false);
190+
expect(reason).to.equal('done');
191+
});
192+
193+
it('only apply dismiss to the button that sets it', () => {
194+
const instance = toast({ text: 'a', buttons: [{ text: 'stay', dismiss: false }, { text: 'go' }] });
195+
const [stay, go] = buttons(instance);
196+
stay.click();
197+
expect(instance.exists()).to.equal(true);
198+
go.click();
199+
expect(instance.exists()).to.equal(false);
200+
});
201+
153202
it('keep the reason a handler closed with', () => {
154203
let reason;
155204
const instance = toast({

0 commit comments

Comments
 (0)