section_styling_appearance.py 8.4 KB

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