UNPKG

15 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = exports.styles = void 0;
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
15
16var React = _interopRequireWildcard(require("react"));
17
18var _propTypes = _interopRequireDefault(require("prop-types"));
19
20var _clsx = _interopRequireDefault(require("clsx"));
21
22var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
23
24var _requirePropFactory = _interopRequireDefault(require("../utils/requirePropFactory"));
25
26var _deprecatedPropType = _interopRequireDefault(require("../utils/deprecatedPropType"));
27
28// A grid component using the following libs as inspiration.
29//
30// For the implementation:
31// - https://getbootstrap.com/docs/4.3/layout/grid/
32// - https://github.com/kristoferjoseph/flexboxgrid/blob/master/src/css/flexboxgrid.css
33// - https://github.com/roylee0704/react-flexbox-grid
34// - https://material.angularjs.org/latest/layout/introduction
35//
36// Follow this flexbox Guide to better understand the underlying model:
37// - https://css-tricks.com/snippets/css/a-guide-to-flexbox/
38var SPACINGS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
39var GRID_SIZES = ['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
40
41function generateGrid(globalStyles, theme, breakpoint) {
42 var styles = {};
43 GRID_SIZES.forEach(function (size) {
44 var key = "grid-".concat(breakpoint, "-").concat(size);
45
46 if (size === true) {
47 // For the auto layouting
48 styles[key] = {
49 flexBasis: 0,
50 flexGrow: 1,
51 maxWidth: '100%'
52 };
53 return;
54 }
55
56 if (size === 'auto') {
57 styles[key] = {
58 flexBasis: 'auto',
59 flexGrow: 0,
60 maxWidth: 'none'
61 };
62 return;
63 } // Keep 7 significant numbers.
64
65
66 var width = "".concat(Math.round(size / 12 * 10e7) / 10e5, "%"); // Close to the bootstrap implementation:
67 // https://github.com/twbs/bootstrap/blob/8fccaa2439e97ec72a4b7dc42ccc1f649790adb0/scss/mixins/_grid.scss#L41
68
69 styles[key] = {
70 flexBasis: width,
71 flexGrow: 0,
72 maxWidth: width
73 };
74 }); // No need for a media query for the first size.
75
76 if (breakpoint === 'xs') {
77 (0, _extends2.default)(globalStyles, styles);
78 } else {
79 globalStyles[theme.breakpoints.up(breakpoint)] = styles;
80 }
81}
82
83function getOffset(val) {
84 var div = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
85 var parse = parseFloat(val);
86 return "".concat(parse / div).concat(String(val).replace(String(parse), '') || 'px');
87}
88
89function generateGutter(theme, breakpoint) {
90 var styles = {};
91 SPACINGS.forEach(function (spacing) {
92 var themeSpacing = theme.spacing(spacing);
93
94 if (themeSpacing === 0) {
95 return;
96 }
97
98 styles["spacing-".concat(breakpoint, "-").concat(spacing)] = {
99 margin: "-".concat(getOffset(themeSpacing, 2)),
100 width: "calc(100% + ".concat(getOffset(themeSpacing), ")"),
101 '& > $item': {
102 padding: getOffset(themeSpacing, 2)
103 }
104 };
105 });
106 return styles;
107} // Default CSS values
108// flex: '0 1 auto',
109// flexDirection: 'row',
110// alignItems: 'flex-start',
111// flexWrap: 'nowrap',
112// justifyContent: 'flex-start',
113
114
115var styles = function styles(theme) {
116 return (0, _extends2.default)({
117 /* Styles applied to the root element. */
118 root: {},
119
120 /* Styles applied to the root element if `container={true}`. */
121 container: {
122 boxSizing: 'border-box',
123 display: 'flex',
124 flexWrap: 'wrap',
125 width: '100%'
126 },
127
128 /* Styles applied to the root element if `item={true}`. */
129 item: {
130 boxSizing: 'border-box',
131 margin: '0' // For instance, it's useful when used with a `figure` element.
132
133 },
134
135 /* Styles applied to the root element if `zeroMinWidth={true}`. */
136 zeroMinWidth: {
137 minWidth: 0
138 },
139
140 /* Styles applied to the root element if `direction="column"`. */
141 'direction-xs-column': {
142 flexDirection: 'column'
143 },
144
145 /* Styles applied to the root element if `direction="column-reverse"`. */
146 'direction-xs-column-reverse': {
147 flexDirection: 'column-reverse'
148 },
149
150 /* Styles applied to the root element if `direction="row-reverse"`. */
151 'direction-xs-row-reverse': {
152 flexDirection: 'row-reverse'
153 },
154
155 /* Styles applied to the root element if `wrap="nowrap"`. */
156 'wrap-xs-nowrap': {
157 flexWrap: 'nowrap'
158 },
159
160 /* Styles applied to the root element if `wrap="reverse"`. */
161 'wrap-xs-wrap-reverse': {
162 flexWrap: 'wrap-reverse'
163 },
164
165 /* Styles applied to the root element if `alignItems="center"`. */
166 'align-items-xs-center': {
167 alignItems: 'center'
168 },
169
170 /* Styles applied to the root element if `alignItems="flex-start"`. */
171 'align-items-xs-flex-start': {
172 alignItems: 'flex-start'
173 },
174
175 /* Styles applied to the root element if `alignItems="flex-end"`. */
176 'align-items-xs-flex-end': {
177 alignItems: 'flex-end'
178 },
179
180 /* Styles applied to the root element if `alignItems="baseline"`. */
181 'align-items-xs-baseline': {
182 alignItems: 'baseline'
183 },
184
185 /* Styles applied to the root element if `alignContent="center"`. */
186 'align-content-xs-center': {
187 alignContent: 'center'
188 },
189
190 /* Styles applied to the root element if `alignContent="flex-start"`. */
191 'align-content-xs-flex-start': {
192 alignContent: 'flex-start'
193 },
194
195 /* Styles applied to the root element if `alignContent="flex-end"`. */
196 'align-content-xs-flex-end': {
197 alignContent: 'flex-end'
198 },
199
200 /* Styles applied to the root element if `alignContent="space-between"`. */
201 'align-content-xs-space-between': {
202 alignContent: 'space-between'
203 },
204
205 /* Styles applied to the root element if `alignContent="space-around"`. */
206 'align-content-xs-space-around': {
207 alignContent: 'space-around'
208 },
209
210 /* Styles applied to the root element if `justifyContent="center"`. */
211 'justify-content-xs-center': {
212 justifyContent: 'center'
213 },
214
215 /* Styles applied to the root element if `justifyContent="flex-end"`. */
216 'justify-content-xs-flex-end': {
217 justifyContent: 'flex-end'
218 },
219
220 /* Styles applied to the root element if `justifyContent="space-between"`. */
221 'justify-content-xs-space-between': {
222 justifyContent: 'space-between'
223 },
224
225 /* Styles applied to the root element if `justifyContent="space-around"`. */
226 'justify-content-xs-space-around': {
227 justifyContent: 'space-around'
228 },
229
230 /* Styles applied to the root element if `justifyContent="space-evenly"`. */
231 'justify-content-xs-space-evenly': {
232 justifyContent: 'space-evenly'
233 }
234 }, generateGutter(theme, 'xs'), theme.breakpoints.keys.reduce(function (accumulator, key) {
235 // Use side effect over immutability for better performance.
236 generateGrid(accumulator, theme, key);
237 return accumulator;
238 }, {}));
239};
240
241exports.styles = styles;
242var Grid = /*#__PURE__*/React.forwardRef(function Grid(props, ref) {
243 var _props$alignContent = props.alignContent,
244 alignContent = _props$alignContent === void 0 ? 'stretch' : _props$alignContent,
245 _props$alignItems = props.alignItems,
246 alignItems = _props$alignItems === void 0 ? 'stretch' : _props$alignItems,
247 classes = props.classes,
248 classNameProp = props.className,
249 _props$component = props.component,
250 Component = _props$component === void 0 ? 'div' : _props$component,
251 _props$container = props.container,
252 container = _props$container === void 0 ? false : _props$container,
253 _props$direction = props.direction,
254 direction = _props$direction === void 0 ? 'row' : _props$direction,
255 _props$item = props.item,
256 item = _props$item === void 0 ? false : _props$item,
257 justify = props.justify,
258 _props$justifyContent = props.justifyContent,
259 justifyContent = _props$justifyContent === void 0 ? 'flex-start' : _props$justifyContent,
260 _props$lg = props.lg,
261 lg = _props$lg === void 0 ? false : _props$lg,
262 _props$md = props.md,
263 md = _props$md === void 0 ? false : _props$md,
264 _props$sm = props.sm,
265 sm = _props$sm === void 0 ? false : _props$sm,
266 _props$spacing = props.spacing,
267 spacing = _props$spacing === void 0 ? 0 : _props$spacing,
268 _props$wrap = props.wrap,
269 wrap = _props$wrap === void 0 ? 'wrap' : _props$wrap,
270 _props$xl = props.xl,
271 xl = _props$xl === void 0 ? false : _props$xl,
272 _props$xs = props.xs,
273 xs = _props$xs === void 0 ? false : _props$xs,
274 _props$zeroMinWidth = props.zeroMinWidth,
275 zeroMinWidth = _props$zeroMinWidth === void 0 ? false : _props$zeroMinWidth,
276 other = (0, _objectWithoutProperties2.default)(props, ["alignContent", "alignItems", "classes", "className", "component", "container", "direction", "item", "justify", "justifyContent", "lg", "md", "sm", "spacing", "wrap", "xl", "xs", "zeroMinWidth"]);
277 var className = (0, _clsx.default)(classes.root, classNameProp, container && [classes.container, spacing !== 0 && classes["spacing-xs-".concat(String(spacing))]], item && classes.item, zeroMinWidth && classes.zeroMinWidth, direction !== 'row' && classes["direction-xs-".concat(String(direction))], wrap !== 'wrap' && classes["wrap-xs-".concat(String(wrap))], alignItems !== 'stretch' && classes["align-items-xs-".concat(String(alignItems))], alignContent !== 'stretch' && classes["align-content-xs-".concat(String(alignContent))], (justify || justifyContent) !== 'flex-start' && classes["justify-content-xs-".concat(String(justify || justifyContent))], xs !== false && classes["grid-xs-".concat(String(xs))], sm !== false && classes["grid-sm-".concat(String(sm))], md !== false && classes["grid-md-".concat(String(md))], lg !== false && classes["grid-lg-".concat(String(lg))], xl !== false && classes["grid-xl-".concat(String(xl))]);
278 return /*#__PURE__*/React.createElement(Component, (0, _extends2.default)({
279 className: className,
280 ref: ref
281 }, other));
282});
283process.env.NODE_ENV !== "production" ? Grid.propTypes = {
284 /**
285 * Defines the `align-content` style property.
286 * It's applied for all screen sizes.
287 */
288 alignContent: _propTypes.default.oneOf(['stretch', 'center', 'flex-start', 'flex-end', 'space-between', 'space-around']),
289
290 /**
291 * Defines the `align-items` style property.
292 * It's applied for all screen sizes.
293 */
294 alignItems: _propTypes.default.oneOf(['flex-start', 'center', 'flex-end', 'stretch', 'baseline']),
295
296 /**
297 * The content of the component.
298 */
299 children: _propTypes.default.node,
300
301 /**
302 * Override or extend the styles applied to the component.
303 * See [CSS API](#css) below for more details.
304 */
305 classes: _propTypes.default.object.isRequired,
306
307 /**
308 * @ignore
309 */
310 className: _propTypes.default.string,
311
312 /**
313 * The component used for the root node.
314 * Either a string to use a HTML element or a component.
315 */
316 component: _propTypes.default
317 /* @typescript-to-proptypes-ignore */
318 .elementType,
319
320 /**
321 * If `true`, the component will have the flex *container* behavior.
322 * You should be wrapping *items* with a *container*.
323 */
324 container: _propTypes.default.bool,
325
326 /**
327 * Defines the `flex-direction` style property.
328 * It is applied for all screen sizes.
329 */
330 direction: _propTypes.default.oneOf(['row', 'row-reverse', 'column', 'column-reverse']),
331
332 /**
333 * If `true`, the component will have the flex *item* behavior.
334 * You should be wrapping *items* with a *container*.
335 */
336 item: _propTypes.default.bool,
337
338 /**
339 * Defines the `justify-content` style property.
340 * It is applied for all screen sizes.
341 * @deprecated Use `justifyContent` instead, the prop was renamed
342 */
343 justify: (0, _deprecatedPropType.default)(_propTypes.default.oneOf(['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly']), 'Use `justifyContent` instead, the prop was renamed.'),
344
345 /**
346 * Defines the `justify-content` style property.
347 * It is applied for all screen sizes.
348 */
349 justifyContent: _propTypes.default.oneOf(['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly']),
350
351 /**
352 * Defines the number of grids the component is going to use.
353 * It's applied for the `lg` breakpoint and wider screens if not overridden.
354 */
355 lg: _propTypes.default.oneOf([false, 'auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
356
357 /**
358 * Defines the number of grids the component is going to use.
359 * It's applied for the `md` breakpoint and wider screens if not overridden.
360 */
361 md: _propTypes.default.oneOf([false, 'auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
362
363 /**
364 * Defines the number of grids the component is going to use.
365 * It's applied for the `sm` breakpoint and wider screens if not overridden.
366 */
367 sm: _propTypes.default.oneOf([false, 'auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
368
369 /**
370 * Defines the space between the type `item` component.
371 * It can only be used on a type `container` component.
372 */
373 spacing: _propTypes.default.oneOf(SPACINGS),
374
375 /**
376 * Defines the `flex-wrap` style property.
377 * It's applied for all screen sizes.
378 */
379 wrap: _propTypes.default.oneOf(['nowrap', 'wrap', 'wrap-reverse']),
380
381 /**
382 * Defines the number of grids the component is going to use.
383 * It's applied for the `xl` breakpoint and wider screens.
384 */
385 xl: _propTypes.default.oneOf([false, 'auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
386
387 /**
388 * Defines the number of grids the component is going to use.
389 * It's applied for all the screen sizes with the lowest priority.
390 */
391 xs: _propTypes.default.oneOf([false, 'auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
392
393 /**
394 * If `true`, it sets `min-width: 0` on the item.
395 * Refer to the limitations section of the documentation to better understand the use case.
396 */
397 zeroMinWidth: _propTypes.default.bool
398} : void 0;
399var StyledGrid = (0, _withStyles.default)(styles, {
400 name: 'MuiGrid'
401})(Grid);
402
403if (process.env.NODE_ENV !== 'production') {
404 var requireProp = (0, _requirePropFactory.default)('Grid');
405 StyledGrid.propTypes = (0, _extends2.default)({}, StyledGrid.propTypes, {
406 alignContent: requireProp('container'),
407 alignItems: requireProp('container'),
408 direction: requireProp('container'),
409 justifyContent: requireProp('container'),
410 lg: requireProp('item'),
411 md: requireProp('item'),
412 sm: requireProp('item'),
413 spacing: requireProp('container'),
414 wrap: requireProp('container'),
415 xs: requireProp('item'),
416 zeroMinWidth: requireProp('item')
417 });
418}
419
420var _default = StyledGrid;
421exports.default = _default;
\No newline at end of file