Skip to content

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

Migrating SelectListItem (#84283)

Nick Fitzpatrickfe35cf9e1a95
diff --git a/frontend/src/metabase/common/components/SelectList/BaseSelectListItem.tsx b/frontend/src/metabase/common/components/SelectList/BaseSelectListItem.tsx
index e73ea55a702..cfd91915b8f 100644
--- a/frontend/src/metabase/common/components/SelectList/BaseSelectListItem.tsx
+++ b/frontend/src/metabase/common/components/SelectList/BaseSelectListItem.tsx
@@ -1,13 +1,18 @@
+import cx from "classnames";
import type {
+ HTMLAttributes,
KeyboardEvent,
MouseEvent,
ReactNode,
SyntheticEvent,
} from "react";
+import { forwardRef } from "react";
import { useScrollOnMount } from "metabase/common/hooks/use-scroll-on-mount";
+import CS from "metabase/css/core/index.css";
+import { Box } from "metabase/ui";
-import { BaseItemRoot } from "./SelectListItem.styled";
+import S from "./SelectListItem.module.css";
export interface BaseSelectListItemProps {
id: string | number;
@@ -17,11 +22,37 @@ export interface BaseSelectListItemProps {
isSelected?: boolean;
size?: "small" | "medium";
className?: string;
- hasLeftIcon?: boolean;
- hasRightIcon?: boolean;
as?: any;
}
+type BaseItemRootProps = HTMLAttributes<HTMLLIElement> & {
+ size: "small" | "medium";
+ isSelected: boolean;
+};
+
+const BaseItemRoot = forwardRef<HTMLLIElement, BaseItemRootProps>(
+ function BaseItemRoot({ size, isSelected, className, ...props }, ref) {
+ return (
+ <Box
+ component="li"
+ ref={ref}
+ display="grid"
+ bdrs="xs"
+ className={cx(
+ CS.cursorPointer,
+ CS.alignCenter,
+ size === "small" ? S.small : S.medium,
+ S.itemSpacing,
+ S.highlight,
+ { [S.selected]: isSelected },
+ className,
+ )}
+ {...props}
+ />
+ );
+ },
+);
+
export function BaseSelectListItem({
id,
onSelect,
diff --git a/frontend/src/metabase/common/components/SelectList/SelectListItem.module.css b/frontend/src/metabase/common/components/SelectList/SelectListItem.module.css
index b5e789efe97..68bae81d64c 100644
--- a/frontend/src/metabase/common/components/SelectList/SelectListItem.module.css
+++ b/frontend/src/metabase/common/components/SelectList/SelectListItem.module.css
@@ -1,11 +1,67 @@
+.itemSpacing {
+ margin-bottom: 0.125rem;
+
+ &:last-child {
+ margin-bottom: 0;
+ }
+}
+
+/* Padding is a class, not style props, so consumers can override it with their own className */
+.medium {
+ padding: 0.75rem 0.5rem;
+}
+
+.small {
+ padding: 0.5rem;
+}
+
+.highlight {
+ &.selected {
+ background-color: var(--mb-color-core-brand);
+ }
+
+ @media (hover: hover) {
+ &:hover {
+ background-color: var(--mb-color-core-brand);
+ }
+ }
+}
+
+.grid {
+ grid-template-columns: 1fr;
+ gap: 0.5rem;
+
+ &.hasLeftIcon {
+ grid-template-columns: min-content 1fr;
+ }
+
+ &.hasRightIcon {
+ grid-template-columns: 1fr min-content;
+ }
+
+ &.hasLeftIcon.hasRightIcon {
+ grid-template-columns: min-content 1fr min-content;
+ }
+}
+
+.title {
+ min-width: 0;
+ word-break: break-word;
+}
+
.icon {
justify-self: end;
color: var(--select-list-item-icon-color);
}
-.root {
- &[aria-selected="true"] .icon,
- &:hover .icon {
+.selected .icon,
+.selected .title {
+ color: var(--mb-color-text-primary-inverse);
+}
+
+@media (hover: hover) {
+ .highlight:hover .icon,
+ .highlight:hover .title {
color: var(--mb-color-text-primary-inverse);
}
}
diff --git a/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx b/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx
deleted file mode 100644
index f895cf092cf..00000000000
--- a/frontend/src/metabase/common/components/SelectList/SelectListItem.styled.tsx
+++ /dev/null
@@ -1,65 +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";
-
-import { Text, type TextProps } from "metabase/ui";
-
-// Unjustified type cast. FIXME
-export const ItemTitle = styled(Text)<TextProps>`
- margin: 0;
- min-width: 0;
- word-break: break-word;
-` as unknown as typeof Text;
-
-const activeItemCss = css`
- background-color: var(--mb-color-core-brand);
-
- ${ItemTitle} {
- color: var(--mb-color-text-primary-inverse);
- }
-`;
-
-const VERTICAL_PADDING_BY_SIZE = {
- small: "0.5rem",
- medium: "0.75rem",
-};
-
-export const BaseItemRoot = styled.li<{
- size: "small" | "medium";
- isSelected: boolean;
-}>`
- display: grid;
- align-items: center;
- cursor: pointer;
- padding: ${(props) => VERTICAL_PADDING_BY_SIZE[props.size]} 0.5rem;
- border-radius: 6px;
- margin-bottom: 2px;
-
- &:last-child {
- margin-bottom: 0;
- }
-
- @media (hover: hover) {
- &:hover {
- ${activeItemCss}
- }
- }
-
- ${(props) => props.isSelected && activeItemCss}
-`;
-
-const getGridTemplateColumns = (hasLeftIcon: boolean, hasRightIcon: boolean) =>
- `${hasLeftIcon ? "min-content" : ""} 1fr ${
- hasRightIcon ? "min-content" : ""
- }`;
-
-export const ItemRoot = styled(BaseItemRoot)<{
- hasLeftIcon: boolean;
- hasRightIcon: boolean;
-}>`
- display: grid;
- grid-template-columns: ${(props) =>
- getGridTemplateColumns(props.hasLeftIcon, props.hasRightIcon)};
- gap: 0.5rem;
-`;
diff --git a/frontend/src/metabase/common/components/SelectList/SelectListItem.tsx b/frontend/src/metabase/common/components/SelectList/SelectListItem.tsx
index d63f382bd49..99cee42633a 100644
--- a/frontend/src/metabase/common/components/SelectList/SelectListItem.tsx
+++ b/frontend/src/metabase/common/components/SelectList/SelectListItem.tsx
@@ -6,13 +6,12 @@ import {
type EntityIconProps,
} from "metabase/common/components/EntityIcon";
import type { IconProps } from "metabase/ui";
-import { Ellipsified } from "metabase/ui";
+import { Ellipsified, Text } from "metabase/ui";
import { maybeColor } from "metabase/ui/utils/colors";
import type { BaseSelectListItemProps } from "./BaseSelectListItem";
import { BaseSelectListItem } from "./BaseSelectListItem";
import S from "./SelectListItem.module.css";
-import { ItemRoot, ItemTitle } from "./SelectListItem.styled";
export interface SelectListItemProps extends Omit<
BaseSelectListItemProps,
@@ -48,31 +47,36 @@ export function SelectListItem({
return (
<BaseSelectListItem
- as={ItemRoot}
- className={cx(S.root, classNames.root, className)}
+ className={cx(
+ S.grid,
+ { [S.hasLeftIcon]: !!icon, [S.hasRightIcon]: !!rightIcon },
+ classNames.root,
+ className,
+ )}
{...otherProps}
name={name}
aria-label={name}
- hasLeftIcon={!!icon}
- hasRightIcon={!!rightIcon}
>
{icon && (
<ItemIcon
iconUrl={iconUrl}
- className={classNames.icon}
{...iconProps}
+ className={iconProps.className ?? classNames.icon}
/>
)}
- <ItemTitle
- className={classNames.label}
+ <Text
+ className={cx(S.title, classNames.label)}
fw="bold"
lh="normal"
data-testid="option-text"
>
<Ellipsified>{name}</Ellipsified>
- </ItemTitle>
+ </Text>
{rightIconProps.name && (
- <ItemIcon className={classNames.icon} {...rightIconProps} />
+ <ItemIcon
+ {...rightIconProps}
+ className={rightIconProps.className ?? classNames.icon}
+ />
)}
</BaseSelectListItem>
);