Skip to content

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

GDGT-3132 fix misaligned undo toast (#82045)

Will Fry3c23f921e2dd
diff --git a/frontend/src/metabase/common/components/UndoListing.module.css b/frontend/src/metabase/common/components/UndoListing.module.css
index f2f02e24eb8..8bae1c19ba3 100644
--- a/frontend/src/metabase/common/components/UndoListing.module.css
+++ b/frontend/src/metabase/common/components/UndoListing.module.css
@@ -9,9 +9,17 @@
}
.toast {
+ --toast-row-height: rem(24px);
+ --toast-line-height: rem(20px);
+ --toast-icon-size: rem(16px);
+
position: absolute;
bottom: 0;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
width: max-content;
+ min-height: rem(56px);
}
.progress {
@@ -22,3 +30,32 @@
.toast:hover .progress {
animation-play-state: paused;
}
+
+.message {
+ padding-block: calc((var(--toast-row-height) - var(--toast-line-height)) / 2);
+ overflow: hidden;
+ line-height: var(--toast-line-height);
+}
+
+.messageIcon {
+ margin-block-start: calc(
+ (var(--toast-line-height) - var(--toast-icon-size)) / 2
+ );
+}
+
+.controls {
+ min-height: var(--toast-row-height);
+ overflow: hidden;
+}
+
+.actionButton {
+ height: var(--toast-row-height);
+}
+
+.dismissIcon {
+ cursor: pointer;
+
+ &:hover {
+ opacity: 0.7;
+ }
+}
diff --git a/frontend/src/metabase/common/components/UndoListing.styled.tsx b/frontend/src/metabase/common/components/UndoListing.styled.tsx
deleted file mode 100644
index 2586e2c7bb1..00000000000
--- a/frontend/src/metabase/common/components/UndoListing.styled.tsx
+++ /dev/null
@@ -1,71 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import { Link } from "metabase/common/components/Link";
-import type { BoxProps } from "metabase/ui";
-import { Box, Icon } from "metabase/ui";
-import { alpha } from "metabase/ui/colors";
-
-export const UndoList = styled.ul`
- position: fixed;
- left: 0;
- bottom: 0;
- margin: var(--mantine-spacing-lg);
- display: flex;
- flex-direction: column;
- align-items: flex-start;
-`;
-
-export const CardContent = styled.div`
- display: flex;
- align-items: flex-start;
- justify-content: space-between;
-`;
-
-// Unjustified type cast. FIXME
-export const CardContentSide = styled(Box)<BoxProps>`
- display: flex;
- align-items: flex-start;
- overflow: hidden;
-` as unknown as typeof Box;
-
-// Unjustified type cast. FIXME
-export const ControlsCardContent = styled(CardContentSide)`
- align-items: center;
- flex-shrink: 0;
-` as unknown as typeof Box;
-
-export const CardIcon = styled(Icon)`
- position: relative;
- top: 1px;
- margin-right: var(--mantine-spacing-sm);
- flex-shrink: 0;
-`;
-
-export const DefaultText = styled.span`
- font-weight: 700;
-`;
-
-export const UndoButton = styled(Link)`
- font-weight: bold;
- background-color: ${() => alpha("background_page-primary", 0.1)};
- padding: 4px 12px;
- margin-left: var(--mantine-spacing-sm);
- border-radius: 8px;
- white-space: nowrap; /* Prevents button from truncating message */
-
- :hover {
- background-color: ${() => alpha("background_page-primary", 0.3)};
- }
-`;
-
-export const DismissIcon = styled(Icon)<{ color?: string }>`
- position: relative;
- top: 1px;
- margin-left: var(--mantine-spacing-lg);
- cursor: pointer;
-
- :hover {
- opacity: 0.7;
- }
-`;
diff --git a/frontend/src/metabase/common/components/UndoListing.tsx b/frontend/src/metabase/common/components/UndoListing.tsx
index 93ec229ef20..31c0abba5eb 100644
--- a/frontend/src/metabase/common/components/UndoListing.tsx
+++ b/frontend/src/metabase/common/components/UndoListing.tsx
@@ -23,43 +23,38 @@ import {
performUndo,
resumeUndo,
} from "metabase/redux/undo";
-import { Card, Ellipsified, Portal, Progress } from "metabase/ui";
+import {
+ Button,
+ Card,
+ Ellipsified,
+ Flex,
+ Icon,
+ Portal,
+ Progress,
+ Stack,
+} from "metabase/ui";
import { capitalize, inflect } from "metabase/utils/formatting";
-import CS from "./UndoListing.module.css";
-import {
- CardContent,
- CardContentSide,
- CardIcon,
- ControlsCardContent,
- DefaultText,
- DismissIcon,
- UndoButton,
- UndoList,
-} from "./UndoListing.styled";
+import S from "./UndoListing.module.css";
const TOAST_TRANSITION_DURATION = 300;
const MARGIN = 8;
const TOAST_MESSAGE_MAX_LINES = 4;
-function DefaultMessage({
- undo: { verb = t`modified`, count = 1, subject = t`item` },
-}: {
- undo: Undo;
-}) {
- return (
- <DefaultText>
- {count > 1
- ? `${capitalize(verb)} ${count} ${inflect(subject, count)}`
- : `${capitalize(verb)} ${subject}`}
- </DefaultText>
- );
+function defaultMessage({
+ verb = t`modified`,
+ count = 1,
+ subject = t`item`,
+}: Pick<Undo, "verb" | "count" | "subject">) {
+ return count > 1
+ ? `${capitalize(verb)} ${count} ${inflect(subject, count)}`
+ : `${capitalize(verb)} ${subject}`;
}
function renderMessage(undo: Undo) {
const { message } = undo;
if (!message) {
- return <DefaultMessage undo={undo || {}} />;
+ return defaultMessage(undo);
}
return typeof message === "function" ? message(undo) : message;
}
@@ -98,7 +93,7 @@ function UndoToast({
data-testid="toast-undo"
color={undo.toastColor}
role="status"
- className={CS.toast}
+ className={S.toast}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
bg={dark ? "background_page-primary-inverse" : "background_page-primary"}
@@ -126,19 +121,22 @@ function UndoToast({
top={0}
left={0}
w="100%"
- className={CS.progress}
+ className={S.progress}
/* override animation duration based on timeout */
style={{
animationDuration: `${undo.initialTimeout}ms`,
}}
/>
)}
- <CardContent>
- <CardContentSide maw="75ch">
+ <Flex align="flex-start" justify="space-between">
+ <Flex className={S.message} align="flex-start" maw="75ch">
{undo.icon && (
- <CardIcon
+ <Icon
+ className={S.messageIcon}
name={undo.icon}
c={undo.iconColor ?? "text-secondary-inverse"}
+ mr="sm"
+ flex="0 0 auto"
/>
)}
{undo.renderChildren ? (
@@ -148,36 +146,46 @@ function UndoToast({
{renderMessage(undo)}
</Ellipsified>
)}
- </CardContentSide>
- <ControlsCardContent>
+ </Flex>
+ <Flex className={S.controls} align="center" flex="0 0 auto">
{undo.actions && undo.actions.length > 0 && (
- <UndoButton role="button" onClick={onUndo} to="">
+ <Button
+ className={S.actionButton}
+ variant="transparent"
+ color="text-secondary-inverse"
+ size="compact-md"
+ onClick={onUndo}
+ >
{undo.actionLabel ?? t`Undo`}
- </UndoButton>
+ </Button>
)}
{undo.extraAction && (
- <UndoButton
- role="button"
+ <Button
+ className={S.actionButton}
+ variant="transparent"
+ color="text-secondary-inverse"
+ size="compact-md"
onClick={() => {
undo.extraAction?.action();
if (undo.canDismiss) {
onDismiss();
}
}}
- to=""
>
{undo.extraAction.label}
- </UndoButton>
+ </Button>
)}
{undo.canDismiss && (
- <DismissIcon
+ <Icon
+ className={S.dismissIcon}
color={undo.dismissIconColor || "text-secondary-inverse"}
name="close"
onClick={onDismiss}
+ ml="lg"
/>
)}
- </ControlsCardContent>
- </CardContent>
+ </Flex>
+ </Flex>
</Card>
);
}
@@ -229,7 +237,6 @@ export function UndoListOverlay({
onUndo: (undo: Undo) => void;
onDismiss: (undo: Undo) => void;
}) {
- const ref = useRef<HTMLUListElement>(null);
const prevUndos = useRef<Undo[]>([]);
const [heights, setHeights] = useState<number[]>([]);
@@ -291,11 +298,17 @@ export function UndoListOverlay({
return (
<Portal target={target}>
- <UndoList
- ref={ref}
+ <Stack
+ component="ul"
data-testid="undo-list"
aria-label="undo-list"
className={ZIndex.Overlay}
+ pos="fixed"
+ left={0}
+ bottom={0}
+ m="lg"
+ gap={0}
+ align="flex-start"
>
<Group appear enter exit component={null}>
{undos.map(
@@ -324,7 +337,7 @@ export function UndoListOverlay({
),
)}
</Group>
- </UndoList>
+ </Stack>
</Portal>
);
}
diff --git a/frontend/src/metabase/common/components/UndoListing.unit.spec.tsx b/frontend/src/metabase/common/components/UndoListing.unit.spec.tsx
index 8ff9d7cc421..03894ca3251 100644
--- a/frontend/src/metabase/common/components/UndoListing.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/UndoListing.unit.spec.tsx
@@ -1,44 +1,101 @@
-import { renderWithProviders, screen } from "__support__/ui";
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen, waitFor } from "__support__/ui";
import type { Undo } from "metabase/redux/store/undo";
import { UndoListing } from "./UndoListing";
-const AUTO_CONNECT_UNDO: Undo = {
- icon: null,
- message:
- "Auto-connect this filter to all questions containing “Product.Title”, in the current tab?",
- actionLabel: "Auto-connect",
- timeout: 12000,
- timeoutId: null,
- id: 0,
- _domId: 1,
- canDismiss: true,
-};
+type UndoOverride = Partial<Omit<Undo, "id" | "_domId">>;
+
+function makeUndo(override: UndoOverride = {}, id = 0): Undo {
+ return {
+ icon: null,
+ message:
+ "Auto-connect this filter to all questions containing “Product.Title”, in the current tab?",
+ timeout: 12000,
+ timeoutId: null,
+ canDismiss: true,
+ id,
+ _domId: id,
+ ...override,
+ };
+}
+
+async function setup(undo: Undo) {
+ renderWithProviders(<UndoListing />, {
+ storeInitialState: {
+ undo: [undo],
+ },
+ });
+
+ await screen.findByRole("status");
+}
describe("UndoListing", () => {
it("renders list of Undo toasts", async () => {
- renderWithProviders(<UndoListing />, {
- storeInitialState: {
- undo: [AUTO_CONNECT_UNDO],
- },
- });
+ await setup(makeUndo());
+
+ expect(screen.getByRole("list", { name: "undo-list" })).toBeInTheDocument();
+ expect(screen.getByRole("status")).toBeInTheDocument();
+ });
+
+ it("renders progress bar", async () => {
+ await setup(makeUndo({ showProgress: true }));
+
+ expect(screen.getByRole("progressbar")).toBeInTheDocument();
+ });
+
+ it("renders the default message when no message is set", async () => {
+ await setup(
+ makeUndo({ message: null, verb: "archived", subject: "card", count: 1 }),
+ );
- expect(await screen.findByTestId("undo-list")).toBeInTheDocument();
- expect(await screen.findByTestId("toast-undo")).toBeInTheDocument();
+ expect(screen.getByText("Archived card")).toBeInTheDocument();
});
- it("should render progress bar", async () => {
- renderWithProviders(<UndoListing />, {
- storeInitialState: {
- undo: [
- {
- ...AUTO_CONNECT_UNDO,
- showProgress: true,
- },
- ],
- },
+ it("pluralizes the default message with the count", async () => {
+ await setup(
+ makeUndo({ message: null, verb: "archived", subject: "card", count: 3 }),
+ );
+
+ expect(screen.getByText("Archived 3 cards")).toBeInTheDocument();
+ });
+
+ it("renders the undo button with actionLabel when provided", async () => {
+ await setup(
+ makeUndo({ actions: [jest.fn()], actionLabel: "Auto-connect" }),
+ );
+
+ expect(
+ screen.getByRole("button", { name: "Auto-connect" }),
+ ).toBeInTheDocument();
+ });
+
+ it("performs the undo action and dismisses the toast when clicked", async () => {
+ const action = jest.fn();
+ await setup(makeUndo({ actions: [action] }));
+
+ await userEvent.click(screen.getByRole("button", { name: "Undo" }));
+
+ await waitFor(() => {
+ expect(action).toHaveBeenCalled();
+ expect(screen.queryByRole("status")).not.toBeInTheDocument();
});
+ });
- expect(await screen.findByRole("progressbar")).toBeInTheDocument();
+ it("performs and dismisses the toast when the extra action is clicked", async () => {
+ const action = jest.fn();
+ await setup(
+ makeUndo({
+ extraAction: { label: "See all", action },
+ }),
+ );
+
+ await userEvent.click(screen.getByRole("button", { name: "See all" }));
+
+ await waitFor(() => {
+ expect(action).toHaveBeenCalled();
+ expect(screen.queryByRole("status")).not.toBeInTheDocument();
+ });
});
});