Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions src/lib/components/Card/Card.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -193,6 +197,125 @@ $variants: (
}
}

// =============================================================================
// Action Dropdown: rendered in document.body, so it is not nested in the card
// =============================================================================

.dropdown {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check this file seperately after other threads are resolved

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;
Expand Down
13 changes: 13 additions & 0 deletions src/lib/components/Card/Card.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ export const Header: Story = {
render: args => <Card {...args} imagePosition="right" image="https://picsum.photos/seed/motifui/100" />,
};

export const ActionDropdown: Story = {
args: {
action: {
icon: "more_vert",
items: [
Comment thread
atakanbayrak marked this conversation as resolved.
Resolved
{ 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: () => (
<Card
Expand Down
21 changes: 20 additions & 1 deletion src/lib/components/Card/Card.test.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -141,6 +141,25 @@ describe("Card", () => {
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", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the outside click? close on escape (In fact if you did not activate this, please activate in the code)

const onClick = jest.fn();
render(<Card action={{ icon: "more_vert", items: [{ header: "Account" }, { label: "Edit", onClick }] }} />);
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", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it("should not run the click of a disabled dropdown item", () => {
it("should not fire on click of the action items which has its disabled prop as true ", () => {

const onClick = jest.fn();
render(<Card action={{ icon: "more_vert", items: [{ label: "Edit", onClick, disabled: true }] }} />);
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(<Card contentImage={path} />);
Expand Down
58 changes: 55 additions & 3 deletions src/lib/components/Card/components/CardHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"use client";
Comment thread
hatesalp marked this conversation as resolved.
Resolved

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";
Expand All @@ -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;
Expand All @@ -17,18 +22,44 @@ type Props = {
subtitle?: string;
image?: string;
imagePosition?: "left" | "right";
action?: { icon: IconGlobalType; onClick: (event: MouseEvent<HTMLButtonElement>) => 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<HTMLButtonElement>(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<HTMLButtonElement>) => {
item.onClick?.(event);
hide();
};

const maybeContent = title || subtitle || avatarText || avatarIcon || icon || action;

return (
Expand All @@ -46,9 +77,30 @@ const CardHeader = (props: Props) => {
{subtitle && <span className={styles.headerSubtitle}>{subtitle}</span>}
</div>
)}
{action && <IconButton name={action.icon} onClick={action.onClick} size="xxl" variant={iconButtonVariant} />}
{action && <IconButton ref={actionRef} name={action.icon} onClick={action.onClick} size="xxl" variant={iconButtonVariant} />}
</div>
)}
{menuItems &&
attached &&
createPortal(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This menu duplicates Dropdown's (overlay, items, caret styles). Could we extract Dropdown's menu into a DropdownMenu that takes an anchorRef, and use it here with the IconButton? We can talk it over with the team.

@atakanbayrak atakanbayrak Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented this dropdown actions as you mentioned in here at first actually, but Card component became so highly-coupled to Dropdown and its module, so custom dropdown behaved structure is implemented in here. We talked with @aktasmehmet, but he didn't see the code yet so maybe his opinion will be same with yours. Let's discuss this in here. @motif-ui/core-team

<div className={menuClassNames} style={menuStyle} ref={menuRef} {...insideProps} data-testid="cardDropdown">
<ul className={styles.dropdownList}>
{menuItems.map((item, index) => (
<li key={index}>
{item.header ? (
<span className={styles.dropdownHeader}>{item.header}</span>
) : (
<button type="button" className={styles.dropdownItem} disabled={item.disabled} onClick={handleItemClick(item)}>
{item.icon && <GlobalIconWrapper icon={item.icon} style={{ color: item.iconColor }} />}
{item.label && <span>{item.label}</span>}
</button>
)}
</li>
))}
</ul>
</div>,
Comment on lines +86 to +101

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a seperate component

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, I would create a hook here which also handles all the overlay stuff inside and return this part as component and actionRef to use inside ref of the icon button. This way, it will be much cleaner.

document.body,
)}
</div>
)
);
Expand Down
33 changes: 32 additions & 1 deletion src/lib/components/Card/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ import { ButtonProps } from "../Button/types";
import { LinkProps } from "../Link/types";
import { IconButtonProps } from "../IconButton/types";

export type CardDropdownItem = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches DropdownMenuItemProps except onClick vs action. Can we share one type so the same item has one name across components?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to keep separate custom Dropdown structure, we shouldn't create one type for this case. On the other hand, if we decide to use Dropdown this type will be removed anyway.

label?: string;
icon?: IconGlobalType;
iconColor?: string;
header?: string;
Comment on lines +10 to +11

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 props are not necessary right now. They make it more complicated (like usage of header and label, icon, disabled, etc. together.). IconColor is a completely different thing. We dont provide such deep styling prop.

So, please remove both.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And dont forget the jsdoc comment for this type below πŸ˜‰

onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
disabled?: boolean;
};

/** The action at the top right: an icon button that runs onClick, or opens a dropdown with the given items */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the comment

export type CardHeaderAction = { icon: IconGlobalType } & (
{ onClick: (event: MouseEvent<HTMLButtonElement>) => void; items?: never } | { items: CardDropdownItem[]; onClick?: never }
);
Comment on lines +17 to +19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you prevent user having an onClick event when there are items? It might be possible for user to execute a code block when the dropdown opens. So, I suggest:

export type CardHeaderAction = {
  icon: IconGlobalType;
  onClick: (event: MouseEvent<HTMLButtonElement>) => void;
  items?: CardDropdownItem[];
};

and please move this right before CardVariant. It looks like the main type of the component (which is not) at the very top.


export type CardActionButtonPosition = "left" | "center" | "right" | "spaceBetween" | "stretch";

export type CardProps = {
Expand All @@ -14,7 +28,24 @@ export type CardProps = {
iconVariant?: Variant;
avatarIcon?: IconGlobalType;
image?: string;
action?: { icon: IconGlobalType; onClick: (event: MouseEvent<HTMLButtonElement>) => void };
/**
* Runs `onClick`, or opens a dropdown with the given `items`.
* ```
* {
* icon: IconGlobalType;
* onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
* items?: {
* label?: string;
* icon?: IconGlobalType;
* iconColor?: string;
* header?: string;
* onClick?: (event: MouseEvent<HTMLButtonElement>) => void;
* disabled?: boolean;
* }[];
* }
* ```
*/
action?: CardHeaderAction;
Comment thread
hatesalp marked this conversation as resolved.
Resolved
contentTitle?: string;
contentSubtitle?: string;
contentText?: string;
Expand Down
Loading