Skip to content

Commit

Permalink
feat: Use Popover instead of Tooltip for line chart overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
svc-shorpo committed Jan 4, 2024
1 parent 619bd1a commit ac44684
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
44 changes: 30 additions & 14 deletions packages/app/src/HDXLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from 'next/link';
import cx from 'classnames';
import { add, format } from 'date-fns';
import pick from 'lodash/pick';
import { toast } from 'react-toastify';
import {
Bar,
BarChart,
Expand All @@ -17,7 +18,7 @@ import {
XAxis,
YAxis,
} from 'recharts';
import { Tooltip as MTooltip } from '@mantine/core';
import { Popover } from '@mantine/core';

import api from './api';
import { convertGranularityToSeconds, Granularity } from './ChartUtils';
Expand All @@ -30,6 +31,24 @@ import styles from '../styles/HDXLineChart.module.scss';

const MAX_LEGEND_ITEMS = 4;

function CopyableLegendItem({ entry }: any) {
return (
<span
className={styles.legendItem}
style={{ color: entry.color }}
role="button"
onClick={() => {
window.navigator.clipboard.writeText(entry.value);
toast.success(`Copied to clipboard`);
}}
title="Click to expand"
>
<i className="bi bi-circle-fill me-1" style={{ fontSize: 6 }} />
{entry.value}
</span>
);
}

function ExpandableLegendItem({ entry, expanded }: any) {
const [_expanded, setExpanded] = useState(false);
const isExpanded = _expanded || expanded;
Expand Down Expand Up @@ -68,27 +87,24 @@ export const LegendRenderer = memo<{
/>
))}
{restItems.length ? (
<MTooltip
color="gray"
withinPortal
withArrow
label={
<Popover withinPortal withArrow closeOnEscape closeOnClickOutside>
<Popover.Target>
<div className={cx(styles.legendItem, styles.legendMoreLink)}>
+{restItems.length} more
</div>
</Popover.Target>
<Popover.Dropdown p="xs">
<div className={styles.legendTooltipContent}>
{restItems.map((entry, index) => (
<ExpandableLegendItem
<CopyableLegendItem
key={`item-${index}`}
value={entry.value}
entry={entry}
expanded
/>
))}
</div>
}
>
<div className={cx(styles.legendItem, styles.legendMoreLink)}>
+{restItems.length} more
</div>
</MTooltip>
</Popover.Dropdown>
</Popover>
) : null}
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions packages/app/styles/HDXLineChart.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
word-break: break-all;
word-wrap: break-word;
line-height: 1.2;

&:active {
opacity: 0.5;
}
}

.legendMoreLink {
Expand Down

0 comments on commit ac44684

Please sign in to comment.