UNPKG

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