From 74328ca97a879e998b98302425356037f0913c97 Mon Sep 17 00:00:00 2001 From: Junhao Liao Date: Wed, 18 Sep 2024 19:11:55 -0400 Subject: [PATCH] Support clicking on an active tab button to close the tab. --- .../SidebarContainer/PanelTabs/TooltipTab.tsx | 11 ++++++-- .../SidebarContainer/PanelTabs/index.tsx | 18 ++++++------ .../src/components/SidebarContainer/index.tsx | 28 +++++++++++++++---- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/new-log-viewer/src/components/SidebarContainer/PanelTabs/TooltipTab.tsx b/new-log-viewer/src/components/SidebarContainer/PanelTabs/TooltipTab.tsx index 8db23afa..012b3650 100644 --- a/new-log-viewer/src/components/SidebarContainer/PanelTabs/TooltipTab.tsx +++ b/new-log-viewer/src/components/SidebarContainer/PanelTabs/TooltipTab.tsx @@ -13,7 +13,8 @@ import { interface TooltipTabProps { tabName: TAB_NAME, - Icon: SvgIconComponent + Icon: SvgIconComponent, + onTabButtonClick: (tabName: TAB_NAME) => void } /** @@ -22,9 +23,14 @@ interface TooltipTabProps { * @param props * @param props.tabName * @param props.Icon + * @param props.onTabButtonClick * @return */ -const TooltipTab = ({tabName, Icon}: TooltipTabProps) => { +const TooltipTab = ({tabName, Icon, onTabButtonClick}: TooltipTabProps) => { + const handleClick = () => { + onTabButtonClick(tabName); + }; + return ( { className={"sidebar-tab-button"} color={"neutral"} indicatorPlacement={"left"} + slotProps={{root: {onClick: handleClick}}} value={tabName} > diff --git a/new-log-viewer/src/components/SidebarContainer/PanelTabs/index.tsx b/new-log-viewer/src/components/SidebarContainer/PanelTabs/index.tsx index ba7f5cb9..f95d7bb2 100644 --- a/new-log-viewer/src/components/SidebarContainer/PanelTabs/index.tsx +++ b/new-log-viewer/src/components/SidebarContainer/PanelTabs/index.tsx @@ -1,4 +1,4 @@ -import React, { +import { forwardRef, useState, } from "react"; @@ -20,7 +20,6 @@ import TooltipTab from "./TooltipTab"; interface PanelTabsProps { activeTabName: TAB_NAME, onActiveTabNameChange: (newValue: TAB_NAME) => void, - onPanelTabOpen: () => void, } /** @@ -33,7 +32,6 @@ const PanelTabs = forwardRef(( { activeTabName, onActiveTabNameChange, - onPanelTabOpen, }, tabListRef ) => { @@ -43,14 +41,13 @@ const PanelTabs = forwardRef(( setIsSettingsModalOpen(false); }; - const handleTabChange = (__: React.SyntheticEvent | null, value: number | string | null) => { - switch (value) { + const handleTabButtonClick = (tabName: TAB_NAME) => { + switch (tabName) { case TAB_NAME.SETTINGS: setIsSettingsModalOpen(true); break; default: - onActiveTabNameChange(value as TAB_NAME); - onPanelTabOpen(); + onActiveTabNameChange(tabName as TAB_NAME); } }; @@ -61,7 +58,6 @@ const PanelTabs = forwardRef(( orientation={"vertical"} value={activeTabName} variant={"plain"} - onChange={handleTabChange} > (( + tabName={tabName} + onTabButtonClick={handleTabButtonClick}/> ))}
+ tabName={TAB_NAME.SETTINGS} + onTabButtonClick={handleTabButtonClick}/> diff --git a/new-log-viewer/src/components/SidebarContainer/index.tsx b/new-log-viewer/src/components/SidebarContainer/index.tsx index a99ddc85..d3ca5e51 100644 --- a/new-log-viewer/src/components/SidebarContainer/index.tsx +++ b/new-log-viewer/src/components/SidebarContainer/index.tsx @@ -33,15 +33,32 @@ const SidebarContainer = ({children}: SidebarContainerProps) => { const tabListRef = useRef(null); - const handlePanelTabOpen = useCallback(() => { + const hidePanelAndResizeHandle = () => { + setActiveTabName(TAB_NAME.NONE); + document.body.style.setProperty("--ylv-panel-resize-handle-width", "0px"); + }; + + const handleActiveTabNameChange = useCallback((tabName: TAB_NAME) => { + if (null === tabListRef.current) { + console.error("Unexpected null tabListRef.current"); + + return; + } + + if (activeTabName === tabName) { + hidePanelAndResizeHandle(); + setPanelWidth(tabListRef.current.clientWidth); + + return; + } + setActiveTabName(tabName); setPanelWidth(PANEL_DEFAULT_WIDTH_IN_PIXEL); document.body.style.setProperty("--ylv-panel-resize-handle-width", "3px"); - }, []); + }, [activeTabName]); const handleResizeHandleRelease = useCallback(() => { if (panelWidth === tabListRef.current?.clientWidth) { - setActiveTabName(TAB_NAME.NONE); - document.body.style.setProperty("--ylv-panel-resize-handle-width", "0px"); + hidePanelAndResizeHandle(); } }, [panelWidth]); @@ -69,8 +86,7 @@ const SidebarContainer = ({children}: SidebarContainerProps) => { + onActiveTabNameChange={handleActiveTabNameChange}/>