|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import React, { TransitionEvent, useEffect, useRef, useState } from 'react'; |
| 4 | + |
| 5 | +import Box, { BoxProps } from '@mui/material/Box'; |
| 6 | +import Stack from '@mui/material/Stack'; |
| 7 | + |
| 8 | +import { duration } from '@mui/material/styles/createTransitions'; |
| 9 | + |
| 10 | +import useTheme from '@mui/material/styles/useTheme'; |
| 11 | + |
| 12 | +import { CollapseToggle } from './collapseToggle'; |
| 13 | + |
| 14 | +interface Props extends Omit<BoxProps, 'width' | 'minWidth'> { |
| 15 | + collapsible?: boolean; |
| 16 | + minWidth?: number; |
| 17 | + resizable?: boolean; |
| 18 | + width?: number; |
| 19 | +} |
| 20 | + |
| 21 | +export function CollapsibleResizableContainer({ |
| 22 | + children, |
| 23 | + collapsible, |
| 24 | + resizable, |
| 25 | + ...props |
| 26 | +}: Props) { |
| 27 | + const { transitions } = useTheme(); |
| 28 | + const [width, setWidth] = useState<number | undefined>(undefined); // Initial width of the container |
| 29 | + const containerRef = useRef<HTMLDivElement>(null); |
| 30 | + const [isCollapsed, setIsCollapsed] = useState(false); |
| 31 | + const [applyMinWidth, setApplyMinWidth] = useState(false); |
| 32 | + const disableTransition = useRef(false); |
| 33 | + |
| 34 | + const handleMouseDown = (e: React.MouseEvent<HTMLDivElement>) => { |
| 35 | + if (!containerRef.current) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const startX = e.clientX; |
| 40 | + const startWidth = width ?? containerRef.current?.clientWidth; |
| 41 | + disableTransition.current = true; |
| 42 | + |
| 43 | + const onMouseMove = (e: MouseEvent) => { |
| 44 | + setWidth(startWidth + (e.clientX - startX)); |
| 45 | + }; |
| 46 | + |
| 47 | + const onMouseUp = () => { |
| 48 | + document.removeEventListener('mousemove', onMouseMove); |
| 49 | + document.removeEventListener('mouseup', onMouseUp); |
| 50 | + document.body.style.userSelect = ''; |
| 51 | + disableTransition.current = false; |
| 52 | + }; |
| 53 | + |
| 54 | + document.addEventListener('mousemove', onMouseMove); |
| 55 | + document.addEventListener('mouseup', onMouseUp); |
| 56 | + document.body.style.userSelect = 'none'; |
| 57 | + }; |
| 58 | + |
| 59 | + const handleTransitionEnd = (event: TransitionEvent<HTMLDivElement>) => { |
| 60 | + if (event.propertyName !== 'width') { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (!isCollapsed) { |
| 65 | + setApplyMinWidth(true); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (isCollapsed) { |
| 71 | + setApplyMinWidth(false); |
| 72 | + } |
| 73 | + }, [isCollapsed]); |
| 74 | + |
| 75 | + const parsedWidth = Math.max(width ?? props.width ?? 0, props.minWidth ?? 0); |
| 76 | + |
| 77 | + return ( |
| 78 | + <Box |
| 79 | + ref={containerRef} |
| 80 | + position="relative" |
| 81 | + {...props} |
| 82 | + minWidth={applyMinWidth ? (isCollapsed ? 24 : props.minWidth) : undefined} |
| 83 | + onTransitionEnd={handleTransitionEnd} |
| 84 | + sx={{ |
| 85 | + ...props.sx, |
| 86 | + transition: disableTransition.current |
| 87 | + ? undefined |
| 88 | + : `width ${duration.standard}ms ${transitions.easing.easeInOut}`, |
| 89 | + [`.${CollapseToggle.className}`]: { |
| 90 | + opacity: 0, |
| 91 | + }, |
| 92 | + [`&:hover .${CollapseToggle.className}`]: { |
| 93 | + opacity: 1, |
| 94 | + }, |
| 95 | + }} |
| 96 | + width={isCollapsed ? 24 : parsedWidth} |
| 97 | + > |
| 98 | + <Stack flex="1 1 auto" minHeight={0} overflow="hidden"> |
| 99 | + <Stack flex="1 1 auto" minHeight={0} overflow="visible" width={parsedWidth}> |
| 100 | + {children} |
| 101 | + </Stack> |
| 102 | + </Stack> |
| 103 | + {resizable && ( |
| 104 | + <Box |
| 105 | + bottom={0} |
| 106 | + height="100%" |
| 107 | + onMouseDown={handleMouseDown} |
| 108 | + position="absolute" |
| 109 | + right={-5} |
| 110 | + sx={{ cursor: 'ew-resize' }} |
| 111 | + title="Resize" |
| 112 | + top={0} |
| 113 | + width={10} |
| 114 | + zIndex={1} |
| 115 | + /> |
| 116 | + )} |
| 117 | + {collapsible && ( |
| 118 | + <CollapseToggle collapsed={isCollapsed} onClick={() => setIsCollapsed(!isCollapsed)} /> |
| 119 | + )} |
| 120 | + </Box> |
| 121 | + ); |
| 122 | +} |
0 commit comments