Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(panel): Resizable side panel #856

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/oss-console/src/components/common/DetailsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import Paper from '@mui/material/Paper';
import { useTheme } from '@mui/material/styles';
import styled from '@mui/system/styled';
import { detailsPanelId } from '@clients/common/constants';
import { detailsPanelWidth } from './constants';
import classnames from 'classnames';
import useResize from '@clients/oss-console/components/hooks/useResize';
import { defaultDetailsPanelWidth } from './constants';

const StyledPaper = styled(Paper)(({ theme }) => ({
display: 'flex',
flex: '1 1 100%',
maxHeight: '100%',
paddingBottom: theme.spacing(2),
pointerEvents: 'initial',
width: detailsPanelWidth,
'& .dragger': {
width: '3px',
cursor: 'ew-resize',
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
zIndex: 100,
},
}));

interface DetailsPanelProps {
Expand All @@ -29,6 +39,9 @@ export const DetailsPanel: React.FC<PropsWithChildren<DetailsPanelProps>> = ({
open = false,
}) => {
const theme = useTheme();

const { width, enableResize } = useResize({ minWidth: defaultDetailsPanelWidth });

return (
<Drawer
anchor="right"
Expand All @@ -50,7 +63,8 @@ export const DetailsPanel: React.FC<PropsWithChildren<DetailsPanelProps>> = ({
open={open}
key="detailsPanel"
>
<StyledPaper id={detailsPanelId} square>
<StyledPaper id={detailsPanelId} square sx={{ width }}>
<div onMouseDown={enableResize} className={classnames('dragger')} />
{children}
</StyledPaper>
</Drawer>
Expand Down
2 changes: 1 addition & 1 deletion packages/oss-console/src/components/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { env } from '@clients/common/environment';
import { InterpreterOptions } from 'xstate';

export const detailsPanelWidth = 432;
export const defaultDetailsPanelWidth = 432;

export const labels = {
moreOptionsButton: 'Display more options',
Expand Down
51 changes: 51 additions & 0 deletions packages/oss-console/src/components/hooks/useResize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Reference: https://stackoverflow.com/a/68742668

import { useCallback, useEffect, useState } from 'react';

type UseResizeProps = {
minWidth: number;
};

type UseResizeReturn = {
width: number;
enableResize: () => void;
};

const useResize = ({ minWidth }: UseResizeProps): UseResizeReturn => {
const [isResizing, setIsResizing] = useState(false);
const [width, setWidth] = useState(minWidth);

const enableResize = useCallback(() => {
setIsResizing(true);

Check warning on line 19 in packages/oss-console/src/components/hooks/useResize.ts

View check run for this annotation

Codecov / codecov/patch

packages/oss-console/src/components/hooks/useResize.ts#L19

Added line #L19 was not covered by tests
}, [setIsResizing]);

const disableResize = useCallback(() => {
setIsResizing(false);

Check warning on line 23 in packages/oss-console/src/components/hooks/useResize.ts

View check run for this annotation

Codecov / codecov/patch

packages/oss-console/src/components/hooks/useResize.ts#L23

Added line #L23 was not covered by tests
}, [setIsResizing]);

const resize = useCallback(
(e: MouseEvent) => {
if (isResizing) {
const newWidth = document.body.offsetWidth - e.clientX; // You may want to add some offset here from props
if (newWidth >= minWidth) {
setWidth(newWidth);

Check warning on line 31 in packages/oss-console/src/components/hooks/useResize.ts

View check run for this annotation

Codecov / codecov/patch

packages/oss-console/src/components/hooks/useResize.ts#L27-L31

Added lines #L27 - L31 were not covered by tests
}
}
},
[minWidth, isResizing, setWidth],
);

useEffect(() => {
document.addEventListener('mousemove', resize);
document.addEventListener('mouseup', disableResize);

return () => {
document.removeEventListener('mousemove', resize);
document.removeEventListener('mouseup', disableResize);
};
}, [disableResize, resize]);

return { width, enableResize };
};

export default useResize;
Loading