Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
susiekims committed Jan 24, 2024
1 parent b472e81 commit 7c677c0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const SampleLegendChart = ({theme} = {theme: 'Default'}) => {
export const SampleLegendContainer = ({theme} = {theme: 'Default'}) => {
const selectedTheme = useTheme(theme);
const colors = getSeriesColors(6, selectedTheme);
const {legend, width} = useLegend({
const {legend} = useLegend({
data: [
{
shape: 'Line',
Expand Down Expand Up @@ -253,7 +253,6 @@ export const SampleLegendContainer = ({theme} = {theme: 'Default'}) => {
colorVisionType=""
data={legend}
onDimensionChange={() => {}}
width={width}
/>
</div>
</SimpleContainer>
Expand Down
17 changes: 8 additions & 9 deletions packages/polaris-viz/src/components/Legend/Legend.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {Fragment, type RefObject} from 'react';
import {Fragment} from 'react';
import type {RefObject} from 'react';
import {DEFAULT_THEME_NAME} from '@shopify/polaris-viz-core';

import {useExternalHideEvents} from '../../hooks';
import type {LegendData} from '../../types';

import {LegendItem} from './components/';
import type { LegendItemDimension } from './components/LegendItem/LegendItem';


import {LegendItem} from './components';
import type {LegendItemDimension} from './components';

export interface LegendProps {
data: LegendData[];
Expand All @@ -16,7 +15,7 @@ export interface LegendProps {
theme?: string;
itemDimensions?: RefObject<LegendItemDimension[]>;
backgroundColor?: string;
lastVisibleIndex?: number,
indexOffset?: number;
}

export function Legend({
Expand All @@ -25,8 +24,8 @@ export function Legend({
data,
theme = DEFAULT_THEME_NAME,
itemDimensions,
lastVisibleIndex = 0,
backgroundColor
indexOffset = 0,
backgroundColor,
}: LegendProps) {
const {hiddenIndexes} = useExternalHideEvents();

Expand All @@ -41,7 +40,7 @@ export function Legend({
{...legend}
activeIndex={activeIndex}
colorVisionType={colorVisionType}
index={index + lastVisibleIndex}
index={index + indexOffset}
theme={theme}
backgroundColor={backgroundColor}
onDimensionChange={(dimensions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
font-size: 12px;
font-family: $font-stack-base;
white-space: nowrap;
min-width: 0;
}

.Text {
overflow: hidden;
text-overflow: ellipsis;
}

.IconContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
getColorVisionEventAttrs,
getColorVisionStylesForActiveIndex,
} from '@shopify/polaris-viz-core';
import {useEffect, type ReactNode, useRef} from 'react';
import type {ReactNode} from 'react';
import {useEffect, useRef} from 'react';

import {
LEGEND_ITEM_LEFT_PADDING,
Expand All @@ -18,7 +19,7 @@ import style from './LegendItem.scss';

export interface LegendItemDimension {
width: number;
height: number
height: number;
}

export interface LegendItemProps extends LegendData {
Expand All @@ -29,6 +30,7 @@ export interface LegendItemProps extends LegendData {
theme?: string;
onDimensionChange?: ({width, height}: LegendItemDimension) => void;
backgroundColor?: string;
truncate?: boolean;
}

export function LegendItem({
Expand All @@ -44,9 +46,10 @@ export function LegendItem({
value,
onDimensionChange,
backgroundColor,
truncate = true,
}: LegendItemProps) {
const selectedTheme = useTheme(theme);
const ref = useRef<HTMLButtonElement | null>(null)
const ref = useRef<HTMLButtonElement | null>(null);

useEffect(() => {
if (onDimensionChange && ref.current != null) {
Expand All @@ -64,6 +67,7 @@ export function LegendItem({
});

const background = backgroundColor ?? selectedTheme.legend.backgroundColor;
const maxWidth = value ? '200px' : '100px';

return (
<button
Expand All @@ -77,6 +81,7 @@ export function LegendItem({
paddingLeft: LEGEND_ITEM_LEFT_PADDING,
paddingRight: LEGEND_ITEM_RIGHT_PADDING,
gap: LEGEND_ITEM_GAP,
maxWidth: truncate ? maxWidth : undefined,
}}
className={style.Legend}
ref={ref}
Expand All @@ -92,9 +97,21 @@ export function LegendItem({
renderSeriesIcon()
)}
<span className={style.TextContainer}>
<span style={{color: selectedTheme.legend.labelColor}}>{name}</span>
<span
className={style.Text}
style={{
color: selectedTheme.legend.labelColor,
}}
>
{name}
</span>
{value == null ? null : (
<span style={{color: selectedTheme.legend.valueColor}}>{value}</span>
<span
className={style.Text}
style={{color: selectedTheme.legend.valueColor}}
>
{value}
</span>
)}
</span>
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {LegendItem} from './LegendItem';
export type {LegendItemDimension} from './LegendItem';
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {LegendItem} from './LegendItem';
export type {LegendItemDimension} from './LegendItem';
1 change: 1 addition & 0 deletions packages/polaris-viz/src/components/Legend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {Legend} from './Legend';
export {LegendItem} from './components';
export type {LegendProps} from './Legend';
export {estimateLegendItemWidth} from './utilities/estimateLegendItemWidth';
export type {LegendItemDimension} from './components';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {CSSProperties, Dispatch, SetStateAction} from 'react';
import {useEffect, useMemo, useRef, useState} from 'react';
import {Fragment, useEffect, useMemo, useRef, useState} from 'react';
import isEqual from 'fast-deep-equal';
import {
getColorVisionEventAttrs,
Expand Down Expand Up @@ -94,7 +94,14 @@ export function LegendContainer({
displayedData: allData.slice(0, lastVisibleIndex || 1),
hiddenData: allData.slice(lastVisibleIndex || 1, allData.length),
};
}, [allData, width, leftMargin, horizontalMargin, activatorWidth]);
}, [
allData,
width,
leftMargin,
horizontalMargin,
activatorWidth,
enableHideOverflow,
]);

const hasHiddenData =
enableHideOverflow && displayedData.length < allData.length;
Expand Down Expand Up @@ -168,7 +175,7 @@ export function LegendContainer({
style={{...styleMap[direction], ...shouldCenterTiles(position)}}
>
{renderLegendContent?.(colorVisionInteractionMethods) ?? (
<>
<Fragment>
<Legend
activeIndex={activeIndex}
colorVisionType={colorVisionType}
Expand All @@ -189,7 +196,7 @@ export function LegendContainer({
setActivatorWidth={setActivatorWidth}
/>
)}
</>
</Fragment>
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
}

.Tooltip {
position: absolute;
display: flex;
flex-direction: column;
padding: 4px;
backdrop-filter: blur(5px);
border-radius: 5px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.2), 0 2px 10px rgba(0, 0, 0, 0.1);
position: absolute;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {ReactNode, useCallback, useEffect, useRef, useState} from 'react';
import {Legend} from '../../Legend/Legend';
import type {LegendData} from '../../../types';

import style from './HiddenLegendTooltip.scss';
import {Fragment, useCallback, useEffect, useRef, useState} from 'react';
import type {ReactNode} from 'react';
import {createPortal} from 'react-dom';
import {changeColorOpacity, useTheme} from '@shopify/polaris-viz-core';

import type {LegendData} from '../../../types';
import {TOOLTIP_BG_OPACITY} from '../../../constants';
import {useBrowserCheck} from '../../../hooks/useBrowserCheck';
import {createPortal} from 'react-dom';
import {useRootContainer} from '../../../hooks/useRootContainer';
import {TOOLTIP_MARGIN} from '../../TooltipWrapper';
import {Legend} from '../../Legend';

import style from './HiddenLegendTooltip.scss';

interface Props {
activeIndex: number;
Expand Down Expand Up @@ -87,10 +89,10 @@ export function HiddenLegendPopover({
top: getYPosition(),
left: getXPosition(),
});
}, [setPosition, setActivatorWidth]);
}, [setPosition]);

return (
<>
<Fragment>
<button
className={style.MoreText}
ref={activatorRef}
Expand Down Expand Up @@ -121,12 +123,12 @@ export function HiddenLegendPopover({
colorVisionType={colorVisionType}
data={data}
theme={theme}
lastVisibleIndex={lastVisibleIndex}
indexOffset={lastVisibleIndex}
backgroundColor="transparent"
/>
</div>,
container,
)}
</>
</Fragment>
);
}

0 comments on commit 7c677c0

Please sign in to comment.