scroll_area_documentation.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. with ui.row():
  5. with ui.card().classes('w-48 h-32'):
  6. with ui.scroll_area().classes('w-full'):
  7. ui.label('I will scroll... ' * 10)
  8. with ui.card().classes('w-48 h-32'):
  9. ui.label('I will not scroll... ' * 10)
  10. def more() -> None:
  11. @text_demo('Handling Scroll Events', '''
  12. You can use the `on_scroll` argument in `ui.scroll_area` to handle scroll events.
  13. The callback receives a `ScrollEventArguments` object with the following attributes:
  14. - `sender`: the scroll_area object that generated the event
  15. - `client`: the matching client
  16. - `info`: the scroll area information as described in Quasar documentation ScrollArea API
  17. The scroll info is represented as a `ScrollInfo` dataclass
  18. ''')
  19. def scroll_events():
  20. number = ui.number(label='scroll position: ')
  21. with ui.card().classes('w-48 h-32'):
  22. with ui.scroll_area(on_scroll=lambda x: number.set_value(x.info.verticalPercentage)).classes('w-full'):
  23. ui.label('I will scroll... ' * 10)
  24. @text_demo('Setting the scroll position', '''
  25. You can use the `set_scroll_position` method of `ui.scroll_area` to programmatically set the scroll position.
  26. This can be useful for navigation or synchronization of multiple scroll_area elements
  27. ''')
  28. def scroll_events():
  29. with ui.row():
  30. with ui.column().classes('w-32'):
  31. number = ui.number(label='scroll position', value=0, min=0, max=1,
  32. on_change=lambda x: sa1.set_scroll_position(x.value)).classes('w-full')
  33. ui.button(icon='add', on_click=lambda: number.set_value(number.value + 0.1))
  34. ui.button(icon='remove', on_click=lambda: number.set_value(number.value - 0.1))
  35. with ui.card().classes('w-48 h-48'):
  36. with ui.scroll_area(
  37. on_scroll=lambda x: sa2.set_scroll_position(x.info.verticalPercentage)
  38. ).classes('w-full') as sa1:
  39. ui.label('I will scroll... ' * 100)
  40. with ui.card().classes('w-48 h-48'):
  41. with ui.scroll_area().classes('w-full') as sa2:
  42. ui.label('I will scroll... ' * 100)