From b62bf8bf50abd36e13210b6461618f7f5fd36054 Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Mon, 23 Nov 2026 10:57:04 +0300 Subject: [PATCH 1/4] card dropdown action addition --- src/lib/components/Card/Card.test.tsx | 11 ++++- .../components/Card/components/CardHeader.tsx | 49 +++++++++++++++++-- src/lib/components/Card/types.ts | 8 ++- 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/lib/components/Card/Card.test.tsx b/src/lib/components/Card/Card.test.tsx index 29b6352f..e71256f4 100644 --- a/src/lib/components/Card/Card.test.tsx +++ b/src/lib/components/Card/Card.test.tsx @@ -1,5 +1,5 @@ import Card from "@/components/Card/Card"; -import { render, screen } from "@testing-library/react"; +import { fireEvent, render, screen } from "@testing-library/react"; import Button from "@/components/Button"; import Link from "@/components/Link"; import IconButton from "@/components/IconButton"; @@ -141,6 +141,15 @@ describe("Card", () => { expect(onClick).toHaveBeenCalled(); }); + it("should open a dropdown menu with the given items when the action has items", () => { + const action = jest.fn(); + render(); + expect(screen.queryByTestId("cardActionMenu")).not.toBeInTheDocument(); + fireEvent.click(screen.getByText("more_vert")); + fireEvent.click(screen.getByText("Edit")); + expect(action).toHaveBeenCalledTimes(1); + }); + 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 042e2790..3ef56bb8 100644 --- a/src/lib/components/Card/components/CardHeader.tsx +++ b/src/lib/components/Card/components/CardHeader.tsx @@ -1,12 +1,19 @@ +"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"; +import DropdownMenuItem from "@/components/Dropdown/components/DropdownMenuItem"; +import { DropdownContext } from "@/components/Dropdown/context/DropdownContext"; +import dropdownStyles from "@/components/Dropdown/Dropdown.module.scss"; import { sanitizeModuleClasses } from "../../../../utils/cssUtils"; -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 { CardHeaderAction, CardVariant } from "../types"; type Props = { icon?: IconGlobalType; @@ -17,17 +24,37 @@ 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" ? "secondary" : variant === "warning" ? "strong" : "negative"; const headerClassName = sanitizeModuleClasses(styles, "header", image && imagePosition === "right" && "headerImageAlignRight"); const headerIconClassName = sanitizeModuleClasses(styles, "headerIcon", iconVariant && `headerIcon-${iconVariant}`); + const menuClassNames = sanitizeModuleClasses(dropdownStyles, "Menu", "md", "callout", placement.startsWith("top") ? "top" : "bottom"); const maybeContent = title || subtitle || avatarText || avatarIcon || icon || action; @@ -46,9 +73,23 @@ const CardHeader = (props: Props) => { {subtitle && {subtitle}} )} - {action && } + {action && } )} + {menuItems && + attached && + createPortal( + +
+
    + {menuItems.map((item, index) => ( + + ))} +
+
+
, + document.body, + )} ) ); diff --git a/src/lib/components/Card/types.ts b/src/lib/components/Card/types.ts index c98305b4..d0696751 100644 --- a/src/lib/components/Card/types.ts +++ b/src/lib/components/Card/types.ts @@ -3,6 +3,12 @@ import type { IconGlobalType, Variant } from "../../types"; import { ButtonProps } from "../Button/types"; import { LinkProps } from "../Link/types"; import { IconButtonProps } from "../IconButton/types"; +import type { DropdownMenuItemProps } from "../Dropdown/types"; + +/** The icon button at the top right: runs onClick, or opens a dropdown menu with the items of Dropdown */ +export type CardHeaderAction = { icon: IconGlobalType } & ( + { onClick: (event: MouseEvent) => void; items?: never } | { items: DropdownMenuItemProps[]; onClick?: never } +); export type CardActionButtonPosition = "left" | "center" | "right" | "spaceBetween" | "stretch"; @@ -14,7 +20,7 @@ export type CardProps = { iconVariant?: Variant; avatarIcon?: IconGlobalType; image?: string; - action?: { icon: IconGlobalType; onClick: (event: MouseEvent) => void }; + action?: CardHeaderAction; contentTitle?: string; contentSubtitle?: string; contentText?: string; From 03af5080c4be5eba0d4d8c0130d5667d7b90b16c Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Tue, 24 Nov 2026 16:01:06 +0300 Subject: [PATCH 2/4] month-yearpicker --- src/lib/components/Card/Card.module.scss | 125 ++++++++++++++++++ src/lib/components/Card/Card.test.tsx | 20 ++- .../components/Card/components/CardHeader.tsx | 39 ++++-- src/lib/components/Card/types.ts | 14 +- 4 files changed, 176 insertions(+), 22 deletions(-) diff --git a/src/lib/components/Card/Card.module.scss b/src/lib/components/Card/Card.module.scss index e61d265e..0f6aaed6 100644 --- a/src/lib/components/Card/Card.module.scss +++ b/src/lib/components/Card/Card.module.scss @@ -53,6 +53,12 @@ $variants: ( ) ); +$dropdown-gap: var(--base-sizing-10x); +$caret-margin: var(--base-sizing-12x); + +// The caret points near the action's right edge. It is kept in the dropdown, e.g. when the dropdown is shifted into the screen. +$caret-center: clamp($caret-margin, calc(var(--overlay-anchor-right) - $caret-margin), calc(100% - $caret-margin)); + .Root { border-radius: var(--theme-sizing-radius-default); background: var(--theme-color-surface-base-bg-light); @@ -193,6 +199,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.test.tsx b/src/lib/components/Card/Card.test.tsx index e71256f4..98b1a8af 100644 --- a/src/lib/components/Card/Card.test.tsx +++ b/src/lib/components/Card/Card.test.tsx @@ -141,13 +141,23 @@ describe("Card", () => { expect(onClick).toHaveBeenCalled(); }); - it("should open a dropdown menu with the given items when the action has items", () => { - const action = jest.fn(); - render(); - expect(screen.queryByTestId("cardActionMenu")).not.toBeInTheDocument(); + 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(action).toHaveBeenCalledTimes(1); + expect(onClick).not.toHaveBeenCalled(); }); it("should render an image in the content part when contentImage is set as a url or path", () => { diff --git a/src/lib/components/Card/components/CardHeader.tsx b/src/lib/components/Card/components/CardHeader.tsx index 3ef56bb8..827abc36 100644 --- a/src/lib/components/Card/components/CardHeader.tsx +++ b/src/lib/components/Card/components/CardHeader.tsx @@ -5,15 +5,13 @@ import { createPortal } from "react-dom"; import styles from "../Card.module.scss"; import Avatar from "@/components/Avatar"; import IconButton from "@/components/IconButton"; -import DropdownMenuItem from "@/components/Dropdown/components/DropdownMenuItem"; -import { DropdownContext } from "@/components/Dropdown/context/DropdownContext"; -import dropdownStyles from "@/components/Dropdown/Dropdown.module.scss"; import { sanitizeModuleClasses } from "../../../../utils/cssUtils"; +import type { MouseEvent } from "react"; import type { IconGlobalType, Variant } from "../../../types"; import ImageView from "@/components/ImageView"; import GlobalIconWrapper from "../../Motif/GlobalIconWrapper/GlobalIconWrapper"; import useAnchoredOverlay from "../../../hooks/useAnchoredOverlay"; -import type { CardHeaderAction, CardVariant } from "../types"; +import type { CardDropdownItem, CardHeaderAction, CardVariant } from "../types"; type Props = { icon?: IconGlobalType; @@ -54,7 +52,13 @@ const CardHeader = (props: Props) => { const iconButtonVariant = variant === "neutral" || variant === "secondary" ? "secondary" : variant === "warning" ? "strong" : "negative"; const headerClassName = sanitizeModuleClasses(styles, "header", image && imagePosition === "right" && "headerImageAlignRight"); const headerIconClassName = sanitizeModuleClasses(styles, "headerIcon", iconVariant && `headerIcon-${iconVariant}`); - const menuClassNames = sanitizeModuleClasses(dropdownStyles, "Menu", "md", "callout", placement.startsWith("top") ? "top" : "bottom"); + + 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; @@ -79,15 +83,22 @@ const CardHeader = (props: Props) => { {menuItems && attached && createPortal( - -
-
    - {menuItems.map((item, index) => ( - - ))} -
-
-
, +
+
    + {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 d0696751..233b0093 100644 --- a/src/lib/components/Card/types.ts +++ b/src/lib/components/Card/types.ts @@ -3,11 +3,19 @@ import type { IconGlobalType, Variant } from "../../types"; import { ButtonProps } from "../Button/types"; import { LinkProps } from "../Link/types"; import { IconButtonProps } from "../IconButton/types"; -import type { DropdownMenuItemProps } from "../Dropdown/types"; -/** The icon button at the top right: runs onClick, or opens a dropdown menu with the items of Dropdown */ +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: DropdownMenuItemProps[]; onClick?: never } + { onClick: (event: MouseEvent) => void; items?: never } | { items: CardDropdownItem[]; onClick?: never } ); export type CardActionButtonPosition = "left" | "center" | "right" | "spaceBetween" | "stretch"; From b98095bd648a4353aa10548abfee9fa500d38082 Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Thu, 26 Nov 2026 10:01:18 +0300 Subject: [PATCH 3/4] fix --- src/lib/components/Card/Card.module.scss | 2 +- src/lib/components/Card/Card.stories.tsx | 13 +++++++++++++ src/lib/components/Card/types.ts | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/lib/components/Card/Card.module.scss b/src/lib/components/Card/Card.module.scss index 0f6aaed6..a072afdc 100644 --- a/src/lib/components/Card/Card.module.scss +++ b/src/lib/components/Card/Card.module.scss @@ -57,7 +57,7 @@ $dropdown-gap: var(--base-sizing-10x); $caret-margin: var(--base-sizing-12x); // The caret points near the action's right edge. It is kept in the dropdown, e.g. when the dropdown is shifted into the screen. -$caret-center: clamp($caret-margin, calc(var(--overlay-anchor-right) - $caret-margin), calc(100% - $caret-margin)); +$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); 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: () => ( ) => void; + * items?: { + * label?: string; + * icon?: IconGlobalType; + * iconColor?: string; + * header?: string; + * onClick?: (event: MouseEvent) => void; + * disabled?: boolean; + * }[]; + * } + * ``` + */ action?: CardHeaderAction; contentTitle?: string; contentSubtitle?: string; From e462ed51eeb3e4eb7085f7fbcefc67c1d30dbaad Mon Sep 17 00:00:00 2001 From: Atakan Bayrak Date: Fri, 27 Nov 2026 09:42:43 +0300 Subject: [PATCH 4/4] fix --- src/lib/components/Card/Card.module.scss | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib/components/Card/Card.module.scss b/src/lib/components/Card/Card.module.scss index a072afdc..5b59b24f 100644 --- a/src/lib/components/Card/Card.module.scss +++ b/src/lib/components/Card/Card.module.scss @@ -55,8 +55,6 @@ $variants: ( $dropdown-gap: var(--base-sizing-10x); $caret-margin: var(--base-sizing-12x); - -// The caret points near the action's right edge. It is kept in the dropdown, e.g. when the dropdown is shifted into the screen. $caret-center: clamp($caret-margin, calc((var(--overlay-anchor-left) + var(--overlay-anchor-right)) / 2), calc(100% - $caret-margin)); .Root {