dialog_documentation.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.dialog)
  4. def main_demo() -> None:
  5. with ui.dialog() as dialog, ui.card():
  6. ui.label('Hello world!')
  7. ui.button('Close', on_click=dialog.close)
  8. ui.button('Open a dialog', on_click=dialog.open)
  9. @doc.demo('Awaitable dialog', '''
  10. Dialogs can be awaited.
  11. Use the `submit` method to close the dialog and return a result.
  12. Canceling the dialog by clicking in the background or pressing the escape key yields `None`.
  13. ''')
  14. def async_dialog_demo():
  15. with ui.dialog() as dialog, ui.card():
  16. ui.label('Are you sure?')
  17. with ui.row():
  18. ui.button('Yes', on_click=lambda: dialog.submit('Yes'))
  19. ui.button('No', on_click=lambda: dialog.submit('No'))
  20. async def show():
  21. result = await dialog
  22. ui.notify(f'You chose {result}')
  23. ui.button('Await a dialog', on_click=show)
  24. @doc.demo('Replacing content', '''
  25. The content of a dialog can be changed.
  26. ''')
  27. def replace_content():
  28. def replace():
  29. dialog.clear()
  30. with dialog, ui.card().classes('w-64 h-64'):
  31. ui.label('New Content')
  32. dialog.open()
  33. with ui.dialog() as dialog, ui.card():
  34. ui.label('Hello world!')
  35. ui.button('Open', on_click=dialog.open)
  36. ui.button('Replace', on_click=replace)
  37. @doc.demo('Events', '''
  38. Dialogs emit events when they are opened or closed.
  39. See the [Quasar documentation](https://quasar.dev/vue-components/dialog) for more information.
  40. ''')
  41. def events():
  42. with ui.dialog().props('backdrop-filter="blur(8px) brightness(40%)"') as dialog:
  43. ui.label('Press ESC to close').classes('text-3xl text-white')
  44. dialog.on('show', lambda: ui.notify('Dialog opened'))
  45. dialog.on('hide', lambda: ui.notify('Dialog closed'))
  46. dialog.on('escape-key', lambda: ui.notify('ESC pressed'))
  47. ui.button('Open', on_click=dialog.open)
  48. doc.reference(ui.dialog)