holoviz/panel · #9119
fix: Lay out nested plots before laying out a React child figure
panel/models/layout.ts36 + / 0 −
@@ -1,5 +1,6 @@ import {div, px} from "@bokehjs/core/dom" import type {DOMView} from "@bokehjs/core/dom_view"+import type {View} from "@bokehjs/core/view" import {isArray} from "@bokehjs/core/util/types" import {unreachable} from "@bokehjs/core/util/assert" import {WidgetView} from "@bokehjs/models/widgets/widget"@@ -14,6 +15,36 @@ import { render_resource_error, } from "./resources" +function* unmanaged_layout_views(view: View): Generator<LayoutDOMView> {+ for (const child of view.children()) {+ if (child instanceof LayoutDOMView && !(view instanceof LayoutDOMView)) {+ yield child+ }+ yield* unmanaged_layout_views(child)+ }+}++/**+ * Lays out the layout views nested in `view` that no layout manages, e.g.+ * the internal plot of a `SizeBar`, if they have not been laid out yet.+ *+ * Such a view only builds its layout in its own `after_render`, but the view+ * containing it may read that layout in its `update_layout`, which here can+ * run first. Once the view has been laid out it builds a fresh layout on+ * every `after_render`, so the caller must lay out `view` again if any of+ * the returned views ends up with a different layout. Can be removed once+ * https://github.com/bokeh/bokeh/issues/15794 is fixed.+ */+export function prepare_nested_layouts(view: View): LayoutDOMView[] {+ const nested = [...unmanaged_layout_views(view)]+ for (const nested_view of nested) {+ if (nested_view.layout == null) {+ nested_view.update_layout()+ }+ }+ return nested+}+ /** * Re-renders a view, including its layout. *@@ -36,8 +67,13 @@ import { export function rerender_view(view: DOMView, layout: boolean = true): void { if (view instanceof LayoutDOMView) { view.render()+ const nested = prepare_nested_layouts(view) view.update_layout()+ const nested_layouts = nested.map((nested_view) => nested_view.layout) view.r_after_render()+ if (nested.some((nested_view, i) => nested_view.layout !== nested_layouts[i])) {+ view.update_layout()+ } if (layout) { view.compute_layout() }panel/models/reactive_esm.ts4 + / 1 −
@@ -19,7 +19,7 @@ import {Model} from "@bokehjs/model" import {serializeEvent} from "./event-to-object" import {DOMEvent} from "./html"-import {HTMLBox, HTMLBoxView, rerender_view, reset_size, set_size} from "./layout"+import {HTMLBox, HTMLBoxView, prepare_nested_layouts, rerender_view, reset_size, set_size} from "./layout" import {resource_url, resources} from "./resources" import {convertUndefined, formatError} from "./util" @@ -380,6 +380,9 @@ export class ReactiveESMView extends HTMLBoxView { } for (const child_view of this.child_views) { if (child_view instanceof LayoutDOMView) {+ if (!(child_view instanceof ReactiveESMView)) {+ prepare_nested_layouts(child_view)+ } child_view.update_layout() } }panel/tests/ui/test_custom.py29 + / 0 −
@@ -7,6 +7,7 @@ pytest.importorskip("playwright") +from bokeh.models import SizeBar from bokeh.plotting import figure from playwright.sync_api import expect @@ -1334,6 +1335,34 @@ def test_children_bokeh_plot_added_with_layout_before_mount(page, component): assert errors == [] +@pytest.mark.internet+@pytest.mark.parametrize('component', [JSChildren, ReactChildren])+def test_children_bokeh_plot_with_size_bar(page, component):+ errors, msgs = [], []+ page.on('pageerror', lambda error: errors.append(str(error)))+ page.on('console', lambda msg: msgs.append(msg))+ plot = figure(height=300, sizing_mode='stretch_width')+ renderer = plot.circle([1, 2, 3], [1, 2, 3], radius=[0.1, 0.2, 0.3])+ plot.add_layout(SizeBar(renderer=renderer), 'right')+ example = component(objects=[Bokeh(plot, sizing_mode='stretch_width')], sizing_mode='stretch_width')++ page.set_viewport_size({'width': 900, 'height': 500})+ serve_component(page, example)++ size_bar = page.locator('.bk-SizeBar .bk-Canvas')+ expect(size_bar).to_be_visible()+ wait_until(lambda: (box := size_bar.bounding_box()) and box['width'] > 0 and box['height'] > 0, page)+ x = size_bar.bounding_box()['x']++ page.set_viewport_size({'width': 600, 'height': 500})++ wait_until(lambda: size_bar.bounding_box()['x'] <= x - 250, page)+ box = size_bar.bounding_box()+ assert box['width'] > 0 and box['height'] > 0+ assert errors == []+ assert [msg for msg in msgs if msg.type == 'error' and 'favicon' not in msg.location['url']] == []++ JS_CODE_BEFORE = """ export function render() { const h1 = document.createElement('h1')