-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
CardMedia.js
112 lines (105 loc) · 3.1 KB
/
CardMedia.js
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
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import withStyles from '../styles/withStyles';
import { chainPropTypes } from '@material-ui/utils';
export const styles = {
/* Styles applied to the root element. */
root: {
display: 'block',
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
},
/* Styles applied to the root element if `component="video, audio, picture, iframe, or img"`. */
media: {
width: '100%',
},
/* Styles applied to the root element if `component="picture or img"`. */
img: {
// ⚠️ object-fit is not supported by IE 11.
objectFit: 'cover',
},
};
const MEDIA_COMPONENTS = ['video', 'audio', 'picture', 'iframe', 'img'];
const CardMedia = React.forwardRef(function CardMedia(props, ref) {
const {
children,
classes,
className,
component: Component = 'div',
image,
src,
style,
...other
} = props;
const isMediaComponent = MEDIA_COMPONENTS.indexOf(Component) !== -1;
const composedStyle =
!isMediaComponent && image ? { backgroundImage: `url("${image}")`, ...style } : style;
return (
<Component
className={clsx(
classes.root,
{
[classes.media]: isMediaComponent,
[classes.img]: ['picture', 'img'].indexOf(Component) !== -1,
},
className,
)}
ref={ref}
style={composedStyle}
src={isMediaComponent ? image || src : undefined}
{...other}
>
{children}
</Component>
);
});
CardMedia.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: chainPropTypes(PropTypes.node, (props) => {
if (!props.children && !props.image && !props.src && !props.component) {
return new Error(
'Material-UI: Either `children`, `image`, `src` or `component` prop must be specified.',
);
}
return null;
}),
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes /* @typescript-to-proptypes-ignore */.elementType,
/**
* Image to be displayed as a background image.
* Either `image` or `src` prop must be specified.
* Note that caller must specify height otherwise the image will not be visible.
*/
image: PropTypes.string,
/**
* An alias for `image` property.
* Available only with media components.
* Media components: `video`, `audio`, `picture`, `iframe`, `img`.
*/
src: PropTypes.string,
/**
* @ignore
*/
style: PropTypes.object,
};
export default withStyles(styles, { name: 'MuiCardMedia' })(CardMedia);