Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Migrate Link, ListItem, and LoadingSpinner from Emotion to Mantine + CSS modules (#77323)

Andrei Chernykhb8673198cd4c
diff --git a/frontend/src/metabase/common/components/Link/Link.module.css b/frontend/src/metabase/common/components/Link/Link.module.css
new file mode 100644
index 00000000000..67b3e223795
--- /dev/null
+++ b/frontend/src/metabase/common/components/Link/Link.module.css
@@ -0,0 +1,33 @@
+.link {
+ transition: opacity 0.3s linear;
+
+ &:focus {
+ outline: 2px solid var(--mb-color-focus);
+ }
+
+ &:focus:not(:focus-visible) {
+ outline: none;
+ }
+}
+
+.disabled {
+ opacity: 0.4;
+ pointer-events: none;
+}
+
+.brand {
+ color: var(--mb-color-core-brand);
+
+ &:hover {
+ text-decoration: underline;
+ }
+}
+
+.brandBold {
+ color: var(--mb-color-core-brand);
+ font-weight: bold;
+
+ &:hover {
+ text-decoration: underline;
+ }
+}
diff --git a/frontend/src/metabase/common/components/Link/Link.styled.tsx b/frontend/src/metabase/common/components/Link/Link.styled.tsx
deleted file mode 100644
index 0cba2641515..00000000000
--- a/frontend/src/metabase/common/components/Link/Link.styled.tsx
+++ /dev/null
@@ -1,45 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import { css } from "@emotion/react";
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { focusOutlineStyle } from "metabase/common/style/input";
-import { doNotForwardProps } from "metabase/common/utils/doNotForwardProps";
-import {
- RouterLink as Link,
- type RouterLinkProps as LinkProps,
-} from "metabase/router/react-router";
-
-type LinkVariantProp = { variant?: "default" | "brand" | "brandBold" };
-
-export const LinkRoot = styled(
- Link,
- doNotForwardProps("variant"),
-)<LinkVariantProp>`
- opacity: ${(props) => (props.disabled ? "0.4" : "")};
- pointer-events: ${(props) => (props.disabled ? "none" : "")};
- transition: opacity 0.3s linear;
-
- ${focusOutlineStyle("core-brand")};
-
- ${(props) => variants[props.variant ?? "default"] ?? ""}
-` as unknown as React.FC<LinkProps & LinkVariantProp>;
-
-const variants = {
- default: "",
- brand: css`
- color: var(--mb-color-core-brand);
-
- &:hover {
- text-decoration: underline;
- }
- `,
- brandBold: css`
- color: var(--mb-color-core-brand);
- font-weight: bold;
-
- &:hover {
- text-decoration: underline;
- }
- `,
-};
diff --git a/frontend/src/metabase/common/components/Link/Link.tsx b/frontend/src/metabase/common/components/Link/Link.tsx
index 93aee4dd235..fa3a5e2fed4 100644
--- a/frontend/src/metabase/common/components/Link/Link.tsx
+++ b/frontend/src/metabase/common/components/Link/Link.tsx
@@ -1,27 +1,39 @@
+import cx from "classnames";
+
+import { RouterLink } from "metabase/router/react-router";
import { Tooltip } from "metabase/ui";
-import { LinkRoot } from "./Link.styled";
+import S from "./Link.module.css";
import type { LinkProps } from "./types";
-const LinkInner = ({
+export const Link = ({
to,
children,
disabled,
tooltip,
variant,
+ className,
...props
}: LinkProps): JSX.Element => {
const link = (
- <LinkRoot
+ <RouterLink
{...props}
+ className={cx(
+ S.link,
+ {
+ [S.disabled]: disabled,
+ [S.brand]: variant === "brand",
+ [S.brandBold]: variant === "brandBold",
+ },
+ className,
+ )}
to={to}
disabled={disabled}
tabIndex={disabled ? -1 : undefined}
aria-disabled={disabled}
- variant={variant}
>
{children}
- </LinkRoot>
+ </RouterLink>
);
const tooltipProps =
@@ -39,7 +51,3 @@ const LinkInner = ({
link
);
};
-
-export const Link = Object.assign(LinkInner, {
- Root: LinkRoot,
-});
diff --git a/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.module.css b/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.module.css
new file mode 100644
index 00000000000..16d1706c8ae
--- /dev/null
+++ b/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.module.css
@@ -0,0 +1,31 @@
+@keyframes spinner {
+ from {
+ transform: rotate(0deg);
+ }
+
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+.spinnerIcon {
+ box-sizing: border-box;
+ border: var(--border-width) solid transparent;
+ border-top-color: currentColor;
+ border-radius: 50%;
+ animation: spinner 1.3s infinite cubic-bezier(0.785, 0.135, 0.15, 0.86);
+
+ &::after {
+ content: "";
+ display: inherit;
+ box-sizing: inherit;
+ width: inherit;
+ height: inherit;
+ border: var(--border-width) solid currentColor;
+ border-radius: 50%;
+ opacity: 0.25;
+ position: relative;
+ top: calc(-1 * var(--border-width));
+ left: calc(-1 * var(--border-width));
+ }
+}
diff --git a/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.styled.tsx b/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.styled.tsx
deleted file mode 100644
index 42cea41dbb4..00000000000
--- a/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.styled.tsx
+++ /dev/null
@@ -1,50 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import { keyframes } from "@emotion/react";
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-const spinnerAnimation = keyframes`
- from {
- transform: rotate(0deg);
- }
- to {
- transform: rotate(360deg);
- }
-`;
-
-export const SpinnerRoot = styled.div`
- display: flex;
- align-items: center;
- font-size: 0;
-`;
-
-interface SpinnerIconProps {
- iconSize: number;
- borderWidth: number;
-}
-
-export const SpinnerIcon = styled.div<SpinnerIconProps>`
- display: inline-block;
- box-sizing: border-box;
- width: ${(props) => `${props.iconSize}px`};
- height: ${(props) => `${props.iconSize}px`};
- border: ${(props) => `${props.borderWidth}px`} solid transparent;
- border-top-color: currentColor;
- border-radius: ${(props) => `${props.iconSize / 2}px`};
- animation: ${spinnerAnimation} 1.3s infinite
- cubic-bezier(0.785, 0.135, 0.15, 0.86);
-
- &::after {
- content: "";
- display: inherit;
- box-sizing: inherit;
- width: inherit;
- height: inherit;
- border: ${(props) => `${props.borderWidth}px`} solid currentColor;
- border-radius: ${(props) => `${props.iconSize / 2}px`};
- opacity: 0.25;
- position: relative;
- top: ${(props) => `-${props.borderWidth}px`};
- left: ${(props) => `-${props.borderWidth}px`};
- }
-`;
diff --git a/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.tsx b/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.tsx
index d38ab615716..d9824c1efe4 100644
--- a/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.tsx
+++ b/frontend/src/metabase/common/components/LoadingSpinner/LoadingSpinner.tsx
@@ -1,7 +1,7 @@
-import { Icon } from "metabase/ui";
+import { Box, Flex, Icon } from "metabase/ui";
import { isReducedMotionPreferred } from "metabase/utils/dom";
-import { SpinnerIcon, SpinnerRoot } from "./LoadingSpinner.styled";
+import S from "./LoadingSpinner.module.css";
interface Props {
className?: string;
@@ -10,27 +10,31 @@ interface Props {
"data-testid"?: string;
}
-const BaseLoadingSpinner = ({
+/**
+ * @deprecated: use Loader from "metabase/ui"
+ */
+export const LoadingSpinner = ({
className,
size = 32,
borderWidth = 4,
"data-testid": dataTestId,
}: Props) => (
- <SpinnerRoot
+ <Flex
+ align="center"
+ fz={0}
className={className}
data-testid={dataTestId ?? "loading-indicator"}
>
{isReducedMotionPreferred() ? (
<Icon name="hourglass" size="24" />
) : (
- <SpinnerIcon iconSize={size} borderWidth={borderWidth} />
+ <Box
+ display="inline-block"
+ w={`${size}px`}
+ h={`${size}px`}
+ className={S.spinnerIcon}
+ style={{ "--border-width": `${borderWidth}px` }}
+ />
)}
- </SpinnerRoot>
+ </Flex>
);
-
-/**
- * @deprecated: use Loader from "metabase/ui"
- */
-export const LoadingSpinner = Object.assign(BaseLoadingSpinner, {
- Root: SpinnerRoot,
-});
diff --git a/frontend/src/metabase/reference/components/ListItem/ListItem.module.css b/frontend/src/metabase/reference/components/ListItem/ListItem.module.css
new file mode 100644
index 00000000000..0627b875180
--- /dev/null
+++ b/frontend/src/metabase/reference/components/ListItem/ListItem.module.css
@@ -0,0 +1,10 @@
+.link {
+ &:hover {
+ color: var(--mb-color-core-brand);
+ }
+}
+
+.disabled {
+ pointer-events: none;
+ opacity: 0.4;
+}
diff --git a/frontend/src/metabase/reference/components/ListItem/ListItem.styled.tsx b/frontend/src/metabase/reference/components/ListItem/ListItem.styled.tsx
deleted file mode 100644
index bc2a31496e0..00000000000
--- a/frontend/src/metabase/reference/components/ListItem/ListItem.styled.tsx
+++ /dev/null
@@ -1,26 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import { css } from "@emotion/react";
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { Link } from "metabase/router";
-interface Props {
- disabled?: boolean;
-}
-
-export const Root = styled.li<Props>`
- position: relative;
-
- ${(props) =>
- props.disabled &&
- css`
- pointer-events: none;
- opacity: 0.4;
- `};
-`;
-
-export const ListItemLink = styled(Link)`
- &:hover {
- color: var(--mb-color-core-brand);
- }
-`;
diff --git a/frontend/src/metabase/reference/components/ListItem/ListItem.tsx b/frontend/src/metabase/reference/components/ListItem/ListItem.tsx
index 847a0be2bc6..72cfb29ee51 100644
--- a/frontend/src/metabase/reference/components/ListItem/ListItem.tsx
+++ b/frontend/src/metabase/reference/components/ListItem/ListItem.tsx
@@ -2,11 +2,12 @@ import cx from "classnames";
import { memo } from "react";
import CS from "metabase/css/core/index.css";
-import S from "metabase/reference/components/List/List.module.css";
+import ListS from "metabase/reference/components/List/List.module.css";
+import { Link } from "metabase/router";
import { Box, Card, Ellipsified, Icon } from "metabase/ui";
import type { IconName } from "metabase-types/api";
-import { ListItemLink, Root } from "./ListItem.styled";
+import S from "./ListItem.module.css";
interface ListItemBaseProps {
"data-testid"?: string;
@@ -39,16 +40,16 @@ const ListItemInner = ({
c="inherit"
data-testid="data-reference-list-item"
>
- <div className={cx(S.item)}>
- <div className={S.itemIcons}>
- {icon && <Icon className={S.chartIcon} name={icon} size={16} />}
+ <div className={cx(ListS.item)}>
+ <div className={ListS.itemIcons}>
+ {icon && <Icon className={ListS.chartIcon} name={icon} size={16} />}
</div>
- <div className={cx(S.itemBody, CS.flexColumn)}>
- <Box className={S.itemTitle} lh="1.5">
+ <div className={cx(ListS.itemBody, CS.flexColumn)}>
+ <Box className={ListS.itemTitle} lh="1.5">
<Ellipsified tooltip={name}>{name}</Ellipsified>
</Box>
{(description || placeholder) && (
- <Box className={cx(S.itemSubtitle)} mt="sm">
+ <Box className={cx(ListS.itemSubtitle)} mt="sm">
{description || placeholder}
</Box>
)}
@@ -59,16 +60,24 @@ const ListItemInner = ({
if (disabled) {
return (
- <Root data-testid={dataTestId} disabled>
+ <Box
+ component="li"
+ pos="relative"
+ className={S.disabled}
+ data-disabled="true"
+ data-testid={dataTestId}
+ >
{card}
- </Root>
+ </Box>
);
}
return (
- <Root data-testid={dataTestId}>
- <ListItemLink to={url}>{card}</ListItemLink>
- </Root>
+ <Box component="li" pos="relative" data-testid={dataTestId}>
+ <Link to={url} className={S.link}>
+ {card}
+ </Link>
+ </Box>
);
};
diff --git a/frontend/src/metabase/reference/databases/TableList.unit.spec.tsx b/frontend/src/metabase/reference/databases/TableList.unit.spec.tsx
index 7773798e4d1..0dd57f44ecd 100644
--- a/frontend/src/metabase/reference/databases/TableList.unit.spec.tsx
+++ b/frontend/src/metabase/reference/databases/TableList.unit.spec.tsx
@@ -9,8 +9,6 @@ import TableList from "./TableList";
const databaseId = getNextId();
-const disabledStyle = "pointer-events: none; opacity: 0.4;";
-
const incompleteTable = createMockTable({
id: getNextId(),
db_id: databaseId,
@@ -76,7 +74,7 @@ describe("TableList", () => {
const tableItem = screen.getAllByTestId("table-list-item")[tableIndex];
const link = within(tableItem).queryByRole("link");
- expect(tableItem).toHaveStyle(disabledStyle);
+ expect(tableItem).toHaveAttribute("data-disabled", "true");
expect(link).not.toBeInTheDocument();
});
@@ -89,7 +87,7 @@ describe("TableList", () => {
const tableItem = screen.getAllByTestId("table-list-item")[tableIndex];
const link = within(tableItem).queryByRole("link");
- expect(tableItem).toHaveStyle(disabledStyle);
+ expect(tableItem).toHaveAttribute("data-disabled", "true");
expect(link).not.toBeInTheDocument();
});
@@ -102,7 +100,7 @@ describe("TableList", () => {
const tableItem = screen.getAllByTestId("table-list-item")[tableIndex];
const link = within(tableItem).queryByRole("link");
- expect(tableItem).not.toHaveStyle(disabledStyle);
+ expect(tableItem).not.toHaveAttribute("data-disabled");
expect(link).toBeInTheDocument();
expect(link).toHaveAttribute(
"href",