Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-8188: Fix breadcrumbs hidden menu (not opening) #1250

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useState, useMemo, useEffect, useCallback } from 'react';
import React, { useContext, useState, useMemo, useEffect, useCallback, useRef } from 'react';

import Icon from '../../../common/icon/icon';

Expand All @@ -9,6 +9,7 @@ import { LoadedLocationsMapContext, MarkedLocationIdContext, GridActiveLocationI

const Breadcrumbs = () => {
const Translator = getTranslator();
const hiddenListWrapperRef = useRef();
const [, setMarkedLocationId] = useContext(MarkedLocationIdContext);
const [gridActiveLocationId, setGridActiveLocationId] = useContext(GridActiveLocationIdContext);
const [loadedLocationsFullMap, dispatchLoadedLocationsAction] = useContext(LoadedLocationsMapContext);
Expand Down Expand Up @@ -41,6 +42,10 @@ const Breadcrumbs = () => {
const toggleHiddenListVisible = useCallback(() => {
setHiddenListVisible(!hiddenListVisible);
}, [setHiddenListVisible, hiddenListVisible]);
const handleTogglerClick = (event) => {
event.stopPropagation();
toggleHiddenListVisible();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we need to stop this propagation because otherwise it may be caught again in hideHiddenMenuOnClickOutside. Also, note that this event is a react event, which may have a different propagation than a standard DOM event.

};
const renderHiddenList = () => {
if (!hiddenItems.length) {
return null;
Expand All @@ -56,8 +61,8 @@ const Breadcrumbs = () => {
});

return (
<div className="c-breadcrumbs__hidden-list-wrapper">
<button className={toggleClassNames} onClick={toggleHiddenListVisible} type="button">
<div ref={hiddenListWrapperRef} className="c-breadcrumbs__hidden-list-wrapper">
<button className={toggleClassNames} onClick={handleTogglerClick} type="button">
<Icon name="options" extraClasses="ibexa-icon--small-medium" />
</button>
<ul className={hiddenListClassNames}>
Expand Down Expand Up @@ -88,14 +93,24 @@ const Breadcrumbs = () => {
};

useEffect(() => {
if (hiddenListVisible) {
window.document.body.addEventListener('click', toggleHiddenListVisible, false);
} else {
window.document.body.removeEventListener('click', toggleHiddenListVisible, false);
if (!hiddenListVisible) {
return;
}

return () => window.document.body.removeEventListener('click', toggleHiddenListVisible, false);
}, [hiddenListVisible, toggleHiddenListVisible]);
const hideHiddenMenuOnClickOutside = (event) => {
const { target } = event;

if (hiddenListWrapperRef.current?.contains(target) ?? false) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in theory ?? false is unnecessary, I think? as if it does not exist, it's undefined, which is falsy as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to be explicit here so that it will be easier to understand when reading.

}

setHiddenListVisible(false);
};

window.document.body.addEventListener('click', hideHiddenMenuOnClickOutside, false);

return () => window.document.body.removeEventListener('click', hideHiddenMenuOnClickOutside, false);
}, [hiddenListVisible, setHiddenListVisible, hiddenListWrapperRef]);

if (loadedLocationsMap.some((loadedLocation) => loadedLocation.parentLocationId !== 1 && !loadedLocation.location)) {
return null;
Expand Down
Loading