Skip to content

Commit 98ba8d3

Browse files
author
Ricardo Lüders
committed
feat(theme): card theme component
1 parent 07c18bf commit 98ba8d3

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/lib/components/Card/Card.spec.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ describe('Components / Card', () => {
4343
it('should use `base` classes', () => {
4444
const theme = {
4545
card: {
46-
base: 'text-blue-100',
46+
root: {
47+
base: 'text-blue-100',
48+
},
4749
},
4850
};
4951
render(
@@ -58,7 +60,9 @@ describe('Components / Card', () => {
5860
it('should use `children` classes', () => {
5961
const theme = {
6062
card: {
61-
children: 'text-blue-900',
63+
root: {
64+
children: 'text-blue-900',
65+
},
6266
},
6367
};
6468
render(
@@ -76,9 +80,11 @@ describe('Components / Card', () => {
7680
it('should use `horizontal` classes', () => {
7781
const theme = {
7882
card: {
79-
horizontal: {
80-
off: 'text-blue-200',
81-
on: 'text-blue-300',
83+
root: {
84+
horizontal: {
85+
off: 'text-blue-200',
86+
on: 'text-blue-300',
87+
},
8288
},
8389
},
8490
};
@@ -98,7 +104,9 @@ describe('Components / Card', () => {
98104
it('should use `href` classes', () => {
99105
const theme = {
100106
card: {
101-
href: 'text-blue-700',
107+
root: {
108+
href: 'text-blue-700',
109+
},
102110
},
103111
};
104112
render(

src/lib/components/Card/Card.tsx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import classNames from 'classnames';
22
import type { ComponentProps, FC, PropsWithChildren } from 'react';
3+
import { DeepPartial } from '..';
4+
import { mergeDeep } from '../../helpers/mergeDeep';
35
import { useTheme } from '../Flowbite/ThemeContext';
46

57
export interface FlowbiteCardTheme {
8+
root: FlowbiteCardRootTheme;
9+
img: FlowbiteCardImageTheme;
10+
}
11+
12+
export interface FlowbiteCardRootTheme {
613
base: string;
714
children: string;
815
horizontal: {
916
off: string;
1017
on: string;
1118
};
1219
href: string;
13-
img: {
14-
base: string;
15-
horizontal: {
16-
off: string;
17-
on: string;
18-
};
20+
}
21+
22+
export interface FlowbiteCardImageTheme {
23+
base: string;
24+
horizontal: {
25+
off: string;
26+
on: string;
1927
};
2028
}
2129

@@ -24,6 +32,7 @@ export interface CardProps extends PropsWithChildren<ComponentProps<'div'>> {
2432
href?: string;
2533
imgAlt?: string;
2634
imgSrc?: string;
35+
theme?: DeepPartial<FlowbiteCardTheme>;
2736
}
2837

2938
export const Card: FC<CardProps> = ({
@@ -33,16 +42,22 @@ export const Card: FC<CardProps> = ({
3342
href,
3443
imgAlt,
3544
imgSrc,
45+
theme: customTheme = {},
3646
...props
3747
}): JSX.Element => {
38-
const theme = useTheme().theme.card;
39-
4048
const Component = typeof href === 'undefined' ? 'div' : 'a';
4149
const theirProps = props as object;
4250

51+
const theme = mergeDeep(useTheme().theme.card, customTheme);
52+
4353
return (
4454
<Component
45-
className={classNames(theme.base, theme.horizontal[horizontal ? 'on' : 'off'], href && theme.href, className)}
55+
className={classNames(
56+
theme.root.base,
57+
theme.root.horizontal[horizontal ? 'on' : 'off'],
58+
href && theme.root.href,
59+
className,
60+
)}
4661
data-testid="flowbite-card"
4762
href={href}
4863
{...theirProps}
@@ -54,7 +69,7 @@ export const Card: FC<CardProps> = ({
5469
src={imgSrc}
5570
/>
5671
)}
57-
<div className={theme.children}>{children}</div>
72+
<div className={theme.root.children}>{children}</div>
5873
</Component>
5974
);
6075
};

src/lib/theme/default.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,15 @@ const theme: FlowbiteTheme = {
269269
},
270270
},
271271
card: {
272-
base: 'flex rounded-lg border border-gray-200 bg-white shadow-md dark:border-gray-700 dark:bg-gray-800',
273-
children: 'flex h-full flex-col justify-center gap-4 p-6',
274-
horizontal: {
275-
off: 'flex-col',
276-
on: 'flex-col md:max-w-xl md:flex-row',
272+
root: {
273+
base: 'flex rounded-lg border border-gray-200 bg-white shadow-md dark:border-gray-700 dark:bg-gray-800',
274+
children: 'flex h-full flex-col justify-center gap-4 p-6',
275+
horizontal: {
276+
off: 'flex-col',
277+
on: 'flex-col md:max-w-xl md:flex-row',
278+
},
279+
href: 'hover:bg-gray-100 dark:hover:bg-gray-700',
277280
},
278-
href: 'hover:bg-gray-100 dark:hover:bg-gray-700',
279281
img: {
280282
base: '',
281283
horizontal: {

0 commit comments

Comments
 (0)