1
- import { useState } from 'react'
1
+ import { useEffect , useState } from 'react'
2
2
// eslint-disable-next-line import/no-extraneous-dependencies
3
3
import Draggable , { DraggableEventHandler } from 'react-draggable'
4
4
5
- import { AnimatePresence , AppThemeType , motion , useMainContext , useTheme } from '@devtron-labs/devtron-fe-common-lib'
5
+ import {
6
+ animate ,
7
+ AnimatePresence ,
8
+ AppThemeType ,
9
+ motion ,
10
+ useMainContext ,
11
+ useTheme ,
12
+ } from '@devtron-labs/devtron-fe-common-lib'
6
13
14
+ import { SIDE_PANEL_MAX_ASIDE_WIDTH , SIDE_PANEL_MIN_ASIDE_WIDTH } from './constants'
7
15
import { SidePanelDocumentation } from './SidePanelDocumentation'
8
16
import { SidePanelProps } from './types'
9
17
10
18
import './SidePanel.scss'
11
19
12
- const MAX_ASIDE_WIDTH = 525
13
- const MIN_ASIDE_WIDTH = 350
14
-
15
- export const SidePanel = ( { asideWidth, setAsideWidth } : SidePanelProps ) => {
20
+ export const SidePanel = ( { asideWidth } : SidePanelProps ) => {
16
21
// STATES
17
22
const [ contentOverlay , setContentOverlay ] = useState ( false )
18
23
@@ -22,16 +27,32 @@ export const SidePanel = ({ asideWidth, setAsideWidth }: SidePanelProps) => {
22
27
23
28
const { open } = sidePanelConfig
24
29
30
+ useEffect ( ( ) => {
31
+ if ( open ) {
32
+ const controls = animate ( asideWidth , SIDE_PANEL_MIN_ASIDE_WIDTH , {
33
+ duration : 0.2 ,
34
+ ease : 'easeInOut' ,
35
+ } )
36
+ return controls . stop
37
+ }
38
+
39
+ const controls = animate ( asideWidth , 0 , {
40
+ duration : 0.3 ,
41
+ ease : 'easeInOut' ,
42
+ } )
43
+ return controls . stop
44
+ } , [ open ] )
45
+
25
46
// HANDLERS
26
47
const handleClose = ( ) => {
27
- setAsideWidth ( MIN_ASIDE_WIDTH )
48
+ asideWidth . set ( SIDE_PANEL_MIN_ASIDE_WIDTH )
28
49
setSidePanelConfig ( { open : false } )
29
50
}
30
51
31
52
const handleDrag : DraggableEventHandler = ( _ , data ) => {
32
- const newWidth = asideWidth - data . deltaX
33
- const clamped = Math . max ( MIN_ASIDE_WIDTH , Math . min ( MAX_ASIDE_WIDTH , newWidth ) )
34
- setAsideWidth ( clamped )
53
+ const newWidth = asideWidth . get ( ) - data . deltaX
54
+ const clamped = Math . max ( SIDE_PANEL_MIN_ASIDE_WIDTH , Math . min ( SIDE_PANEL_MAX_ASIDE_WIDTH , newWidth ) )
55
+ asideWidth . set ( clamped )
35
56
}
36
57
37
58
const handleDragStart = ( ) => setContentOverlay ( true )
@@ -41,7 +62,13 @@ export const SidePanel = ({ asideWidth, setAsideWidth }: SidePanelProps) => {
41
62
return (
42
63
< AnimatePresence >
43
64
{ open && (
44
- < >
65
+ < motion . aside
66
+ initial = { { x : SIDE_PANEL_MIN_ASIDE_WIDTH , opacity : 0 } }
67
+ animate = { { x : 0 , opacity : 1 } }
68
+ exit = { { x : SIDE_PANEL_MIN_ASIDE_WIDTH , opacity : 0 } }
69
+ transition = { { duration : 0.2 , ease : 'easeInOut' } }
70
+ className = "flexbox"
71
+ >
45
72
< Draggable
46
73
handle = ".aside-drag"
47
74
defaultClassNameDragging = "aside-drag--dragging"
@@ -58,26 +85,17 @@ export const SidePanel = ({ asideWidth, setAsideWidth }: SidePanelProps) => {
58
85
onStart = { handleDragStart }
59
86
onStop = { handleDragStop }
60
87
>
61
- < div className = "aside-drag flex dc__cursor-col-resize dc__zi-10" >
88
+ < div className = "aside-drag flex px-7 dc__cursor-col-resize dc__zi-10" >
62
89
< div className = "aside-drag__handle px-1 br-1" />
63
90
</ div >
64
91
</ Draggable >
65
- < motion . aside
66
- initial = { { x : 350 , opacity : 0 } }
67
- animate = { { x : 0 , opacity : 1 } }
68
- exit = { { x : 350 , opacity : 0 } }
69
- transition = { {
70
- duration : 0.2 ,
71
- type : 'spring' ,
72
- stiffness : 300 ,
73
- damping : 30 ,
74
- } }
75
- className = { `dc__position-rel mt-8 mr-8 mb-8 br-6 bg__primary flexbox-col dc__overflow-hidden ${ appTheme === AppThemeType . dark ? 'border__primary-translucent' : '' } ` }
92
+ < div
93
+ className = { `flex-grow-1 dc__position-rel mt-8 mr-8 mb-8 br-6 bg__primary flexbox-col dc__overflow-hidden ${ appTheme === AppThemeType . dark ? 'border__primary-translucent' : '' } ` }
76
94
>
77
95
{ contentOverlay && < div className = "dc__position-abs w-100 h-100 dc__zi-1" /> }
78
96
< SidePanelDocumentation onClose = { handleClose } />
79
- </ motion . aside >
80
- </ >
97
+ </ div >
98
+ </ motion . aside >
81
99
) }
82
100
</ AnimatePresence >
83
101
)
0 commit comments