The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Finish the legend's Emotion to Mantine migration (#84460)
Andrei Chernykh0b7fe4f43caf
diff --git a/frontend/src/metabase/dashboard/components/DashCard/DashCardVisualization.tsx b/frontend/src/metabase/dashboard/components/DashCard/DashCardVisualization.tsxindex 8976ccacc78..fb7817cb13f 100644--- a/frontend/src/metabase/dashboard/components/DashCard/DashCardVisualization.tsx+++ b/frontend/src/metabase/dashboard/components/DashCard/DashCardVisualization.tsx@@ -40,7 +40,7 @@ import type { LoadingViewProps } from "metabase/visualizations/components/Visualimport {LEGEND_LABEL_FONT_SIZE,LEGEND_LABEL_FONT_WEIGHT,-} from "metabase/visualizations/components/legend/LegendCaption";+} from "metabase/visualizations/components/legend/constants";import type {CardSlownessStatus,ClickObject,diff --git a/frontend/src/metabase/visualizations/components/legend/Legend.module.css b/frontend/src/metabase/visualizations/components/legend/Legend.module.cssindex c23419a7deb..6d2ff601d61 100644--- a/frontend/src/metabase/visualizations/components/legend/Legend.module.css+++ b/frontend/src/metabase/visualizations/components/legend/Legend.module.css@@ -1,37 +1,3 @@-.horizontalRoot {- overflow: hidden;-}--.overflowLabel {- cursor: pointer;- white-space: nowrap;-- &:hover {- color: var(--mb-color-core-brand);- }-}-.popoverScroll {overflow-x: hidden;}--.itemLabel {- transition: opacity 0.25s linear;-}--.hoverableLabel:hover {- color: var(--mb-color-core-brand);-}--.itemTitle {- color: var(--mb-color-text-primary);- overflow: hidden;-}--.clickableTitle {- cursor: pointer;-- &:hover {- color: var(--mb-color-core-brand);- }-}diff --git a/frontend/src/metabase/visualizations/components/legend/Legend.tsx b/frontend/src/metabase/visualizations/components/legend/Legend.tsxindex 29eeb686e88..8677de1d2b0 100644--- a/frontend/src/metabase/visualizations/components/legend/Legend.tsx+++ b/frontend/src/metabase/visualizations/components/legend/Legend.tsx@@ -1,7 +1,7 @@import cx from "classnames";import { t } from "ttag";-import _ from "underscore";+import CS from "metabase/css/core/index.css";import { Box, Flex, Popover, Text, rem } from "metabase/ui";import type { HoveredObject } from "metabase/viz-core";@@ -50,7 +50,7 @@ export const Legend = ({onToggleSeriesVisibility,isReversed,}: LegendProps) => {- const items = isReversed ? _.clone(originalItems).reverse() : originalItems;+ const items = isReversed ? [...originalItems].reverse() : originalItems;const overflowIndex = visibleIndex + visibleLength;const visibleItems = items.slice(visibleIndex, overflowIndex);@@ -59,7 +59,7 @@ export const Legend = ({return (<Flex- className={cx({ [S.horizontalRoot]: !isVertical }, className)}+ className={cx({ [CS.overflowHidden]: !isVertical }, className)}aria-label={t`Legend`}direction={isVertical ? "column" : "row"}align={isVertical ? "stretch" : "center"}@@ -97,8 +97,12 @@ export const Legend = ({<Popover.Target><Textcomponent="div"- className={S.overflowLabel}- c="text-secondary"+ className={cx(+ CS.textSecondary,+ CS.textBrandHover,+ CS.cursorPointer,+ CS.textNoWrap,+ )}fz={typography}lh={typography}>diff --git a/frontend/src/metabase/visualizations/components/legend/LegendActions.styled.tsx b/frontend/src/metabase/visualizations/components/legend/LegendActions.styled.tsxdeleted file mode 100644index a10725b4403..00000000000--- a/frontend/src/metabase/visualizations/components/legend/LegendActions.styled.tsx+++ /dev/null@@ -1,9 +0,0 @@-// eslint-disable-next-line eslint-js/no-restricted-imports-import styled from "@emotion/styled";--export const LegendActionsRoot = styled.div`- flex: 0 0 auto;- position: relative;- align-items: center;- margin-left: auto;-`;diff --git a/frontend/src/metabase/visualizations/components/legend/LegendActions.tsx b/frontend/src/metabase/visualizations/components/legend/LegendActions.tsxindex eb2ad18e981..3fbde8e67c3 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendActions.tsx+++ b/frontend/src/metabase/visualizations/components/legend/LegendActions.tsx@@ -1,11 +1,15 @@import type { ReactNode } from "react";-import { LegendActionsRoot } from "metabase/visualizations/components/legend/LegendActions.styled";+import { Box } from "metabase/ui";interface LegendActionsProps {children?: ReactNode;}export const LegendActions = ({ children }: LegendActionsProps) => {- return <LegendActionsRoot>{children}</LegendActionsRoot>;+ return (+ <Box pos="relative" flex="0 0 auto" ml="auto">+ {children}+ </Box>+ );};diff --git a/frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.tsx b/frontend/src/metabase/visualizations/components/legend/LegendCaption.tsxsimilarity index 78%rename from frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.tsxrename to frontend/src/metabase/visualizations/components/legend/LegendCaption.tsxindex 9639e3e8f9e..8f0ac1cd799 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.tsx+++ b/frontend/src/metabase/visualizations/components/legend/LegendCaption.tsx@@ -7,18 +7,12 @@ import { useTranslateContent } from "metabase/content-translation/hooks";import CS from "metabase/css/core/index.css";import DashboardS from "metabase/css/dashboard.module.css";import type { IconProps } from "metabase/ui";-import { Ellipsified, Icon, Menu, Tooltip } from "metabase/ui";+import { Box, Ellipsified, Flex, Icon, Menu, Tooltip } from "metabase/ui";import { SAVING_DOM_IMAGE_OVERFLOW_VISIBLE_CLASS } from "metabase/viz-core";-import { LegendActions } from "../LegendActions";--import {- LegendCaptionRoot,- LegendDescriptionIcon,- LegendLabel,- LegendLabelIcon,- LegendRightContent,-} from "./LegendCaption.styled";+import { LegendActions } from "./LegendActions";+import { LegendLabel } from "./LegendLabel";+import { LEGEND_LABEL_FONT_SIZE } from "./constants";function shouldHideDescription(width: number | undefined) {const HIDE_DESCRIPTION_THRESHOLD = 100;@@ -87,6 +81,9 @@ export const LegendCaption = ({const titleElement = (<LegendLabelclassName={cx(+ CS.flex,+ CS.alignCenter,+ CS.overflowHidden,DashboardS.fullscreenNormalText,// html2canvas doesn't support `text-overflow: ellipsis` (#45499) https://github.com/niklasvh/html2canvas/issues/324@@ -100,23 +97,35 @@ export const LegendCaption = ({<Ellipsifieddata-testid="legend-caption-title"className={SAVING_DOM_IMAGE_OVERFLOW_VISIBLE_CLASS}+ fz={LEGEND_LABEL_FONT_SIZE}>{tc(title)}</Ellipsified>{title && hasTitleMenuItems && (<Icon- style={{ flexShrink: 0, marginRight: 10 }}+ className={cx(CS.ml1, CS.flexNoShrink)}name="chevrondown"size={10}- className={CS.ml1}+ mr={10}/>)}</LegendLabel>);return (- <LegendCaptionRoot className={className} data-testid="legend-caption">- {icon && <LegendLabelIcon {...icon} />}+ <Flex+ className={className}+ align="center"+ miw={0}+ data-testid="legend-caption"+ >+ {icon && (+ <Icon+ {...icon}+ className={cx(CS.flexNoShrink, icon.className)}+ mr="xxs"+ />+ )}{hasTitleMenuItems ? (<Menu><Menu.Target>{titleElement}</Menu.Target>@@ -137,17 +146,23 @@ export const LegendCaption = ({}maw="22em">- <LegendDescriptionIcon- name="info"- className={cx(CS.hoverChild, CS.hoverChildSmooth)}+ <Box+ component="span"+ className={cx(+ CS.textLight,+ CS.textMediumHover,+ CS.hoverChild,+ CS.hoverChildSmooth,+ )}mt="3px"mr="lg"- />+ ml="xxs"+ >+ <Icon name="info" />+ </Box></Tooltip>)}- <LegendRightContent>- {actionButtons && <LegendActions>{actionButtons}</LegendActions>}- </LegendRightContent>- </LegendCaptionRoot>+ {actionButtons && <LegendActions>{actionButtons}</LegendActions>}+ </Flex>);};diff --git a/frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.styled.tsx b/frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.styled.tsxdeleted file mode 100644index 979eff65365..00000000000--- a/frontend/src/metabase/visualizations/components/legend/LegendCaption/LegendCaption.styled.tsx+++ /dev/null@@ -1,59 +0,0 @@-// eslint-disable-next-line eslint-js/no-restricted-imports-import styled from "@emotion/styled";-import { forwardRef } from "react";--import { Box, type BoxProps, Icon, type IconProps } from "metabase/ui";--import { LegendLabel as BaseLegendLabel } from "../LegendLabel";--export const LegendCaptionRoot = styled.div`- display: flex;- align-items: center;- min-width: 0;-`;--export const LEGEND_LABEL_FONT_SIZE = "0.875rem";-export const LEGEND_LABEL_FONT_WEIGHT = 700;--export const LegendLabel = styled(BaseLegendLabel)`- overflow: hidden;- margin-top: 2px;- padding: 0.25rem 0 0;- display: flex;- flex-direction: row;- align-items: center;- font-size: ${LEGEND_LABEL_FONT_SIZE};- font-weight: ${LEGEND_LABEL_FONT_WEIGHT};-`;--export const LegendLabelIcon = styled(Icon)`- flex-shrink: 0;- margin-right: 0.25rem;-`;--export const LegendDescriptionIcon = styled(- forwardRef<- HTMLDivElement,- BoxProps & { name: IconProps["name"]; "data-testid"?: string }- >(function LegendDescriptionIcon({ name = "info", ...props }, ref) {- return (- <Box component="span" ref={ref} {...props}>- <Icon name={name} />- </Box>- );- }),-)`- color: var(--mb-color-text-disabled);- margin: 0 0.25rem;-- &:hover {- color: var(--mb-color-text-secondary);- }-`;--export const LegendRightContent = styled.div`- display: flex;- justify-content: flex-end;- margin-left: auto;- align-items: center;-`;diff --git a/frontend/src/metabase/visualizations/components/legend/LegendCaption/index.ts b/frontend/src/metabase/visualizations/components/legend/LegendCaption/index.tsdeleted file mode 100644index c0ddea9da71..00000000000--- a/frontend/src/metabase/visualizations/components/legend/LegendCaption/index.ts+++ /dev/null@@ -1,5 +0,0 @@-export { LegendCaption } from "./LegendCaption";-export {- LEGEND_LABEL_FONT_SIZE,- LEGEND_LABEL_FONT_WEIGHT,-} from "./LegendCaption.styled";diff --git a/frontend/src/metabase/visualizations/components/legend/LegendItem.module.css b/frontend/src/metabase/visualizations/components/legend/LegendItem.module.cssnew file mode 100644index 00000000000..8b6e9fe0e7d--- /dev/null+++ b/frontend/src/metabase/visualizations/components/legend/LegendItem.module.css@@ -0,0 +1,3 @@+.root {+ transition: opacity 0.25s linear;+}diff --git a/frontend/src/metabase/visualizations/components/legend/LegendItem.tsx b/frontend/src/metabase/visualizations/components/legend/LegendItem.tsxindex dd4b4b3c59c..66541ccb1ee 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendItem.tsx+++ b/frontend/src/metabase/visualizations/components/legend/LegendItem.tsx@@ -1,10 +1,11 @@import cx from "classnames";import { memo } from "react";+import CS from "metabase/css/core/index.css";import DashboardS from "metabase/css/dashboard.module.css";import { Box, Ellipsified, Flex, rem } from "metabase/ui";-import S from "./Legend.module.css";+import S from "./LegendItem.module.css";import { LegendItemDot } from "./LegendItemDot";import { LEGEND_SIZES, type LegendSize } from "./layout";@@ -41,6 +42,7 @@ const LegendItemInner = ({onToggleSeriesVisibility,}: LegendItemProps) => {const { dotSize, dotGap, typography } = LEGEND_SIZES[size];+ const isClickable = onSelectSeries != null;const handleDotClick = (event: React.MouseEvent) => {onToggleSeriesVisibility?.(event, index);@@ -59,36 +61,36 @@ const LegendItemInner = ({};return (- <Flex align="center" miw={0} data-testid="legend-item">- <Flex- className={cx(S.itemLabel, {- [S.hoverableLabel]: onHoverChange != null,- })}- align="center"- w="100%"- opacity={isMuted ? 0.4 : 1}- onMouseEnter={onHoverChange && handleItemMouseEnter}- onMouseLeave={onHoverChange && handleItemMouseLeave}+ <Flex+ className={S.root}+ align="center"+ miw={0}+ opacity={isMuted ? 0.4 : 1}+ onMouseEnter={onHoverChange && handleItemMouseEnter}+ onMouseLeave={onHoverChange && handleItemMouseLeave}+ data-testid="legend-item"+ >+ <LegendItemDot+ color={item.color}+ size={rem(dotSize)}+ isVisible={item.visible ?? true}+ onClick={onToggleSeriesVisibility && handleDotClick}+ />+ <Box+ className={cx(+ DashboardS.fullscreenNormalText,+ CS.textPrimary,+ CS.overflowHidden,+ { [CS.cursorPointer]: isClickable, [CS.textBrandHover]: isClickable },+ )}+ fz={typography}+ lh={typography}+ ml={dotGap}+ miw={0}+ onClick={onSelectSeries && handleItemClick}>- <LegendItemDot- color={item.color}- size={rem(dotSize)}- isVisible={item.visible ?? true}- onClick={onToggleSeriesVisibility && handleDotClick}- />- <Box- className={cx(DashboardS.fullscreenNormalText, S.itemTitle, {- [S.clickableTitle]: onSelectSeries != null,- })}- fz={typography}- lh={typography}- ml={dotGap}- miw={0}- onClick={onSelectSeries && handleItemClick}- >- <Ellipsified>{item.name}</Ellipsified>- </Box>- </Flex>+ <Ellipsified>{item.name}</Ellipsified>+ </Box></Flex>);};diff --git a/frontend/src/metabase/visualizations/components/legend/LegendLabel.module.css b/frontend/src/metabase/visualizations/components/legend/LegendLabel.module.cssindex 7ba60307a20..287c02590ab 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendLabel.module.css+++ b/frontend/src/metabase/visualizations/components/legend/LegendLabel.module.css@@ -1,24 +1,3 @@-.text {- color: var(--mb-color-text-primary);- font-weight: bold;-- && {- padding-top: 0;- margin-top: 0;- }-}-.link {line-height: 1.5rem;- cursor: pointer;-- /* Increases specificity to override default link styles */- && {- padding-top: 0;- margin-top: 0;- }-- &:hover {- color: var(--mb-color-core-brand);- }}diff --git a/frontend/src/metabase/visualizations/components/legend/LegendLabel.tsx b/frontend/src/metabase/visualizations/components/legend/LegendLabel.tsxindex d971874c1a9..ce4ef1c5337 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendLabel.tsx+++ b/frontend/src/metabase/visualizations/components/legend/LegendLabel.tsx@@ -12,6 +12,7 @@ import {import type { LinkProps } from "metabase/common/components/Link";import { Link } from "metabase/common/components/Link";+import CS from "metabase/css/core/index.css";import { isEmbeddingSdk } from "metabase/embedding-sdk/config";import { Box } from "metabase/ui";@@ -53,20 +54,24 @@ export const LegendLabel = forwardRef([onClick],);+ const labelClassName = cx(CS.textPrimary, CS.textBold, className, {+ [S.link]: onClick,+ [CS.cursorPointer]: onClick,+ [CS.textBrandHover]: onClick,+ });+if (!href || !onClick) {return (<divref={ref}- className={cx(S.text, className, {- [S.link]: onClick,- })}+ className={labelClassName}onClick={onClick}onFocus={onFocus}onMouseEnter={onMouseEnter}onMouseDown={onMouseDown}onTouchStart={onTouchStart}data-testid="legend-label"- data-is-clickable={!!onClick}+ data-is-clickable={Boolean(onClick)}>{children}</div>@@ -79,14 +84,14 @@ export const LegendLabel = forwardRef(return (<Boxref={ref}- className={cx(S.text, S.link, className)}+ className={labelClassName}onClick={handleLinkClick}onFocus={onFocus}onMouseEnter={onMouseEnter}onMouseDown={onMouseDown}onTouchStart={onTouchStart}data-testid="legend-label"- data-is-clickable={!!onClick}+ data-is-clickable={Boolean(onClick)}>{children}</Box>@@ -96,7 +101,7 @@ export const LegendLabel = forwardRef(return (<Linkref={ref}- className={cx(S.text, S.link, className)}+ className={labelClassName}to={href}onClick={handleLinkClick}onFocus={onFocus}@@ -104,7 +109,7 @@ export const LegendLabel = forwardRef(onMouseDown={onMouseDown}onTouchStart={onTouchStart}data-testid="legend-label"- data-is-clickable={!!onClick}+ data-is-clickable={Boolean(onClick)}>{children}</Link>diff --git a/frontend/src/metabase/visualizations/components/legend/LegendLayout.module.css b/frontend/src/metabase/visualizations/components/legend/LegendLayout.module.cssindex e7cf17c56f0..1d2359fd7af 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendLayout.module.css+++ b/frontend/src/metabase/visualizations/components/legend/LegendLayout.module.css@@ -1,27 +1,4 @@-.root {- display: flex;- flex: 1 1 auto;- min-width: 0;- min-height: 0;-}--.main {- display: flex;- flex: 1 1 auto;- flex-direction: column;- min-width: 0;- min-height: 0;-}--.chart {- display: flex;- flex: 1 0 auto;- flex-direction: column;- min-height: 0;-}-.verticalLegend {/* the column stretches to the chart height but must never grow it */contain: size;- overflow: hidden;}diff --git a/frontend/src/metabase/visualizations/components/legend/LegendLayout.tsx b/frontend/src/metabase/visualizations/components/legend/LegendLayout.tsxindex 5ca46fbe8e8..cd043e41b87 100644--- a/frontend/src/metabase/visualizations/components/legend/LegendLayout.tsx+++ b/frontend/src/metabase/visualizations/components/legend/LegendLayout.tsx@@ -1,6 +1,7 @@import cx from "classnames";import { type ReactNode, useMemo } from "react";+import CS from "metabase/css/core/index.css";import { Box, Flex } from "metabase/ui";import type { TextWidthMeasurer } from "metabase/utils/measure-text";import type { HoveredObject } from "metabase/viz-core";@@ -101,10 +102,14 @@ export const LegendLayout = ({};return (- <div className={cx(S.root, className)}>- <div className={S.main}>+ <Flex className={className} flex="1 1 auto" miw={0} mih={0}>+ <Flex direction="column" flex="1 1 auto" miw={0} mih={0}>{actionButtons && <LegendActions>{actionButtons}</LegendActions>}- {hasDimensions && <div className={S.chart}>{children}</div>}+ {hasDimensions && (+ <Flex direction="column" flex="1 0 auto" mih={0}>+ {children}+ </Flex>+ )}{isVisible && layout.type === "horizontal" && (<Flexjustify="center"@@ -118,10 +123,10 @@ export const LegendLayout = ({<Legend {...legendProps} /></Flex>)}- </div>+ </Flex>{isVisible && layout.type === "vertical" && (<Box- className={S.verticalLegend}+ className={cx(S.verticalLegend, CS.overflowHidden)}flex="0 0 auto"miw={0}ml={verticalGap}@@ -135,6 +140,6 @@ export const LegendLayout = ({/></Box>)}- </div>+ </Flex>);};diff --git a/frontend/src/metabase/visualizations/components/legend/constants.ts b/frontend/src/metabase/visualizations/components/legend/constants.tsnew file mode 100644index 00000000000..af3070c9bdb--- /dev/null+++ b/frontend/src/metabase/visualizations/components/legend/constants.ts@@ -0,0 +1,3 @@+// DashCardVisualization measures the caption title with these, so they must match how LegendCaption renders it+export const LEGEND_LABEL_FONT_SIZE = "0.875rem";+export const LEGEND_LABEL_FONT_WEIGHT = 700;diff --git a/frontend/src/metabase/visualizations/components/legend/layout.ts b/frontend/src/metabase/visualizations/components/legend/layout.tsindex c84856a4cab..586e6200bc4 100644--- a/frontend/src/metabase/visualizations/components/legend/layout.ts+++ b/frontend/src/metabase/visualizations/components/legend/layout.ts@@ -115,8 +115,6 @@ interface GetLegendLayoutOptions {alwaysVisible?: boolean;}-const sum = (values: number[]) => values.reduce((total, v) => total + v, 0);-export const getOverflowLabel = (count: number) =>c("{0} is the number of hidden legend items").t`+ ${count} more`;@@ -153,7 +151,9 @@ export function getLegendLayout({availableWidth,horizontalWidth - LEGEND_PADDING * 2,);- const itemsWidth = sum(itemWidths) + config.itemGap * (items.length - 1);+ const itemsWidth =+ itemWidths.reduce((total, width) => total + width, 0) ++ config.itemGap * (items.length - 1);if (itemsWidth <= availableHorizontalWidth) {return { type: "horizontal" };}