diff --git a/src/lib/components/Card/Card.module.scss b/src/lib/components/Card/Card.module.scss
index e61d265e..5b59b24f 100644
--- a/src/lib/components/Card/Card.module.scss
+++ b/src/lib/components/Card/Card.module.scss
@@ -53,6 +53,10 @@ $variants: (
)
);
+$dropdown-gap: var(--base-sizing-10x);
+$caret-margin: var(--base-sizing-12x);
+$caret-center: clamp($caret-margin, calc((var(--overlay-anchor-left) + var(--overlay-anchor-right)) / 2), calc(100% - $caret-margin));
+
.Root {
border-radius: var(--theme-sizing-radius-default);
background: var(--theme-color-surface-base-bg-light);
@@ -193,6 +197,125 @@ $variants: (
}
}
+// =============================================================================
+// Action Dropdown: rendered in document.body, so it is not nested in the card
+// =============================================================================
+
+.dropdown {
+ z-index: z-layer("anchored");
+ position: absolute;
+ width: max-content;
+ box-sizing: border-box;
+ margin-inline: var(--base-sizing-8x);
+
+ // The caret: a bordered triangle (::before) with the dropdown's fill (::after) on it
+ &::before,
+ &::after {
+ content: " ";
+ position: absolute;
+ border: solid transparent;
+ pointer-events: none;
+ }
+
+ &::before {
+ left: calc($caret-center - var(--base-sizing-6x));
+ }
+
+ &::after {
+ left: calc($caret-center - var(--base-sizing-4x));
+ }
+
+ &.bottom {
+ padding-top: $dropdown-gap;
+
+ &::before,
+ &::after {
+ top: var(--base-sizing-2x);
+ }
+
+ &::before {
+ border-width: 0 var(--base-sizing-6x) var(--base-sizing-10x);
+ border-bottom-color: var(--theme-color-surface-base-base7);
+ }
+
+ &::after {
+ border-width: var(--base-sizing-2x) var(--base-sizing-4x) var(--base-sizing-10x);
+ border-bottom-color: var(--theme-color-surface-base-bg-light);
+ }
+ }
+
+ &.top {
+ padding-bottom: $dropdown-gap;
+
+ &::before,
+ &::after {
+ bottom: var(--base-sizing-2x);
+ }
+
+ &::before {
+ border-width: var(--base-sizing-10x) var(--base-sizing-6x) 0;
+ border-top-color: var(--theme-color-surface-base-base7);
+ }
+
+ &::after {
+ border-width: var(--base-sizing-10x) var(--base-sizing-4x) var(--base-sizing-2x);
+ border-top-color: var(--theme-color-surface-base-bg-light);
+ }
+ }
+}
+
+// Positioned, so it is painted over the caret's border (::before) and under its fill (::after), which joins the two
+.dropdownList {
+ position: relative;
+ list-style: none;
+ margin: 0;
+ padding: 0;
+ border: solid var(--base-sizing-1x) var(--theme-color-border-light-default);
+ border-radius: var(--theme-sizing-radius-default);
+ background-color: var(--theme-color-surface-base-global-light);
+ overflow: hidden;
+ @include shadow(6);
+
+ li:not(:last-child) {
+ border-bottom: solid var(--base-sizing-1x) var(--theme-color-surface-base-base6);
+ }
+}
+
+.dropdownHeader,
+.dropdownItem {
+ display: flex;
+ align-items: center;
+ gap: var(--base-sizing-10x);
+ padding: var(--base-sizing-10x);
+ white-space: nowrap;
+}
+
+.dropdownHeader {
+ color: var(--theme-color-text-base-caption-light);
+ @include font("base", "md", "500");
+}
+
+.dropdownItem {
+ width: 100%;
+ border: none;
+ background: none;
+ color: var(--theme-color-text-base-subtitle);
+ cursor: pointer;
+ @include font("body", "md");
+ @include transition(color background-color);
+
+ &:hover {
+ background-color: var(--theme-color-surface-base-base4);
+ color: var(--theme-color-text-base-text-dark);
+ }
+
+ &:disabled {
+ cursor: default;
+ background: none;
+ color: var(--theme-color-text-base-caption-light);
+ }
+}
+
@each $variant, $bgColor, $titleColor, $subtitleColor, $avatarBgColor, $avatarTextColor in $variants {
.#{$variant} {
background-color: $bgColor;
diff --git a/src/lib/components/Card/Card.stories.tsx b/src/lib/components/Card/Card.stories.tsx
index 58291538..5c9d57e1 100644
--- a/src/lib/components/Card/Card.stories.tsx
+++ b/src/lib/components/Card/Card.stories.tsx
@@ -36,6 +36,19 @@ export const Header: Story = {
render: args => ,
};
+export const ActionDropdown: Story = {
+ args: {
+ action: {
+ icon: "more_vert",
+ items: [
+ { label: "Edit", icon: "settings", onClick: () => console.log("Edit clicked!") },
+ { label: "Share", icon: "share", onClick: () => console.log("Share clicked!") },
+ { label: "Delete", icon: "delete", disabled: true },
+ ],
+ },
+ },
+};
+
export const Content: Story = {
render: () => (
{
expect(onClick).toHaveBeenCalled();
});
+ it("should open a dropdown with the given items when the action has items and close it after an item is clicked", () => {
+ const onClick = jest.fn();
+ render();
+ expect(screen.queryByTestId("cardDropdown")).not.toBeInTheDocument();
+ fireEvent.click(screen.getByText("more_vert"));
+ expect(screen.getByText("Account")).toBeInTheDocument();
+ fireEvent.click(screen.getByText("Edit"));
+ expect(onClick).toHaveBeenCalledTimes(1);
+ expect(screen.queryByTestId("cardDropdown")).not.toBeInTheDocument();
+ });
+
+ it("should not run the click of a disabled dropdown item", () => {
+ const onClick = jest.fn();
+ render();
+ fireEvent.click(screen.getByText("more_vert"));
+ fireEvent.click(screen.getByText("Edit"));
+ expect(onClick).not.toHaveBeenCalled();
+ });
+
it("should render an image in the content part when contentImage is set as a url or path", () => {
const path = "/imagePath";
render();
diff --git a/src/lib/components/Card/components/CardHeader.tsx b/src/lib/components/Card/components/CardHeader.tsx
index 8caaafa7..1a4b244e 100644
--- a/src/lib/components/Card/components/CardHeader.tsx
+++ b/src/lib/components/Card/components/CardHeader.tsx
@@ -1,3 +1,7 @@
+"use client";
+
+import { useRef } from "react";
+import { createPortal } from "react-dom";
import styles from "../Card.module.scss";
import Avatar from "@/components/Avatar";
import IconButton from "@/components/IconButton";
@@ -6,7 +10,8 @@ import type { MouseEvent } from "react";
import type { IconGlobalType, Variant } from "../../../types";
import ImageView from "@/components/ImageView";
import GlobalIconWrapper from "../../Motif/GlobalIconWrapper/GlobalIconWrapper";
-import type { CardVariant } from "../types";
+import useAnchoredOverlay from "../../../hooks/useAnchoredOverlay";
+import type { CardDropdownItem, CardHeaderAction, CardVariant } from "../types";
type Props = {
icon?: IconGlobalType;
@@ -17,18 +22,44 @@ type Props = {
subtitle?: string;
image?: string;
imagePosition?: "left" | "right";
- action?: { icon: IconGlobalType; onClick: (event: MouseEvent) => void };
+ action?: CardHeaderAction;
variant: CardVariant;
};
const CardHeader = (props: Props) => {
const { icon, iconVariant, avatarIcon, avatarText, action, image, imagePosition, subtitle, title, variant } = props;
+ const menuItems = action?.items;
+
+ const actionRef = useRef(null);
+ const {
+ attached,
+ hide,
+ style: menuStyle,
+ placement,
+ overlayRef: menuRef,
+ insideProps,
+ } = useAnchoredOverlay({
+ anchorRef: actionRef,
+ placement: "bottomRight",
+ toggleOnAnchorClick: !!menuItems,
+ tabBridge: true,
+ closeOnOutsideClick: true,
+ closeOnEscape: true,
+ inactive: !menuItems,
+ });
const isVisible = title || subtitle || avatarText || avatarIcon || icon || image || action;
const iconButtonVariant = variant === "neutral" || variant === "secondary" ? variant : variant === "warning" ? "strong" : "negative";
const headerClassName = sanitizeModuleClasses(styles, "header", image && imagePosition === "right" && "headerImageAlignRight");
const headerIconClassName = sanitizeModuleClasses(styles, "headerIcon", iconVariant && `headerIcon-${iconVariant}`);
+ const menuClassNames = sanitizeModuleClasses(styles, "dropdown", placement.startsWith("top") ? "top" : "bottom");
+
+ const handleItemClick = (item: CardDropdownItem) => (event: MouseEvent) => {
+ item.onClick?.(event);
+ hide();
+ };
+
const maybeContent = title || subtitle || avatarText || avatarIcon || icon || action;
return (
@@ -46,9 +77,30 @@ const CardHeader = (props: Props) => {
{subtitle && {subtitle}}
)}
- {action && }
+ {action && }
)}
+ {menuItems &&
+ attached &&
+ createPortal(
+
+
+ {menuItems.map((item, index) => (
+ -
+ {item.header ? (
+ {item.header}
+ ) : (
+
+ )}
+
+ ))}
+
+
,
+ document.body,
+ )}
)
);
diff --git a/src/lib/components/Card/types.ts b/src/lib/components/Card/types.ts
index c98305b4..374494f6 100644
--- a/src/lib/components/Card/types.ts
+++ b/src/lib/components/Card/types.ts
@@ -4,6 +4,20 @@ import { ButtonProps } from "../Button/types";
import { LinkProps } from "../Link/types";
import { IconButtonProps } from "../IconButton/types";
+export type CardDropdownItem = {
+ label?: string;
+ icon?: IconGlobalType;
+ iconColor?: string;
+ header?: string;
+ onClick?: (event: MouseEvent) => void;
+ disabled?: boolean;
+};
+
+/** The action at the top right: an icon button that runs onClick, or opens a dropdown with the given items */
+export type CardHeaderAction = { icon: IconGlobalType } & (
+ { onClick: (event: MouseEvent) => void; items?: never } | { items: CardDropdownItem[]; onClick?: never }
+);
+
export type CardActionButtonPosition = "left" | "center" | "right" | "spaceBetween" | "stretch";
export type CardProps = {
@@ -14,7 +28,24 @@ export type CardProps = {
iconVariant?: Variant;
avatarIcon?: IconGlobalType;
image?: string;
- action?: { icon: IconGlobalType; onClick: (event: MouseEvent) => void };
+ /**
+ * Runs `onClick`, or opens a dropdown with the given `items`.
+ * ```
+ * {
+ * icon: IconGlobalType;
+ * onClick?: (event: MouseEvent) => void;
+ * items?: {
+ * label?: string;
+ * icon?: IconGlobalType;
+ * iconColor?: string;
+ * header?: string;
+ * onClick?: (event: MouseEvent) => void;
+ * disabled?: boolean;
+ * }[];
+ * }
+ * ```
+ */
+ action?: CardHeaderAction;
contentTitle?: string;
contentSubtitle?: string;
contentText?: string;