section_styling_appearance.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. from nicegui import events, ui
  2. from ..windows import browser_window, python_window
  3. from . import (
  4. add_style_documentation,
  5. colors_documentation,
  6. dark_mode_documentation,
  7. doc,
  8. element_filter_documentation,
  9. query_documentation,
  10. )
  11. doc.title('Styling & Appearance')
  12. @doc.demo('Styling', '''
  13. NiceGUI uses the [Quasar Framework](https://quasar.dev/) and hence has its full design power.
  14. 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):
  15. Have a look at [the Quasar documentation](https://quasar.dev/vue-components/button#design) for all styling props.
  16. Props with a leading `:` can contain JavaScript expressions that are evaluated on the client.
  17. You can also apply [Tailwind CSS](https://tailwindcss.com/) utility classes with the `classes` method.
  18. If you really need to apply CSS, you can use the `style` method. Here the delimiter is `;` instead of a blank space.
  19. All three functions also provide `remove` and `replace` parameters in case the predefined look is not wanted in a particular styling.
  20. ''')
  21. def design_demo():
  22. ui.radio(['x', 'y', 'z'], value='x').props('inline color=green')
  23. ui.button(icon='touch_app').props('outline round').classes('shadow-lg')
  24. ui.label('Stylish!').style('color: #6E93D6; font-size: 200%; font-weight: 300')
  25. doc.text('Try styling NiceGUI elements!', '''
  26. Try out how
  27. [Tailwind CSS classes](https://tailwindcss.com/),
  28. [Quasar props](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components),
  29. and CSS styles affect NiceGUI elements.
  30. ''')
  31. @doc.ui
  32. def styling_demo():
  33. with ui.row():
  34. ui.label('Select an element from those available and start styling it!').classes('mx-auto my-auto')
  35. select_element = ui.select({
  36. ui.label: 'ui.label',
  37. ui.checkbox: 'ui.checkbox',
  38. ui.switch: 'ui.switch',
  39. ui.input: 'ui.input',
  40. ui.textarea: 'ui.textarea',
  41. ui.button: 'ui.button',
  42. }, value=ui.button, on_change=lambda: live_demo_ui.refresh()).props('dense')
  43. @ui.refreshable
  44. def live_demo_ui():
  45. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  46. with python_window(classes='w-full max-w-[44rem]'):
  47. with ui.column().classes('w-full gap-2'):
  48. ui.markdown(f'''
  49. ```py
  50. from nicegui import ui
  51. element = {select_element.options[select_element.value]}('element')
  52. ```
  53. ''').classes('mb-[-0.25em]')
  54. with ui.row().classes('items-center gap-0 w-full'):
  55. def handle_classes(e: events.ValueChangeEventArguments):
  56. try:
  57. element.classes(replace=e.value)
  58. except ValueError:
  59. pass
  60. ui.markdown("`element.classes('`")
  61. ui.input(on_change=handle_classes).classes('text-mono grow').props('dense hide-bottom-space')
  62. ui.markdown("`')`")
  63. with ui.row().classes('items-center gap-0 w-full'):
  64. def handle_props(e: events.ValueChangeEventArguments):
  65. element.props.clear()
  66. element.props['label'] = 'Button'
  67. element.props['color'] = 'primary'
  68. try:
  69. element.props(e.value)
  70. except ValueError:
  71. pass
  72. element.update()
  73. ui.markdown("`element.props('`")
  74. ui.input(on_change=handle_props).classes('text-mono grow').props('dense hide-bottom-space')
  75. ui.markdown("`')`")
  76. with ui.row().classes('items-center gap-0 w-full'):
  77. def handle_style(e: events.ValueChangeEventArguments):
  78. try:
  79. element.style(replace=e.value)
  80. except ValueError:
  81. pass
  82. ui.markdown("`element.style('`")
  83. ui.input(on_change=handle_style).classes('text-mono grow').props('dense hide-bottom-space')
  84. ui.markdown("`')`")
  85. ui.markdown('''
  86. ```py
  87. ui.run()
  88. ```
  89. ''')
  90. with browser_window(classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  91. element: ui.element = select_element.value('element')
  92. live_demo_ui()
  93. @doc.demo('Tailwind CSS', '''
  94. [Tailwind CSS](https://tailwindcss.com/) is a CSS framework for rapidly building custom user interfaces.
  95. NiceGUI provides a fluent, auto-complete friendly interface for adding Tailwind classes to UI elements.
  96. You can discover available classes by navigating the methods of the `tailwind` property.
  97. The builder pattern allows you to chain multiple classes together (as shown with "Label A").
  98. You can also call the `tailwind` property with a list of classes (as shown with "Label B").
  99. Although this is very similar to using the `classes` method, it is more convenient for Tailwind classes due to auto-completion.
  100. Last but not least, you can also predefine a style and apply it to multiple elements (labels C and D).
  101. Note that sometimes Tailwind is overruled by Quasar styles, e.g. when using `ui.button('Button').tailwind('bg-red-500')`.
  102. This is a known limitation and not fully in our control.
  103. But we try to provide solutions like the `color` parameter: `ui.button('Button', color='red-500')`.
  104. ''')
  105. def tailwind_demo():
  106. from nicegui import Tailwind
  107. ui.label('Label A').tailwind.font_weight('extrabold').text_color('blue-600').background_color('orange-200')
  108. ui.label('Label B').tailwind('drop-shadow', 'font-bold', 'text-green-600')
  109. red_style = Tailwind().text_color('red-600').font_weight('bold')
  110. label_c = ui.label('Label C')
  111. red_style.apply(label_c)
  112. ui.label('Label D').tailwind(red_style)
  113. @doc.demo('Tailwind CSS Layers', '''
  114. Tailwind CSS' `@layer` directive allows you to define custom classes that can be used in your HTML.
  115. NiceGUI supports this feature by allowing you to add custom classes to the `components` layer.
  116. This way, you can define your own classes and use them in your UI elements.
  117. In the example below, we define a custom class `blue-box` and apply it to two labels.
  118. Note that the style tag is of type `text/tailwindcss` and not `text/css`.
  119. ''')
  120. def tailwind_layers():
  121. ui.add_head_html('''
  122. <style type="text/tailwindcss">
  123. @layer components {
  124. .blue-box {
  125. @apply bg-blue-500 p-12 text-center shadow-lg rounded-lg text-white;
  126. }
  127. }
  128. </style>
  129. ''')
  130. with ui.row():
  131. ui.label('Hello').classes('blue-box')
  132. ui.label('world').classes('blue-box')
  133. doc.intro(element_filter_documentation)
  134. doc.intro(query_documentation)
  135. doc.intro(colors_documentation)
  136. @doc.demo('CSS Variables', '''
  137. You can customize the appearance of NiceGUI by setting CSS variables.
  138. Currently, the following variables with their default values are available:
  139. - `--nicegui-default-padding: 1rem`
  140. - `--nicegui-default-gap: 1rem`
  141. ''')
  142. def css_variables_demo():
  143. # ui.add_css('''
  144. # :root {
  145. # --nicegui-default-padding: 0.5rem;
  146. # --nicegui-default-gap: 3rem;
  147. # }
  148. # ''')
  149. # with ui.card():
  150. # ui.label('small padding')
  151. # ui.label('large gap')
  152. # END OF DEMO
  153. with ui.card().classes('p-[0.5rem] gap-[3rem]'):
  154. ui.label('small padding')
  155. ui.label('large gap')
  156. @doc.demo("Overwrite Tailwind's Default Style", '''
  157. Tailwind resets the default style of HTML elements, like the font size of `h2` elements in this example.
  158. You can overwrite these defaults by adding a style tag with type `text/tailwindcss`.
  159. Without this type, the style will be evaluated too early and will be overwritten by Tailwind.
  160. ''')
  161. def overwrite_tailwind_style_demo():
  162. ui.add_head_html('''
  163. <style type="text/tailwindcss">
  164. h2 {
  165. font-size: 150%;
  166. }
  167. </style>
  168. ''')
  169. ui.html('<h2>Hello world!</h2>')
  170. doc.intro(dark_mode_documentation)
  171. doc.intro(add_style_documentation)