Hej, chce stworzyc customowy resizer do mojego diva w ReactJS. Zrobilem tak jak w kodzie ponizej (czyli zawsze document.getElementById()
) i dziala ale zastanawiam sie czy jest to dobrze napisane czy nieelegancko i istnieje lepsza metoda zgodna z ReactJS? Myslalem zeby jakos ref
a podac ale probowalem zastapic document.getElementById()
przez ref.current
i nie dzialalo.
const ResizeHandle = ({
targetRef,
}: {
targetRef: React.RefObject<HTMLDivElement>;
}) => {
let resizerRef = useRef<HTMLDivElement>(null);
let currentPosition: number;
let mouseDown: boolean;
let lastClientY: number;
function touchMouseStart(e: any) {
e.preventDefault();
// if (!targetRef.current) return;
mouseDown = true;
if (e.touches) currentPosition = e.touches[0].clientY;
else currentPosition = e.clientY;
lastClientY = document.getElementById('editor-content')!.offsetHeight;
// console.log("LastClientY");
// console.log(lastClientY);
// document.addEventListener("mousemove", documentHorizontalMouseMove);
// document.addEventListener("mouseup", stopHorizontalResize);
}
function touchMouseEnd(e: any) {
e.preventDefault();
// if (!targetRef.current) return;
mouseDown = false;
lastClientY = document.getElementById('editor-content')!.offsetHeight;
// lastClientY = ref.current?.offsetHeight;
// console.log("LastClientY");
// console.log(lastClientY);
}
function touchMouseMove(e: any) {
e.preventDefault();
// if (!targetRef.current) return;
if (!mouseDown) return;
if (e.touches) var position = e.touches[0].clientY;
else var position = e.clientY;
// console.log("pozycja");
// console.log(pozycja);
var diff = position - currentPosition;
// console.log("wysokosc");
// console.log(wysokosc);
document.getElementById('editor-content')!.style.height =
lastClientY + diff + 'px';
}
return (
<div
ref={resizerRef}
className='mr-0 h-[30px] w-full cursor-s-resize rounded-b-[6px] border-t-[1px] bg-muted '
onPointerDown={(e: any) =>
resizerRef.current?.setPointerCapture(e.pointerId)
}
onPointerUp={(e: any) =>
resizerRef.current?.releasePointerCapture(e.pointerId)
}
onMouseDown={touchMouseStart}
onMouseUp={touchMouseEnd}
onTouchStart={touchMouseStart}
onTouchEnd={touchMouseEnd}
onTouchCancel={touchMouseEnd}
onClick={(e) => e.preventDefault()}
onTouchMove={touchMouseMove}
onMouseMove={touchMouseMove}
></div>
);
};
return (
<div
className={cn(
'relative z-10 flex w-full flex-col rounded-[6px] border-[1px] border-border bg-background p-0',
editorVariants({ className, variant })
)}
>
<MenuBar editor={editor!}></MenuBar>
<EditorContent
id='editor-content'
ref={editorContentRef}
editor={editor}
className='h-full max-h-full w-full flex-grow-[1] overflow-auto p-[12px] pt-[6px]'
onClick={() => editor?.commands.focus()}
/>
{resizable && <ResizeHandle targetRef={editorContentRef} />}
</div>
);