1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React, { useState, useRef, useEffect } from 'react';
6 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
7 import classnames from 'classnames';
8 import { ArvadosTheme } from 'common/custom-theme';
9 import { OverflowMenu, OverflowChild } from './ms-toolbar-overflow-menu';
11 type CssRules = 'visible' | 'inVisible' | 'toolbarWrapper' | 'overflowStyle';
13 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
16 visibility: 'visible',
22 pointerEvents: 'none',
27 padding: '0 0px 0 20px',
39 children: OverflowChild[];
43 export const IntersectionObserverWrapper = withStyles(styles)((props: WrapperProps & WithStyles<CssRules>) => {
44 const { classes, children, menuLength } = props;
45 const lastEntryId = (children[menuLength - 1] as any).props['data-targetid'];
46 const navRef = useRef<any>(null);
47 const [visibilityMap, setVisibilityMap] = useState<Record<string, boolean>>({});
48 const [numHidden, setNumHidden] = useState(() => findNumHidden(visibilityMap));
50 const prevNumHidden = useRef(numHidden);
52 const handleIntersection = (entries) => {
53 const updatedEntries: Record<string, boolean> = {};
54 entries.forEach((entry) => {
55 const targetid = entry.target.dataset.targetid as string;
56 //if true, the element is visible
57 if (entry.isIntersecting) {
58 updatedEntries[targetid] = true;
60 updatedEntries[targetid] = false;
64 setVisibilityMap((prev) => ({
67 [lastEntryId]: Object.keys(updatedEntries)[0] === lastEntryId,
71 //ensures that the last element is always visible if the second to last is visible
73 if ((prevNumHidden.current > 1 || prevNumHidden.current === 0) && numHidden === 1) {
74 setVisibilityMap((prev) => ({
79 prevNumHidden.current = numHidden;
80 }, [numHidden, lastEntryId]);
83 setNumHidden(findNumHidden(visibilityMap));
86 useEffect((): any => {
88 const observer = new IntersectionObserver(handleIntersection, {
90 rootMargin: '0px -30px 0px 0px',
93 // We are adding observers to child elements of the container div
94 // with ref as navRef. Notice that we are adding observers
95 // only if we have the data attribute targetid on the child element
97 Array.from(navRef.current.children).forEach((item: any) => {
98 if (item.dataset.targetid) {
99 observer.observe(item);
103 observer.disconnect();
105 // eslint-disable-next-line
108 function findNumHidden(visMap: {}) {
109 return Object.values(visMap).filter((x) => x === false).length;
114 className={classes.toolbarWrapper}
117 {React.Children.map(children, (child) => {
118 return React.cloneElement(child, {
119 className: classnames(child.props.className, {
120 [classes.visible]: !!visibilityMap[child.props['data-targetid']],
121 [classes.inVisible]: !visibilityMap[child.props['data-targetid']],
127 visibilityMap={visibilityMap}
128 className={classes.overflowStyle}
130 {children.filter((child) => !child.props['data-targetid'].includes("Divider"))}