18128: Adds panel indication and auto-scroll on mouse hovering.
authorLucas Di Pentima <lucas.dipentima@curii.com>
Fri, 19 Nov 2021 22:36:32 +0000 (19:36 -0300)
committerLucas Di Pentima <lucas.dipentima@curii.com>
Thu, 2 Dec 2021 23:01:57 +0000 (20:01 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas.dipentima@curii.com>

src/components/multi-panel-view/multi-panel-view.tsx

index 35daa13cba586cc3e84c1d0195aac85ccd4bff7f..dbb379218a4b57163bcec88ed2769be1ce6d5874 100644 (file)
@@ -2,8 +2,16 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import React, { ReactElement, ReactNode, useState } from 'react';
-import { Button, Grid, StyleRulesCallback, Tooltip, withStyles, WithStyles } from "@material-ui/core";
+import React, { MutableRefObject, ReactElement, ReactNode, useEffect, useRef, useState } from 'react';
+import {
+    Button,
+    Grid,
+    Paper,
+    StyleRulesCallback,
+    Tooltip,
+    withStyles,
+    WithStyles
+} from "@material-ui/core";
 import { GridProps } from '@material-ui/core/Grid';
 import { isArray } from 'lodash';
 import { DefaultView } from 'components/default-view/default-view';
 import { GridProps } from '@material-ui/core/Grid';
 import { isArray } from 'lodash';
 import { DefaultView } from 'components/default-view/default-view';
@@ -32,7 +40,9 @@ interface MPVHideablePanelDataProps {
     name: string;
     visible: boolean;
     maximized: boolean;
     name: string;
     visible: boolean;
     maximized: boolean;
+    illuminated: boolean;
     children: ReactNode;
     children: ReactNode;
+    panelRef?: MutableRefObject<any>;
 }
 
 interface MPVHideablePanelActionProps {
 }
 
 interface MPVHideablePanelActionProps {
@@ -42,16 +52,18 @@ interface MPVHideablePanelActionProps {
 
 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
 
 
 type MPVHideablePanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
 
-const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, ...props}: MPVHideablePanelProps) =>
+const MPVHideablePanel = ({doHidePanel, doMaximizePanel, name, visible, maximized, illuminated, ...props}: MPVHideablePanelProps) =>
     visible
     ? <>
     visible
     ? <>
-        {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel,panelName: name, panelMaximized: maximized })}
+        {React.cloneElement((props.children as ReactElement), { doHidePanel, doMaximizePanel, panelName: name, panelMaximized: maximized, panelIlluminated: illuminated, panelRef: props.panelRef })}
     </>
     : null;
 
 interface MPVPanelDataProps {
     panelName?: string;
     panelMaximized?: boolean;
     </>
     : null;
 
 interface MPVPanelDataProps {
     panelName?: string;
     panelMaximized?: boolean;
+    panelIlluminated?: boolean;
+    panelRef?: MutableRefObject<any>;
 }
 
 interface MPVPanelActionProps {
 }
 
 interface MPVPanelActionProps {
@@ -65,10 +77,20 @@ export type MPVPanelProps = MPVPanelDataProps & MPVPanelActionProps;
 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
 
 // Grid item compatible component for layout and MPV props passing
 type MPVPanelContentProps = {children: ReactElement} & MPVPanelProps & GridProps;
 
 // Grid item compatible component for layout and MPV props passing
-export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName, panelMaximized, ...props}: MPVPanelContentProps) =>
-    <Grid item {...props}>
-        {React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })}
+export const MPVPanelContent = ({doHidePanel, doMaximizePanel, panelName, panelMaximized, panelIlluminated, panelRef, ...props}: MPVPanelContentProps) => {
+    useEffect(() => {
+        if (panelRef && panelRef.current) {
+            panelRef.current.scrollIntoView({behavior: 'smooth'});
+        }
+    }, [panelRef]);
+
+    return <Grid item {...props}>
+        <span ref={panelRef} /> {/* Element to scroll to when the panel is selected */}
+        <Paper style={{height: '100%'}} elevation={panelIlluminated ? 8 : 0}>
+            {React.cloneElement(props.children, { doHidePanel, doMaximizePanel, panelName, panelMaximized })}
+        </Paper>
     </Grid>;
     </Grid>;
+}
 
 export interface MPVPanelState {
     name: string;
 
 export interface MPVPanelState {
     name: string;
@@ -91,16 +113,25 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
             (panelStates[idx] &&
                 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
     const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
             (panelStates[idx] &&
                 (panelStates[idx].visible || panelStates[idx].visible === undefined)));
     const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
+    const [brightenedPanel, setBrightenedPanel] = useState<number>(-1);
+    const panelRef = useRef<any>(null);
 
     let panels: JSX.Element[] = [];
     let toggles: JSX.Element[] = [];
 
     if (isArray(children)) {
         for (let idx = 0; idx < children.length; idx++) {
 
     let panels: JSX.Element[] = [];
     let toggles: JSX.Element[] = [];
 
     if (isArray(children)) {
         for (let idx = 0; idx < children.length; idx++) {
-            const toggleFn = (idx: number) => () => {
+            const showFn = (idx: number) => () => {
+                setPanelVisibility([
+                    ...panelVisibility.slice(0, idx),
+                    true,
+                    ...panelVisibility.slice(idx+1)
+                ]);
+            };
+            const hideFn = (idx: number) => () => {
                 setPanelVisibility([
                     ...panelVisibility.slice(0, idx),
                 setPanelVisibility([
                     ...panelVisibility.slice(0, idx),
-                    !panelVisibility[idx],
+                    false,
                     ...panelVisibility.slice(idx+1)
                 ])
             };
                     ...panelVisibility.slice(idx+1)
                 ])
             };
@@ -118,12 +149,10 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
             const panelName = panelStates === undefined
                 ? `Panel ${idx+1}`
                 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
             const panelName = panelStates === undefined
                 ? `Panel ${idx+1}`
                 : (panelStates[idx] && panelStates[idx].name) || `Panel ${idx+1}`;
-            const toggleVariant = panelVisibility[idx]
-                ? "contained"
-                : "text";
+            const toggleVariant = "outlined";
             const toggleTooltip = panelVisibility[idx]
             const toggleTooltip = panelVisibility[idx]
-                ? `Hide ${panelName} panel`
-                : `Show ${panelName} panel`;
+                ? ''
+                :`Show ${panelName} panel`;
             const panelIsMaximized = panelVisibility[idx] &&
                 panelVisibility.filter(e => e).length === 1;
 
             const panelIsMaximized = panelVisibility[idx] &&
                 panelVisibility.filter(e => e).length === 1;
 
@@ -132,7 +161,9 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
                 <Tooltip title={toggleTooltip} disableFocusListener>
                     <Button variant={toggleVariant} size="small" color="primary"
                         className={classNames(classes.button)}
                 <Tooltip title={toggleTooltip} disableFocusListener>
                     <Button variant={toggleVariant} size="small" color="primary"
                         className={classNames(classes.button)}
-                        onClick={toggleFn(idx)}>
+                        onMouseEnter={() => setBrightenedPanel(idx)}
+                        onMouseLeave={() => setBrightenedPanel(-1)}
+                        onClick={showFn(idx)}>
                             {panelName}
                             {toggleIcon}
                     </Button>
                             {panelName}
                             {toggleIcon}
                     </Button>
@@ -141,8 +172,9 @@ const MPVContainerComponent = ({children, panelStates, classes, ...props}: MPVCo
 
             const aPanel =
                 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
 
             const aPanel =
                 <MPVHideablePanel key={idx} visible={panelVisibility[idx]} name={panelName}
-                    maximized={panelIsMaximized}
-                    doHidePanel={toggleFn(idx)} doMaximizePanel={maximizeFn(idx)}>
+                    panelRef={(idx === brightenedPanel) ? panelRef : undefined}
+                    maximized={panelIsMaximized} illuminated={idx === brightenedPanel}
+                    doHidePanel={hideFn(idx)} doMaximizePanel={maximizeFn(idx)}>
                     {children[idx]}
                 </MPVHideablePanel>;
             panels = [...panels, aPanel];
                     {children[idx]}
                 </MPVHideablePanel>;
             panels = [...panels, aPanel];