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

Ms2/resizable element relative width #197

Merged
merged 2 commits into from
Dec 19, 2023
Merged
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
37 changes: 25 additions & 12 deletions src/management-system-v2/components/ResizableElement.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use client';

import { CssSize, cssSizeToPixel } from '@/lib/css-units-helper';
import React, {
useState,
useEffect,
PropsWithChildren,
CSSProperties,
forwardRef,
useImperativeHandle,
Dispatch,
SetStateAction,
} from 'react';

type ResizableElementProps = PropsWithChildren<{
initialWidth: number;
minWidth: number;
maxWidth: number;
minWidth: CssSize;
maxWidth: CssSize;
onWidthChange?: (width: number) => void;
style?: CSSProperties;
}>;

export type ResizableElementRefType = Dispatch<SetStateAction<number>>;
export type ResizableElementRefType = (size: CssSize) => void;

let isResizing = false;
const ResizableElement = forwardRef<ResizableElementRefType, ResizableElementProps>(
Expand All @@ -27,35 +28,44 @@ const ResizableElement = forwardRef<ResizableElementRefType, ResizableElementPro
) {
const [width, setWidth] = useState(initialWidth);

useImperativeHandle(ref, () => setWidth);
useImperativeHandle(ref, () => (size: CssSize) => setWidth(cssSizeToPixel(size)));

const onMouseDown = (e: React.MouseEvent) => {
const onMouseDown = (e: { stopPropagation: () => void; preventDefault: () => void }) => {
e.stopPropagation();
e.preventDefault();
isResizing = true;
};

const onMouseUp = (e: MouseEvent) => {
const onMouseUp = () => {
isResizing = false;
};

const onMouseMove = (e: MouseEvent) => {
const onUserMovement = (clientX: number) => {
if (isResizing) {
let offsetRight = document.body.offsetWidth - (e.clientX - document.body.offsetLeft);
let offsetRight = document.body.offsetWidth - (clientX - document.body.offsetLeft);

const minPixels = cssSizeToPixel(minWidth);
const maxPixels = cssSizeToPixel(maxWidth);

if (offsetRight > minWidth && offsetRight < maxWidth) {
if (offsetRight > minPixels && offsetRight < maxPixels) {
setWidth(offsetRight);
if (onWidthChange) onWidthChange(width);
}
}
};

useEffect(() => {
const onMouseMove = (e: MouseEvent) => onUserMovement(e.clientX);
const onTouchMove = (e: TouchEvent) =>
onUserMovement(e.touches[e.touches.length - 1].clientX);

document.addEventListener('mousemove', onMouseMove);
document.addEventListener('touchmove', onTouchMove);
document.addEventListener('mouseup', onMouseUp);

return () => {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('touchmove', onTouchMove);
document.removeEventListener('mouseup', onMouseUp);
};
});
Expand All @@ -64,7 +74,9 @@ const ResizableElement = forwardRef<ResizableElementRefType, ResizableElementPro
<div
style={{
...style,
width: width,
width,
maxWidth,
minWidth,
}}
>
<div
Expand All @@ -86,6 +98,7 @@ const ResizableElement = forwardRef<ResizableElementRefType, ResizableElementPro
cursor: 'ew-resize',
}}
onMouseDown={onMouseDown}
onTouchStart={onMouseDown}
/>
{children}
</div>
Expand Down
35 changes: 35 additions & 0 deletions src/management-system-v2/lib/css-units-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use client';

type Unit = 'px' | 'vw' | 'vh' | 'rem' | '';

export type CssSize = `${number}${Unit}` | number;

function convertVwToPixel(valueInVw: number) {
return (valueInVw * window.innerWidth) / 100;
}

function convertVhToPixel(valueInVh: number) {
return (valueInVh * window.innerHeight) / 100;
}

function convertRemToPixel(valueInRem: number) {
return valueInRem;
}

export function cssSizeToPixel(size: CssSize) {
if (typeof size === 'number') return size;

const unit = size.slice(-2) as Unit;
const value = parseFloat(size);

switch (unit) {
case 'rem':
return convertRemToPixel(value);
case 'vw':
return convertVwToPixel(value);
case 'vh':
return convertVhToPixel(value);
default:
return value;
}
}