1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import clsx from 'clsx';
|
6 | import debounce from '../utils/debounce';
|
7 | import withStyles from '../styles/withStyles';
|
8 | import isMuiElement from '../utils/isMuiElement';
|
9 | export const styles = {
|
10 |
|
11 | root: {
|
12 | boxSizing: 'border-box',
|
13 | flexShrink: 0
|
14 | },
|
15 |
|
16 |
|
17 | tile: {
|
18 | position: 'relative',
|
19 | display: 'block',
|
20 |
|
21 | height: '100%',
|
22 | overflow: 'hidden'
|
23 | },
|
24 |
|
25 |
|
26 | imgFullHeight: {
|
27 | height: '100%',
|
28 | transform: 'translateX(-50%)',
|
29 | position: 'relative',
|
30 | left: '50%'
|
31 | },
|
32 |
|
33 |
|
34 | imgFullWidth: {
|
35 | width: '100%',
|
36 | position: 'relative',
|
37 | transform: 'translateY(-50%)',
|
38 | top: '50%'
|
39 | }
|
40 | };
|
41 |
|
42 | const fit = (imgEl, classes) => {
|
43 | if (!imgEl || !imgEl.complete) {
|
44 | return;
|
45 | }
|
46 |
|
47 | if (imgEl.width / imgEl.height > imgEl.parentElement.offsetWidth / imgEl.parentElement.offsetHeight) {
|
48 | imgEl.classList.remove(...classes.imgFullWidth.split(' '));
|
49 | imgEl.classList.add(...classes.imgFullHeight.split(' '));
|
50 | } else {
|
51 | imgEl.classList.remove(...classes.imgFullHeight.split(' '));
|
52 | imgEl.classList.add(...classes.imgFullWidth.split(' '));
|
53 | }
|
54 | };
|
55 |
|
56 | function ensureImageCover(imgEl, classes) {
|
57 | if (!imgEl) {
|
58 | return;
|
59 | }
|
60 |
|
61 | if (imgEl.complete) {
|
62 | fit(imgEl, classes);
|
63 | } else {
|
64 | imgEl.addEventListener('load', () => {
|
65 | fit(imgEl, classes);
|
66 | });
|
67 | }
|
68 | }
|
69 |
|
70 | let warnedOnce = false;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | const GridListTile = React.forwardRef(function GridListTile(props, ref) {
|
79 | if (process.env.NODE_ENV !== 'production') {
|
80 | if (!warnedOnce) {
|
81 | warnedOnce = true;
|
82 | console.error(['Material-UI: The GridListTile component was renamed to ImageListItem to align with the current Material Design naming.', '', "You should use `import { ImageListItem } from '@material-ui/core'`", "or `import ImageListItem from '@material-ui/core/ImageListItem'`."].join('\n'));
|
83 | }
|
84 | }
|
85 |
|
86 |
|
87 | const {
|
88 | children,
|
89 | classes,
|
90 | className,
|
91 |
|
92 | cols = 1,
|
93 | component: Component = 'li',
|
94 |
|
95 | rows = 1
|
96 | } = props,
|
97 | other = _objectWithoutPropertiesLoose(props, ["children", "classes", "className", "cols", "component", "rows"]);
|
98 |
|
99 | const imgRef = React.useRef(null);
|
100 | React.useEffect(() => {
|
101 | ensureImageCover(imgRef.current, classes);
|
102 | });
|
103 | React.useEffect(() => {
|
104 | const handleResize = debounce(() => {
|
105 | fit(imgRef.current, classes);
|
106 | });
|
107 | window.addEventListener('resize', handleResize);
|
108 | return () => {
|
109 | handleResize.clear();
|
110 | window.removeEventListener('resize', handleResize);
|
111 | };
|
112 | }, [classes]);
|
113 | return React.createElement(Component, _extends({
|
114 | className: clsx(classes.root, className),
|
115 | ref: ref
|
116 | }, other), React.createElement("div", {
|
117 | className: classes.tile
|
118 | }, React.Children.map(children, child => {
|
119 | if (! React.isValidElement(child)) {
|
120 | return null;
|
121 | }
|
122 |
|
123 | if (child.type === 'img' || isMuiElement(child, ['Image'])) {
|
124 | return React.cloneElement(child, {
|
125 | ref: imgRef
|
126 | });
|
127 | }
|
128 |
|
129 | return child;
|
130 | })));
|
131 | });
|
132 | process.env.NODE_ENV !== "production" ? GridListTile.propTypes = {
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 | children: PropTypes.node,
|
139 |
|
140 | |
141 |
|
142 |
|
143 |
|
144 | classes: PropTypes.object.isRequired,
|
145 |
|
146 | |
147 |
|
148 |
|
149 | className: PropTypes.string,
|
150 |
|
151 | |
152 |
|
153 |
|
154 | cols: PropTypes.number,
|
155 |
|
156 | |
157 |
|
158 |
|
159 |
|
160 | component: PropTypes
|
161 |
|
162 | .elementType,
|
163 |
|
164 | |
165 |
|
166 |
|
167 | rows: PropTypes.number
|
168 | } : void 0;
|
169 | export default withStyles(styles, {
|
170 | name: 'MuiGridListTile'
|
171 | })(GridListTile); |
\ | No newline at end of file |