-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
CardMedia.js
154 lines (142 loc) · 4.12 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
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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
function getStyles(props, context) {
const {cardMedia} = context.muiTheme;
return {
root: {
position: 'relative',
},
overlayContainer: {
position: 'absolute',
top: 0,
bottom: 0,
right: 0,
left: 0,
},
overlay: {
height: '100%',
position: 'relative',
},
overlayContent: {
position: 'absolute',
bottom: 0,
right: 0,
left: 0,
paddingTop: 8,
background: cardMedia.overlayContentBackground,
},
media: {},
mediaChild: {
verticalAlign: 'top',
maxWidth: '100%',
minWidth: '100%',
width: '100%',
},
};
}
class CardMedia extends Component {
static propTypes = {
/**
* If true, a click on this card component expands the card.
*/
actAsExpander: PropTypes.bool,
/**
* Can be used to render elements inside the Card Media.
*/
children: PropTypes.node,
/**
* If true, this card component is expandable.
*/
expandable: PropTypes.bool,
/**
* Override the inline-styles of the Card Media.
*/
mediaStyle: PropTypes.object,
/**
* Can be used to render overlay element in Card Media.
*/
overlay: PropTypes.node,
/**
* Override the inline-styles of the overlay container.
*/
overlayContainerStyle: PropTypes.object,
/**
* Override the inline-styles of the overlay content.
*/
overlayContentStyle: PropTypes.object,
/**
* Override the inline-styles of the overlay element.
*/
overlayStyle: PropTypes.object,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
render() {
const {
actAsExpander, // eslint-disable-line no-unused-vars
children,
expandable, // eslint-disable-line no-unused-vars
mediaStyle,
overlay,
overlayContainerStyle,
overlayContentStyle,
overlayStyle,
style,
...other
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
const rootStyle = Object.assign(styles.root, style);
const extendedMediaStyle = Object.assign(styles.media, mediaStyle);
const extendedOverlayContainerStyle = Object.assign(styles.overlayContainer, overlayContainerStyle);
const extendedOverlayContentStyle = Object.assign(styles.overlayContent, overlayContentStyle);
const extendedOverlayStyle = Object.assign(styles.overlay, overlayStyle);
const titleColor = this.context.muiTheme.cardMedia.titleColor;
const subtitleColor = this.context.muiTheme.cardMedia.subtitleColor;
const color = this.context.muiTheme.cardMedia.color;
const styledChildren = React.Children.map(children, (child) => {
if (!child) {
return child;
}
return React.cloneElement(child, {
style: prepareStyles(Object.assign({}, styles.mediaChild, child.props.style)),
});
});
const overlayChildren = React.Children.map(overlay, (child) => {
const childMuiName = (child && child.type) ? child.type.muiName : null;
if (childMuiName === 'CardHeader' || childMuiName === 'CardTitle') {
return React.cloneElement(child, {
titleColor: titleColor,
subtitleColor: subtitleColor,
});
} else if (childMuiName === 'CardText') {
return React.cloneElement(child, {
color: color,
});
} else {
return child;
}
});
return (
<div {...other} style={prepareStyles(rootStyle)}>
<div style={prepareStyles(extendedMediaStyle)}>
{styledChildren}
</div>
{overlay ?
<div style={prepareStyles(extendedOverlayContainerStyle)}>
<div style={prepareStyles(extendedOverlayStyle)}>
<div style={prepareStyles(extendedOverlayContentStyle)}>
{overlayChildren}
</div>
</div>
</div> : ''}
</div>
);
}
}
export default CardMedia;