-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
GridTile.js
250 lines (231 loc) · 6.71 KB
/
GridTile.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import React, {Component} from 'react';
import PropTypes from 'prop-types';
function getStyles(props, context) {
const {
baseTheme,
gridTile,
} = context.muiTheme;
const actionPos = props.actionIcon && props.actionPosition;
const styles = {
root: {
position: 'relative',
display: 'block',
height: '100%',
overflow: 'hidden',
},
titleBar: {
position: 'absolute',
left: 0,
right: 0,
[props.titlePosition]: 0,
height: props.subtitle ? 68 : 48,
background: props.titleBackground,
display: 'flex',
alignItems: 'center',
},
titleWrap: {
flexGrow: 1,
marginLeft: actionPos !== 'left' ? baseTheme.spacing.desktopGutterLess : 0,
marginRight: actionPos === 'left' ? baseTheme.spacing.desktopGutterLess : 0,
color: gridTile.textColor,
overflow: 'hidden',
},
title: {
fontSize: '16px',
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
},
subtitle: {
fontSize: '12px',
textOverflow: 'ellipsis',
overflow: 'hidden',
whiteSpace: 'nowrap',
},
actionIcon: {
order: actionPos === 'left' ? -1 : 1,
},
childImg: {
height: '100%',
transform: 'translateX(-50%)',
position: 'relative',
left: '50%',
},
};
return styles;
}
class GridTile extends Component {
static propTypes = {
/**
* An IconButton element to be used as secondary action target
* (primary action target is the tile itself).
*/
actionIcon: PropTypes.element,
/**
* Position of secondary action IconButton.
*/
actionPosition: PropTypes.oneOf(['left', 'right']),
/**
* Theoretically you can pass any node as children, but the main use case is to pass an img,
* in whichcase GridTile takes care of making the image "cover" available space
* (similar to background-size: cover or to object-fit:cover).
*/
children: PropTypes.node,
/**
* Width of the tile in number of grid cells.
*/
cols: PropTypes.number,
/**
* Either a string used as tag name for the tile root element, or a ReactElement.
* This is useful when you have, for example, a custom implementation of
* a navigation link (that knows about your routes) and you want to use it as the primary tile action.
* In case you pass a ReactElement, please ensure that it passes all props,
* accepts styles overrides and render it's children.
*/
containerElement: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
/**
* Height of the tile in number of grid cells.
*/
rows: PropTypes.number,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* String or element serving as subtitle (support text).
*/
subtitle: PropTypes.node,
/**
* Override the inline-styles of the subtitle element.
*/
subtitleStyle: PropTypes.object,
/**
* Title to be displayed on tile.
*/
title: PropTypes.node,
/**
* Style used for title bar background.
* Useful for setting custom gradients for example
*/
titleBackground: PropTypes.string,
/**
* Position of the title bar (container of title, subtitle and action icon).
*/
titlePosition: PropTypes.oneOf(['top', 'bottom']),
/**
* Override the inline-styles of the title element.
*/
titleStyle: PropTypes.object,
};
static defaultProps = {
titlePosition: 'bottom',
titleBackground: 'rgba(0, 0, 0, 0.4)',
actionPosition: 'right',
cols: 1,
rows: 1,
containerElement: 'div',
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
componentDidMount() {
this.ensureImageCover();
}
componentDidUpdate() {
this.ensureImageCover();
}
ensureImageCover() {
let imgEl = this.refs.img;
if (imgEl) {
const fit = () => {
if (imgEl.offsetWidth < imgEl.parentNode.offsetWidth) {
const {isRtl} = this.context.muiTheme;
imgEl.style.height = 'auto';
if (isRtl) {
imgEl.style.right = '0';
} else {
imgEl.style.left = '0';
}
imgEl.style.width = '100%';
imgEl.style.top = '50%';
imgEl.style.transform = imgEl.style.WebkitTransform = 'translateY(-50%)';
}
imgEl.removeEventListener('load', fit);
imgEl = null; // prevent closure memory leak
};
if (imgEl.complete) {
fit();
} else {
imgEl.addEventListener('load', fit);
}
}
}
render() {
const {
title,
subtitle,
titlePosition, // eslint-disable-line no-unused-vars
titleBackground, // eslint-disable-line no-unused-vars
titleStyle,
subtitleStyle,
actionIcon, // eslint-disable-line no-unused-vars
actionPosition, // eslint-disable-line no-unused-vars
style,
children,
containerElement,
...other
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
const mergedRootStyles = Object.assign(styles.root, style);
let titleBar = null;
if (title) {
titleBar = (
<div key="titlebar" style={prepareStyles(styles.titleBar)}>
<div style={prepareStyles(styles.titleWrap)}>
<div style={prepareStyles(Object.assign(styles.title, titleStyle))}>
{title}
</div>
{subtitle ? (
<div style={prepareStyles(Object.assign(styles.subtitle, subtitleStyle))}>
{subtitle}
</div>
) : null}
</div>
{actionIcon ? (
<div style={prepareStyles(styles.actionIcon)}>
{actionIcon}
</div>
) : null}
</div>
);
}
let newChildren = children;
// if there is a single image passed as children
// clone it and add our styles
if (React.Children.count(children) === 1) {
newChildren = React.Children.map(children, (child) => {
if (child.type === 'img') {
return React.cloneElement(child, {
key: 'img',
ref: 'img',
style: prepareStyles(Object.assign({}, styles.childImg, child.props.style)),
});
} else {
return child;
}
});
}
const containerProps = {
style: prepareStyles(mergedRootStyles),
...other,
};
return React.isValidElement(containerElement) ?
React.cloneElement(containerElement, containerProps, [newChildren, titleBar]) :
React.createElement(containerElement, containerProps, [newChildren, titleBar]);
}
}
export default GridTile;