Skip to content

Commit

Permalink
fix: close settings dropdown on click
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Feb 10, 2024
1 parent f0f488a commit 3183b8b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/app/common/hooks/use-onclickoutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const getOptions = (event: HandledEventsType) => {
return;
};

export function useOnClickOutside(ref: RefObject<HTMLElement>, handler: Handler | null) {
export function useOnClickOutside(
ref: RefObject<HTMLElement>,
handler: Handler | null,
exceptionIds: string[] = []
) {
const noHandler = !handler;
const handlerRef = useLatest(handler);

Expand All @@ -41,6 +45,14 @@ export function useOnClickOutside(ref: RefObject<HTMLElement>, handler: Handler
return;
}

const clickedElementIsAnException = exceptionIds
.map(el => document.getElementById(el))
.some(el => el?.contains(event.target as Node));

if (clickedElementIsAnException) {
return;
}

handlerRef.current(event);
};

Expand All @@ -53,5 +65,5 @@ export function useOnClickOutside(ref: RefObject<HTMLElement>, handler: Handler
document.removeEventListener(event, listener, getOptions(event) as EventListenerOptions);
});
};
}, [ref, handlerRef, noHandler]);
}, [ref, handlerRef, noHandler, exceptionIds]);
}
1 change: 1 addition & 0 deletions src/app/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function Header(props: HeaderProps) {
<NetworkModeBadge />
{!hideActions && (
<Button
id="settings-menu-btn"
data-testid={SettingsSelectors.SettingsMenuBtn}
onClick={() => setIsShowingSettings(!isShowingSettings)}
variant="ghost"
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/settings-dropdown/settings-dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function SettingsDropdown() {

const isLedger = useHasLedgerKeys();

useOnClickOutside(ref, isShowingSettings ? handleClose : null);
useOnClickOutside(ref, isShowingSettings ? handleClose : null, ['settings-menu-btn']);

// RouteUrls.Activity is nested off / so we need to use a link relative to the route
const linkRelativeType =
Expand Down

0 comments on commit 3183b8b

Please sign in to comment.