Skip to content

Commit

Permalink
refactor: Use MaybeTooltip for conditional tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
bprusinowski committed Sep 29, 2022
1 parent a071f54 commit 08c307b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 79 deletions.
26 changes: 10 additions & 16 deletions app/charts/shared/axis-height-linear.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Tooltip } from "@mui/material";
import { axisLeft } from "d3";
import { select, Selection } from "d3";
import { useEffect, useRef } from "react";
Expand All @@ -10,6 +9,7 @@ import { ScatterplotState } from "@/charts/scatterplot/scatterplot-state";
import { useChartState } from "@/charts/shared/use-chart-state";
import { useChartTheme } from "@/charts/shared/use-chart-theme";
import { useFormatNumber } from "@/configurator/components/ui-helpers";
import { MaybeTooltip } from "@/utils/maybe-tooltip";

export const TICK_MIN_HEIGHT = 50;

Expand Down Expand Up @@ -58,23 +58,17 @@ export const AxisHeightLinear = () => {
return (
<>
<g>
{yAxisDescription ? (
<Tooltip arrow title={yAxisDescription}>
<text
x={0}
y={0}
dy={labelFontSize}
fontSize={labelFontSize}
textDecoration="underline"
>
{yAxisLabel}
</text>
</Tooltip>
) : (
<text x={0} y={0} dy={labelFontSize} fontSize={labelFontSize}>
<MaybeTooltip text={yAxisDescription}>
<text
x={0}
y={0}
dy={labelFontSize}
fontSize={labelFontSize}
textDecoration={yAxisDescription ? "underline" : undefined}
>
{yAxisLabel}
</text>
)}
</MaybeTooltip>
</g>
<g
ref={ref}
Expand Down
20 changes: 4 additions & 16 deletions app/charts/shared/axis-width-linear.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Tooltip } from "@mui/material";
import { axisBottom } from "d3";
import { select, Selection } from "d3";
import { useEffect, useRef } from "react";
Expand All @@ -9,6 +8,7 @@ import { useChartState } from "@/charts/shared/use-chart-state";
import { useChartTheme } from "@/charts/shared/use-chart-theme";
import { useFormatNumber } from "@/configurator/components/ui-helpers";
import { estimateTextWidth } from "@/utils/estimate-text-width";
import { MaybeTooltip } from "@/utils/maybe-tooltip";

export const AxisWidthLinear = () => {
const formatNumber = useFormatNumber();
Expand Down Expand Up @@ -62,30 +62,18 @@ export const AxisWidthLinear = () => {
return (
<>
<g transform={`translate(${margins.left}, ${margins.top})`}>
{xAxisDescription ? (
<Tooltip arrow title={xAxisDescription}>
<text
x={chartWidth}
y={chartHeight + margins.bottom}
dy={-labelFontSize}
fontSize={labelFontSize}
textAnchor="end"
textDecoration="underline"
>
{xAxisLabel}
</text>
</Tooltip>
) : (
<MaybeTooltip text={xAxisDescription}>
<text
x={chartWidth}
y={chartHeight + margins.bottom}
dy={-labelFontSize}
fontSize={labelFontSize}
textAnchor="end"
textDecoration={xAxisDescription ? "underline" : undefined}
>
{xAxisLabel}
</text>
)}
</MaybeTooltip>
</g>
<g
ref={xAxisRef}
Expand Down
24 changes: 14 additions & 10 deletions app/charts/table/table-content.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Box, TableSortLabel, Theme, Tooltip } from "@mui/material";
import { Box, TableSortLabel, Theme } from "@mui/material";
import { makeStyles } from "@mui/styles";
import clsx from "clsx";
import React, { useMemo, useContext } from "react";
import { HeaderGroup } from "react-table";

import { MaybeTooltip } from "@/utils/maybe-tooltip";

import Flex from "../../components/flex";
import { Observation } from "../../domain/data";

Expand Down Expand Up @@ -105,15 +107,17 @@ export const TableContent = ({ children }: { children: React.ReactNode }) => {
active={isCustomSorted}
direction={column.isSortedDesc ? "desc" : "asc"}
>
{description ? (
<Tooltip arrow title={description}>
<span style={{ textDecoration: "underline" }}>
{column.render("Header")}
</span>
</Tooltip>
) : (
column.render("Header")
)}
<MaybeTooltip text={description}>
<span
style={{
textDecoration: description
? "underline"
: undefined,
}}
>
{column.render("Header")}
</span>
</MaybeTooltip>
</TableSortLabel>
</Flex>
);
Expand Down
23 changes: 13 additions & 10 deletions app/components/chart-filters-list.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Box, Typography, Tooltip } from "@mui/material";
import { Box, Typography } from "@mui/material";
import { Fragment } from "react";

import { useQueryFilters } from "@/charts/shared/chart-helpers";
import { ChartConfig, DataSource } from "@/configurator";
import { useTimeFormatUnit } from "@/configurator/components/ui-helpers";
import { useDataCubeMetadataWithComponentValuesQuery } from "@/graphql/query-hooks";
import { useLocale } from "@/locales/use-locale";
import { MaybeTooltip } from "@/utils/maybe-tooltip";

export const ChartFiltersList = ({
dataSetIri,
Expand Down Expand Up @@ -74,15 +75,17 @@ export const ChartFiltersList = ({
{namedFilters.map(({ dimension, value }, i) => (
<Fragment key={dimension.iri}>
<Box component="span" fontWeight="bold">
{dimension.description ? (
<Tooltip arrow title={dimension.description}>
<span style={{ textDecoration: "underline" }}>
{dimension.label}
</span>
</Tooltip>
) : (
dimension.label
)}
<MaybeTooltip text={dimension.description || undefined}>
<span
style={{
textDecoration: dimension.description
? "underline"
: undefined,
}}
>
{dimension.label}
</span>
</MaybeTooltip>

{": "}
</Box>
Expand Down
46 changes: 19 additions & 27 deletions app/components/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
MenuItem,
TypographyProps,
Stack,
Tooltip,
} from "@mui/material";
import { useId } from "@reach/auto-id";
import { timeFormat } from "d3-time-format";
Expand All @@ -35,6 +34,7 @@ import {
import { useBrowseContext } from "@/configurator/components/dataset-browse";
import { Icon } from "@/icons";
import { useLocale } from "@/locales/use-locale";
import { MaybeTooltip } from "@/utils/maybe-tooltip";

export const Label = ({
htmlFor,
Expand All @@ -49,32 +49,24 @@ export const Label = ({
children: ReactNode;
sx?: TypographyProps["sx"];
}) => {
return tooltipText ? (
<Tooltip arrow title={tooltipText}>
<div style={{ width: "max-content", textDecoration: "underline" }}>
<Typography
component="label"
htmlFor={htmlFor}
variant={smaller ? "caption" : "body2"}
color="secondary"
display="flex"
sx={sx}
>
{children}
</Typography>
</div>
</Tooltip>
) : (
<Typography
component="label"
htmlFor={htmlFor}
variant={smaller ? "caption" : "body2"}
color="secondary"
display="flex"
sx={sx}
>
{children}
</Typography>
return (
<MaybeTooltip text={tooltipText}>
<Typography
component="label"
htmlFor={htmlFor}
variant={smaller ? "caption" : "body2"}
color="secondary"
display="flex"
sx={{
...sx,
...(tooltipText
? { width: "max-content", textDecoration: "underline" }
: {}),
}}
>
{children}
</Typography>
</MaybeTooltip>
);
};

Expand Down

0 comments on commit 08c307b

Please sign in to comment.