styling_appearance.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from nicegui import events, ui
  2. from ..demo import browser_window, python_window
  3. from ..tools import load_demo, subheading, text_demo
  4. name = 'styling_appearance'
  5. title = 'Styling & Appearance'
  6. def intro() -> None:
  7. ...
  8. def content() -> None:
  9. @text_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. subheading('Try styling NiceGUI elements!')
  23. ui.markdown('''
  24. Try out how
  25. [Tailwind CSS classes](https://tailwindcss.com/),
  26. [Quasar props](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components),
  27. and CSS styles affect NiceGUI elements.
  28. ''').classes('bold-links arrow-links mb-[-1rem]')
  29. with ui.row():
  30. ui.label('Select an element from those available and start styling it!').classes('mx-auto my-auto')
  31. select_element = ui.select({
  32. ui.label: 'ui.label',
  33. ui.checkbox: 'ui.checkbox',
  34. ui.switch: 'ui.switch',
  35. ui.input: 'ui.input',
  36. ui.textarea: 'ui.textarea',
  37. ui.button: 'ui.button',
  38. }, value=ui.button, on_change=lambda: live_demo_ui.refresh()).props('dense')
  39. @ui.refreshable
  40. def live_demo_ui():
  41. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  42. with python_window(classes='w-full max-w-[44rem]'):
  43. with ui.column().classes('w-full gap-4'):
  44. ui.markdown(f'''
  45. ```py
  46. from nicegui import ui
  47. element = {select_element.options[select_element.value]}('element')
  48. ```
  49. ''').classes('mb-[-0.25em]')
  50. with ui.row().classes('items-center gap-0 w-full px-2'):
  51. def handle_classes(e: events.ValueChangeEventArguments):
  52. try:
  53. element.classes(replace=e.value)
  54. except ValueError:
  55. pass
  56. ui.markdown("`element.classes('`")
  57. ui.input(on_change=handle_classes).classes('mt-[-0.5em] text-mono grow').props('dense')
  58. ui.markdown("`')`")
  59. with ui.row().classes('items-center gap-0 w-full px-2'):
  60. def handle_props(e: events.ValueChangeEventArguments):
  61. element._props = {'label': 'Button', 'color': 'primary'}
  62. try:
  63. element.props(e.value)
  64. except ValueError:
  65. pass
  66. element.update()
  67. ui.markdown("`element.props('`")
  68. ui.input(on_change=handle_props).classes('mt-[-0.5em] text-mono grow').props('dense')
  69. ui.markdown("`')`")
  70. with ui.row().classes('items-center gap-0 w-full px-2'):
  71. def handle_style(e: events.ValueChangeEventArguments):
  72. try:
  73. element.style(replace=e.value)
  74. except ValueError:
  75. pass
  76. ui.markdown("`element.style('`")
  77. ui.input(on_change=handle_style).classes('mt-[-0.5em] text-mono grow').props('dense')
  78. ui.markdown("`')`")
  79. ui.markdown('''
  80. ```py
  81. ui.run()
  82. ```
  83. ''')
  84. with browser_window(classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  85. element: ui.element = select_element.value("element")
  86. live_demo_ui()
  87. @text_demo('Tailwind CSS', '''
  88. [Tailwind CSS](https://tailwindcss.com/) is a CSS framework for rapidly building custom user interfaces.
  89. NiceGUI provides a fluent, auto-complete friendly interface for adding Tailwind classes to UI elements.
  90. You can discover available classes by navigating the methods of the `tailwind` property.
  91. The builder pattern allows you to chain multiple classes together (as shown with "Label A").
  92. You can also call the `tailwind` property with a list of classes (as shown with "Label B").
  93. Although this is very similar to using the `classes` method, it is more convenient for Tailwind classes due to auto-completion.
  94. Last but not least, you can also predefine a style and apply it to multiple elements (labels C and D).
  95. Note that sometimes Tailwind is overruled by Quasar styles, e.g. when using `ui.button('Button').tailwind('bg-red-500')`.
  96. This is a known limitation and not fully in our control.
  97. But we try to provide solutions like the `color` parameter: `ui.button('Button', color='red-500')`.
  98. ''')
  99. def tailwind_demo():
  100. from nicegui import Tailwind
  101. ui.label('Label A').tailwind.font_weight('extrabold').text_color('blue-600').background_color('orange-200')
  102. ui.label('Label B').tailwind('drop-shadow', 'font-bold', 'text-green-600')
  103. red_style = Tailwind().text_color('red-600').font_weight('bold')
  104. label_c = ui.label('Label C')
  105. red_style.apply(label_c)
  106. ui.label('Label D').tailwind(red_style)
  107. load_demo(ui.query)
  108. load_demo(ui.colors)
  109. load_demo(ui.dark_mode)