|
1 | | -import { Box } from "@mui/material"; |
| 1 | +import { Box, Stack, Tooltip } from "@mui/material"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { useTranslation } from "react-i18next"; |
2 | 4 | import { chipColors } from "../SidePanel/enrichmentTerm/enrichmentTermAnnotationBlocks"; |
3 | 5 | import { |
4 | 6 | useFormatHighlightValue, |
5 | 7 | type useFormatHighlightValueParams, |
6 | 8 | } from "./enrichment/useFormatHighlightValue"; |
7 | 9 | import { Value } from "./Value"; |
8 | 10 |
|
| 11 | +function HightlightCaption({ groups }: { groups: string[] }) { |
| 12 | + const { t } = useTranslation(); |
| 13 | + return ( |
| 14 | + <Stack direction="column" spacing={1}> |
| 15 | + {groups.map((group) => ( |
| 16 | + <Stack key={group} direction="row" spacing={1} alignItems="center"> |
| 17 | + <Box |
| 18 | + sx={{ |
| 19 | + width: 24, |
| 20 | + height: 16, |
| 21 | + backgroundColor: chipColors[group as keyof typeof chipColors], |
| 22 | + border: (theme) => `1px solid ${theme.palette.divider}`, |
| 23 | + }} |
| 24 | + /> |
| 25 | + <Box> |
| 26 | + {t(`termEnrichment.${group}`, { |
| 27 | + count: 0, |
| 28 | + })} |
| 29 | + </Box> |
| 30 | + </Stack> |
| 31 | + ))} |
| 32 | + </Stack> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
9 | 36 | export const Highlight = ({ data }: HighlightProps) => { |
10 | 37 | const { groups, displayedGroups, value } = useFormatHighlightValue({ data }); |
| 38 | + const [tooltipOpen, setTooltipOpen] = useState(false); |
11 | 39 |
|
12 | 40 | if (displayedGroups.length === 0) { |
13 | 41 | return <Value data={value} />; |
14 | 42 | } |
15 | 43 |
|
16 | 44 | return ( |
17 | | - <Box |
18 | | - component="mark" |
19 | | - data-term={data?.attributes?.term} |
20 | | - data-group={groups.join(" ")} |
21 | | - data-underlined-group={displayedGroups.join(" ")} |
22 | | - role="mark" |
23 | | - sx={{ |
24 | | - color: "inherit", |
25 | | - backgroundColor: "transparent", |
26 | | - }} |
27 | | - style={{ |
28 | | - boxShadow: displayedGroups |
29 | | - .map((g, index) => |
30 | | - index === 0 |
31 | | - ? `inset 0 -3px 0 ${chipColors[g as keyof typeof chipColors]}` |
32 | | - : `0 ${index * 3}px 0 ${chipColors[g as keyof typeof chipColors]}`, |
33 | | - ) |
34 | | - .join(", "), |
| 45 | + <Tooltip |
| 46 | + title={<HightlightCaption groups={displayedGroups} />} |
| 47 | + placement="top" |
| 48 | + slotProps={{ |
| 49 | + tooltip: { |
| 50 | + sx: { |
| 51 | + backgroundColor: "white", |
| 52 | + color: "text.primary", |
| 53 | + padding: 2, |
| 54 | + border: (theme) => `1px solid ${theme.palette.divider}`, |
| 55 | + }, |
| 56 | + }, |
35 | 57 | }} |
| 58 | + open={tooltipOpen} |
36 | 59 | > |
37 | | - <Value data={value} /> |
38 | | - </Box> |
| 60 | + <Box |
| 61 | + component="mark" |
| 62 | + data-term={data?.attributes?.term} |
| 63 | + data-group={groups.join(" ")} |
| 64 | + data-underlined-group={displayedGroups.join(" ")} |
| 65 | + role="mark" |
| 66 | + sx={{ |
| 67 | + color: "inherit", |
| 68 | + backgroundColor: "transparent", |
| 69 | + }} |
| 70 | + style={{ |
| 71 | + boxShadow: displayedGroups |
| 72 | + .map((g, index) => |
| 73 | + index === 0 |
| 74 | + ? `inset 0 -3px 0 ${chipColors[g as keyof typeof chipColors]}` |
| 75 | + : `0 ${index * 3}px 0 ${chipColors[g as keyof typeof chipColors]}`, |
| 76 | + ) |
| 77 | + .join(", "), |
| 78 | + }} |
| 79 | + onMouseOver={(e) => { |
| 80 | + e.stopPropagation(); |
| 81 | + const target = e.target; |
| 82 | + if (!(target instanceof Element)) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // enters delay is not support in controlled state tooltips, so we implement it manually |
| 87 | + setTimeout(() => { |
| 88 | + if (!target.matches(":hover")) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + setTooltipOpen(true); |
| 93 | + }, 500); |
| 94 | + }} |
| 95 | + onMouseOut={() => { |
| 96 | + setTooltipOpen(false); |
| 97 | + }} |
| 98 | + > |
| 99 | + <Value data={value} /> |
| 100 | + </Box> |
| 101 | + </Tooltip> |
39 | 102 | ); |
40 | 103 | }; |
41 | 104 |
|
|
0 commit comments