Skip to content

Commit

Permalink
Clean up PR
Browse files Browse the repository at this point in the history
  • Loading branch information
susiekims committed Jan 24, 2024
1 parent 9879369 commit b472e81
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 541 deletions.
1 change: 0 additions & 1 deletion .storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ const Container = ({children, theme}: ContainerProps) => {
overflow: 'hidden',
margin: '-1rem',
background: selectedTheme.chartContainer.backgroundColor,
padding: '1000px 0',
}}
>
<div
Expand Down
1 change: 0 additions & 1 deletion packages/polaris-viz/src/components/ComboChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ export function Chart({
data={legend}
onDimensionChange={setLegendDimensions}
renderLegendContent={renderLegendContent}
width={width}
/>
)}
</ChartElements.Div>
Expand Down
1 change: 0 additions & 1 deletion packages/polaris-viz/src/components/DonutChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ export function Chart({
direction={legendDirection}
position={legendPosition}
maxWidth={maxLegendWidth}
width={width}
renderLegendContent={
shouldRenderLegendContentWithValues
? renderLegendContentWithValues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ export function Chart({
data={legend}
onDimensionChange={setLegendDimensions}
renderLegendContent={renderLegendContent}
width={width}
/>
)}
</ChartElements.Div>
Expand Down
4 changes: 1 addition & 3 deletions packages/polaris-viz/src/components/Legend/Legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,5 @@ export function Legend({
);
});

return (
<Fragment>{items}</Fragment>
);
return <Fragment>{items}</Fragment>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type {
import {classNames} from '../../utilities';

import style from './LegendContainer.scss';
import {HiddenLegendPopover} from './components/HiddenLegendPopover';
import {HiddenLegendPopover} from './components/HiddenLegendTooltip';

const LEGEND_GAP = 10;

Expand All @@ -37,10 +37,11 @@ export interface LegendContainerProps {
maxWidth?: number;
renderLegendContent?: RenderLegendContent;
onItemDimensionChange?: any;
width: number;
renderHiddenLegendLabel?: RenderHiddenLegendLabel;
/* If enabled, hides overflowing legend items with "+ n more" */
hideOverflow?: boolean;
enableHideOverflow?: boolean;
/* Width is required if enableHideOverflow is true */
width?: number;
renderHiddenLegendLabel?: RenderHiddenLegendLabel;
}

export function LegendContainer({
Expand All @@ -52,8 +53,8 @@ export function LegendContainer({
position = 'bottom-right',
maxWidth,
renderLegendContent,
width,
hideOverflow = false,
width = 0,
enableHideOverflow = false,
renderHiddenLegendLabel = (count) => `+${count} more`,
}: LegendContainerProps) {
const selectedTheme = useTheme();
Expand All @@ -70,15 +71,16 @@ export function LegendContainer({
const leftMargin = isPositionLeft ? 0 : horizontalMargin;

const legendItemDimensions = useRef([{width: 0, height: 0}]);
const [buttonWidth, setButtonWidth] = useState(0);
const [activatorWidth, setActivatorWidth] = useState(0);

const {displayedData, hiddenData} = useMemo(() => {
if (!hideOverflow) {
if (!enableHideOverflow) {
return {displayedData: allData, hiddenData: []};
}

let lastVisibleIndex = allData.length;
const containerWidth = width - leftMargin - horizontalMargin - buttonWidth;
const containerWidth =
width - leftMargin - horizontalMargin - activatorWidth;

legendItemDimensions.current.reduce((totalWidth, card, index) => {
if (totalWidth + card.width + index * LEGEND_GAP > containerWidth) {
Expand All @@ -92,16 +94,10 @@ export function LegendContainer({
displayedData: allData.slice(0, lastVisibleIndex || 1),
hiddenData: allData.slice(lastVisibleIndex || 1, allData.length),
};
}, [
legendItemDimensions.current,
direction,
allData,
width,
leftMargin,
horizontalMargin,
]);
}, [allData, width, leftMargin, horizontalMargin, activatorWidth]);

const hasHiddenData = hideOverflow && displayedData.length < allData.length;
const hasHiddenData =
enableHideOverflow && displayedData.length < allData.length;

const styleMap: {[key: string]: CSSProperties} = {
horizontal: {
Expand All @@ -110,7 +106,7 @@ export function LegendContainer({
? `0 ${horizontalMargin}px ${LEGENDS_BOTTOM_MARGIN}px ${leftMargin}px`
: `${LEGENDS_TOP_MARGIN}px ${horizontalMargin}px 0 ${leftMargin}px`,
flexDirection: 'row',
flexWrap: hasHiddenData ? 'nowrap' : 'wrap',
flexWrap: enableHideOverflow ? 'nowrap' : 'wrap',
},
vertical: {
alignItems: 'flex-start',
Expand Down Expand Up @@ -190,7 +186,7 @@ export function LegendContainer({
allData.length - displayedData.length,
)}
lastVisibleIndex={allData.length - hiddenData.length}
setButtonWidth={setButtonWidth}
setActivatorWidth={setActivatorWidth}
/>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
border-radius: 2px;
}

.LegendItems {
.Tooltip {
display: flex;
flex-direction: column;
padding: 4px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import {ReactNode, useCallback, useEffect, useRef, useState} from 'react';
import {Legend} from '../../Legend/Legend';
import type {LegendData} from '../../../types';

import style from './HiddenLegendPopover.scss';
import style from './HiddenLegendTooltip.scss';
import {changeColorOpacity, useTheme} from '@shopify/polaris-viz-core';
import {TOOLTIP_BG_OPACITY} from '../../../constants';
import {useBrowserCheck} from '../../../hooks/useBrowserCheck';
import {useColorVisionEvents} from '../../../hooks';
import {createPortal} from 'react-dom';
import {useRootContainer} from '../../../hooks/useRootContainer';
import {TOOLTIP_MARGIN} from '../../../components/TooltipWrapper';
import {TOOLTIP_MARGIN} from '../../TooltipWrapper';

interface Props {
activeIndex: number;
colorVisionType: string;
data: LegendData[];
label: ReactNode;
setButtonWidth: (width: number) => void;
setActivatorWidth: (width: number) => void;
theme?: string;
lastVisibleIndex?: number;
}
Expand All @@ -28,11 +27,10 @@ export function HiddenLegendPopover({
theme,
label,
lastVisibleIndex = 0,
setButtonWidth,
setActivatorWidth,
}: Props) {
const selectedTheme = useTheme();
const {isFirefox} = useBrowserCheck();
useColorVisionEvents(true, {});
const container = useRootContainer('hidden-legend-tooltip');
const tooltipRef = useRef<HTMLDivElement>(null);
const activatorRef = useRef<HTMLButtonElement>(null);
Expand All @@ -48,8 +46,8 @@ export function HiddenLegendPopover({
return;
}
const activator = activatorRef.current.getBoundingClientRect();
setButtonWidth(activator.width);
}, [setButtonWidth]);
setActivatorWidth(activator.width);
}, [setActivatorWidth]);

const getTooltipPosition = useCallback(() => {
if (tooltipRef.current == null || activatorRef.current == null) {
Expand All @@ -69,7 +67,6 @@ export function HiddenLegendPopover({
xPosition + tooltip.width + TOOLTIP_MARGIN > window.innerWidth;

if (goesPastRightOfWindow) {
// console.log({goesPastRightOfWindow});
return xPosition - tooltip.width + activator.width;
}
return xPosition;
Expand All @@ -80,7 +77,6 @@ export function HiddenLegendPopover({
yPosition + tooltip.height + TOOLTIP_MARGIN >=
window.innerHeight + window.scrollY;
if (goesPastBottomOfWindow) {
// console.log({goesPastBottomOfWindow});
return yPosition - tooltip.height - activator.height;
}

Expand All @@ -91,7 +87,7 @@ export function HiddenLegendPopover({
top: getYPosition(),
left: getXPosition(),
});
}, [setPosition, setButtonWidth]);
}, [setPosition, setActivatorWidth]);

return (
<>
Expand All @@ -100,15 +96,16 @@ export function HiddenLegendPopover({
ref={activatorRef}
onMouseEnter={getTooltipPosition}
onMouseLeave={() => setActive(false)}
onFocus={getTooltipPosition}
onBlur={() => setActive(false)}
>
{label}
</button>

{createPortal(
<div
className={style.LegendItems}
className={style.Tooltip}
ref={tooltipRef}
id="hidden-legend-items-tooltip"
style={{
visibility: active ? 'visible' : 'hidden',
zIndex: active ? 1 : -100000,
Expand Down
4 changes: 2 additions & 2 deletions packages/polaris-viz/src/components/LineChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function Chart({
xAxisOptions,
yAxisOptions,
}: ChartProps) {
useColorVisionEvents(data.length > 1, dimensions);
useColorVisionEvents(data.length > 1);

const selectedTheme = useTheme(theme);
const {isPerformanceImpacted} = useChartContext();
Expand Down Expand Up @@ -429,7 +429,7 @@ export function Chart({
renderLegendContent={renderLegendContent}
renderHiddenLegendLabel={renderHiddenLegendLabel}
width={width}
hideOverflow
enableHideOverflow
/>
)}
</Fragment>
Expand Down
Loading

0 comments on commit b472e81

Please sign in to comment.