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
Improves type-checking with color function, removes unnecessary .includes() calls. Adds maybeColor for the few places where it'd be needed. (#67030)
Brad Anderson7640c6ff021b
diff --git a/frontend/src/metabase/common/components/Badge/Badge.styled.tsx b/frontend/src/metabase/common/components/Badge/Badge.styled.tsxindex 7214fb1a16d..59da532125b 100644--- a/frontend/src/metabase/common/components/Badge/Badge.styled.tsx+++ b/frontend/src/metabase/common/components/Badge/Badge.styled.tsx@@ -8,7 +8,7 @@ import type { LinkProps } from "metabase/common/components/Link";import Link from "metabase/common/components/Link";import { doNotForwardProps } from "metabase/common/utils/doNotForwardProps";import { Icon } from "metabase/ui";-import { color } from "metabase/ui/utils/colors";+import { maybeColor } from "metabase/ui/utils/colors";interface RawMaybeLinkProps {to?: string;@@ -29,7 +29,7 @@ export function RawMaybeLink({const hoverStyle = (props: RawMaybeLinkProps) => css`cursor: pointer;- ${props.activeColor ? `color: ${color(props.activeColor)};` : ""}+ ${props.activeColor ? `color: ${maybeColor(props.activeColor)};` : ""}`;export const MaybeLink = styled(RawMaybeLink)`@@ -38,7 +38,7 @@ export const MaybeLink = styled(RawMaybeLink)`font-size: 0.875em;font-weight: bold;${(props) =>- props.inactiveColor ? `color: ${color(props.inactiveColor)};` : ""}+ props.inactiveColor ? `color: ${maybeColor(props.inactiveColor)};` : ""}min-width: ${(props) => (props.isSingleLine ? 0 : "")};:hover {diff --git a/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx b/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsxindex b97355d55a5..c467e6e96d4 100644--- a/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx+++ b/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx@@ -5,7 +5,7 @@ import styled from "@emotion/styled";import type { ColorName } from "metabase/lib/colors/types";import { Icon, Text, type TextProps } from "metabase/ui";-import { color } from "metabase/ui/utils/colors";+import { maybeColor } from "metabase/ui/utils/colors";export const ItemTitle = styled(Text)<TextProps>`margin: 0;@@ -13,7 +13,7 @@ export const ItemTitle = styled(Text)<TextProps>`` as unknown as typeof Text;export const ItemIcon = styled(Icon)<{ color?: ColorName | string | null }>`- color: ${(props) => color(props.color ?? "text-light")};+ color: ${(props) => maybeColor(props.color ?? "text-light")};justify-self: end;`;diff --git a/frontend/src/metabase/list-view/components/ListView/styling.ts b/frontend/src/metabase/list-view/components/ListView/styling.tsindex 349f725c47f..2fba528155f 100644--- a/frontend/src/metabase/list-view/components/ListView/styling.ts+++ b/frontend/src/metabase/list-view/components/ListView/styling.ts@@ -1,5 +1,5 @@import type { IconName } from "metabase/ui";-import { color } from "metabase/ui/utils/colors";+import { color, maybeColor } from "metabase/ui/utils/colors";export const ENTITY_ICONS = {"entity/UserTable": "person",@@ -44,7 +44,7 @@ export function getIconBackground(iconColor?: string) {}return iconColor !== color("text-primary")- ? `color-mix(in srgb, ${color(iconColor)}, transparent 88%)`+ ? `color-mix(in srgb, ${maybeColor(iconColor)}, transparent 88%)`: "var(--mb-color-white)";}@@ -57,7 +57,7 @@ const CATEGORY_COLORS = ["accent5","accent6","accent7",-];+] as const;// Get a consistent color for a category value based on its hashexport const getCategoryColor = (categoryValue: any, columnName: string) => {diff --git a/frontend/src/metabase/ui/components/feedback/Alert/Alert.config.tsx b/frontend/src/metabase/ui/components/feedback/Alert/Alert.config.tsxindex 570e4529615..966899c1b18 100644--- a/frontend/src/metabase/ui/components/feedback/Alert/Alert.config.tsx+++ b/frontend/src/metabase/ui/components/feedback/Alert/Alert.config.tsx@@ -14,14 +14,12 @@ export const alertOverrides: MantineThemeOverride["components"] = {title: AlertStyles.title,},vars: (_theme, props) => {- if (- isColorName(props.color) &&- isColorName(`background-${props.color}`)- ) {+ const bgColor = `background-${props.color}`;+ if (isColorName(props.color) && isColorName(bgColor)) {return {root: {"--alert-color": color(props.color),- "--alert-bg": color(`background-${props.color}`),+ "--alert-bg": color(bgColor),},};} else {diff --git a/frontend/src/metabase/ui/utils/colors.ts b/frontend/src/metabase/ui/utils/colors.tsindex 8daa2bf2352..ad32d4d90fe 100644--- a/frontend/src/metabase/ui/utils/colors.ts+++ b/frontend/src/metabase/ui/utils/colors.ts@@ -60,13 +60,19 @@ export function getThemeColors(* @param colorName* @returns string referencing a css variable*/-export function color(colorName: ColorName | string): string {- if (isColorName(colorName)) {- return `var(--mb-color-${colorName})`;- }- return colorName;+export function color(colorName: ColorName): string {+ return `var(--mb-color-${colorName})`;}export const isColorName = (name?: string | null): name is ColorName => {return !!name && allColorNames.includes(name);};++/**+ * Prefer to use `color()` instead.+ * Only use `maybeColor()` if you can't be sure you're going to have a `ColorName` as input,+ * e.g. the value comes from an endpoint, upstream type-checking is too loose, etc.+ */+export const maybeColor = (maybeColorName: ColorName | string): string => {+ return isColorName(maybeColorName) ? color(maybeColorName) : maybeColorName;+};