Skip to content

Commit 7134a9f

Browse files
feat: CardRoot
1 parent feeb1a3 commit 7134a9f

File tree

7 files changed

+82
-37
lines changed

7 files changed

+82
-37
lines changed

packages/mobile/src/cards/CardMedia.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { memo } from 'react';
1+
import { memo } from 'react';
22
import {
33
defaultMediaDimension,
44
defaultMediaSize,
@@ -11,8 +11,7 @@ import type {
1111
} from '@coinbase/cds-common/types';
1212

1313
import { Pictogram, SpotSquare } from '../illustrations';
14-
15-
import { CardRemoteImage } from './CardRemoteImage';
14+
import { getSource, RemoteImage } from '../media/RemoteImage';
1615

1716
export type CardMediaProps = CommonCardMediaProps;
1817

@@ -50,8 +49,10 @@ export const CardMedia = memo(function CardMedia({ placement = 'end', ...props }
5049
);
5150
case 'image':
5251
return (
53-
<CardRemoteImage
52+
<RemoteImage
5453
alt={props.alt ?? ''}
54+
resizeMode="cover"
55+
source={getSource(props.src)}
5556
src={props.src}
5657
testID={props.testID}
5758
{...imageProps[placement]}

packages/mobile/src/cards/CardRemoteImage.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React, { forwardRef, memo } from 'react';
2+
import type { View } from 'react-native';
3+
4+
import { HStack } from '../layout/HStack';
5+
import { Pressable, type PressableProps } from '../system/Pressable';
6+
7+
export type CardRootBaseProps = {
8+
children: React.ReactNode;
9+
renderAsPressable?: boolean;
10+
};
11+
12+
export type CardRootProps = CardRootBaseProps & PressableProps;
13+
14+
export const CardRoot = memo(
15+
forwardRef<View, CardRootBaseProps>(({ children, renderAsPressable, ...props }, ref) => {
16+
const Component = renderAsPressable ? Pressable : HStack;
17+
return (
18+
<Component ref={ref} {...props}>
19+
{children}
20+
</Component>
21+
);
22+
}),
23+
);
24+
25+
CardRoot.displayName = 'CardRoot';

packages/mobile/src/cards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './CardFooter';
44
export * from './CardGroup';
55
export * from './CardHeader';
66
export * from './CardMedia';
7+
export * from './CardRoot';
78
// Card variants
89
export * from './AnnouncementCard';
910
export * from './FeatureEntryCard';

packages/web/src/cards/CardRemoteImage.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { forwardRef, memo } from 'react';
2+
3+
import type { Polymorphic } from '../core/polymorphism';
4+
import { HStack } from '../layout/HStack';
5+
import { Pressable, type PressableBaseProps } from '../system/Pressable';
6+
7+
export type CardRootBaseProps = Polymorphic.ExtendableProps<
8+
PressableBaseProps,
9+
{
10+
children?: React.ReactNode;
11+
renderAsPressable?: boolean;
12+
}
13+
>;
14+
15+
export type CardRootProps<AsComponent extends React.ElementType = 'article'> = Polymorphic.Props<
16+
AsComponent,
17+
CardRootBaseProps
18+
>;
19+
20+
type CardRootComponent = (<AsComponent extends React.ElementType = 'article'>(
21+
props: Polymorphic.Props<AsComponent, CardRootBaseProps>,
22+
) => Polymorphic.ReactReturn) &
23+
Polymorphic.ReactNamed;
24+
25+
export const CardRoot: CardRootComponent = memo(
26+
forwardRef<React.ReactElement<CardRootBaseProps>, CardRootBaseProps>(
27+
<AsComponent extends React.ElementType>(
28+
{ renderAsPressable, children, ...props }: CardRootProps<AsComponent>,
29+
ref?: Polymorphic.Ref<AsComponent>,
30+
) => {
31+
if (renderAsPressable) {
32+
const { as, ...pressableRestProps } = props;
33+
return (
34+
<Pressable ref={ref} as={as ?? 'button'} {...(pressableRestProps as any)}>
35+
{children}
36+
</Pressable>
37+
);
38+
} else {
39+
const { as, ...hstackRestProps } = props;
40+
return (
41+
<HStack ref={ref} as={as ?? 'article'} {...(hstackRestProps as any)}>
42+
{children}
43+
</HStack>
44+
);
45+
}
46+
},
47+
),
48+
);
49+
50+
CardRoot.displayName = 'CardRoot';

packages/web/src/cards/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './CardFooter';
44
export * from './CardGroup';
55
export * from './CardHeader';
66
export * from './CardMedia';
7+
export * from './CardRoot';
78
// Card variants
89
export * from './AnnouncementCard';
910
export * from './FeatureEntryCard';

0 commit comments

Comments
 (0)