Skip to content

Commit 8f5a243

Browse files
committed
cleanup bordered app
1 parent ded9050 commit 8f5a243

5 files changed

Lines changed: 73 additions & 104 deletions

File tree

src/components/BorderedApp/BorderedApp.tsx

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React, { useRef } from "react";
22
import useWindowManagerStore from "../../stores/windowManagerStore";
3-
4-
import "./BorderedApp.scss";
53
import { Dimensions } from "../../hooks/useDragToResize";
64
import BorderedAppMenu, {
75
BorderedAppMenuItemProps,
86
} from "./BorderedAppMenu/BorderedAppMenu";
97
import usePositionableElement from "../../hooks/usePositionableElement";
108

9+
import "./BorderedApp.scss";
1110
interface BorderedAppProps {
1211
title: string;
1312
type: string;
@@ -32,28 +31,20 @@ function BorderedApp({
3231
// Need a ref to point to the app for moving it around the screen
3332
const appRef = useRef<HTMLDivElement>(null);
3433

35-
// Need a ref for each of the cardinal (n,e,s,w)
36-
// and ordinal (ne, se, sw, nw) directions. These represent
37-
// the edges and corners of the app and are used for resizing it
38-
const resizeNRef = useRef<HTMLDivElement>(null);
39-
const resizeERef = useRef<HTMLDivElement>(null);
40-
const resizeSRef = useRef<HTMLDivElement>(null);
41-
const resizeWRef = useRef<HTMLDivElement>(null);
42-
const resizeNERef = useRef<HTMLDivElement>(null);
43-
const resizeNWRef = useRef<HTMLDivElement>(null);
44-
const resizeSERef = useRef<HTMLDivElement>(null);
45-
const resizeSWRef = useRef<HTMLDivElement>(null);
46-
47-
const { clickToMoveRef, maximize, minimize } = usePositionableElement({
34+
const {
35+
resizeHandleN,
36+
resizeHandleNE,
37+
resizeHandleE,
38+
resizeHandleSE,
39+
resizeHandleS,
40+
resizeHandleSW,
41+
resizeHandleW,
42+
resizeHandleNW,
43+
moveHandle,
44+
maximize,
45+
minimize,
46+
} = usePositionableElement({
4847
elementRef: appRef,
49-
resizeERef,
50-
resizeNRef,
51-
resizeSRef,
52-
resizeWRef,
53-
resizeNERef,
54-
resizeNWRef,
55-
resizeSERef,
56-
resizeSWRef,
5748
minDimensions,
5849
});
5950

@@ -70,13 +61,13 @@ function BorderedApp({
7061
height: initialDimensions.height,
7162
}}
7263
>
73-
<div className="bordered-app__corner-nw" ref={resizeNWRef} />
74-
<div className="bordered-app__edge-n" ref={resizeNRef} />
75-
<div className="bordered-app__corner-ne" ref={resizeNERef} />
76-
<div className="bordered-app__edge-e" ref={resizeERef} />
64+
<div className="bordered-app__corner-nw" ref={resizeHandleNW} />
65+
<div className="bordered-app__edge-n" ref={resizeHandleN} />
66+
<div className="bordered-app__corner-ne" ref={resizeHandleNE} />
67+
<div className="bordered-app__edge-e" ref={resizeHandleE} />
7768
<div
7869
className="bordered-app__title-bar"
79-
ref={clickToMoveRef}
70+
ref={moveHandle}
8071
onDoubleClick={maximize}
8172
>
8273
<div className="bordered-app__window-menus-wrapper">
@@ -111,10 +102,10 @@ function BorderedApp({
111102
</div>
112103
</div>
113104
<div className="bordered-app__content">{children}</div>
114-
<div className="bordered-app__corner-sw" ref={resizeSWRef} />
115-
<div className="bordered-app__edge-s" ref={resizeSRef} />
116-
<div className="bordered-app__corner-se" ref={resizeSERef} />
117-
<div className="bordered-app__edge-w" ref={resizeWRef} />
105+
<div className="bordered-app__corner-sw" ref={resizeHandleSW} />
106+
<div className="bordered-app__edge-s" ref={resizeHandleS} />
107+
<div className="bordered-app__corner-se" ref={resizeHandleSE} />
108+
<div className="bordered-app__edge-w" ref={resizeHandleW} />
118109
</div>
119110
);
120111
}

src/hooks/useDragToMove.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ interface UseDraggableProps {
3030
}
3131

3232
// complex logic should be a hook, not a component
33-
const useDragToMove = ({
34-
moveRef = null,
35-
elementRect,
36-
}: UseDraggableProps): [React.RefCallback<HTMLElement>, boolean] => {
33+
const useDragToMove = ({ moveRef = null, elementRect }: UseDraggableProps) => {
3734
const [pressed, setPressed] = useState(false);
3835
const ref = useRef<HTMLElement | null>(null);
3936

4037
const unsubscribe = useRef<VoidFunction>();
41-
const legacyRef: RefCallback<HTMLElement> = useCallback(
38+
const moveHandle: RefCallback<HTMLElement> = useCallback(
4239
(elem) => {
4340
ref.current = elem;
4441
if (unsubscribe.current) {
@@ -110,7 +107,7 @@ const useDragToMove = ({
110107
// actually it makes sense to return an array only when
111108
// you expect that on the caller side all of the fields
112109
// will be usually renamed
113-
return [legacyRef, pressed];
110+
return { moveHandle, pressed };
114111

115112
// > seems the best of them all to me
116113
// this code doesn't look pretty anymore, huh?

src/hooks/useDragToResize.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MutableRefObject, useEffect } from "react";
1+
import { MutableRefObject, useEffect, useRef } from "react";
22

33
// reference: https://stackoverflow.com/questions/62436814/react-drag-corner-of-element-to-resize-contents
44

@@ -27,29 +27,22 @@ interface UseDragToResizeProps {
2727
elementRef: React.RefObject<HTMLElement>;
2828
elementRect: MutableRefObject<Partial<Rect>>;
2929
minDimensions: Dimensions;
30-
resizeNRef?: React.RefObject<HTMLElement> | null;
31-
resizeNERef?: React.RefObject<HTMLElement> | null;
32-
resizeERef?: React.RefObject<HTMLElement> | null;
33-
resizeSERef?: React.RefObject<HTMLElement> | null;
34-
resizeSRef?: React.RefObject<HTMLElement> | null;
35-
resizeSWRef?: React.RefObject<HTMLElement> | null;
36-
resizeWRef?: React.RefObject<HTMLElement> | null;
37-
resizeNWRef?: React.RefObject<HTMLElement> | null;
3830
}
3931

4032
function useDragToResize({
4133
elementRef,
42-
resizeERef,
43-
resizeNRef,
44-
resizeSRef,
45-
resizeWRef,
46-
resizeNERef,
47-
resizeNWRef,
48-
resizeSERef,
49-
resizeSWRef,
5034
minDimensions,
5135
elementRect,
52-
}: UseDragToResizeProps): void {
36+
}: UseDragToResizeProps) {
37+
const resizeHandleN = useRef<HTMLDivElement>(null);
38+
const resizeHandleNE = useRef<HTMLDivElement>(null);
39+
const resizeHandleE = useRef<HTMLDivElement>(null);
40+
const resizeHandleSE = useRef<HTMLDivElement>(null);
41+
const resizeHandleS = useRef<HTMLDivElement>(null);
42+
const resizeHandleSW = useRef<HTMLDivElement>(null);
43+
const resizeHandleW = useRef<HTMLDivElement>(null);
44+
const resizeHandleNW = useRef<HTMLDivElement>(null);
45+
5346
useEffect(() => {
5447
/**
5548
* Whenever a mousedown event happens on any of the resize refs,
@@ -222,25 +215,25 @@ function useDragToResize({
222215
//#endregion
223216

224217
//#region add/remove handlers
225-
resizeERef?.current?.addEventListener("mousedown", handleMouseDownE);
226-
resizeNRef?.current?.addEventListener("mousedown", handleMouseDownN);
227-
resizeSRef?.current?.addEventListener("mousedown", handleMouseDownS);
228-
resizeWRef?.current?.addEventListener("mousedown", handleMouseDownW);
229-
230-
resizeNERef?.current?.addEventListener("mousedown", handleMouseDownNE);
231-
resizeSERef?.current?.addEventListener("mousedown", handleMouseDownSE);
232-
resizeSWRef?.current?.addEventListener("mousedown", handleMouseDownSW);
233-
resizeNWRef?.current?.addEventListener("mousedown", handleMouseDownNW);
234-
235-
const _resizeERef = resizeERef;
236-
const _resizeNRef = resizeNRef;
237-
const _resizeSRef = resizeSRef;
238-
const _resizeWRef = resizeWRef;
239-
240-
const _resizeNERef = resizeNERef;
241-
const _resizeSERef = resizeSERef;
242-
const _resizeSWRef = resizeSWRef;
243-
const _resizeNWRef = resizeNWRef;
218+
resizeHandleE?.current?.addEventListener("mousedown", handleMouseDownE);
219+
resizeHandleN?.current?.addEventListener("mousedown", handleMouseDownN);
220+
resizeHandleS?.current?.addEventListener("mousedown", handleMouseDownS);
221+
resizeHandleW?.current?.addEventListener("mousedown", handleMouseDownW);
222+
223+
resizeHandleNE?.current?.addEventListener("mousedown", handleMouseDownNE);
224+
resizeHandleSE?.current?.addEventListener("mousedown", handleMouseDownSE);
225+
resizeHandleSW?.current?.addEventListener("mousedown", handleMouseDownSW);
226+
resizeHandleNW?.current?.addEventListener("mousedown", handleMouseDownNW);
227+
228+
const _resizeERef = resizeHandleE;
229+
const _resizeNRef = resizeHandleN;
230+
const _resizeSRef = resizeHandleS;
231+
const _resizeWRef = resizeHandleW;
232+
233+
const _resizeNERef = resizeHandleNE;
234+
const _resizeSERef = resizeHandleSE;
235+
const _resizeSWRef = resizeHandleSW;
236+
const _resizeNWRef = resizeHandleNW;
244237

245238
return () => {
246239
_resizeERef?.current?.removeEventListener("mousedown", handleMouseDownE);
@@ -266,6 +259,17 @@ function useDragToResize({
266259
};
267260
//#endregion
268261
});
262+
263+
return {
264+
resizeHandleN: resizeHandleN,
265+
resizeHandleNE: resizeHandleNE,
266+
resizeHandleE: resizeHandleE,
267+
resizeHandleSE: resizeHandleSE,
268+
resizeHandleS: resizeHandleS,
269+
resizeHandleSW: resizeHandleSW,
270+
resizeHandleW: resizeHandleW,
271+
resizeHandleNW: resizeHandleNW,
272+
};
269273
}
270274

271275
export default useDragToResize;

src/hooks/usePositionableElement.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ import useWindowMinMax from "./useWindowMinMax";
66
interface UsePositionableElementProps {
77
elementRef: React.RefObject<HTMLElement>;
88
minDimensions: Dimensions;
9-
resizeNRef?: React.RefObject<HTMLElement> | null;
10-
resizeNERef?: React.RefObject<HTMLElement> | null;
11-
resizeERef?: React.RefObject<HTMLElement> | null;
12-
resizeSERef?: React.RefObject<HTMLElement> | null;
13-
resizeSRef?: React.RefObject<HTMLElement> | null;
14-
resizeSWRef?: React.RefObject<HTMLElement> | null;
15-
resizeWRef?: React.RefObject<HTMLElement> | null;
16-
resizeNWRef?: React.RefObject<HTMLElement> | null;
179
}
1810

1911
/**
@@ -25,14 +17,6 @@ interface UsePositionableElementProps {
2517
function usePositionableElement({
2618
elementRef,
2719
minDimensions,
28-
resizeNRef,
29-
resizeNERef,
30-
resizeERef,
31-
resizeSERef,
32-
resizeSRef,
33-
resizeSWRef,
34-
resizeWRef,
35-
resizeNWRef,
3620
}: UsePositionableElementProps) {
3721
// Hold a single ref for the elements rect,
3822
// so we dont have to call getBoundingClientRect every
@@ -62,24 +46,16 @@ function usePositionableElement({
6246
// by dragging the corners or edges of the element.
6347
// It needs to know the elementRect, so any changes
6448
// it makes can be shared with the other hooks.
65-
useDragToResize({
49+
const resizeHandles = useDragToResize({
6650
elementRef,
6751
elementRect,
6852
minDimensions,
69-
resizeNRef,
70-
resizeNERef,
71-
resizeERef,
72-
resizeSERef,
73-
resizeSRef,
74-
resizeSWRef,
75-
resizeWRef,
76-
resizeNWRef,
7753
});
7854

7955
// The move hook allows the app to be moved when
8056
// the element that has the clickToMoveRef is clicked and dragged.
8157
// Again, it needs to know the elementRect for the same reason mentioned above.
82-
const [clickToMoveRef] = useDragToMove({
58+
const { moveHandle } = useDragToMove({
8359
moveRef: elementRef,
8460
elementRect,
8561
});
@@ -92,9 +68,10 @@ function usePositionableElement({
9268
});
9369

9470
return {
95-
clickToMoveRef,
71+
moveHandle,
9672
minimize,
9773
maximize,
74+
...resizeHandles,
9875
};
9976
}
10077

src/stores/windowManagerStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { ReactNode, createRef } from "react";
22
import { create } from "zustand";
33

44
interface WindowManagerStoreState {
5+
contentRef: React.RefObject<HTMLDivElement>;
56
/**
67
* A map of all windows.
78
* The key is the type of window, and the value is another map.
@@ -12,16 +13,15 @@ interface WindowManagerStoreState {
1213
getWindows: () => Array<ReactNode>;
1314
addWindow: (windowType: string, windowId: string, window: ReactNode) => void;
1415
closeWindow: (windowType: string, windowId: string) => void;
15-
contentRef: React.RefObject<HTMLDivElement>;
1616
}
1717
const useWindowManagerStore = create<WindowManagerStoreState>()((set, get) => ({
18+
contentRef: createRef<HTMLDivElement>(),
1819
windowsMap: new Map(),
1920
getWindows() {
2021
return Array.from(get().windowsMap.values()).flatMap((map) =>
2122
Array.from(map.values())
2223
);
2324
},
24-
contentRef: createRef<HTMLDivElement>(),
2525
addWindow(windowType, windowId, window) {
2626
const windowsMap = get().windowsMap;
2727
const windowsOfType = windowsMap.get(windowType);

0 commit comments

Comments
 (0)