Skip to content

Commit

Permalink
Close #140. Update _maybeResize() to handle an empty lmWidget container
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Mar 19, 2024
1 parent 6719b0e commit 4453e64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions js/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,32 @@ class IPyWidgetOutput extends Shiny.OutputBinding {
this._maybeResize(lmWidget);
}
_maybeResize(lmWidget: HTMLElement): void {
const impl = lmWidget.children[0];
if (impl.children.length > 0) {
return this._doResize(impl);
if (this._hasImplementation(lmWidget)) {
return this._doResize();
}

// Some widget implementation (e.g., ipyleaflet, pydeck) won't actually
// have rendered to the DOM at this point, so wait until they do
const mo = new MutationObserver((mutations) => {
if (impl.children.length > 0) {
if (this._hasImplementation(lmWidget)) {
mo.disconnect();
this._doResize(impl);
this._doResize();
}
});

mo.observe(impl, {childList: true});
mo.observe(lmWidget, {childList: true});
}
_doResize(impl: Element): void {
_doResize(): void {
// Trigger resize event to force layout (setTimeout() is needed for altair)
// TODO: debounce this call?
setTimeout(() => {
window.dispatchEvent(new Event('resize'))
}, 0);
}
_hasImplementation(lmWidget: HTMLElement): boolean {
const impl = lmWidget.children[0];
return impl && impl.children.length > 0;
}
}

Shiny.outputBindings.register(new IPyWidgetOutput(), "shiny.IPyWidgetOutput");
Expand Down
2 changes: 1 addition & 1 deletion shinywidgets/static/output.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4453e64

Please sign in to comment.