Skip to content

Commit

Permalink
fix: overlapping element menus (#3993)
Browse files Browse the repository at this point in the history
* fix overlapping menus
  • Loading branch information
timarney authored Jul 12, 2024
1 parent 6cf2d39 commit 1975c77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/hooks/form-builder/useHandleAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ export const useHandleAdd = () => {
treeView?.current?.addItem(String(id));

const el = document.getElementById(`item-${id}`);

if (!el) return;

// Close all panel menus before focussing on the new element
const closeAll = new CustomEvent("close-all-panel-menus");
window && window.dispatchEvent(closeAll);

el?.focus();
},
[add, create, groupId, treeView]
Expand Down
18 changes: 17 additions & 1 deletion lib/hooks/form-builder/useIsWithin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useFocusWithin } from "react-aria";

export const useIsWithin = () => {
Expand All @@ -13,6 +13,22 @@ export const useIsWithin = () => {
},
});

// Add event listener for a custom close-all-panel-menus event
// This fixes an issue where the panel menu would still be open
// after adding and focusing on a new element
useEffect(() => {
window &&
window.addEventListener("close-all-panel-menus", () => {
setFocusWithin(false);
});

return () => {
window.removeEventListener("close-all-panel-menus", () => {
setFocusWithin(false);
});
};
}, []);

const isWithin = isFocusWithin ? true : false;

return { focusWithinProps, isWithin };
Expand Down

0 comments on commit 1975c77

Please sign in to comment.