UNPKG

4.58 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import chainPropTypes from '@mui/utils/chainPropTypes';
7import composeClasses from '@mui/utils/composeClasses';
8import { styled } from "../zero-styled/index.js";
9import { useDefaultProps } from "../DefaultPropsProvider/index.js";
10import { getCardMediaUtilityClass } from "./cardMediaClasses.js";
11import { jsx as _jsx } from "react/jsx-runtime";
12const useUtilityClasses = ownerState => {
13 const {
14 classes,
15 isMediaComponent,
16 isImageComponent
17 } = ownerState;
18 const slots = {
19 root: ['root', isMediaComponent && 'media', isImageComponent && 'img']
20 };
21 return composeClasses(slots, getCardMediaUtilityClass, classes);
22};
23const CardMediaRoot = styled('div', {
24 name: 'MuiCardMedia',
25 slot: 'Root',
26 overridesResolver: (props, styles) => {
27 const {
28 ownerState
29 } = props;
30 const {
31 isMediaComponent,
32 isImageComponent
33 } = ownerState;
34 return [styles.root, isMediaComponent && styles.media, isImageComponent && styles.img];
35 }
36})({
37 display: 'block',
38 backgroundSize: 'cover',
39 backgroundRepeat: 'no-repeat',
40 backgroundPosition: 'center',
41 variants: [{
42 props: {
43 isMediaComponent: true
44 },
45 style: {
46 width: '100%'
47 }
48 }, {
49 props: {
50 isImageComponent: true
51 },
52 style: {
53 objectFit: 'cover'
54 }
55 }]
56});
57const MEDIA_COMPONENTS = ['video', 'audio', 'picture', 'iframe', 'img'];
58const IMAGE_COMPONENTS = ['picture', 'img'];
59const CardMedia = /*#__PURE__*/React.forwardRef(function CardMedia(inProps, ref) {
60 const props = useDefaultProps({
61 props: inProps,
62 name: 'MuiCardMedia'
63 });
64 const {
65 children,
66 className,
67 component = 'div',
68 image,
69 src,
70 style,
71 ...other
72 } = props;
73 const isMediaComponent = MEDIA_COMPONENTS.includes(component);
74 const composedStyle = !isMediaComponent && image ? {
75 backgroundImage: `url("${image}")`,
76 ...style
77 } : style;
78 const ownerState = {
79 ...props,
80 component,
81 isMediaComponent,
82 isImageComponent: IMAGE_COMPONENTS.includes(component)
83 };
84 const classes = useUtilityClasses(ownerState);
85 return /*#__PURE__*/_jsx(CardMediaRoot, {
86 className: clsx(classes.root, className),
87 as: component,
88 role: !isMediaComponent && image ? 'img' : undefined,
89 ref: ref,
90 style: composedStyle,
91 ownerState: ownerState,
92 src: isMediaComponent ? image || src : undefined,
93 ...other,
94 children: children
95 });
96});
97process.env.NODE_ENV !== "production" ? CardMedia.propTypes /* remove-proptypes */ = {
98 // ┌────────────────────────────── Warning ──────────────────────────────┐
99 // │ These PropTypes are generated from the TypeScript type definitions. │
100 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
101 // └─────────────────────────────────────────────────────────────────────┘
102 /**
103 * The content of the component.
104 */
105 children: chainPropTypes(PropTypes.node, props => {
106 if (!props.children && !props.image && !props.src && !props.component) {
107 return new Error('MUI: Either `children`, `image`, `src` or `component` prop must be specified.');
108 }
109 return null;
110 }),
111 /**
112 * Override or extend the styles applied to the component.
113 */
114 classes: PropTypes.object,
115 /**
116 * @ignore
117 */
118 className: PropTypes.string,
119 /**
120 * The component used for the root node.
121 * Either a string to use a HTML element or a component.
122 */
123 component: PropTypes.elementType,
124 /**
125 * Image to be displayed as a background image.
126 * Either `image` or `src` prop must be specified.
127 * Note that caller must specify height otherwise the image will not be visible.
128 */
129 image: PropTypes.string,
130 /**
131 * An alias for `image` property.
132 * Available only with media components.
133 * Media components: `video`, `audio`, `picture`, `iframe`, `img`.
134 */
135 src: PropTypes.string,
136 /**
137 * @ignore
138 */
139 style: PropTypes.object,
140 /**
141 * The system prop that allows defining system overrides as well as additional CSS styles.
142 */
143 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
144} : void 0;
145export default CardMedia;
\No newline at end of file