--- /dev/null
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import React, { ReactElement, ReactNode, useState } from 'react';
+import { Button, Grid } from "@material-ui/core";
+import { GridProps } from '@material-ui/core/Grid';
+import { isArray } from 'lodash';
+import { DefaultView } from 'components/default-view/default-view';
+import { InfoIcon } from 'components/icon/icon';
+import { ReactNodeArray } from 'prop-types';
+
+interface MPVHideablePanelDataProps {
+ name: string;
+ visible: boolean;
+ children: ReactNode;
+}
+
+interface MPVHideablePanelActionProps {
+ doHidePanel: () => void;
+}
+
+type MPVPanelProps = MPVHideablePanelDataProps & MPVHideablePanelActionProps;
+
+const MPVHideablePanel = ({doHidePanel, name, visible, ...props}: MPVPanelProps) =>
+ visible
+ ? <>
+ {React.cloneElement((props.children as ReactElement), { doHidePanel, panelName: name })}
+ </>
+ : null;
+
+interface MPVPanelContentDataProps {
+ panelName?: string;
+ children: ReactElement;
+}
+
+interface MPVPanelContentActionProps {
+ doHidePanel?: () => void;
+}
+
+type MPVPanelContentProps = MPVPanelContentDataProps & MPVPanelContentActionProps & GridProps;
+
+// Grid item compatible component for layout and MPV props passing
+export const MPVPanelContent = ({doHidePanel, panelName, ...props}: MPVPanelContentProps) =>
+ <Grid item {...props}>
+ {React.cloneElement(props.children, { doHidePanel, panelName })}
+ </Grid>;
+
+export interface MPVContainerDataProps {
+ panelNames?: string[];
+}
+
+type MPVContainerProps = MPVContainerDataProps & GridProps;
+
+// Grid container compatible component that also handles panel toggling.
+export const MPVContainer = ({children, panelNames, ...props}: MPVContainerProps) => {
+ if (children === undefined || children === null || children === {}) {
+ children = [];
+ } else if (!isArray(children)) {
+ children = [children];
+ }
+ const visibility = (children as ReactNodeArray).map(() => true);
+ const [panelVisibility, setPanelVisibility] = useState<boolean[]>(visibility);
+
+ let panels: JSX.Element[] = [];
+ let toggles: JSX.Element[] = [];
+
+ if (isArray(children)) {
+ for (let idx = 0; idx < children.length; idx++) {
+ const toggleFn = (idx: number) => () => {
+ setPanelVisibility([
+ ...panelVisibility.slice(0, idx),
+ !panelVisibility[idx],
+ ...panelVisibility.slice(idx+1)
+ ])
+ };
+ const toggleLabel = panelVisibility[idx] ? 'Hide' : 'Show'
+ const panelName = panelNames === undefined
+ ? `Panel ${idx+1}`
+ : panelNames[idx] || `Panel ${idx+1}`;
+
+
+ toggles = [
+ ...toggles,
+ <Button onClick={toggleFn(idx)}>{toggleLabel} {panelName}</Button>
+ ];
+
+ const aPanel =
+ <MPVHideablePanel visible={panelVisibility[idx]} name={panelName} doHidePanel={toggleFn(idx)}>
+ {children[idx]}
+ </MPVHideablePanel>;
+ panels = [...panels, aPanel];
+ };
+ };
+
+ return <Grid container {...props}>
+ { toggles }
+ { panelVisibility.includes(true)
+ ? panels
+ : <Grid container alignItems='center' justify='center'>
+ <DefaultView messages={["All panels are hidden.", "Click on the buttons above to show them."]} icon={InfoIcon} />
+ </Grid> }
+ </Grid>;
+};