|
| 1 | +import { Resizable } from 're-resizable'; |
| 2 | +import React from 'react'; |
| 3 | +import Draggable from 'react-draggable'; |
| 4 | +import { Box, BoxProps, IconButton, Tooltip } from '../../base'; |
| 5 | +import { CloseIcon, CollapseAllIcon, ExpandAllIcon } from '../../icons'; |
| 6 | +import { PanelDragHandleIcon } from '../../icons/PanelDragHandle'; |
| 7 | +import { useTheme } from '../../theme'; |
| 8 | +import { ErrorBoundary } from '../ErrorBoundary'; |
| 9 | +import { |
| 10 | + DragHandle, |
| 11 | + DrawerHeader, |
| 12 | + HeaderActionsContainer, |
| 13 | + HeaderContainer, |
| 14 | + PanelBody, |
| 15 | + PanelContainer, |
| 16 | + ResizableContent |
| 17 | +} from './style'; |
| 18 | + |
| 19 | +export type PanelProps = { |
| 20 | + isOpen: boolean; |
| 21 | + children: React.ReactNode; |
| 22 | + areAllExpanded?: boolean; |
| 23 | + toggleExpandAll?: () => void; |
| 24 | + handleClose: () => void; |
| 25 | + sx?: BoxProps['sx']; |
| 26 | + id?: string; |
| 27 | + intitialPosition?: { |
| 28 | + left?: string | number; |
| 29 | + right?: string | number; |
| 30 | + top?: string | number; |
| 31 | + bottom?: string | number; |
| 32 | + }; |
| 33 | +}; |
| 34 | + |
| 35 | +const Panel_: React.FC<PanelProps> = ({ |
| 36 | + isOpen, |
| 37 | + id = 'panel', |
| 38 | + children, |
| 39 | + areAllExpanded, |
| 40 | + toggleExpandAll, |
| 41 | + handleClose, |
| 42 | + intitialPosition, |
| 43 | + sx |
| 44 | +}) => { |
| 45 | + const theme = useTheme(); |
| 46 | + if (!isOpen) return null; |
| 47 | + return ( |
| 48 | + <Draggable handle=".drag-handle"> |
| 49 | + <PanelContainer theme={theme} intitialPosition={intitialPosition} sx={sx}> |
| 50 | + <Resizable |
| 51 | + defaultSize={{ width: '18rem', height: 'auto' }} |
| 52 | + onResize={() => { |
| 53 | + window.dispatchEvent(new Event('panel-resize')); |
| 54 | + }} |
| 55 | + enable={{ |
| 56 | + top: true, |
| 57 | + right: true, |
| 58 | + bottom: true, |
| 59 | + left: true, |
| 60 | + topRight: true, |
| 61 | + bottomRight: true, |
| 62 | + bottomLeft: true, |
| 63 | + topLeft: true |
| 64 | + }} |
| 65 | + > |
| 66 | + <ResizableContent> |
| 67 | + <ErrorBoundary> |
| 68 | + <div className="drag-handle"> |
| 69 | + <DrawerHeader> |
| 70 | + <Box display="flex" justifyContent="flex-end" padding="8px"> |
| 71 | + {toggleExpandAll && ( |
| 72 | + <Tooltip title={areAllExpanded ? 'Collapse All' : 'Expand All'}> |
| 73 | + <IconButton onClick={toggleExpandAll}> |
| 74 | + {areAllExpanded ? <CollapseAllIcon /> : <ExpandAllIcon />} |
| 75 | + </IconButton> |
| 76 | + </Tooltip> |
| 77 | + )} |
| 78 | + </Box> |
| 79 | + <DragHandle> |
| 80 | + <PanelDragHandleIcon /> |
| 81 | + </DragHandle> |
| 82 | + <HeaderContainer> |
| 83 | + <HeaderActionsContainer |
| 84 | + id={`${id}-panel-header-actions-container`} |
| 85 | + ></HeaderActionsContainer> |
| 86 | + <IconButton onClick={handleClose}> |
| 87 | + <CloseIcon /> |
| 88 | + </IconButton> |
| 89 | + </HeaderContainer> |
| 90 | + </DrawerHeader> |
| 91 | + </div> |
| 92 | + <PanelBody className="panel-body">{children}</PanelBody> |
| 93 | + </ErrorBoundary> |
| 94 | + </ResizableContent> |
| 95 | + </Resizable> |
| 96 | + </PanelContainer> |
| 97 | + </Draggable> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +export const Panel: React.FC<PanelProps> = ({ ...props }) => { |
| 102 | + return <Panel_ {...props} />; |
| 103 | +}; |
0 commit comments