X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/9fb0741fb5dc74d7142c1d7b217ba60c242c3266..44c8f9ed561513b607d3eca752ad3e1efd376f56:/services/workbench2/src/components/multiselect-toolbar/ms-toolbar-overflow-wrapper.tsx diff --git a/services/workbench2/src/components/multiselect-toolbar/ms-toolbar-overflow-wrapper.tsx b/services/workbench2/src/components/multiselect-toolbar/ms-toolbar-overflow-wrapper.tsx index 3d8cf2f453..e0f32f1fa6 100644 --- a/services/workbench2/src/components/multiselect-toolbar/ms-toolbar-overflow-wrapper.tsx +++ b/services/workbench2/src/components/multiselect-toolbar/ms-toolbar-overflow-wrapper.tsx @@ -24,32 +24,36 @@ const styles: StyleRulesCallback = (theme: ArvadosTheme) => ({ toolbarWrapper: { display: 'flex', overflow: 'hidden', - padding: '0 20px', + padding: '0 0px 0 20px', width: '100%', }, overflowStyle: { order: 99, position: 'sticky', right: '-2rem', - backgroundColor: 'white', + width: 0, }, }); type WrapperProps = { children: OverflowChild[]; - length: number; + menuLength: number; }; export const IntersectionObserverWrapper = withStyles(styles)((props: WrapperProps & WithStyles) => { - const { classes, children, length } = props; - + const { classes, children, menuLength } = props; + const lastEntryId = (children[menuLength - 1] as any).props['data-targetid']; const navRef = useRef(null); - const [visibilityMap, setVisibilityMap] = useState({}); + const [visibilityMap, setVisibilityMap] = useState>({}); + const [numHidden, setNumHidden] = useState(() => findNumHidden(visibilityMap)); + + const prevNumHidden = useRef(numHidden); const handleIntersection = (entries) => { - const updatedEntries = {}; + const updatedEntries: Record = {}; entries.forEach((entry) => { - const targetid = entry.target.dataset.targetid; + const targetid = entry.target.dataset.targetid as string; + //if true, the element is visible if (entry.isIntersecting) { updatedEntries[targetid] = true; } else { @@ -60,14 +64,30 @@ export const IntersectionObserverWrapper = withStyles(styles)((props: WrapperPro setVisibilityMap((prev) => ({ ...prev, ...updatedEntries, + [lastEntryId]: Object.keys(updatedEntries)[0] === lastEntryId, })); }; + //ensures that the last element is always visible if the second to last is visible + useEffect(() => { + if ((prevNumHidden.current > 1 || prevNumHidden.current === 0) && numHidden === 1) { + setVisibilityMap((prev) => ({ + ...prev, + [lastEntryId]: true, + })); + } + prevNumHidden.current = numHidden; + }, [numHidden, lastEntryId]); + + useEffect(() => { + setNumHidden(findNumHidden(visibilityMap)); + }, [visibilityMap]); + useEffect((): any => { - setVisibilityMap({}) + setVisibilityMap({}); const observer = new IntersectionObserver(handleIntersection, { root: navRef.current, - rootMargin: '0px -20px 0px 0px', + rootMargin: '0px -30px 0px 0px', threshold: 1, }); // We are adding observers to child elements of the container div @@ -82,7 +102,12 @@ export const IntersectionObserverWrapper = withStyles(styles)((props: WrapperPro return () => { observer.disconnect(); }; - }, [length]); + // eslint-disable-next-line + }, [menuLength]); + + function findNumHidden(visMap: {}) { + return Object.values(visMap).filter((x) => x === false).length; + } return (
- {children} - + {numHidden >= 2 && ( + + {children.filter((child) => !child.props['data-targetid'].includes("Divider"))} + + )}
); });