-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(jupyter): Fix initial viewport sizing and React 18 migration #3253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright contributors to the kepler.gl project | ||
|
|
||
| import React, {useEffect, useState, useRef} from 'react'; | ||
| import {useEffect, useState, useRef} from 'react'; | ||
| import styled from 'styled-components'; | ||
| const ReactHelmet = require('react-helmet'); | ||
| const Helmet = ReactHelmet ? ReactHelmet.Helmet : null; | ||
|
|
@@ -58,19 +58,18 @@ function App() { | |
|
|
||
| const width = rootElm.current.offsetWidth; | ||
| const height = rootElm.current.offsetHeight; | ||
| const dimensionToSet = { | ||
| ...(width && width !== windowDimension.width ? {width} : {}), | ||
| ...(height && height !== windowDimension.height ? {height} : {}) | ||
| }; | ||
|
|
||
| setDimension(dimensionToSet); | ||
| if (width && height) { | ||
| setDimension({width, height}); | ||
| } | ||
| }; | ||
|
|
||
| // in Jupyter Lab, parent component has transition when window resize. | ||
| // in Jupyter Lab, parent component has transition when window resize. | ||
| // need to delay call to get the final parent width, | ||
| const resizeDelay = () => window.setTimeout(handleResize, 500); | ||
|
|
||
| useEffect(() => { | ||
| // Call handleResize on mount to set initial dimensions | ||
| handleResize(); | ||
| window.addEventListener('resize', resizeDelay); | ||
| return () => window.removeEventListener('resize', resizeDelay); | ||
| }, []); | ||
|
yharby marked this conversation as resolved.
Comment on lines
70
to
75
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,51 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| // Copyright contributors to the kepler.gl project | ||
|
|
||
| import React from 'react'; | ||
| import ReactDOM from 'react-dom'; | ||
| import {useEffect, useRef} from 'react'; | ||
| import {createRoot} from 'react-dom/client'; | ||
| import {Provider} from 'react-redux'; | ||
| import App from './app'; | ||
| import Window from 'global/window'; | ||
| import {addDataConfigToKeplerGl} from '../kepler.gl'; | ||
|
|
||
| function renderRoot({id, store, ele}) { | ||
| const Root = () => ( | ||
| // Store root instances to avoid creating multiple roots for the same element | ||
| const rootInstances = new WeakMap(); | ||
|
|
||
| // Separate component to handle data loading after mount | ||
| function DataLoader({store, onRenderComplete}) { | ||
| const hasLoadedData = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| // This runs AFTER component tree is mounted and Provider is fully subscribed | ||
| // Load data from Window.__keplerglDataConfig (HTML export mode) | ||
| if (!hasLoadedData.current && Window.__keplerglDataConfig) { | ||
| hasLoadedData.current = true; | ||
| const {data, config, options} = Window.__keplerglDataConfig; | ||
| addDataConfigToKeplerGl({data, config, options, store}); | ||
| } | ||
| // Signal render complete for callback-based loading (Jupyter widget mode) | ||
| if (onRenderComplete) { | ||
| onRenderComplete(); | ||
| } | ||
| }, [store, onRenderComplete]); | ||
|
Comment on lines
+18
to
+30
|
||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function renderRoot({id, store, ele, onRenderComplete}) { | ||
| // Use React 18 createRoot API - reuse existing root if available | ||
| let root = rootInstances.get(ele); | ||
| if (!root) { | ||
| root = createRoot(ele); | ||
| rootInstances.set(ele, root); | ||
| } | ||
|
|
||
| root.render( | ||
| <Provider store={store}> | ||
| <DataLoader store={store} onRenderComplete={onRenderComplete} /> | ||
| <App /> | ||
| </Provider> | ||
| ); | ||
|
|
||
| ReactDOM.render(<Root />, ele); | ||
| } | ||
|
|
||
| export default renderRoot; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dimension update logic has been simplified, but this may cause unnecessary re-renders. The previous implementation only updated state when dimensions actually changed:
The new logic always calls
setDimension({width, height})even if the dimensions haven't changed. Consider adding a check to only update when dimensions actually change: