Skip to content

Commit

Permalink
♻️ abstract to handleResizeComplete and apply event to element
Browse files Browse the repository at this point in the history
  • Loading branch information
timbryandev committed Oct 4, 2023
1 parent 0417701 commit 16ecd12
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions lib/Resizable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {
};

const stopDrag = () => {
if (onResizeComplete) {
onResizeComplete(lastPosition);
}
document.removeEventListener("mousemove", doDragRef.current, false);
document.removeEventListener("mouseup", stopDragRef.current, false);
};
Expand Down Expand Up @@ -105,6 +102,12 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {
handle.style.top = `${yPosition - handleOffset}px`;
};

const handleResizeComplete = () => {
if (onResizeComplete) {
onResizeComplete(lastPosition);
}
};

useEffect(() => {
doResize(rememberPosition ? lastPosition : initialWidth);

Expand All @@ -121,6 +124,7 @@ export function Resizable(props: PropsWithChildren<ResizableProps>) {
className="resizable__gutter"
onMouseDown={initDrag}
onMouseMove={setGutterHandlePosition}
onMouseUp={handleResizeComplete}
>
<span ref={handleRef} className="resizable__gutter-handle" />
</span>
Expand Down
7 changes: 4 additions & 3 deletions stories/components/Resizable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export function Resizable() {
</div>
</StoryItem>

{/* eslint-disable-next-line no-use-before-define */}
<OnResizeStory />
</>
);
Expand All @@ -146,13 +147,13 @@ function OnResizeStory() {
const [resizedTo, setResizedTo] = useState(0);
return (
<StoryItem
title="OnResize event"
description="If you resize me, I'll tell you what px value i was resized too"
title="OnResizeComplete event"
description="Once you're finished resizing me, I'll tell you what px value i was resized too"
>
<div style={{ width: "100%" }}>
<ResizableComponent
initialWidth={300}
onResize={setResizedTo}
onResizeComplete={setResizedTo}
useGutterOffset
>
<div style={{ border: "1px solid green" }}>
Expand Down

0 comments on commit 16ecd12

Please sign in to comment.