Skip to content

Commit 985cc7e

Browse files
committed
make toasts disappear after dur seconds.
1 parent 9d93c6b commit 985cc7e

File tree

4 files changed

+56
-11
lines changed

4 files changed

+56
-11
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,4 @@ cython_debug/
163163
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164164
# and can be added to the global gitignore or merged into this file. For a more nuclear
165165
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
166-
#.idea/
166+
.idea/

docs/api_reference/api_reference.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ def ex_toasts2():
434434
fn2code_string(ex_toasts1),
435435
P("To define toast colors, set the class of the alert wrapped by the toast:"),
436436
fn2code_string(ex_toasts2),
437+
P("Toasts will disappear automatically after 5 seconds. To change the duration of the toast set the `dur` param like this `Toast('Content', dur=10)`."),
437438
Toast, ToastHT, ToastVT,
438439
title="Alerts & Toasts")
439440

monsterui/daisy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ def _generate_next_value_(name, start, count, last_values): return f'toast-{name
109109
def Toast(*c, # Content for toast (often test)
110110
cls='', # Classes for toast (often `ToastHT` and `ToastVT` options)
111111
alert_cls='', # classes for altert (often `AlertT` options)
112+
dur=5.0, # no. of seconds before the toast disappears
112113
**kwargs # Additional args for outer container (`Div` tag)
113114
)->FT: # Div(Alert(...), cls='toast')
114115
"Toasts are stacked announcements, positioned on the corner of page."
115116
a = Alert(*c, cls=alert_cls)
116-
return Div(a, cls=('toast', stringify(cls)), **kwargs)
117+
_id = fh.unqid()
118+
js = Script('''(()=>setTimeout(()=>document.querySelector('[data-mui="%s"]').remove(),%s))()'''%(_id,dur*1000))
119+
return Div(a, js, data_mui=_id, cls=('toast', stringify(cls)), **kwargs)

nbs/03_daisy.ipynb

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,14 @@
351351
"def Toast(*c, # Content for toast (often test)\n",
352352
" cls='', # Classes for toast (often `ToastHT` and `ToastVT` options)\n",
353353
" alert_cls='', # classes for altert (often `AlertT` options)\n",
354+
" dur=5.0, # no. of seconds before the toast disappears\n",
354355
" **kwargs # Additional args for outer container (`Div` tag)\n",
355356
" )->FT: # Div(Alert(...), cls='toast')\n",
356357
" \"Toasts are stacked announcements, positioned on the corner of page.\"\n",
357358
" a = Alert(*c, cls=alert_cls)\n",
358-
" return Div(a, cls=('toast', stringify(cls)), **kwargs)"
359+
" _id = fh.unqid()\n",
360+
" js = Script('''(()=>setTimeout(()=>document.querySelector('[data-mui=\"%s\"]').remove(),%s))()'''%(_id,dur*1000))\n",
361+
" return Div(a, js, data_mui=_id, cls=('toast', stringify(cls)), **kwargs)"
359362
]
360363
},
361364
{
@@ -376,15 +379,15 @@
376379
"data": {
377380
"text/markdown": [
378381
"```html\n",
379-
"<div class=\"toast toast-start toast-middle\">\n",
382+
"<div data-mui=\"_yRjKNgMBRCah1Xnjm5yP-w\" class=\"toast toast-start toast-middle\">\n",
380383
" <div class=\"alert \" role=\"alert\">\n",
381384
"<span>New message arrived.</span> </div>\n",
382-
"</div>\n",
385+
"<script>(()=&gt;setTimeout(()=&gt;document.querySelector(&#x27;[data-mui=&quot;_yRjKNgMBRCah1Xnjm5yP-w&quot;]&#x27;).remove(),5000.0))()</script></div>\n",
383386
"\n",
384387
"```"
385388
],
386389
"text/plain": [
387-
"div((div((span(('New message arrived.',),{}),),{'class': 'alert ', 'role': 'alert'}),),{'class': 'toast toast-start toast-middle'})"
390+
"div((div((span(('New message arrived.',),{}),),{'class': 'alert ', 'role': 'alert'}), script(('(()=>setTimeout(()=>document.querySelector(\\'[data-mui=\"_yRjKNgMBRCah1Xnjm5yP-w\"]\\').remove(),5000.0))()',),{})),{'data-mui': '_yRjKNgMBRCah1Xnjm5yP-w', 'class': 'toast toast-start toast-middle'})"
388391
]
389392
},
390393
"execution_count": null,
@@ -414,15 +417,15 @@
414417
"data": {
415418
"text/markdown": [
416419
"```html\n",
417-
"<div class=\"toast \">\n",
420+
"<div data-mui=\"_HJB8HS8NTsOSXq5TJbCaWA\" class=\"toast \">\n",
418421
" <div class=\"alert alert-info\" role=\"alert\">\n",
419422
"<span>New message arrived.</span> </div>\n",
420-
"</div>\n",
423+
"<script>(()=&gt;setTimeout(()=&gt;document.querySelector(&#x27;[data-mui=&quot;_HJB8HS8NTsOSXq5TJbCaWA&quot;]&#x27;).remove(),5000.0))()</script></div>\n",
421424
"\n",
422425
"```"
423426
],
424427
"text/plain": [
425-
"div((div((span(('New message arrived.',),{}),),{'class': 'alert alert-info', 'role': 'alert'}),),{'class': 'toast '})"
428+
"div((div((span(('New message arrived.',),{}),),{'class': 'alert alert-info', 'role': 'alert'}), script(('(()=>setTimeout(()=>document.querySelector(\\'[data-mui=\"_HJB8HS8NTsOSXq5TJbCaWA\"]\\').remove(),5000.0))()',),{})),{'data-mui': '_HJB8HS8NTsOSXq5TJbCaWA', 'class': 'toast '})"
426429
]
427430
},
428431
"execution_count": null,
@@ -434,6 +437,44 @@
434437
"Toast(\"New message arrived.\", alert_cls=AlertT.info)"
435438
]
436439
},
440+
{
441+
"cell_type": "markdown",
442+
"id": "b380807d",
443+
"metadata": {},
444+
"source": [
445+
"Set the Toast duration to 10 seconds."
446+
]
447+
},
448+
{
449+
"cell_type": "code",
450+
"execution_count": null,
451+
"id": "180451d1",
452+
"metadata": {},
453+
"outputs": [
454+
{
455+
"data": {
456+
"text/markdown": [
457+
"```html\n",
458+
"<div data-mui=\"_Wjau44fWRlSG_WP73WgL6w\" class=\"toast \">\n",
459+
" <div class=\"alert alert-info\" role=\"alert\">\n",
460+
"<span>New message arrived.</span> </div>\n",
461+
"<script>(()=&gt;setTimeout(()=&gt;document.querySelector(&#x27;[data-mui=&quot;_Wjau44fWRlSG_WP73WgL6w&quot;]&#x27;).remove(),10000.0))()</script></div>\n",
462+
"\n",
463+
"```"
464+
],
465+
"text/plain": [
466+
"div((div((span(('New message arrived.',),{}),),{'class': 'alert alert-info', 'role': 'alert'}), script(('(()=>setTimeout(()=>document.querySelector(\\'[data-mui=\"_Wjau44fWRlSG_WP73WgL6w\"]\\').remove(),10000.0))()',),{})),{'data-mui': '_Wjau44fWRlSG_WP73WgL6w', 'class': 'toast '})"
467+
]
468+
},
469+
"execution_count": null,
470+
"metadata": {},
471+
"output_type": "execute_result"
472+
}
473+
],
474+
"source": [
475+
"Toast(\"New message arrived.\", alert_cls=AlertT.info, dur=10.0)"
476+
]
477+
},
437478
{
438479
"cell_type": "code",
439480
"execution_count": null,
@@ -456,9 +497,9 @@
456497
],
457498
"metadata": {
458499
"kernelspec": {
459-
"display_name": "python3",
500+
"display_name": "python",
460501
"language": "python",
461-
"name": "python3"
502+
"name": "python"
462503
}
463504
},
464505
"nbformat": 4,

0 commit comments

Comments
 (0)