-
Notifications
You must be signed in to change notification settings - Fork 15
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
||
|
@@ -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); | ||
|
@@ -41,6 +42,10 @@ const Breadcrumbs = () => { | |
const toggleHiddenListVisible = useCallback(() => { | ||
setHiddenListVisible(!hiddenListVisible); | ||
}, [setHiddenListVisible, hiddenListVisible]); | ||
const handleTogglerClick = (event) => { | ||
event.stopPropagation(); | ||
toggleHiddenListVisible(); | ||
}; | ||
const renderHiddenList = () => { | ||
if (!hiddenItems.length) { | ||
return null; | ||
|
@@ -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}> | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in theory There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
There was a problem hiding this comment.
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.