Browse Source

Merge pull request #1970 from zauberzeug/nested-page-layout

Raise RuntimeError if a page layout element is nested
Falko Schindler 1 năm trước cách đây
mục cha
commit
9f592549f5
1 tập tin đã thay đổi với 11 bổ sung0 xóa
  1. 11 0
      nicegui/page_layout.py

+ 11 - 0
nicegui/page_layout.py

@@ -4,6 +4,7 @@ from . import context
 from .element import Element
 from .elements.mixins.value_element import ValueElement
 from .functions.html import add_body_html
+from .logging import log
 
 DrawerSides = Literal['left', 'right']
 
@@ -43,6 +44,7 @@ class Header(ValueElement):
         :param wrap: whether the header should wrap its content (default: `True`)
         :param add_scroll_padding: whether to automatically prevent link targets from being hidden behind the header (default: `True`)
         """
+        _check_current_slot()
         with context.get_client().layout:
             super().__init__(tag='q-header', value=value, on_value_change=None)
         self._classes.append('nicegui-header')
@@ -106,6 +108,7 @@ class Drawer(Element):
         :param top_corner: whether the drawer expands into the top corner (default: `False`)
         :param bottom_corner: whether the drawer expands into the bottom corner (default: `False`)
         """
+        _check_current_slot()
         with context.get_client().layout:
             super().__init__('q-drawer')
         if value is None:
@@ -224,6 +227,7 @@ class Footer(ValueElement):
         :param elevated: whether the footer should have a shadow (default: `False`)
         :param wrap: whether the footer should wrap its content (default: `True`)
         """
+        _check_current_slot()
         with context.get_client().layout:
             super().__init__(tag='q-footer', value=value, on_value_change=None)
         self.classes('nicegui-footer')
@@ -264,3 +268,10 @@ class PageSticky(Element):
         super().__init__('q-page-sticky')
         self._props['position'] = position
         self._props['offset'] = [x_offset, y_offset]
+
+
+def _check_current_slot() -> None:
+    parent = context.get_slot().parent
+    if parent != parent.client.content:
+        log.warning('Layout elements should not be nested but must be direct children of the page content. '
+                    'This will be raising an exception in NiceGUI 1.5')  # DEPRECATED