@@ -117,120 +117,122 @@ export type PlotlyModule = Pick<
117117 * - Supports all Plotly.js event handlers through props
118118 * - Includes built-in resize handling when useResize is enabled
119119 */
120- export const createPlotComponent = ( Plotly : PlotlyModule ) => ( props : PlotProps ) => {
121- const _props = mergeProps (
122- {
123- id : "solid-plotly" ,
124- config : { } as PlotlyConfig ,
125- data : [ ] as PlotlyData [ ] ,
126- frames : [ ] as PlotlyFrame [ ] ,
127- layout : { } as PlotlyLayout ,
128- useResize : true ,
129- style : {
130- position : "relative" ,
131- display : "inline-block" ,
132- } as JSX . CSSProperties ,
133- } ,
134- props ,
135- ) ;
136-
137- let container ! : HTMLElement ;
138- let plotElement ! : PlotlyHTMLElement ;
139- const [ initialized , setInitialized ] = createSignal ( false ) ;
140-
141- const [ containerProps ] = splitProps ( _props , [ "id" , "class" , "style" , "ref" ] ) ;
142- const [ eventProps ] = splitProps ( _props , PLOT_COMPONENT_EVENT_NAMES ) ;
143- const [ plotProps ] = splitProps ( _props , [
144- "data" ,
145- "layout" ,
146- "config" ,
147- "onInitialized" ,
148- "onPurge" ,
149- "onUpdate" ,
150- "onError" ,
151- "onResize" ,
152- "useResize" ,
153- ] ) ;
154-
155- const layout = ( ) =>
156- plotProps . useResize ? { ...plotProps . layout , autosize : true } : plotProps . layout ;
157-
158- const config = ( ) =>
159- plotProps . useResize ? { ...plotProps . config , responsive : true } : plotProps . config ;
160-
161- onMount ( async ( ) => {
162- const [ error , element ] = await tryCatch (
163- Plotly . newPlot ( container , plotProps . data , layout ( ) , config ( ) ) ,
120+ export const createPlotComponent =
121+ ( Plotly : PlotlyModule ) =>
122+ ( props : PlotProps ) : JSX . Element => {
123+ const _props = mergeProps (
124+ {
125+ id : "solid-plotly" ,
126+ config : { } as PlotlyConfig ,
127+ data : [ ] as PlotlyData [ ] ,
128+ frames : [ ] as PlotlyFrame [ ] ,
129+ layout : { } as PlotlyLayout ,
130+ useResize : true ,
131+ style : {
132+ position : "relative" ,
133+ display : "inline-block" ,
134+ } as JSX . CSSProperties ,
135+ } ,
136+ props ,
164137 ) ;
165138
166- if ( error ) {
167- plotProps . onError ?.( error ) ;
168- return ;
169- }
170-
171- Plotly . Plots . resize ( element ) ;
172- attachComponentEvents ( element , eventProps ) ;
173-
174- plotProps . onInitialized ?.( getFigureData ( element ) , element ) ;
175- plotElement = element ;
176-
177- setInitialized ( true ) ;
178- } ) ;
179-
180- createEffect ( ( ) => {
181- if ( ! initialized ( ) ) return ;
182-
183- const updatePlot = async ( ) => {
139+ let container ! : HTMLElement ;
140+ let plotElement ! : PlotlyHTMLElement ;
141+ const [ initialized , setInitialized ] = createSignal ( false ) ;
142+
143+ const [ containerProps ] = splitProps ( _props , [ "id" , "class" , "style" , "ref" ] ) ;
144+ const [ eventProps ] = splitProps ( _props , PLOT_COMPONENT_EVENT_NAMES ) ;
145+ const [ plotProps ] = splitProps ( _props , [
146+ "data" ,
147+ "layout" ,
148+ "config" ,
149+ "onInitialized" ,
150+ "onPurge" ,
151+ "onUpdate" ,
152+ "onError" ,
153+ "onResize" ,
154+ "useResize" ,
155+ ] ) ;
156+
157+ const layout = ( ) =>
158+ plotProps . useResize ? { ...plotProps . layout , autosize : true } : plotProps . layout ;
159+
160+ const config = ( ) =>
161+ plotProps . useResize ? { ...plotProps . config , responsive : true } : plotProps . config ;
162+
163+ onMount ( async ( ) => {
184164 const [ error , element ] = await tryCatch (
185- Plotly . react ( container , plotProps . data , layout ( ) , config ( ) ) ,
165+ Plotly . newPlot ( container , plotProps . data , layout ( ) , config ( ) ) ,
186166 ) ;
187167
188168 if ( error ) {
189169 plotProps . onError ?.( error ) ;
190170 return ;
191171 }
192172
193- plotProps . onUpdate ?.( getFigureData ( element ) , element ) ;
194- } ;
173+ Plotly . Plots . resize ( element ) ;
174+ attachComponentEvents ( element , eventProps ) ;
175+
176+ plotProps . onInitialized ?.( getFigureData ( element ) , element ) ;
177+ plotElement = element ;
178+
179+ setInitialized ( true ) ;
180+ } ) ;
181+
182+ createEffect ( ( ) => {
183+ if ( ! initialized ( ) ) return ;
184+
185+ const updatePlot = async ( ) => {
186+ const [ error , element ] = await tryCatch (
187+ Plotly . react ( container , plotProps . data , layout ( ) , config ( ) ) ,
188+ ) ;
195189
196- void updatePlot ( ) ;
197- } ) ;
190+ if ( error ) {
191+ plotProps . onError ?.( error ) ;
192+ return ;
193+ }
198194
199- createEffect ( ( ) => {
200- if ( ! initialized ( ) || ! plotProps . useResize ) return ;
195+ plotProps . onUpdate ?. ( getFigureData ( element ) , element ) ;
196+ } ;
201197
202- const resizeHandler = ( ) => {
203- Plotly . Plots . resize ( plotElement ) ;
204- plotProps . onResize ?.( ) ;
205- } ;
198+ void updatePlot ( ) ;
199+ } ) ;
200+
201+ createEffect ( ( ) => {
202+ if ( ! initialized ( ) || ! plotProps . useResize ) return ;
203+
204+ const resizeHandler = ( ) => {
205+ Plotly . Plots . resize ( plotElement ) ;
206+ plotProps . onResize ?.( ) ;
207+ } ;
206208
207- const resizeObserver = new ResizeObserver ( resizeHandler ) ;
208- resizeObserver . observe ( container ) ;
209+ const resizeObserver = new ResizeObserver ( resizeHandler ) ;
210+ resizeObserver . observe ( container ) ;
211+
212+ onCleanup ( ( ) => {
213+ resizeObserver . disconnect ( ) ;
214+ } ) ;
215+ } ) ;
209216
210217 onCleanup ( ( ) => {
211- resizeObserver . disconnect ( ) ;
218+ if ( ! initialized ( ) ) return ;
219+
220+ removeComponentEvents ( plotElement , eventProps ) ;
221+ Plotly . purge ( plotElement ) ;
222+ plotProps . onPurge ?.( getFigureData ( plotElement ) , plotElement ) ;
223+
224+ setInitialized ( false ) ;
212225 } ) ;
213- } ) ;
214-
215- onCleanup ( ( ) => {
216- if ( ! initialized ( ) ) return ;
217-
218- removeComponentEvents ( plotElement , eventProps ) ;
219- Plotly . purge ( plotElement ) ;
220- plotProps . onPurge ?.( getFigureData ( plotElement ) , plotElement ) ;
221-
222- setInitialized ( false ) ;
223- } ) ;
224-
225- return (
226- < div
227- id = { containerProps . id }
228- class = { containerProps . class }
229- style = { containerProps . style }
230- ref = { ( el ) => {
231- container = el ;
232- containerProps . ref ?.( el ) ;
233- } }
234- />
235- ) ;
236- } ;
226+
227+ return (
228+ < div
229+ id = { containerProps . id }
230+ class = { containerProps . class }
231+ style = { containerProps . style }
232+ ref = { ( el ) => {
233+ container = el ;
234+ containerProps . ref ?.( el ) ;
235+ } }
236+ />
237+ ) ;
238+ } ;
0 commit comments