Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#2822 from yhua1998/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa authored Sep 13, 2023
2 parents b589f48 + 48e6087 commit 0387903
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/components/sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useEffect, useRef, useCallback } from "react";

import styles from "./home.module.scss";

Expand Down Expand Up @@ -67,18 +67,26 @@ function useDragSideBar() {
lastUpdateTime.current = Date.now();
const d = e.clientX - startX.current;
const nextWidth = limit(startDragWidth.current + d);
config.update((config) => (config.sidebarWidth = nextWidth));
config.update((config) => {
if (nextWidth < MIN_SIDEBAR_WIDTH) {
config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
} else {
config.sidebarWidth = nextWidth;
}
});
});

const handleMouseUp = useRef(() => {
startDragWidth.current = config.sidebarWidth ?? 300;
// In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
// startDragWidth.current = config.sidebarWidth ?? 300;
window.removeEventListener("mousemove", handleMouseMove.current);
window.removeEventListener("mouseup", handleMouseUp.current);
});

const onDragMouseDown = (e: MouseEvent) => {
startX.current = e.clientX;

// Remembers the initial width each time the mouse is pressed
startDragWidth.current = config.sidebarWidth;
window.addEventListener("mousemove", handleMouseMove.current);
window.addEventListener("mouseup", handleMouseUp.current);
};
Expand Down

0 comments on commit 0387903

Please sign in to comment.