-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Card.tsx
208 lines (196 loc) · 6.53 KB
/
Card.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import * as React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { OverridableComponent } from '@mui/types';
import {
unstable_capitalize as capitalize,
unstable_isMuiElement as isMuiElement,
} from '@mui/utils';
import { useThemeProps } from '../styles';
import styled from '../styles/styled';
import { getCardUtilityClass } from './cardClasses';
import { CardProps, CardTypeMap } from './CardProps';
import { resolveSxValue } from '../styles/styleUtils';
import { CardRowContext } from './CardContext';
const useUtilityClasses = (ownerState: CardProps) => {
const { size, variant, color, row } = ownerState;
const slots = {
root: [
'root',
variant && `variant${capitalize(variant)}`,
color && `color${capitalize(color)}`,
size && `size${capitalize(size)}`,
row && 'row',
],
};
return composeClasses(slots, getCardUtilityClass, {});
};
const CardRoot = styled('div', {
name: 'JoyCard',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{ ownerState: CardProps }>(({ theme, ownerState }) => [
{
// a context variable for any child component
'--Card-childRadius':
'max((var(--Card-radius) - var(--variant-borderWidth, 0px)) - var(--Card-padding), min(var(--Card-padding) / 2, (var(--Card-radius) - var(--variant-borderWidth, 0px)) / 2))',
// AspectRatio integration
'--AspectRatio-radius': 'var(--Card-childRadius)',
// Link integration
'--internal-action-margin': 'calc(-1 * var(--variant-borderWidth, 0px))',
// Link, Radio, Checkbox integration
'--internal-action-radius': resolveSxValue(
{ theme, ownerState },
'borderRadius',
'var(--Card-radius)',
),
// CardCover integration
'--CardCover-radius': 'calc(var(--Card-radius) - var(--variant-borderWidth, 0px))',
// CardOverflow integration
'--CardOverflow-offset': `calc(-1 * var(--Card-padding))`,
'--CardOverflow-radius': 'calc(var(--Card-radius) - var(--variant-borderWidth, 0px))',
// Divider integration
'--Divider-inset': 'calc(-1 * var(--Card-padding))',
...(ownerState.size === 'sm' && {
'--Card-radius': theme.vars.radius.sm,
'--Card-padding': '0.5rem',
}),
...(ownerState.size === 'md' && {
'--Card-radius': theme.vars.radius.md,
'--Card-padding': '1rem',
fontSize: theme.vars.fontSize.md,
}),
...(ownerState.size === 'lg' && {
'--Card-radius': theme.vars.radius.lg,
'--Card-padding': '1.5rem',
}),
padding: 'var(--Card-padding)',
borderRadius: 'var(--Card-radius)',
boxShadow: theme.shadow.sm,
backgroundColor: theme.vars.palette.background.surface,
fontFamily: theme.vars.fontFamily.body,
// TODO: discuss the theme transition.
// This value is copied from mui-material Sheet.
transition: 'box-shadow 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
position: 'relative',
display: 'flex',
flexDirection: ownerState.row ? 'row' : 'column',
},
theme.variants[ownerState.variant!]?.[ownerState.color!],
]);
const Card = React.forwardRef(function Card(inProps, ref) {
const props = useThemeProps<typeof inProps & CardProps>({
props: inProps,
name: 'JoyCard',
});
const {
className,
color = 'neutral',
component = 'div',
size = 'md',
variant = 'plain',
children,
row = false,
...other
} = props;
const ownerState = {
...props,
color,
component,
row,
size,
variant,
};
const classes = useUtilityClasses(ownerState);
return (
<CardRowContext.Provider value={row}>
<CardRoot
as={component}
ownerState={ownerState}
className={clsx(classes.root, className)}
ref={ref}
{...other}
>
{React.Children.map(children, (child, index) => {
if (!React.isValidElement(child)) {
return child;
}
const extraProps: Record<string, any> = {};
if (isMuiElement(child, ['Divider'])) {
extraProps.inset = 'inset' in child.props ? child.props.inset : 'context';
const orientation = row ? 'vertical' : 'horizontal';
extraProps.orientation =
'orientation' in child.props ? child.props.orientation : orientation;
}
if (index === 0) {
extraProps['data-first-child'] = '';
}
if (index === React.Children.count(children) - 1) {
extraProps['data-last-child'] = '';
}
return React.cloneElement(child, extraProps);
})}
</CardRoot>
</CardRowContext.Provider>
);
}) as OverridableComponent<CardTypeMap>;
Card.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Used to render icon or text elements inside the Card if `src` is not set.
* This can be an element, or just a string.
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'neutral'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['danger', 'info', 'neutral', 'primary', 'success', 'warning']),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, flex direction is set to 'row'.
* @default false
*/
row: PropTypes.bool,
/**
* The size of the component.
* It accepts theme values between 'sm' and 'lg'.
* @default 'md'
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['lg', 'md', 'sm']),
PropTypes.string,
]),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* The variant to use.
* @default 'plain'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default Card;