styling_appearance.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from nicegui import events, ui
  2. from ...demo import browser_window, python_window
  3. from ...model import SectionDocumentation
  4. from ..more.colors_documentation import ColorsDocumentation
  5. from ..more.dark_mode_documentation import DarkModeDocumentation
  6. from ..more.query_documentation import QueryDocumentation
  7. class StylingAppearanceDocumentation(SectionDocumentation, title='Styling & Appearance', name='styling_appearance'):
  8. def content(self) -> None:
  9. @self.demo('Styling', '''
  10. NiceGUI uses the [Quasar Framework](https://quasar.dev/) version 1.0 and hence has its full design power.
  11. Each NiceGUI element provides a `props` method whose content is passed [to the Quasar component](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components):
  12. Have a look at [the Quasar documentation](https://quasar.dev/vue-components/button#design) for all styling props.
  13. Props with a leading `:` can contain JavaScript expressions that are evaluated on the client.
  14. You can also apply [Tailwind CSS](https://tailwindcss.com/) utility classes with the `classes` method.
  15. If you really need to apply CSS, you can use the `style` method. Here the delimiter is `;` instead of a blank space.
  16. All three functions also provide `remove` and `replace` parameters in case the predefined look is not wanted in a particular styling.
  17. ''')
  18. def design_demo():
  19. ui.radio(['x', 'y', 'z'], value='x').props('inline color=green')
  20. ui.button(icon='touch_app').props('outline round').classes('shadow-lg')
  21. ui.label('Stylish!').style('color: #6E93D6; font-size: 200%; font-weight: 300')
  22. self.text('Try styling NiceGUI elements!', '''
  23. Try out how
  24. [Tailwind CSS classes](https://tailwindcss.com/),
  25. [Quasar props](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components),
  26. and CSS styles affect NiceGUI elements.
  27. ''')
  28. @self.ui
  29. def styling_demo():
  30. with ui.row():
  31. ui.label('Select an element from those available and start styling it!').classes('mx-auto my-auto')
  32. select_element = ui.select({
  33. ui.label: 'ui.label',
  34. ui.checkbox: 'ui.checkbox',
  35. ui.switch: 'ui.switch',
  36. ui.input: 'ui.input',
  37. ui.textarea: 'ui.textarea',
  38. ui.button: 'ui.button',
  39. }, value=ui.button, on_change=lambda: live_demo_ui.refresh()).props('dense')
  40. @ui.refreshable
  41. def live_demo_ui():
  42. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  43. with python_window(classes='w-full max-w-[44rem]'):
  44. with ui.column().classes('w-full gap-4'):
  45. ui.markdown(f'''
  46. ```py
  47. from nicegui import ui
  48. element = {select_element.options[select_element.value]}('element')
  49. ```
  50. ''').classes('mb-[-0.25em]')
  51. with ui.row().classes('items-center gap-0 w-full px-2'):
  52. def handle_classes(e: events.ValueChangeEventArguments):
  53. try:
  54. element.classes(replace=e.value)
  55. except ValueError:
  56. pass
  57. ui.markdown("`element.classes('`")
  58. ui.input(on_change=handle_classes).classes('mt-[-0.5em] text-mono grow').props('dense')
  59. ui.markdown("`')`")
  60. with ui.row().classes('items-center gap-0 w-full px-2'):
  61. def handle_props(e: events.ValueChangeEventArguments):
  62. element._props = {'label': 'Button', 'color': 'primary'}
  63. try:
  64. element.props(e.value)
  65. except ValueError:
  66. pass
  67. element.update()
  68. ui.markdown("`element.props('`")
  69. ui.input(on_change=handle_props).classes('mt-[-0.5em] text-mono grow').props('dense')
  70. ui.markdown("`')`")
  71. with ui.row().classes('items-center gap-0 w-full px-2'):
  72. def handle_style(e: events.ValueChangeEventArguments):
  73. try:
  74. element.style(replace=e.value)
  75. except ValueError:
  76. pass
  77. ui.markdown("`element.style('`")
  78. ui.input(on_change=handle_style).classes('mt-[-0.5em] text-mono grow').props('dense')
  79. ui.markdown("`')`")
  80. ui.markdown('''
  81. ```py
  82. ui.run()
  83. ```
  84. ''')
  85. with browser_window(classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  86. element: ui.element = select_element.value("element")
  87. live_demo_ui()
  88. @self.demo('Tailwind CSS', '''
  89. [Tailwind CSS](https://tailwindcss.com/) is a CSS framework for rapidly building custom user interfaces.
  90. NiceGUI provides a fluent, auto-complete friendly interface for adding Tailwind classes to UI elements.
  91. You can discover available classes by navigating the methods of the `tailwind` property.
  92. The builder pattern allows you to chain multiple classes together (as shown with "Label A").
  93. You can also call the `tailwind` property with a list of classes (as shown with "Label B").
  94. Although this is very similar to using the `classes` method, it is more convenient for Tailwind classes due to auto-completion.
  95. Last but not least, you can also predefine a style and apply it to multiple elements (labels C and D).
  96. Note that sometimes Tailwind is overruled by Quasar styles, e.g. when using `ui.button('Button').tailwind('bg-red-500')`.
  97. This is a known limitation and not fully in our control.
  98. But we try to provide solutions like the `color` parameter: `ui.button('Button', color='red-500')`.
  99. ''')
  100. def tailwind_demo():
  101. from nicegui import Tailwind
  102. ui.label('Label A').tailwind.font_weight('extrabold').text_color('blue-600').background_color('orange-200')
  103. ui.label('Label B').tailwind('drop-shadow', 'font-bold', 'text-green-600')
  104. red_style = Tailwind().text_color('red-600').font_weight('bold')
  105. label_c = ui.label('Label C')
  106. red_style.apply(label_c)
  107. ui.label('Label D').tailwind(red_style)
  108. self.intro(QueryDocumentation())
  109. self.intro(ColorsDocumentation())
  110. self.intro(DarkModeDocumentation())