21315: functionality to skip first hidden button in place Arvados-DCO-1.1-Signed...
[arvados.git] / services / workbench2 / src / components / multiselect-toolbar / ms-toolbar-overflow-wrapper.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
10
11 type CssRules = 'visible' | 'inVisible' | 'toolbarWrapper' | 'overflowStyle';
12
13 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
14     visible: {
15         order: 0,
16         visibility: 'visible',
17         opacity: 1,
18     },
19     inVisible: {
20         order: 100,
21         visibility: 'hidden',
22         pointerEvents: 'none',
23     },
24     toolbarWrapper: {
25         display: 'flex',
26         overflow: 'hidden',
27         padding: '0 20px',
28         width: '100%',
29     },
30     overflowStyle: {
31         order: 99,
32         position: 'sticky',
33         right: '-2rem',
34         backgroundColor: 'white',
35     },
36 });
37
38 type WrapperProps = {
39     children: OverflowChild[];
40     menuLength: number;
41 };
42
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({});
48
49     const handleIntersection = (entries) => {
50         const updatedEntries = {};
51         entries.forEach((entry) => {
52             const targetid = entry.target.dataset.targetid;
53             if (entry.isIntersecting) {
54                 updatedEntries[targetid] = true;
55             } else {
56                 updatedEntries[targetid] = false;
57             }
58         });
59
60         setVisibilityMap((prev) => ({
61                 ...prev,
62                 ...updatedEntries,
63                 [lastEntryId]: Object.keys(updatedEntries)[0] === lastEntryId,
64             })
65         );
66     };
67
68     useEffect((): any => {
69         setVisibilityMap({})
70         const observer = new IntersectionObserver(handleIntersection, {
71             root: navRef.current,
72             rootMargin: '0px -20px 0px 0px',
73             threshold: 1,
74         });
75         // We are adding observers to child elements of the container div
76         // with ref as navRef. Notice that we are adding observers
77         // only if we have the data attribute targetid on the child element
78         if (navRef.current)
79             Array.from(navRef.current.children).forEach((item: any) => {
80                 if (item.dataset.targetid) {
81                     observer.observe(item);
82                 }
83             });
84         return () => {
85             observer.disconnect();
86         };
87         // eslint-disable-next-line 
88     }, [menuLength]);
89
90     const numHidden = (visMap: {}) => {
91         return Object.values(visMap).filter((x) => x === false).length;
92     };
93
94     return (
95         <div
96             className={classes.toolbarWrapper}
97             ref={navRef}
98         >
99             {React.Children.map(children, (child) => {
100                 return React.cloneElement(child, {
101                     className: classnames(child.props.className, {
102                         [classes.visible]: !!visibilityMap[child.props['data-targetid']],
103                         [classes.inVisible]: !visibilityMap[child.props['data-targetid']],
104                     }),
105                 });
106             })}
107             {numHidden(visibilityMap) >= 2 && (
108                 <OverflowMenu
109                     visibilityMap={visibilityMap}
110                     className={classes.overflowStyle}
111                 >
112                     {children}
113                 </OverflowMenu>
114             )}
115         </div>
116     );
117 });