UNPKG

3.43 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import withStyles from '../styles/withStyles';
7import { chainPropTypes } from '@material-ui/utils';
8export const styles = {
9 /* Styles applied to the root element. */
10 root: {
11 display: 'block',
12 backgroundSize: 'cover',
13 backgroundRepeat: 'no-repeat',
14 backgroundPosition: 'center'
15 },
16
17 /* Styles applied to the root element if `component="video, audio, picture, iframe, or img"`. */
18 media: {
19 width: '100%'
20 },
21
22 /* Styles applied to the root element if `component="picture or img"`. */
23 img: {
24 // ⚠️ object-fit is not supported by IE 11.
25 objectFit: 'cover'
26 }
27};
28const MEDIA_COMPONENTS = ['video', 'audio', 'picture', 'iframe', 'img'];
29const CardMedia = /*#__PURE__*/React.forwardRef(function CardMedia(props, ref) {
30 const {
31 children,
32 classes,
33 className,
34 component: Component = 'div',
35 image,
36 src,
37 style
38 } = props,
39 other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "component", "image", "src", "style"]);
40
41 const isMediaComponent = MEDIA_COMPONENTS.indexOf(Component) !== -1;
42 const composedStyle = !isMediaComponent && image ? _extends({
43 backgroundImage: `url("${image}")`
44 }, style) : style;
45 return /*#__PURE__*/React.createElement(Component, _extends({
46 className: clsx(classes.root, className, isMediaComponent && classes.media, "picture img".indexOf(Component) !== -1 && classes.img),
47 ref: ref,
48 style: composedStyle,
49 src: isMediaComponent ? image || src : undefined
50 }, other), children);
51});
52process.env.NODE_ENV !== "production" ? CardMedia.propTypes = {
53 // ----------------------------- Warning --------------------------------
54 // | These PropTypes are generated from the TypeScript type definitions |
55 // | To update them edit the d.ts file and run "yarn proptypes" |
56 // ----------------------------------------------------------------------
57
58 /**
59 * The content of the component.
60 */
61 children: chainPropTypes(PropTypes.node, props => {
62 if (!props.children && !props.image && !props.src && !props.component) {
63 return new Error('Material-UI: Either `children`, `image`, `src` or `component` prop must be specified.');
64 }
65
66 return null;
67 }),
68
69 /**
70 * Override or extend the styles applied to the component.
71 * See [CSS API](#css) below for more details.
72 */
73 classes: PropTypes.object,
74
75 /**
76 * @ignore
77 */
78 className: PropTypes.string,
79
80 /**
81 * The component used for the root node.
82 * Either a string to use a HTML element or a component.
83 */
84 component: PropTypes
85 /* @typescript-to-proptypes-ignore */
86 .elementType,
87
88 /**
89 * Image to be displayed as a background image.
90 * Either `image` or `src` prop must be specified.
91 * Note that caller must specify height otherwise the image will not be visible.
92 */
93 image: PropTypes.string,
94
95 /**
96 * An alias for `image` property.
97 * Available only with media components.
98 * Media components: `video`, `audio`, `picture`, `iframe`, `img`.
99 */
100 src: PropTypes.string,
101
102 /**
103 * @ignore
104 */
105 style: PropTypes.object
106} : void 0;
107export default withStyles(styles, {
108 name: 'MuiCardMedia'
109})(CardMedia);
\No newline at end of file