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
Migrate FormField and FileInput off Emotion (#83881)
Andrei Chernykh3bfe6652b755
diff --git a/frontend/src/metabase/actions/containers/ActionCreator/FormCreator/FormFieldEditor/FormFieldEditor.module.css b/frontend/src/metabase/actions/containers/ActionCreator/FormCreator/FormFieldEditor/FormFieldEditor.module.cssindex 40a84cee72c..150cb37e2b3 100644--- a/frontend/src/metabase/actions/containers/ActionCreator/FormCreator/FormFieldEditor/FormFieldEditor.module.css+++ b/frontend/src/metabase/actions/containers/ActionCreator/FormCreator/FormFieldEditor/FormFieldEditor.module.css@@ -7,7 +7,7 @@border-top: 1px solid var(--mb-color-border-neutral);}-/* FormSelectWidget still renders the Emotion FormField, so its root margin and label color are reset here */+/* FormSelectWidget renders a FormField, whose root margin and label color are reset here */.previewField > div {margin-bottom: 0;}diff --git a/frontend/src/metabase/common/components/FileInput/FileInput.module.css b/frontend/src/metabase/common/components/FileInput/FileInput.module.cssnew file mode 100644index 00000000000..d83321b60f8--- /dev/null+++ b/frontend/src/metabase/common/components/FileInput/FileInput.module.css@@ -0,0 +1,23 @@+.input {+ order: 1;++ &:focus {+ outline: none;+ }++ &::file-selector-button {+ padding-top: 0.5rem;+ padding-right: 2rem;+ visibility: hidden;+ }+}++.button {+ border: 1px solid var(--mb-color-border-neutral);+ border-radius: 0.375rem;+ user-select: none;+}++.input:focus-visible + .button {+ outline: 2px solid var(--mb-color-input-focus);+}diff --git a/frontend/src/metabase/common/components/FileInput/FileInput.styled.tsx b/frontend/src/metabase/common/components/FileInput/FileInput.styled.tsxdeleted file mode 100644index f58bdeccf48..00000000000--- a/frontend/src/metabase/common/components/FileInput/FileInput.styled.tsx+++ /dev/null@@ -1,52 +0,0 @@-// eslint-disable-next-line eslint-js/no-restricted-imports-import styled from "@emotion/styled";--export const InputRoot = styled.label`- display: flex;-`;--interface InputFieldProps {- hasValue: boolean;-}--export const InputField = styled.input<InputFieldProps>`- color: var(--mb-color-text-primary);- flex: 1 1 auto;- font-family: inherit;- font-weight: ${(props) => (props.hasValue ? "bold" : "")};- order: 1;-- &:active,- &:focus {- outline: none;- }-- &::file-selector-button {- padding-top: 0.5rem;- padding-right: 2rem;- visibility: hidden;- }-`;--export const InputButton = styled.span`- border: 1px solid var(--mb-color-border-neutral);- border-radius: 6px;- box-sizing: border-box;- color: var(--mb-color-text-primary);- cursor: pointer;- display: inline-block;- font-family: inherit;- font-size: 0.875rem;- font-weight: bold;- padding: 0.5rem 0.75rem;- white-space: nowrap;- user-select: none;-- ${InputField}:focus + & {- outline: 2px solid var(--mb-color-input-focus);- }-- ${InputField}:not(:focus-visible) + & {- outline: none;- }-`;diff --git a/frontend/src/metabase/common/components/FileInput/FileInput.tsx b/frontend/src/metabase/common/components/FileInput/FileInput.tsxindex e8d3bc118ab..1a721f0c3cb 100644--- a/frontend/src/metabase/common/components/FileInput/FileInput.tsx+++ b/frontend/src/metabase/common/components/FileInput/FileInput.tsx@@ -1,8 +1,12 @@+import cx from "classnames";import type { ChangeEvent, FocusEvent, HTMLAttributes, Ref } from "react";import { forwardRef, useCallback, useState } from "react";import { t } from "ttag";-import { InputButton, InputField, InputRoot } from "./FileInput.styled";+import CS from "metabase/css/core/index.css";+import { Box, Text } from "metabase/ui";++import S from "./FileInput.module.css";export type FileInputAttributes = Omit<HTMLAttributes<HTMLLabelElement>,@@ -19,7 +23,15 @@ export interface FileInputProps extends FileInputAttributes {}export const FileInput = forwardRef(function FileInput(- { name, autoFocus, onChange, onFocus, onBlur, ...props }: FileInputProps,+ {+ name,+ autoFocus,+ className,+ onChange,+ onFocus,+ onBlur,+ ...props+ }: FileInputProps,ref: Ref<HTMLLabelElement>,): JSX.Element {const [hasValue, setHasValue] = useState(false);@@ -34,17 +46,29 @@ export const FileInput = forwardRef(function FileInput();return (- <InputRoot ref={ref} {...props}>- <InputField+ <label ref={ref} {...props} className={cx(CS.flex, className)}>+ <Box+ component="input"type="file"+ className={S.input}name={name}- hasValue={hasValue}+ c="text-primary"+ flex="1 1 auto"+ ff="inherit"+ fw={hasValue ? "bold" : undefined}autoFocus={autoFocus}onChange={handleChange}onFocus={onFocus}onBlur={onBlur}/>- <InputButton>{t`Select a file`}</InputButton>- </InputRoot>+ <Text+ component="span"+ className={cx(S.button, CS.cursorPointer, CS.textNoWrap)}+ fw="bold"+ lh="md"+ px="md"+ py="sm"+ >{t`Select a file`}</Text>+ </label>);});diff --git a/frontend/src/metabase/common/components/FormField/FormField.module.css b/frontend/src/metabase/common/components/FormField/FormField.module.cssnew file mode 100644index 00000000000..4a6d94f5ba1--- /dev/null+++ b/frontend/src/metabase/common/components/FormField/FormField.module.css@@ -0,0 +1,32 @@+/* Classes rather than style props, so consumers can override the root margin and the label color */+.root {+ margin-bottom: 1.25rem;+}++.selfCenter {+ align-self: center;+}++.label {+ color: var(--mb-color-text-secondary);+}++.invalid {+ color: var(--mb-color-feedback-negative);+}++.root:focus-within .label {+ color: var(--mb-color-text-secondary);+}++.root:focus-within .labelError {+ display: none;+}++.infoIcon {+ color: var(--mb-color-background_page-tertiary-inverse);++ &:hover {+ color: var(--mb-color-core-brand);+ }+}diff --git a/frontend/src/metabase/common/components/FormField/FormField.styled.tsx b/frontend/src/metabase/common/components/FormField/FormField.styled.tsxdeleted file mode 100644index ea09a600a5e..00000000000--- a/frontend/src/metabase/common/components/FormField/FormField.styled.tsx+++ /dev/null@@ -1,115 +0,0 @@-// eslint-disable-next-line eslint-js/no-restricted-imports-import styled from "@emotion/styled";--import { Icon } from "metabase/ui";-import { color } from "metabase/ui/utils/colors";--import type { FieldAlignment, FieldOrientation } from "./types";--interface FormCaptionProps {- alignment: FieldAlignment;- orientation: FieldOrientation;- hasDescription: boolean;-}--export const FieldCaption = styled.div<FormCaptionProps>`- align-self: ${(props) =>- props.orientation !== "vertical" && !props.hasDescription ? "center" : ""};- margin-left: ${(props) =>- props.orientation === "horizontal" &&- props.alignment === "start" &&- "0.5rem"};- margin-right: ${(props) =>- props.orientation === "horizontal" &&- props.alignment === "end" &&- "0.5rem"};-`;--interface FieldLabelProps {- hasError: boolean;-}--export const FieldLabel = styled.label<FieldLabelProps>`- display: block;- color: ${(props) =>- props.hasError ? color("feedback-negative") : color("text-secondary")};- font-size: 0.77rem;- font-weight: 900;-`;--export const OptionalTag = styled.span`- color: var(--mb-color-text-secondary);- font-size: 0.77rem;- font-weight: 900;- margin-left: 0.25rem;-`;--interface FieldLabelContainerProps {- orientation: FieldOrientation;- hasDescription: boolean;-}--export const FieldLabelContainer = styled.div<FieldLabelContainerProps>`- display: flex;- align-items: center;- margin-bottom: ${(props) =>- props.orientation === "vertical" || props.hasDescription ? "0.5em" : ""};-`;--export const FieldLabelError = styled.span`- color: var(--mb-color-feedback-negative);-`;--export const FieldDescription = styled.div`- color: var(--mb-color-text-secondary);- margin-bottom: 0.5rem;-`;--export const FieldInfoIcon = styled(Icon)`- color: var(--mb-color-background_page-tertiary-inverse);- margin-left: 0.5rem;- width: 0.75rem;- height: 0.75rem;-- &:hover {- color: var(--mb-color-core-brand);- }-`;--export const FieldInfoLabel = styled.div`- color: var(--mb-color-text-secondary);- font-size: 0.75rem;- margin-left: auto;- cursor: default;-`;--interface FieldRootProps {- alignment: FieldAlignment;- orientation: FieldOrientation;-}--export const FieldRoot = styled.div<FieldRootProps>`- display: ${(props) => props.orientation === "horizontal" && "flex"};- justify-content: ${(props) =>- props.alignment === "end" &&- props.orientation === "horizontal" &&- "space-between"};- margin-bottom: 1.25rem;-- &:focus-within {- ${FieldLabel} {- color: var(--mb-color-text-secondary);- }-- ${FieldLabelError} {- display: none;- }- }-`;--export const FieldTitleActions = styled.div`- margin-left: auto;- font-size: 0.77rem;- font-weight: 900;- color: var(--mb-color-text-secondary);-`;diff --git a/frontend/src/metabase/common/components/FormField/FormField.tsx b/frontend/src/metabase/common/components/FormField/FormField.tsxindex bd7871b0f7a..9eb213d0b38 100644--- a/frontend/src/metabase/common/components/FormField/FormField.tsx+++ b/frontend/src/metabase/common/components/FormField/FormField.tsx@@ -1,21 +1,12 @@+import cx from "classnames";import type { HTMLAttributes, ReactNode, Ref } from "react";import { forwardRef } from "react";import { t } from "ttag";-import { Tooltip } from "metabase/ui";+import CS from "metabase/css/core/index.css";+import { Box, Flex, Icon, Text, Tooltip } from "metabase/ui";-import {- FieldCaption,- FieldDescription,- FieldInfoIcon,- FieldInfoLabel,- FieldLabel,- FieldLabelContainer,- FieldLabelError,- FieldRoot,- FieldTitleActions,- OptionalTag,-} from "./FormField.styled";+import S from "./FormField.module.css";import type { FieldAlignment, FieldOrientation } from "./types";export interface FormFieldProps extends HTMLAttributes<HTMLDivElement> {@@ -31,6 +22,43 @@ export interface FormFieldProps extends HTMLAttributes<HTMLDivElement> {infoTooltip?: string;}+type FieldLabelContainerProps = {+ orientation: FieldOrientation;+ hasDescription: boolean;+ children?: ReactNode;+};++const FieldLabelContainer = ({+ orientation,+ hasDescription,+ children,+}: FieldLabelContainerProps) => (+ <Flex+ align="center"+ mb={orientation === "vertical" || hasDescription ? "xs" : undefined}+ >+ {children}+ </Flex>+);++type FieldLabelProps = {+ hasError: boolean;+ htmlFor?: string;+ children?: ReactNode;+};++const FieldLabel = ({ hasError, htmlFor, children }: FieldLabelProps) => (+ <Box+ component="label"+ className={cx(S.label, { [S.invalid]: hasError })}+ htmlFor={htmlFor}+ fz="sm"+ fw={900}+ >+ {children}+ </Box>+);+const FormFieldInner = forwardRef(function FormField({title,@@ -44,6 +72,7 @@ const FormFieldInner = forwardRef(function FormField(infoTooltip,children,optional,+ className,...props}: FormFieldProps,ref: Ref<HTMLDivElement>,@@ -51,20 +80,27 @@ const FormFieldInner = forwardRef(function FormField(const hasTitle = Boolean(title);const hasDescription = Boolean(description);const hasError = Boolean(error);+ const isHorizontal = orientation === "horizontal";return (- <FieldRoot+ <Box{...props}ref={ref}- alignment={alignment}- orientation={orientation}+ className={cx(+ S.root,+ {+ [CS.flex]: isHorizontal,+ [CS.justifyBetween]: isHorizontal && alignment === "end",+ },+ className,+ )}>{alignment === "start" && children}{(hasTitle || hasDescription) && (- <FieldCaption- alignment={alignment}- orientation={orientation}- hasDescription={hasDescription}+ <Box+ className={cx({ [S.selfCenter]: isHorizontal && !hasDescription })}+ ml={isHorizontal && alignment === "start" ? "sm" : undefined}+ mr={isHorizontal && alignment === "end" ? "sm" : undefined}><FieldLabelContainerorientation={orientation}@@ -74,34 +110,65 @@ const FormFieldInner = forwardRef(function FormField(<FieldLabel hasError={hasError} htmlFor={htmlFor}>{title}{hasError && (- <FieldLabelError role="alert">: {error}</FieldLabelError>+ <Text+ component="span"+ className={S.labelError}+ inherit+ lh="inherit"+ c="feedback-negative"+ role="alert"+ >+ : {error}+ </Text>)}</FieldLabel>)}{!!optional && !hasError && (- <OptionalTag>{t`(optional)`}</OptionalTag>+ <Text+ component="span"+ c="text-secondary"+ fz="sm"+ fw={900}+ lh="md"+ ml="xxs"+ >{t`(optional)`}</Text>)}{(infoLabel || infoTooltip) && (<Tooltip multiline label={infoTooltip}>{infoLabel ? (- <FieldInfoLabel>{infoLabel}</FieldInfoLabel>+ <Text+ className={CS.cursorDefault}+ c="text-secondary"+ fz="sm"+ lh="md"+ ml="auto"+ >+ {infoLabel}+ </Text>) : (- <FieldInfoIcon name="info" />+ <Icon className={S.infoIcon} name="info" size={12} ml="sm" />)}</Tooltip>)}- {actions && <FieldTitleActions>{actions}</FieldTitleActions>}+ {actions && (+ <Text c="text-secondary" fz="sm" fw={900} lh="md" ml="auto">+ {actions}+ </Text>+ )}</FieldLabelContainer>- {description && <FieldDescription>{description}</FieldDescription>}- </FieldCaption>+ {description && (+ <Text c="text-secondary" lh="md" mb="sm">+ {description}+ </Text>+ )}+ </Box>)}{alignment === "end" && children}- </FieldRoot>+ </Box>);});export const FormField = Object.assign(FormFieldInner, {- Root: FieldRoot,Label: FieldLabel,LabelContainer: FieldLabelContainer,});diff --git a/frontend/src/metabase/databases/components/styles.ts b/frontend/src/metabase/databases/components/styles.tsindex d4bee599dc8..343f38b7802 100644--- a/frontend/src/metabase/databases/components/styles.ts+++ b/frontend/src/metabase/databases/components/styles.ts@@ -1,6 +1,11 @@import type { FieldType } from "../types";export function getSharedFieldStyleProps(type?: FieldType) {+ // the file input isn't a Mantine field, so it takes no style props+ if (type === "textFile") {+ return {};+ }+// our boolean (Switch) fields don't support the labelPropsconst labelProps =type !== "boolean"