Skip to content

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

Migrate EditableText, EditBar and DashboardSelector off Emotion (#83785)

Andrei Chernykhfd32a2790c49
diff --git a/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.styled.tsx b/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.styled.tsx
deleted file mode 100644
index 52aa2998b48..00000000000
--- a/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.styled.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-// eslint-disable-next-line eslint-js/no-restricted-imports
-import styled from "@emotion/styled";
-
-import { SelectButton } from "metabase/common/components/SelectButton/SelectButton";
-
-export const DashboardPickerButton = styled(SelectButton)`
- font-weight: 400;
-`;
diff --git a/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.tsx b/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.tsx
index 68f05005815..598911c7a19 100644
--- a/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.tsx
+++ b/frontend/src/metabase/common/components/DashboardSelector/DashboardSelector.tsx
@@ -4,11 +4,10 @@ import { t } from "ttag";
import { skipToken, useGetDashboardQuery } from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
import { DashboardPickerModal } from "metabase/common/components/Pickers/DashboardPicker";
+import { SelectButton } from "metabase/common/components/SelectButton";
import { Flex, Group, Icon } from "metabase/ui";
import type { DashboardId } from "metabase-types/api";
-import { DashboardPickerButton } from "./DashboardSelector.styled";
-
interface DashboardSelectorProps {
onChange: (value?: DashboardId) => void;
value?: DashboardId;
@@ -37,7 +36,7 @@ export const DashboardSelector = ({
if (isLoading) {
return (
<Flex>
- <DashboardPickerButton disabled>{t`Loading...`}</DashboardPickerButton>
+ <SelectButton disabled>{t`Loading...`}</SelectButton>
</Flex>
);
}
@@ -55,12 +54,9 @@ export const DashboardSelector = ({
return (
<Flex>
- <DashboardPickerButton
- fullWidth={fullWidth}
- onClick={() => setIsOpen(true)}
- >
+ <SelectButton fullWidth={fullWidth} onClick={() => setIsOpen(true)}>
{dashboard?.name || t`Pick a dashboard`}
- </DashboardPickerButton>
+ </SelectButton>
{isOpen && (
<DashboardPickerModal
value={
diff --git a/frontend/src/metabase/common/components/EditBar/EditBar.styled.tsx b/frontend/src/metabase/common/components/EditBar/EditBar.styled.tsx
deleted file mode 100644
index df0987b262b..00000000000
--- a/frontend/src/metabase/common/components/EditBar/EditBar.styled.tsx
+++ /dev/null
@@ -1,29 +0,0 @@
-import isPropValid from "@emotion/is-prop-valid";
-// eslint-disable-next-line eslint-js/no-restricted-imports
-import styled from "@emotion/styled";
-
-import { FullWidthContainer } from "metabase/styled-components/layout/FullWidthContainer";
-import { Icon } from "metabase/ui";
-import { alpha } from "metabase/ui/colors";
-
-export const Root = styled(FullWidthContainer, {
- shouldForwardProp: isPropValid,
-})<{ admin: boolean }>`
- display: flex;
- align-items: center;
- justify-content: space-between;
- position: relative;
- padding-top: 0.5rem;
- padding-bottom: 0.5rem;
- background-color: ${(props) =>
- alpha(props.admin ? "accent7" : "core-brand", 0.85)};
-`;
-
-export const EditIcon = styled(Icon)`
- color: var(--mb-color-text-primary-inverse);
-`;
-
-export const Title = styled.span`
- color: var(--mb-color-text-primary-inverse);
- font-weight: 700;
-`;
diff --git a/frontend/src/metabase/common/components/EditBar/EditBar.tsx b/frontend/src/metabase/common/components/EditBar/EditBar.tsx
index cc3c101bede..9424845eace 100644
--- a/frontend/src/metabase/common/components/EditBar/EditBar.tsx
+++ b/frontend/src/metabase/common/components/EditBar/EditBar.tsx
@@ -1,10 +1,9 @@
import type { ReactNode } from "react";
-import { Flex, Group } from "metabase/ui";
+import { FullWidthContainer } from "metabase/styled-components/layout/FullWidthContainer";
+import { Flex, Group, Icon, Text } from "metabase/ui";
-import { EditIcon, Root, Title } from "./EditBar.styled";
-
-type Props = {
+type EditBarProps = {
title: string;
center?: ReactNode;
buttons: ReactNode;
@@ -20,19 +19,29 @@ export function EditBar({
admin = false,
className,
"data-testid": dataTestId,
-}: Props) {
+}: EditBarProps) {
+ // The admin bar is purple in both themes, so its text stays white instead of following the inverse token
+ const textColor = admin ? "white" : "text-primary-inverse";
+
return (
- <Root
+ <Flex
+ component={FullWidthContainer}
className={className}
- admin={admin}
+ align="center"
+ justify="space-between"
+ pos="relative"
+ py="sm"
+ bg={admin ? "admin-navbar" : "core-brand"}
data-testid={dataTestId ?? "edit-bar"}
>
<Group gap="sm" align="center" wrap="nowrap">
- <EditIcon name="pencil" size={12} />
- <Title>{title}</Title>
+ <Icon name="pencil" size={12} c={textColor} />
+ <Text component="span" c={textColor} fw="bold" lh="md">
+ {title}
+ </Text>
</Group>
{center && <div>{center}</div>}
<Flex gap="md">{buttons}</Flex>
- </Root>
+ </Flex>
);
}
diff --git a/frontend/src/metabase/common/components/EditableText/EditableText.module.css b/frontend/src/metabase/common/components/EditableText/EditableText.module.css
new file mode 100644
index 00000000000..aa17ec3f6dd
--- /dev/null
+++ b/frontend/src/metabase/common/components/EditableText/EditableText.module.css
@@ -0,0 +1,60 @@
+/* Classes rather than style props: consumers override these through an outer Box's style prop and their own classes */
+.root {
+ position: relative;
+ margin: 0;
+ padding: 0.25rem;
+ color: var(--mb-color-text-primary);
+ overflow-wrap: break-word;
+}
+
+.border {
+ border: 1px solid transparent;
+ border-radius: 0.25rem;
+}
+
+.editable:hover,
+.editable:focus-within {
+ border-color: var(--mb-color-border-neutral);
+}
+
+.editing {
+ border-color: var(--mb-color-border-neutral);
+}
+
+/* Mirrors the value in a hidden pseudo-element so the box grows with the absolutely positioned textarea */
+.mirror::after {
+ content: attr(data-value);
+ visibility: hidden;
+ white-space: pre-wrap;
+ overflow-wrap: break-word;
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ line-clamp: inherit;
+ -webkit-line-clamp: inherit;
+ user-select: none;
+}
+
+.textarea {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ min-height: 0;
+ padding: inherit;
+ color: inherit;
+ font-size: inherit;
+ font-weight: inherit;
+ line-height: inherit;
+ cursor: pointer;
+ border: none;
+ resize: none;
+ outline: none;
+ overflow: hidden;
+ background: transparent;
+
+ &:focus,
+ &:disabled {
+ cursor: text;
+ }
+}
diff --git a/frontend/src/metabase/common/components/EditableText/EditableText.styled.tsx b/frontend/src/metabase/common/components/EditableText/EditableText.styled.tsx
deleted file mode 100644
index 8e010882393..00000000000
--- a/frontend/src/metabase/common/components/EditableText/EditableText.styled.tsx
+++ /dev/null
@@ -1,73 +0,0 @@
-// eslint-disable-next-line eslint-js/no-restricted-imports
-import { css } from "@emotion/react";
-// eslint-disable-next-line eslint-js/no-restricted-imports
-import styled from "@emotion/styled";
-
-interface EditableTextRootProps {
- isEditing?: boolean;
- isDisabled: boolean;
- isEditingMarkdown?: boolean;
-}
-
-export const EditableTextRoot = styled.div<EditableTextRootProps>`
- position: relative;
- margin: 0;
- color: var(--mb-color-text-primary);
- padding: 0.25rem;
- border: 1px solid transparent;
- border-radius: 4px;
- word-wrap: break-word;
-
- &:hover,
- &:focus-within {
- border-color: ${(props) =>
- !props.isDisabled && "var(--mb-color-border-neutral)"};
- }
-
- ${(props) =>
- props.isEditing &&
- !props.isDisabled &&
- css`
- border-color: var(--mb-color-border-neutral);
- `}
-
- ${({ isEditingMarkdown }) =>
- isEditingMarkdown &&
- css`
- &:after {
- content: attr(data-value);
- visibility: hidden;
- white-space: pre-wrap;
- word-wrap: break-word;
- display: -webkit-box;
- -webkit-box-orient: vertical;
- line-clamp: inherit;
- -webkit-line-clamp: inherit;
- user-select: none;
- }
- `}
-`;
-
-export const EditableTextArea = styled.textarea`
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- min-height: 0;
- padding: inherit;
- color: inherit;
- font-size: inherit;
- font-weight: inherit;
- line-height: inherit;
- cursor: ${(props) => (props.disabled ? "text" : "pointer")};
- border: none;
- resize: none;
- outline: none;
- overflow: hidden;
- background: transparent;
-
- &:focus {
- cursor: text;
- }
-`;
diff --git a/frontend/src/metabase/common/components/EditableText/EditableText.tsx b/frontend/src/metabase/common/components/EditableText/EditableText.tsx
index 27fde5a95d9..ac5957eee31 100644
--- a/frontend/src/metabase/common/components/EditableText/EditableText.tsx
+++ b/frontend/src/metabase/common/components/EditableText/EditableText.tsx
@@ -1,3 +1,4 @@
+import cx from "classnames";
import type {
ChangeEvent,
FocusEvent,
@@ -7,13 +8,20 @@ import type {
MouseEvent,
Ref,
} from "react";
-import { forwardRef, useCallback, useEffect, useRef, useState } from "react";
+import {
+ createElement,
+ forwardRef,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
import { usePrevious } from "react-use";
import { Markdown } from "metabase/common/components/Markdown";
import { Box, type BoxProps } from "metabase/ui";
-import { EditableTextArea, EditableTextRoot } from "./EditableText.styled";
+import S from "./EditableText.module.css";
export type EditableTextAttributes = Omit<
HTMLAttributes<HTMLDivElement>,
@@ -37,7 +45,7 @@ export interface EditableTextProps extends BoxProps, EditableTextAttributes {
"data-testid"?: string;
}
-const EditableTextInner = forwardRef(function EditableText(
+export const EditableText = forwardRef(function EditableText(
{
initialValue,
placeholder,
@@ -53,6 +61,7 @@ const EditableTextInner = forwardRef(function EditableText(
onBlur,
"data-testid": dataTestId,
as,
+ className,
...props
}: EditableTextProps,
ref: Ref<HTMLDivElement>,
@@ -136,13 +145,15 @@ const EditableTextInner = forwardRef(function EditableText(
return (
<Box
- component={EditableTextRoot}
- as={as}
+ // Box cannot take a union of element names as its component
+ renderRoot={(rootProps) => createElement(as ?? "div", rootProps)}
+ className={cx(S.root, S.border, className, {
+ [S.editable]: !isDisabled,
+ [S.editing]: isEditing && !isDisabled,
+ [S.mirror]: !shouldShowMarkdown,
+ })}
onClick={isMarkdown ? handleRootElementClick : undefined}
ref={ref}
- isEditing={isEditing}
- isDisabled={isDisabled}
- isEditingMarkdown={!shouldShowMarkdown}
data-value={`${displayValue}\u00A0`}
data-testid="editable-text"
tabIndex={isDisabled ? -1 : 0}
@@ -165,7 +176,8 @@ const EditableTextInner = forwardRef(function EditableText(
{shouldShowMarkdown ? (
<Markdown>{inputValue}</Markdown>
) : (
- <EditableTextArea
+ <textarea
+ className={S.textarea}
ref={inputRef}
value={inputValue}
placeholder={placeholder}
@@ -185,7 +197,3 @@ const EditableTextInner = forwardRef(function EditableText(
});
const shouldPassKeyToTextarea = (key: string) => key !== "Enter";
-
-export const EditableText = Object.assign(EditableTextInner, {
- Root: EditableTextRoot,
-});