element_documentation.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.element)
  4. def main_demo() -> None:
  5. with ui.element('div').classes('p-2 bg-blue-100'):
  6. ui.label('inside a colored div')
  7. @doc.demo('Register event handlers', '''
  8. The event handler can be a Python function, a JavaScript function or a combination of both:
  9. - If you want to handle the event on the server with all (serializable) event arguments,
  10. use a Python ``handler``.
  11. - If you want to handle the event on the client side without emitting anything to the server,
  12. use ``js_handler`` with a JavaScript function handling the event.
  13. - If you want to handle the event on the server with a subset or transformed version of the event arguments,
  14. use ``js_handler`` with a JavaScript function emitting the transformed arguments using ``emit()``, and
  15. use a Python ``handler`` to handle these arguments on the server side.
  16. The ``js_handler`` can also decide to selectively emit arguments to the server,
  17. in which case the Python ``handler`` will not always be called.
  18. *Updated in version 2.18.0: Both handlers can be specified at the same time.*
  19. ''')
  20. def register_event_handlers() -> None:
  21. ui.button('Python handler') \
  22. .on('click',
  23. lambda e: ui.notify(f'click: ({e.args["clientX"]}, {e.args["clientY"]})'))
  24. ui.button('JavaScript handler') \
  25. .on('click',
  26. js_handler='(e) => alert(`click: (${e.clientX}, ${e.clientY})`)')
  27. ui.button('Combination') \
  28. .on('click',
  29. lambda e: ui.notify(f'click: {e.args}'),
  30. js_handler='(e) => emit(e.clientX, e.clientY)')
  31. @doc.demo('Move elements', '''
  32. This demo shows how to move elements between or within containers.
  33. ''')
  34. def move_elements() -> None:
  35. with ui.card() as a:
  36. ui.label('A')
  37. x = ui.label('X')
  38. with ui.card() as b:
  39. ui.label('B')
  40. ui.button('Move X to A', on_click=lambda: x.move(a))
  41. ui.button('Move X to B', on_click=lambda: x.move(b))
  42. ui.button('Move X to top', on_click=lambda: x.move(target_index=0))
  43. @doc.demo('Move elements to slots', '''
  44. This demo shows how to move elements between slots within an element.
  45. ''')
  46. def move_elements_to_slots() -> None:
  47. with ui.card() as card:
  48. name = ui.input('Name', value='Paul')
  49. name.add_slot('append')
  50. icon = ui.icon('face')
  51. ui.button('Move into input', on_click=lambda: icon.move(name, target_slot='append'))
  52. ui.button('Move out of input', on_click=lambda: icon.move(card))
  53. @doc.demo('Default props', '''
  54. You can set default props for all elements of a certain class.
  55. This way you can avoid repeating the same props over and over again.
  56. Default props only apply to elements created after the default props were set.
  57. Subclasses inherit the default props of their parent class.
  58. ''')
  59. def default_props() -> None:
  60. ui.button.default_props('rounded outline')
  61. ui.button('Button A')
  62. ui.button('Button B')
  63. # END OF DEMO
  64. ui.button.default_props(remove='rounded outline')
  65. @doc.demo('Default classes', '''
  66. You can set default classes for all elements of a certain class.
  67. This way you can avoid repeating the same classes over and over again.
  68. Default classes only apply to elements created after the default classes were set.
  69. Subclasses inherit the default classes of their parent class.
  70. ''')
  71. def default_classes() -> None:
  72. ui.label.default_classes('bg-blue-100 p-2')
  73. ui.label('Label A')
  74. ui.label('Label B')
  75. # END OF DEMO
  76. ui.label.default_classes(remove='bg-blue-100 p-2')
  77. @doc.demo('Default style', '''
  78. You can set a default style for all elements of a certain class.
  79. This way you can avoid repeating the same style over and over again.
  80. A default style only applies to elements created after the default style was set.
  81. Subclasses inherit the default style of their parent class.
  82. ''')
  83. def default_style() -> None:
  84. ui.label.default_style('color: tomato')
  85. ui.label('Label A')
  86. ui.label('Label B')
  87. # END OF DEMO
  88. ui.label.default_style(remove='color: tomato')
  89. doc.reference(ui.element)