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));
49 const prevNumHidden = useRef(numHidden);
51 const handleIntersection = (entries) => {
52 const updatedEntries: Record<string, boolean> = {};
53 entries.forEach((entry) => {
54 const targetid = entry.target.dataset.targetid as string;
55 //if true, the element is visible
56 if (entry.isIntersecting) {
57 updatedEntries[targetid] = true;
59 updatedEntries[targetid] = false;
63 setVisibilityMap((prev) => ({
66 [lastEntryId]: Object.keys(updatedEntries)[0] === lastEntryId,
70 //ensures that the last element is always visible if the second to last is visible
72 if ((prevNumHidden.current > 1 || prevNumHidden.current === 0) && numHidden === 1) {
73 setVisibilityMap((prev) => ({
78 prevNumHidden.current = numHidden;
79 }, [numHidden, lastEntryId]);
82 setNumHidden(findNumHidden(visibilityMap));
85 useEffect((): any => {
87 const observer = new IntersectionObserver(handleIntersection, {
89 rootMargin: '0px -30px 0px 0px',
92 // We are adding observers to child elements of the container div
93 // with ref as navRef. Notice that we are adding observers
94 // only if we have the data attribute targetid on the child element
96 Array.from(navRef.current.children).forEach((item: any) => {
97 if (item.dataset.targetid) {
98 observer.observe(item);
102 observer.disconnect();
104 // eslint-disable-next-line
107 function findNumHidden(visMap: {}) {
108 return Object.values(visMap).filter((x) => x === false).length;
113 className={classes.toolbarWrapper}
116 {React.Children.map(children, (child) => {
117 return React.cloneElement(child, {
118 className: classnames(child.props.className, {
119 [classes.visible]: !!visibilityMap[child.props['data-targetid']],
120 [classes.inVisible]: !visibilityMap[child.props['data-targetid']],
126 visibilityMap={visibilityMap}
127 className={classes.overflowStyle}
129 {children.filter((child) => !child.props['data-targetid'].includes("Divider"))}