Skip to content

Commit e5ea50b

Browse files
committed
feat: Choose Hx level for Card title ref #160
1 parent c038ea1 commit e5ea50b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/Card.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type CardProps = {
1515
id?: string;
1616
className?: string;
1717
title: ReactNode;
18+
titleAs?: `h${2 | 3 | 4 | 5 | 6}`;
1819
desc?: ReactNode;
1920
imageUrl?: string;
2021
imageAlt?: string;
@@ -81,6 +82,7 @@ export const Card = memo(
8182
id: props_id,
8283
className,
8384
title,
85+
titleAs: HtmlTitleTag = "h3",
8486
linkProps,
8587
desc,
8688
imageUrl,
@@ -146,7 +148,7 @@ export const Card = memo(
146148
>
147149
<div className={cx(fr.cx("fr-card__body"), classes.body)}>
148150
<div className={cx(fr.cx("fr-card__content"), classes.content)}>
149-
<h3 className={cx(fr.cx("fr-card__title"), classes.title)}>
151+
<HtmlTitleTag className={cx(fr.cx("fr-card__title"), classes.title)}>
150152
{linkProps !== undefined ? (
151153
<Link
152154
{...linkProps}
@@ -157,7 +159,7 @@ export const Card = memo(
157159
) : (
158160
title
159161
)}
160-
</h3>
162+
</HtmlTitleTag>
161163
{desc !== undefined && (
162164
<p className={cx(fr.cx("fr-card__desc"), classes.desc)}>{desc}</p>
163165
)}

stories/Card.stories.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ const { meta, getStory } = getStoryFactory({
1818
- [See source code](https://github.com/codegouvfr/react-dsfr/blob/main/src/Card.tsx)`,
1919
"argTypes": {
2020
"title": { "description": `Required.` },
21+
titleAs: {
22+
"description": `Heading level`,
23+
"options": (() => {
24+
const headings = ["h2", "h3", "h4", "h5", "h6"] as const;
25+
assert<Equals<typeof headings[number] | undefined, CardProps["titleAs"]>>();
26+
return headings;
27+
})(),
28+
"control": { "type": "radio" }
29+
},
2130
"desc": { "description": `` },
2231
linkProps: {
2332
"description": `Required only if enlargeLink is true. The Card Link props.`
@@ -30,9 +39,7 @@ const { meta, getStory } = getStoryFactory({
3039
"description": "Card title text sizing",
3140
"options": (() => {
3241
const sizes = ["small", "medium", "large"] as const;
33-
3442
assert<Equals<typeof sizes[number] | undefined, CardProps["size"]>>();
35-
3643
return sizes;
3744
})(),
3845
"control": { "type": "radio" }
@@ -49,6 +56,7 @@ export default meta;
4956
const defaultProps = {
5057
"enlargeLink": true as const,
5158
"title": "Intitulé de la carte (sur lequel se trouve le lien)",
59+
"titleAs": "h3" as const,
5260
"linkProps": {
5361
"href": "#"
5462
},

0 commit comments

Comments
 (0)