@@ -10,7 +10,7 @@ import AppsMenu from 'components/Sidebar/AppsMenu.react';
1010import AppName from 'components/Sidebar/AppName.react' ;
1111import isInsidePopover from 'lib/isInsidePopover' ;
1212import Pin from 'components/Sidebar/Pin.react' ;
13- import React , { useEffect , useState , useContext } from 'react' ;
13+ import React , { useEffect , useState , useContext , useRef } from 'react' ;
1414import SidebarHeader from 'components/Sidebar/SidebarHeader.react' ;
1515import SidebarSection from 'components/Sidebar/SidebarSection.react' ;
1616import SidebarSubItem from 'components/Sidebar/SidebarSubItem.react' ;
@@ -36,6 +36,9 @@ const Sidebar = ({
3636 const [ appsMenuOpen , setAppsMenuOpen ] = useState ( false ) ;
3737 const [ collapsed , setCollapsed ] = useState ( false ) ;
3838 const [ fixed , setFixed ] = useState ( true ) ;
39+ const initialWidth = parseInt ( localStorage . getItem ( 'sidebarWidth' ) || '300' , 10 ) ;
40+ const [ width , setWidth ] = useState ( initialWidth ) ;
41+ const widthRef = useRef ( initialWidth ) ;
3942 const [ dashboardUser , setDashboardUser ] = useState ( '' ) ;
4043 fetch ( mountPath ) . then ( response => {
4144 setDashboardUser ( response . headers . get ( 'username' ) ) ;
@@ -66,6 +69,11 @@ const Sidebar = ({
6669 } ;
6770 } ) ;
6871
72+ useEffect ( ( ) => {
73+ document . documentElement . style . setProperty ( '--sidebar-width' , `${ width } px` ) ;
74+ widthRef . current = width ;
75+ } , [ width ] ) ;
76+
6977 const sidebarClasses = [ styles . sidebar ] ;
7078 if ( fixed ) {
7179 document . body . className = document . body . className . replace ( ' expanded' , '' ) ;
@@ -112,6 +120,24 @@ const Sidebar = ({
112120 }
113121 } ;
114122
123+ const startResize = e => {
124+ e . preventDefault ( ) ;
125+ const startX = e . clientX ;
126+ const startWidth = widthRef . current ;
127+ const doDrag = ev => {
128+ const newWidth = Math . max ( 200 , startWidth + ev . clientX - startX ) ;
129+ widthRef . current = newWidth ;
130+ setWidth ( newWidth ) ;
131+ } ;
132+ const stopDrag = ( ) => {
133+ document . removeEventListener ( 'mousemove' , doDrag ) ;
134+ document . removeEventListener ( 'mouseup' , stopDrag ) ;
135+ localStorage . setItem ( 'sidebarWidth' , widthRef . current . toString ( ) ) ;
136+ } ;
137+ document . addEventListener ( 'mousemove' , doDrag ) ;
138+ document . addEventListener ( 'mouseup' , stopDrag ) ;
139+ } ;
140+
115141 let sidebarContent ;
116142 if ( appsMenuOpen ) {
117143 const apps = [ ]
@@ -192,6 +218,7 @@ const Sidebar = ({
192218 </ a >
193219 </ div >
194220 ) }
221+ < div className = { styles . resizeHandle } onMouseDown = { startResize } />
195222 </ div >
196223 ) ;
197224} ;
0 commit comments