Skip to content

Commit

Permalink
Merge pull request #2380 from zauberzeug/dismiss
Browse files Browse the repository at this point in the history
Ability to dismiss notifications
  • Loading branch information
falkoschindler authored Jan 15, 2024
2 parents 56c755a + 6f0f46a commit d6c109d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions nicegui/elements/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export default {
updated() {
this.notification(this.options);
},
methods: {
dismiss() {
this.notification();
},
},
props: {
options: Object,
},
Expand Down
5 changes: 5 additions & 0 deletions nicegui/elements/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def __init__(self,
Displays a notification on the screen.
In contrast to `ui.notify`, this element allows to update the notification message and other properties once the notification is displayed.
The notification can be removed with `dismiss()`.
:param message: content of the notification
:param position: position on the screen ("top-left", "top-right", "bottom-left", "bottom-right", "top", "bottom", "left", "right" or "center", default: "bottom")
Expand Down Expand Up @@ -163,3 +164,7 @@ def close_button(self) -> Union[bool, str]:
def close_button(self, value: Union[bool, str]) -> None:
self._props['options']['closeBtn'] = value
self.update()

def dismiss(self) -> None:
"""Dismiss the notification."""
self.run_method('dismiss')
14 changes: 14 additions & 0 deletions tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ def test_close_button(screen: Screen):
screen.wait(1.5)
screen.should_not_contain('Hi!')
assert len(b.client.layout.default_slot.children) == 1


def test_dismiss(screen: Screen):
n = ui.notification('Hi!', timeout=None)
b = ui.button('Dismiss', on_click=n.dismiss)

screen.open('/')
screen.should_contain('Hi!')
assert len(b.client.layout.default_slot.children) == 2
screen.wait(1)
screen.click('Dismiss')
screen.wait(1.5)
screen.should_not_contain('Hi!')
assert len(b.client.layout.default_slot.children) == 1
4 changes: 3 additions & 1 deletion website/documentation/content/notification_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ def main_demo() -> None:
import asyncio

async def compute():
n = ui.notification()
n = ui.notification(timeout=None)
for i in range(10):
n.message = f'Computing {i/10:.0%}'
n.spinner = True
await asyncio.sleep(0.2)
n.message = 'Done!'
n.spinner = False
await asyncio.sleep(1)
n.dismiss()

ui.button('Compute', on_click=compute)

Expand Down

0 comments on commit d6c109d

Please sign in to comment.