From c2af42f2609c7a397e74517152867759ae32154a Mon Sep 17 00:00:00 2001 From: "Mr. Algorithm" <11aravipratapsingh@gmail.com> Date: Sun, 1 Feb 2026 04:06:51 +0530 Subject: [PATCH] feat: implement expandable TradingView widgets with immersive full-screen mode --- app/(root)/stocks/[symbol]/page.tsx | 2 + components/TradingViewWidget.tsx | 64 +++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/app/(root)/stocks/[symbol]/page.tsx b/app/(root)/stocks/[symbol]/page.tsx index cacbaf0..de8d835 100644 --- a/app/(root)/stocks/[symbol]/page.tsx +++ b/app/(root)/stocks/[symbol]/page.tsx @@ -39,6 +39,7 @@ export default async function StockDetails({ params }: StockDetailsPageProps) { config={CANDLE_CHART_WIDGET_CONFIG(symbol)} className="custom-chart" height={600} + allowExpand={true} /> diff --git a/components/TradingViewWidget.tsx b/components/TradingViewWidget.tsx index 3461891..51aabb4 100644 --- a/components/TradingViewWidget.tsx +++ b/components/TradingViewWidget.tsx @@ -1,8 +1,10 @@ 'use client'; -import React, { memo } from 'react'; +import React, { memo, useState, useEffect } from 'react'; import useTradingViewWidget from "@/hooks/useTradingViewWidget"; -import {cn} from "@/lib/utils"; +import { cn } from "@/lib/utils"; +import { Maximize2, Minimize2 } from 'lucide-react'; +import { Button } from '@/components/ui/button'; interface TradingViewWidgetProps { title?: string; @@ -10,19 +12,63 @@ interface TradingViewWidgetProps { config: Record; height?: number; className?: string; + allowExpand?: boolean; } -const TradingViewWidget = ({ title, scriptUrl, config, height = 600, className }: TradingViewWidgetProps) => { - const containerRef = useTradingViewWidget(scriptUrl, config, height); +const TradingViewWidget = ({ title, scriptUrl, config, height = 600, className, allowExpand = false }: TradingViewWidgetProps) => { + const [isExpanded, setIsExpanded] = useState(false); + const [windowHeight, setWindowHeight] = useState(0); + + useEffect(() => { + if (typeof window !== 'undefined') { + setWindowHeight(window.innerHeight); + const handleResize = () => setWindowHeight(window.innerHeight); + window.addEventListener('resize', handleResize); + return () => window.removeEventListener('resize', handleResize); + } + }, []); + + const currentHeight = isExpanded ? windowHeight : height; + + const widgetConfig = { + ...config, + height: currentHeight, + width: "100%", + autosize: true, + }; + + const containerRef = useTradingViewWidget(scriptUrl, widgetConfig, currentHeight); + + const toggleExpand = () => { + setIsExpanded(!isExpanded); + }; return ( - - {title && {title}} - - + + + {title && !isExpanded && {title}} + + {allowExpand && ( + + {isExpanded ? : } + + )} + + + + ); } -export default memo(TradingViewWidget); \ No newline at end of file +export default memo(TradingViewWidget);