11import React , { useEffect , useRef , useState , forwardRef , useImperativeHandle } from "react" ;
2+ import { useTheme } from "./ThemeProvider" ;
23import type {
34 IChartingLibraryWidget ,
45 ChartingLibraryWidgetOptions ,
@@ -101,6 +102,9 @@ const OHLCChart = forwardRef<OHLCChartRef, OHLCChartProps>(
101102 predictionProjections = [ ] ,
102103 onSymbolChange,
103104 } , ref ) => {
105+ const { theme } = useTheme ( ) ;
106+ const isDark = theme === 'dark' ;
107+
104108 const containerRef = useRef < HTMLDivElement > ( null ) ;
105109 const widgetRef = useRef < IChartingLibraryWidget | null > ( null ) ;
106110 const containerIdRef = useRef < string > (
@@ -275,8 +279,6 @@ const OHLCChart = forwardRef<OHLCChartRef, OHLCChartProps>(
275279 ] ,
276280 enabled_features : [
277281 "study_templates" as any ,
278- "save_chart_properties_to_local_storage" as any ,
279- "use_localstorage_for_settings" as any ,
280282 "left_toolbar" as any ,
281283 ] ,
282284 charts_storage_url : "https://saveload.tradingview.com" ,
@@ -286,33 +288,27 @@ const OHLCChart = forwardRef<OHLCChartRef, OHLCChartProps>(
286288 fullscreen : false ,
287289 autosize : true ,
288290 studies_overrides : { } ,
289- theme : "dark" ,
291+ theme : isDark ? "dark" : "light ",
290292 timezone : "Etc/UTC" ,
291- toolbar_bg : "#1E222D" ,
293+ toolbar_bg : isDark ? "#1E222D" : "#F5F5F5 ",
292294 loading_screen : {
293- backgroundColor : "#0D1421" ,
295+ backgroundColor : isDark ? "#0D1421" : "#FFFFFF ",
294296 foregroundColor : "#2962FF" ,
295297 } ,
296298 overrides : {
297- "paneProperties.background" : "#0D1421" ,
299+ "paneProperties.background" : isDark ? "#0D1421" : "#FFFFFF ",
298300 "paneProperties.backgroundType" : "solid" ,
299- "paneProperties.vertGridProperties.color" : "#2A2E39" ,
300- "paneProperties.horzGridProperties.color" : "#2A2E39" ,
301- "mainSeriesProperties.style" : 2 , // Candlestick
302- "mainSeriesProperties.candleStyle.upColor" :
303- bullishColor || "#089981" ,
304- "mainSeriesProperties.candleStyle.downColor" :
305- bearishColor || "#F23645" ,
306- "mainSeriesProperties.candleStyle.borderUpColor" :
307- bullishColor || "#089981" ,
308- "mainSeriesProperties.candleStyle.borderDownColor" :
309- bearishColor || "#F23645" ,
310- "mainSeriesProperties.candleStyle.wickUpColor" :
311- bullishColor || "#089981" ,
312- "mainSeriesProperties.candleStyle.wickDownColor" :
313- bearishColor || "#F23645" ,
314- "scalesProperties.textColor" : "#787B86" ,
315- "scalesProperties.lineColor" : "#2A2E39" ,
301+ "paneProperties.vertGridProperties.color" : isDark ? "#2A2E39" : "#E0E3EB" ,
302+ "paneProperties.horzGridProperties.color" : isDark ? "#2A2E39" : "#E0E3EB" ,
303+ "mainSeriesProperties.style" : 2 ,
304+ "mainSeriesProperties.candleStyle.upColor" : bullishColor || "#089981" ,
305+ "mainSeriesProperties.candleStyle.downColor" : bearishColor || "#F23645" ,
306+ "mainSeriesProperties.candleStyle.borderUpColor" : bullishColor || "#089981" ,
307+ "mainSeriesProperties.candleStyle.borderDownColor" : bearishColor || "#F23645" ,
308+ "mainSeriesProperties.candleStyle.wickUpColor" : bullishColor || "#089981" ,
309+ "mainSeriesProperties.candleStyle.wickDownColor" : bearishColor || "#F23645" ,
310+ "scalesProperties.textColor" : isDark ? "#787B86" : "#555555" ,
311+ "scalesProperties.lineColor" : isDark ? "#2A2E39" : "#E0E3EB" ,
316312 } ,
317313 } ;
318314// Create the chart widget
@@ -322,6 +318,21 @@ widgetRef.current = new TradingView.widget(widgetOptions);
322318 widgetRef . current . onChartReady ( ( ) => {
323319 console . log ( "[OHLCChart] Chart ready, attaching projection manager" ) ;
324320
321+ // Force theme overrides after localStorage restore
322+ try {
323+ const chart = widgetRef . current ?. chart ( ) as any
324+ if ( chart ) {
325+ chart . applyOverrides ( {
326+ 'paneProperties.background' : isDark ? '#0D1421' : '#FFFFFF' ,
327+ 'paneProperties.backgroundType' : 'solid' ,
328+ 'paneProperties.vertGridProperties.color' : isDark ? '#2A2E39' : '#E0E3EB' ,
329+ 'paneProperties.horzGridProperties.color' : isDark ? '#2A2E39' : '#E0E3EB' ,
330+ 'scalesProperties.textColor' : isDark ? '#787B86' : '#555555' ,
331+ 'scalesProperties.lineColor' : isDark ? '#2A2E39' : '#E0E3EB' ,
332+ } )
333+ }
334+ } catch { /* ignore */ }
335+
325336 // Listen for symbol changes in TradingView
326337 if ( onSymbolChange ) {
327338 widgetRef . current ! . activeChart ( ) . onSymbolChanged ( ) . subscribe ( null , ( ( ) => {
@@ -488,12 +499,34 @@ widgetRef.current = new TradingView.widget(widgetOptions);
488499 height ,
489500 bullishColor ,
490501 bearishColor ,
502+ isDark ,
491503 projectionSettings . strategyName ,
492504 projectionSettings . params ,
493505 projectionSettings . horizon ,
494506 projectionSettings . mode ,
495507 ] ) ;
496508
509+ // Apply theme to existing widget without full reinit
510+ useEffect ( ( ) => {
511+ if ( ! widgetRef . current ) return ;
512+ try {
513+ ( widgetRef . current as any ) . changeTheme ( isDark ? 'dark' : 'light' ) . then ( ( ) => {
514+ const chart = widgetRef . current ?. chart ( ) as any
515+ if ( ! chart ) return
516+ chart . applyOverrides ( {
517+ 'paneProperties.background' : isDark ? '#0D1421' : '#FFFFFF' ,
518+ 'paneProperties.backgroundType' : 'solid' ,
519+ 'paneProperties.vertGridProperties.color' : isDark ? '#2A2E39' : '#E0E3EB' ,
520+ 'paneProperties.horzGridProperties.color' : isDark ? '#2A2E39' : '#E0E3EB' ,
521+ 'scalesProperties.textColor' : isDark ? '#787B86' : '#555555' ,
522+ 'scalesProperties.lineColor' : isDark ? '#2A2E39' : '#E0E3EB' ,
523+ } )
524+ } )
525+ } catch {
526+ // changeTheme not available on this widget version — full reinit handles it
527+ }
528+ } , [ isDark ] )
529+
497530 // Update prediction settings when props change
498531 useEffect ( ( ) => {
499532 setPredictionSettings ( {
0 commit comments