Skip to content

Commit f189731

Browse files
committed
fix: close only the front window using the esc key
1 parent 6e439ee commit f189731

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/components/window/Window.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ export interface WindowProps {
1212
export const Window = forwardRef(({ data, contentGetter }: WindowProps, ref: RefObject<HTMLDivElement>): JSX.Element => {
1313
const { isResizable, isStatic, focusParent, id, order, shouldCloseOnEsc } = data;
1414

15-
const { focus: onFocus, close: onClose } = useWindowManager(id);
15+
const { focus: onFocus, close: onClose, frontWindow } = useWindowManager(id);
1616
const [zoom, onToggleZoom] = useWindowZoom(data);
1717

18-
const onEscKey = useCallback(() => shouldCloseOnEsc && onClose(), [onClose, shouldCloseOnEsc]);
18+
const onEscKey = useCallback(() => {
19+
if (shouldCloseOnEsc && frontWindow === id) {
20+
return onClose();
21+
}
22+
}, [frontWindow, id, onClose, shouldCloseOnEsc]);
1923

2024
return (
2125
<WindowFrame

src/hooks/manager.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCallback, useContext, useMemo } from "react";
22
import { WindowManagerContext } from "../context";
3-
import { closeWindow, focusWindow, getWindowsWithOrder, openWindow } from "../store";
3+
import { closeWindow, focusWindow, getOrder, getWindowsWithOrder, openWindow } from "../store";
44
import { WindowId, WindowManagerState, WindowType } from "../types";
55
import { ViewportContext, ViewportContextType } from "../ViewportContext";
66

@@ -43,6 +43,10 @@ export function useWindowManager<K extends number | string = any>(parent?: Windo
4343
);
4444

4545
const windows = useMemo(() => getWindowsWithOrder(state), [state]);
46+
const frontWindow = useMemo(() => {
47+
const order = getOrder(state);
48+
return order[order.length - 1];
49+
}, [state]);
4650

4751
const closeAll = useCallback(
4852
async (w = windows) => {
@@ -51,5 +55,5 @@ export function useWindowManager<K extends number | string = any>(parent?: Windo
5155
[close, windows],
5256
);
5357

54-
return { windows, open, focus, close, closeAll };
58+
return { windows, open, focus, close, closeAll, frontWindow };
5559
}

src/store/selectors.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export const getTopmostModal = createSelector(getWindowsState, getWindowsById, (
4242
export const getWindowsWithOrder = createSelector(
4343
getWindows,
4444
getOrder,
45-
getTopmostModal,
46-
<K extends number | string = any>(windows: WindowType<K>[], order: WindowId[], topmostModal: WindowId): Array<WindowWithOrder<K>> =>
45+
<K extends number | string = any>(windows: WindowType<K>[], order: WindowId[]): Array<WindowWithOrder<K>> =>
4746
windows.map((data) => ({
4847
...data,
4948
order: order.findIndex((id) => data.id === id),

0 commit comments

Comments
 (0)