diff --git a/js/src/output.ts b/js/src/output.ts index cfaf427..c92feb3 100644 --- a/js/src/output.ts +++ b/js/src/output.ts @@ -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"); diff --git a/shinywidgets/static/output.js b/shinywidgets/static/output.js index 60faa5e..fc2289a 100644 --- a/shinywidgets/static/output.js +++ b/shinywidgets/static/output.js @@ -36,7 +36,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpac \***********************/ /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { -eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _jupyter_widgets_html_manager__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @jupyter-widgets/html-manager */ \"@jupyter-widgets/html-manager\");\n/* harmony import */ var _jupyter_widgets_html_manager__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_jupyter_widgets_html_manager__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _comm__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./comm */ \"./src/comm.ts\");\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils */ \"./src/utils.ts\");\nvar _a;\n\n\n\n/******************************************************************************\n * Define a custom HTMLManager for use with Shiny\n ******************************************************************************/\nclass OutputManager extends _jupyter_widgets_html_manager__WEBPACK_IMPORTED_MODULE_0__.HTMLManager {\n // In a soon-to-be-released version of @jupyter-widgets/html-manager,\n // display_view()'s first \"dummy\" argument will be removed... this shim simply\n // makes it so that our manager can work with either version\n // https://github.com/jupyter-widgets/ipywidgets/commit/159bbe4#diff-45c126b24c3c43d2cee5313364805c025e911c4721d45ff8a68356a215bfb6c8R42-R43\n async display_view(view, options) {\n const n_args = super.display_view.length;\n if (n_args === 3) {\n return super.display_view({}, view, options);\n }\n else {\n // @ts-ignore\n return super.display_view(view, options);\n }\n }\n}\n// Define our own custom module loader for Shiny\nconst shinyRequireLoader = async function (moduleName, moduleVersion) {\n // shiny provides require.js and also sets `define.amd=false` to prevent