feat(SidePanel): new side panel tab look-and-feel (#3657)
This commit is contained in:
1 parent
48bbd6281a
commit
85c899b399
22 files changed
+827
-246
No files matched your search
@@ -1,28 +1,27 @@
|
||||
import classnames from 'classnames';
|
||||
import PropTypes from 'prop-types';
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import React, { CSSProperties, useCallback, useEffect, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import SwiperCore, { A11y, Controller, Navigation, Pagination, Scrollbar } from 'swiper';
|
||||
import { Swiper, SwiperSlide } from 'swiper/react';
|
||||
|
||||
import { PanelService, ServicesManager, Types } from '@ohif/core';
|
||||
|
||||
import LegacyButton from '../LegacyButton';
|
||||
import Icon from '../Icon';
|
||||
import IconButton from '../IconButton';
|
||||
import Tooltip from '../Tooltip';
|
||||
|
||||
import 'swiper/css';
|
||||
import 'swiper/css/navigation';
|
||||
import './style.css';
|
||||
|
||||
const borderSize = 4;
|
||||
const expandedWidth = 248;
|
||||
const collapsedWidth = 25;
|
||||
const closeIconWidth = 30;
|
||||
const gridHorizontalPadding = 10;
|
||||
const tabSpacerWidth = 2;
|
||||
const gridAvailableWidth = expandedWidth - closeIconWidth - gridHorizontalPadding;
|
||||
|
||||
const baseStyle = {
|
||||
maxWidth: `${expandedWidth}px`,
|
||||
width: `${expandedWidth}px`,
|
||||
// To align the top of the side panel with the top of the viewport grid, use position relative and offset the
|
||||
// top by the same top offset as the viewport grid. Also adjust the height so that there is no overflow.
|
||||
position: 'relative',
|
||||
top: '0.2%',
|
||||
height: '99.8%',
|
||||
};
|
||||
|
||||
const collapsedHideWidth = expandedWidth - collapsedWidth - borderSize;
|
||||
@@ -38,7 +37,7 @@ const styleMap = {
|
||||
};
|
||||
|
||||
const baseClasses =
|
||||
'transition-all duration-300 ease-in-out h-100 bg-black border-black justify-start box-content flex flex-col';
|
||||
'transition-all duration-300 ease-in-out bg-black border-black justify-start box-content flex flex-col';
|
||||
|
||||
const classesMap = {
|
||||
open: {
|
||||
@@ -52,70 +51,110 @@ const classesMap = {
|
||||
};
|
||||
|
||||
const openStateIconName = {
|
||||
left: 'push-left',
|
||||
right: 'push-right',
|
||||
left: 'side-panel-close-left',
|
||||
right: 'side-panel-close-right',
|
||||
};
|
||||
|
||||
const position = {
|
||||
left: {
|
||||
right: 5,
|
||||
},
|
||||
right: {
|
||||
left: 5,
|
||||
},
|
||||
const getTabWidth = (numTabs: number) => {
|
||||
if (numTabs < 3) {
|
||||
return 68;
|
||||
} else {
|
||||
return 40;
|
||||
}
|
||||
};
|
||||
|
||||
const SidePanel = ({
|
||||
servicesManager,
|
||||
side,
|
||||
className,
|
||||
activeTabIndex: activeTabIndexProp,
|
||||
tabs,
|
||||
}) => {
|
||||
const panelService: PanelService = servicesManager?.services?.panelService;
|
||||
const getGridWidth = (numTabs: number) => {
|
||||
const spacersWidth = (numTabs - 1) * tabSpacerWidth;
|
||||
const tabsWidth = getTabWidth(numTabs) * numTabs;
|
||||
|
||||
if (gridAvailableWidth > tabsWidth + spacersWidth) {
|
||||
return tabsWidth + spacersWidth;
|
||||
}
|
||||
|
||||
return gridAvailableWidth;
|
||||
};
|
||||
|
||||
const getNumGridColumns = (numTabs: number) => {
|
||||
if (numTabs === 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Start by calculating the number of tabs assuming each tab was accompanied by a spacer.
|
||||
const tabWidth = getTabWidth(numTabs);
|
||||
const gridWidth = getGridWidth(numTabs);
|
||||
const numTabsWithOneSpacerEach = Math.floor(gridWidth / (tabWidth + tabSpacerWidth));
|
||||
|
||||
// But there is always one less spacer than tabs, so now check if an extra tab with one less spacer fits.
|
||||
if (
|
||||
(numTabsWithOneSpacerEach + 1) * tabWidth + numTabsWithOneSpacerEach * tabSpacerWidth <=
|
||||
gridWidth
|
||||
) {
|
||||
return numTabsWithOneSpacerEach + 1;
|
||||
}
|
||||
|
||||
return numTabsWithOneSpacerEach;
|
||||
};
|
||||
|
||||
const getGridStyle = (side: string, numTabs: number = 0): CSSProperties => {
|
||||
const gridWidth = getGridWidth(numTabs);
|
||||
const relativePosition = Math.max(0, Math.floor(expandedWidth - gridWidth) / 2 - closeIconWidth);
|
||||
return {
|
||||
position: 'relative',
|
||||
...(side === 'left' ? { right: `${relativePosition}px` } : { left: `${relativePosition}px` }),
|
||||
width: `${gridWidth}px`,
|
||||
};
|
||||
};
|
||||
|
||||
const getTabClassNames = (
|
||||
numColumns: number,
|
||||
numTabs: number,
|
||||
tabIndex: number,
|
||||
isActiveTab: boolean
|
||||
) =>
|
||||
classnames('h-[28px] mb-[2px] cursor-pointer text-white bg-black', {
|
||||
'hover:text-primary-active': !isActiveTab,
|
||||
'rounded-l': tabIndex % numColumns === 0,
|
||||
'rounded-r': (tabIndex + 1) % numColumns === 0 || tabIndex === numTabs - 1,
|
||||
});
|
||||
|
||||
const getTabStyle = (numTabs: number) => {
|
||||
return {
|
||||
width: `${getTabWidth(numTabs)}px`,
|
||||
};
|
||||
};
|
||||
|
||||
const getTabIconClassNames = (numTabs: number, isActiveTab: boolean) => {
|
||||
return classnames('h-full w-full flex items-center justify-center', {
|
||||
'bg-customblue-40': isActiveTab,
|
||||
rounded: isActiveTab,
|
||||
});
|
||||
};
|
||||
|
||||
const SidePanel = ({ side, className, activeTabIndex: activeTabIndexProp, tabs, onOpen }) => {
|
||||
const { t } = useTranslation('SidePanel');
|
||||
|
||||
// Tracks whether this SidePanel has been opened at least once since this SidePanel was inserted into the DOM.
|
||||
// Thus going to the Study List page and back to the viewer resets this flag for a SidePanel.
|
||||
const [hasBeenOpened, setHasBeenOpened] = useState(activeTabIndexProp !== null);
|
||||
const [panelOpen, setPanelOpen] = useState(activeTabIndexProp !== null);
|
||||
const [activeTabIndex, setActiveTabIndex] = useState(activeTabIndexProp ?? 0);
|
||||
const swiperRef = useRef() as any;
|
||||
const [swiper, setSwiper] = useState<any>();
|
||||
|
||||
const prevRef = React.useRef();
|
||||
const nextRef = React.useRef();
|
||||
const [activeTabIndex, setActiveTabIndex] = useState(0);
|
||||
|
||||
const openStatus = panelOpen ? 'open' : 'closed';
|
||||
const style = Object.assign({}, styleMap[openStatus][side], baseStyle);
|
||||
|
||||
const ActiveComponent = tabs[activeTabIndex].content;
|
||||
|
||||
useEffect(() => {
|
||||
if (panelOpen && swiper) {
|
||||
swiper.slideTo(activeTabIndex, 500);
|
||||
}
|
||||
}, [panelOpen, swiper]);
|
||||
|
||||
useEffect(() => {
|
||||
if (swiper) {
|
||||
swiper.params.navigation.prevEl = prevRef.current;
|
||||
swiper.params.navigation.nextEl = nextRef.current;
|
||||
swiper.navigation.init();
|
||||
swiper.navigation.update();
|
||||
}
|
||||
}, [swiper]);
|
||||
const ActiveComponent = tabs[activeTabIndex]?.content;
|
||||
|
||||
const updatePanelOpen = useCallback((panelOpen: boolean) => {
|
||||
setPanelOpen(panelOpen);
|
||||
if (panelOpen) {
|
||||
setHasBeenOpened(true);
|
||||
onOpen?.();
|
||||
}
|
||||
}, []);
|
||||
|
||||
const updateActiveTabIndex = useCallback(
|
||||
(activeTabIndex: number) => {
|
||||
if (activeTabIndex === null) {
|
||||
updatePanelOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setActiveTabIndex(activeTabIndex);
|
||||
updatePanelOpen(true);
|
||||
},
|
||||
@@ -123,24 +162,8 @@ const SidePanel = ({
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (panelService) {
|
||||
const activatePanelSubscription = panelService.subscribe(
|
||||
panelService.EVENTS.ACTIVATE_PANEL,
|
||||
(activatePanelEvent: Types.ActivatePanelEvent) => {
|
||||
if (!hasBeenOpened || activatePanelEvent.forceActive) {
|
||||
const tabIndex = tabs.findIndex(tab => tab.id === activatePanelEvent.panelId);
|
||||
if (tabIndex !== -1) {
|
||||
updateActiveTabIndex(tabIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return () => {
|
||||
activatePanelSubscription.unsubscribe();
|
||||
};
|
||||
}
|
||||
}, [tabs, hasBeenOpened, panelService, updateActiveTabIndex]);
|
||||
updateActiveTabIndex(activeTabIndexProp);
|
||||
}, [activeTabIndexProp, updateActiveTabIndex]);
|
||||
|
||||
const getCloseStateComponent = () => {
|
||||
const _childComponents = Array.isArray(tabs) ? tabs : [tabs];
|
||||
@@ -172,12 +195,10 @@ const SidePanel = ({
|
||||
side === 'left' ? 'justify-end ' : 'justify-start '
|
||||
)}
|
||||
>
|
||||
<IconButton
|
||||
<div
|
||||
id={`${childComponent.name}-btn`}
|
||||
variant="text"
|
||||
color="inherit"
|
||||
size="initial"
|
||||
className="text-primary-active"
|
||||
data-cy={`${childComponent.name}-btn`}
|
||||
className="text-primary-active hover:cursor-pointer"
|
||||
onClick={() => {
|
||||
updateActiveTabIndex(index);
|
||||
}}
|
||||
@@ -190,7 +211,7 @@ const SidePanel = ({
|
||||
height: '22px',
|
||||
}}
|
||||
/>
|
||||
</IconButton>
|
||||
</div>
|
||||
</Tooltip>
|
||||
))}
|
||||
</div>
|
||||
@@ -198,81 +219,116 @@ const SidePanel = ({
|
||||
);
|
||||
};
|
||||
|
||||
const getCloseIcon = () => {
|
||||
return (
|
||||
<div
|
||||
className={classnames(
|
||||
'flex h-[28px] cursor-pointer items-center justify-center',
|
||||
side === 'left' ? 'order-last' : 'order-first'
|
||||
)}
|
||||
style={{ width: `${closeIconWidth}px` }}
|
||||
onClick={() => {
|
||||
updatePanelOpen(prev => !prev);
|
||||
}}
|
||||
data-cy={`side-panel-header-${side}`}
|
||||
>
|
||||
<Icon
|
||||
name={openStateIconName[side]}
|
||||
className="text-primary-active"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const getTabGridComponent = () => {
|
||||
const numCols = getNumGridColumns(tabs.length);
|
||||
|
||||
return (
|
||||
<div className={classnames('flex grow ', side === 'right' ? 'justify-start' : 'justify-end')}>
|
||||
<div
|
||||
className={classnames('bg-primary-dark text-primary-active flex flex-wrap')}
|
||||
style={getGridStyle(side, tabs.length)}
|
||||
>
|
||||
{tabs.map((tab, tabIndex) => {
|
||||
return (
|
||||
<React.Fragment key={tabIndex}>
|
||||
{tabIndex % numCols !== 0 && (
|
||||
<div
|
||||
className={classnames(
|
||||
'flex h-[28px] w-[2px] items-center bg-black',
|
||||
tabSpacerWidth
|
||||
)}
|
||||
>
|
||||
<div className="bg-primary-dark h-[20px] w-full"></div>
|
||||
</div>
|
||||
)}
|
||||
<Tooltip
|
||||
position={'bottom'}
|
||||
key={tabIndex}
|
||||
content={`${tab.label}`}
|
||||
>
|
||||
<div
|
||||
className={getTabClassNames(
|
||||
numCols,
|
||||
tabs.length,
|
||||
tabIndex,
|
||||
tabIndex === activeTabIndex
|
||||
)}
|
||||
style={getTabStyle(tabs.length)}
|
||||
onClick={() => updateActiveTabIndex(tabIndex)}
|
||||
data-cy={`${tab.name}-btn`}
|
||||
>
|
||||
<div className={getTabIconClassNames(tabs.length, tabIndex === activeTabIndex)}>
|
||||
<Icon name={tab.iconName}></Icon>
|
||||
</div>
|
||||
</div>
|
||||
</Tooltip>
|
||||
</React.Fragment>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const getOneTabComponent = () => {
|
||||
return (
|
||||
<div
|
||||
className={classnames(
|
||||
'text-primary-active flex grow cursor-pointer justify-center self-center text-[13px]'
|
||||
)}
|
||||
style={{
|
||||
...(side === 'left'
|
||||
? { marginLeft: `${closeIconWidth}px` }
|
||||
: { marginRight: `${closeIconWidth}px` }),
|
||||
}}
|
||||
data-cy={`${tabs[0].name}-btn`}
|
||||
onClick={() => updatePanelOpen(prev => !prev)}
|
||||
>
|
||||
<span>{tabs[0].label}</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const getOpenStateComponent = () => {
|
||||
return (
|
||||
<div className="bg-primary-dark flex rounded-t pt-1.5 pb-[2px]">
|
||||
{getCloseIcon()}
|
||||
{tabs.length === 1 ? getOneTabComponent() : getTabGridComponent()}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={classnames(className, baseClasses, classesMap[openStatus][side])}
|
||||
style={style}
|
||||
>
|
||||
{panelOpen ? (
|
||||
<React.Fragment>
|
||||
{/** Panel Header with Arrow and Close Actions */}
|
||||
<div
|
||||
className={classnames('flex-static bg-primary-dark flex h-9 cursor-pointer px-[10px]')}
|
||||
onClick={() => {
|
||||
updatePanelOpen(prev => !prev);
|
||||
// slideToActivePanel();
|
||||
}}
|
||||
data-cy={`side-panel-header-${side}`}
|
||||
>
|
||||
{/* TODO This should be redesigned to not be a button. */}
|
||||
<LegacyButton
|
||||
variant="text"
|
||||
color="inherit"
|
||||
border="none"
|
||||
rounded="none"
|
||||
className="flex-static relative flex w-full flex-row items-center px-3"
|
||||
name={tabs.length === 1 ? `${tabs[activeTabIndex].name}` : ''}
|
||||
>
|
||||
<Icon
|
||||
name={openStateIconName[side]}
|
||||
className={classnames(
|
||||
'text-primary-active absolute',
|
||||
side === 'left' && 'order-last'
|
||||
)}
|
||||
style={{ ...position[side] }}
|
||||
/>
|
||||
{/* Todo: ass secondary label here */}
|
||||
<span className="text-primary-active">
|
||||
{tabs.length === 1 && (t(tabs[activeTabIndex].label) as string)}
|
||||
</span>
|
||||
</LegacyButton>
|
||||
</div>
|
||||
{tabs.length > 1 &&
|
||||
_getMoreThanOneTabLayout(
|
||||
swiperRef,
|
||||
setSwiper,
|
||||
prevRef,
|
||||
nextRef,
|
||||
tabs,
|
||||
activeTabIndex,
|
||||
updateActiveTabIndex
|
||||
)}
|
||||
{/** carousel navigation with the arrows */}
|
||||
{/** only show carousel nav if tabs are more than 3 tabs */}
|
||||
{tabs.length > 3 && (
|
||||
<div className="text-primary-active bg-primary-dark flex w-full justify-end gap-2 py-1 px-2">
|
||||
<button
|
||||
ref={prevRef}
|
||||
className="swiper-button-prev-custom"
|
||||
>
|
||||
<Icon
|
||||
name={'icon-prev'}
|
||||
className={classnames('text-primary-active')}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
ref={nextRef}
|
||||
className="swiper-button-next-custom"
|
||||
>
|
||||
<Icon
|
||||
name={'icon-next'}
|
||||
className={classnames('text-primary-active')}
|
||||
/>
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<>
|
||||
{getOpenStateComponent()}
|
||||
<ActiveComponent />
|
||||
</React.Fragment>
|
||||
</>
|
||||
) : (
|
||||
<React.Fragment>{getCloseStateComponent()}</React.Fragment>
|
||||
)}
|
||||
@@ -282,10 +338,10 @@ const SidePanel = ({
|
||||
|
||||
SidePanel.defaultProps = {
|
||||
defaultComponentOpen: null,
|
||||
activeTabIndex: null, // the default is to close the side panel
|
||||
};
|
||||
|
||||
SidePanel.propTypes = {
|
||||
servicesManager: PropTypes.instanceOf(ServicesManager),
|
||||
side: PropTypes.oneOf(['left', 'right']).isRequired,
|
||||
className: PropTypes.string,
|
||||
activeTabIndex: PropTypes.number,
|
||||
@@ -300,74 +356,7 @@ SidePanel.propTypes = {
|
||||
})
|
||||
),
|
||||
]),
|
||||
onOpen: PropTypes.func,
|
||||
};
|
||||
|
||||
function _getMoreThanOneTabLayout(
|
||||
swiperRef: any,
|
||||
setSwiper: React.Dispatch<any>,
|
||||
prevRef: React.MutableRefObject<undefined>,
|
||||
nextRef: React.MutableRefObject<undefined>,
|
||||
tabs: any,
|
||||
activeTabIndex: any,
|
||||
updateActiveTabIndex
|
||||
) {
|
||||
return (
|
||||
<div
|
||||
className="flex-static collapse-sidebar relative"
|
||||
style={{
|
||||
backgroundColor: '#06081f',
|
||||
}}
|
||||
>
|
||||
<div className="w-full">
|
||||
<Swiper
|
||||
onInit={(core: SwiperCore) => {
|
||||
swiperRef.current = core.el;
|
||||
}}
|
||||
simulateTouch={false}
|
||||
modules={[Navigation, Pagination, Scrollbar, A11y, Controller]}
|
||||
slidesPerView={3}
|
||||
spaceBetween={5}
|
||||
onSwiper={swiper => setSwiper(swiper)}
|
||||
navigation={{
|
||||
prevEl: prevRef?.current,
|
||||
nextEl: nextRef?.current,
|
||||
}}
|
||||
>
|
||||
{tabs.map((obj, index) => (
|
||||
<SwiperSlide key={index}>
|
||||
<div
|
||||
className={classnames(
|
||||
index === activeTabIndex ? 'bg-secondary-main text-white' : 'text-aqua-pale',
|
||||
'flex cursor-pointer flex-col items-center justify-center rounded-[4px] px-4 py-1 text-center hover:text-white'
|
||||
)}
|
||||
key={index}
|
||||
onClick={() => {
|
||||
updateActiveTabIndex(index);
|
||||
}}
|
||||
data-cy={`${obj.name}-btn`}
|
||||
>
|
||||
<span>
|
||||
<Icon
|
||||
name={obj.iconName}
|
||||
className={classnames(
|
||||
index === activeTabIndex ? 'text-white' : 'text-primary-active'
|
||||
)}
|
||||
style={{
|
||||
width: '22px',
|
||||
height: '22px',
|
||||
}}
|
||||
/>
|
||||
</span>
|
||||
<span className="mt-[5px] select-none whitespace-nowrap text-[10px] font-medium">
|
||||
{obj.label}
|
||||
</span>
|
||||
</div>
|
||||
</SwiperSlide>
|
||||
))}
|
||||
</Swiper>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default SidePanel;
|
||||
@@ -38,6 +38,7 @@ SidePanel is a component that renders the SidePanels.
|
||||
args={{
|
||||
side: 'left',
|
||||
defaultComponentOpen: 'Example',
|
||||
activeTabIndex: 0,
|
||||
tabs: [
|
||||
{
|
||||
id: 'tab1',
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
.swiper-button-next.swiper-button-disabled {
|
||||
pointer-events: none;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.swiper-button-prev.swiper-button-disabled {
|
||||
pointer-events: none;
|
||||
opacity: 0;
|
||||
}
|
||||
.swiper-button-prev {
|
||||
pointer-events: none;
|
||||
opacity: 0;
|
||||
}
|
||||
.swiper-button-next {
|
||||
pointer-events: none;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.swiper-button-next-custom.swiper-button-disabled {
|
||||
pointer-events: none;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.swiper-button-prev-custom.swiper-button-disabled {
|
||||
pointer-events: none;
|
||||
opacity: 0.6;
|
||||
}
|
||||
Reference in new issue
Block a user