Skip to content
Merged
36 changes: 36 additions & 0 deletions bulma-ui/src/components/Card.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,42 @@ export const HeaderWithIcon: Story = {
},
};

export const BulmaOwnMarkup: Story = {
render: () => (
<Card>
<Card.Header>
<Card.Header.Title as="p">Component</Card.Header.Title>
</Card.Header>
<Card.Content>
<p>
This card matches the markup on Bulma&apos;s own documentation page: a{' '}
<code>{'<p>'}</code> header title (<code>as=&quot;p&quot;</code>) and{' '}
<code>{'<a>'}</code> footer items (<code>as=&quot;a&quot;</code>)
whose whole cell is clickable, rather than a link nested inside a{' '}
<code>{'<span>'}</code>.
</p>
</Card.Content>
<Card.Footer>
<Card.FooterItem as="a" href="#save">
Save
</Card.FooterItem>
<Card.FooterItem as="a" href="#edit">
Edit
</Card.FooterItem>
<Card.FooterItem as="button">Delete</Card.FooterItem>
</Card.Footer>
</Card>
),
parameters: {
docs: {
description: {
story:
"Card.Header.Title and Card.FooterItem take an `as` prop so a card can render the same `p`/`a` markup Bulma's own docs use, instead of always a `div`/`span`.",
},
},
},
};

export const PinnedFooter: Story = {
render: () => (
<Card display="flex" flexDirection="column">
Expand Down
94 changes: 82 additions & 12 deletions bulma-ui/src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import {
prefixedClassNames,
} from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
type AnchorOnlyAttributes,
ANCHOR_ONLY_ATTRS,
omitAttrs,
} from '../helpers/anchorAttrs';
import {
useBulmaClasses,
BulmaClassesProps,
Expand Down Expand Up @@ -286,13 +291,39 @@ export interface CardFooterProps
children?: React.ReactNode;
}

/**
* The set withheld from a non-anchor `Card.FooterItem` (`span`/`button`): the
* derived anchor-only attributes, minus `type` (also valid on a `<button>` as
* `submit`/`button`/`reset`, so stripping it there would remove a working
* attribute — the same trade `DropdownItem`'s own `STRIP_FROM_NON_ANCHOR`
* accepts, in `./Dropdown.tsx`), plus `rel` (React declares it on
* `HTMLAttributes` for every element, so the derived set alone would not
* withhold it — the same addition `Level.Item` makes).
*/
const STRIP_FROM_NON_ANCHOR: Readonly<
Record<Exclude<keyof AnchorOnlyAttributes, 'type'> | 'rel', true>
> = (() => {
const { type: _type, ...rest } = ANCHOR_ONLY_ATTRS;
return { ...rest, rel: true };
})();

/**
* Props for the Card.FooterItem compound component.
*
* The anchor attributes arrive through `AnchorOnlyAttributes` at every `as`,
* and are forwarded only when the tag is an `<a>` — `STRIP_FROM_NON_ANCHOR`
* above withholds them from `span` and `button` at runtime, since narrowing
* the type per `as` is source-breaking (see `Level.Item`'s own note on #672).
*/
export interface CardFooterItemProps
extends
React.HTMLAttributes<HTMLSpanElement>,
AnchorOnlyAttributes,
React.HTMLAttributes<
HTMLSpanElement | HTMLAnchorElement | HTMLButtonElement
>,
Omit<BulmaClassesProps, 'color' | 'backgroundColor'> {
/** Element type to render (default: `span`). Bulma's own card markup uses `a`. */
as?: 'span' | 'a' | 'button';
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
/** Bulma color modifier (text color helper). */
color?: (typeof validColors)[number] | 'inherit' | 'current';
/** Background color helper. */
Expand All @@ -310,8 +341,12 @@ export interface CardFooterItemProps
*/
export interface CardHeaderTitleProps
extends
React.HTMLAttributes<HTMLDivElement>,
React.HTMLAttributes<
HTMLDivElement | HTMLParagraphElement | HTMLHeadingElement
>,
Omit<BulmaClassesProps, 'color' | 'backgroundColor'> {
/** Element type to render (default: `div`). Bulma's own card markup uses `p`; a heading suits a title that should read as one. */
as?: 'div' | 'p' | 'h2' | 'h3' | 'h4';
/** Bulma color modifier (text color helper). */
color?: (typeof validColors)[number] | 'inherit' | 'current';
/** Background color helper. */
Expand Down Expand Up @@ -409,6 +444,7 @@ const CardHeader: React.FC<CardHeaderProps> = ({
* @returns {JSX.Element} The rendered card header title.
*/
const CardHeaderTitle: React.FC<CardHeaderTitleProps> = ({
as = 'div',
className,
children,
centered,
Expand All @@ -422,8 +458,9 @@ const CardHeaderTitle: React.FC<CardHeaderTitleProps> = ({
backgroundColor: bgColor,
...props,
});
const Tag = as;
return (
<div
<Tag
className={classNames(
usePrefixedClassNames('card-header-title', {
'is-centered': centered,
Expand All @@ -434,7 +471,7 @@ const CardHeaderTitle: React.FC<CardHeaderTitleProps> = ({
{...rest}
>
{children}
</div>
</Tag>
);
};

Expand Down Expand Up @@ -583,6 +620,7 @@ const CardFooter: React.FC<CardFooterProps> = ({
* @returns {JSX.Element} The rendered card footer item.
*/
const CardFooterItem: React.FC<CardFooterItemProps> = ({
as = 'span',
className,
children,
color,
Expand All @@ -595,15 +633,47 @@ const CardFooterItem: React.FC<CardFooterItemProps> = ({
backgroundColor: bgColor,
...props,
});
const itemClasses = classNames(
usePrefixedClassNames('card-footer-item'),
bulmaHelperClasses,
className
);

// The anchor's own attributes reach an `<a>` and nothing else — `STRIP_FROM_NON_ANCHOR`
// above names the withheld set and why `type` stays while `rel` is added.
if (as === 'a') {
return (
<a className={itemClasses} {...rest}>
{children}
</a>
);
}

const forwarded = omitAttrs(rest, STRIP_FROM_NON_ANCHOR);

if (as === 'button') {
return (
<button
className={itemClasses}
{...forwarded}
// A footer item button must not submit an enclosing form by default —
// `<button>` defaults to `type="submit"`, and a Save/Cancel action row
// sitting in a form is the common case this `as` exists for.
type={
(
forwarded as {
type?: React.ButtonHTMLAttributes<HTMLButtonElement>['type'];
}
).type ?? 'button'
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
>
{children}
</button>
);
}

return (
<span
className={classNames(
usePrefixedClassNames('card-footer-item'),
bulmaHelperClasses,
className
)}
{...rest}
>
<span className={itemClasses} {...forwarded}>
{children}
</span>
);
Expand Down
133 changes: 133 additions & 0 deletions bulma-ui/src/components/__tests__/Card.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,116 @@ describe('Card Component', () => {
expect(screen.getByText('Footer item')).toBeInTheDocument();
});

describe('Card.FooterItem `as` prop', () => {
it('renders a span by default', () => {
render(<Card.FooterItem data-testid="item">Item</Card.FooterItem>);
expect(screen.getByTestId('item').tagName).toBe('SPAN');
});

it('renders as <a> and forwards anchor-only attributes', () => {
render(
<Card.FooterItem
as="a"
href="/save"
target="_blank"
rel="noopener"
download="file.pdf"
hrefLang="en"
ping="/p"
referrerPolicy="no-referrer"
media="print"
data-testid="item"
>
Save
</Card.FooterItem>
);
const anchor = screen.getByTestId('item');
expect(anchor.tagName).toBe('A');
expect(anchor).toHaveClass('card-footer-item');
expect(anchor).toHaveAttribute('href', '/save');
expect(anchor).toHaveAttribute('target', '_blank');
expect(anchor).toHaveAttribute('rel', 'noopener');
expect(anchor).toHaveAttribute('download', 'file.pdf');
expect(anchor).toHaveAttribute('hreflang', 'en');
expect(anchor).toHaveAttribute('ping', '/p');
expect(anchor).toHaveAttribute('referrerpolicy', 'no-referrer');
expect(anchor).toHaveAttribute('media', 'print');
});

it('withholds anchor-only attributes from a non-anchor tag', () => {
render(
<Card.FooterItem
as="span"
href="/x"
target="_blank"
rel="noopener"
download="file.pdf"
hrefLang="en"
ping="/p"
referrerPolicy="no-referrer"
media="print"
data-testid="item"
>
Text
</Card.FooterItem>
);
const item = screen.getByTestId('item');
expect(item.tagName).toBe('SPAN');
for (const attr of [
'href',
'target',
'rel',
'download',
'hreflang',
'ping',
'referrerpolicy',
'media',
]) {
expect(item).not.toHaveAttribute(attr);
}
});

it('renders as <button> and defaults type to "button"', () => {
render(
<Card.FooterItem as="button" data-testid="item">
Save
</Card.FooterItem>
);
const button = screen.getByTestId('item');
expect(button.tagName).toBe('BUTTON');
expect(button).toHaveClass('card-footer-item');
expect(button).toHaveAttribute('type', 'button');
});

it('preserves an explicit type on a button', () => {
render(
<Card.FooterItem as="button" type="submit" data-testid="item">
Submit
</Card.FooterItem>
);
expect(screen.getByTestId('item')).toHaveAttribute('type', 'submit');
});

it('withholds anchor-only attributes (other than type) from a button', () => {
render(
<Card.FooterItem
as="button"
href="/x"
target="_blank"
rel="noopener"
data-testid="item"
>
Save
</Card.FooterItem>
);
const button = screen.getByTestId('item');
expect(button.tagName).toBe('BUTTON');
expect(button).not.toHaveAttribute('href');
expect(button).not.toHaveAttribute('target');
expect(button).not.toHaveAttribute('rel');
});
});

test('Card.Header.Title renders with correct classes', () => {
render(
<Card.Header.Title
Expand Down Expand Up @@ -538,6 +648,29 @@ describe('Card Component', () => {
expect(headerTitle).toHaveClass('card-header-title', 'is-centered');
});

describe('Card.Header.Title `as` prop', () => {
it('renders a div by default', () => {
render(
<Card.Header.Title data-testid="title">Title</Card.Header.Title>
);
expect(screen.getByTestId('title').tagName).toBe('DIV');
});

it.each(['p', 'h2', 'h3', 'h4'] as const)(
'renders as <%s> when requested',
tag => {
render(
<Card.Header.Title as={tag} data-testid="title">
Title
</Card.Header.Title>
);
const title = screen.getByTestId('title');
expect(title.tagName).toBe(tag.toUpperCase());
expect(title).toHaveClass('card-header-title');
}
);
});

test('Card.Header.Icon renders with correct classes', () => {
render(
<Card.Header.Icon data-testid="header-icon" aria-label="menu">
Expand Down
Loading
Loading