378 lines
13 KiB
JavaScript
378 lines
13 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.styles = void 0;
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread"));
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var _createBreakpoints = require("../styles/createBreakpoints");
|
|
|
|
var _requirePropFactory = _interopRequireDefault(require("../utils/requirePropFactory"));
|
|
|
|
var _Hidden = _interopRequireDefault(require("../Hidden"));
|
|
|
|
// A grid component using the following libs as inspiration.
|
|
//
|
|
// For the implementation:
|
|
// - http://v4-alpha.getbootstrap.com/layout/flexbox-grid/
|
|
// - https://github.com/kristoferjoseph/flexboxgrid/blob/master/src/css/flexboxgrid.css
|
|
// - https://github.com/roylee0704/react-flexbox-grid
|
|
// - https://material.angularjs.org/latest/layout/introduction
|
|
//
|
|
// Follow this flexbox Guide to better understand the underlying model:
|
|
// - https://css-tricks.com/snippets/css/a-guide-to-flexbox/
|
|
var GUTTERS = [0, 8, 16, 24, 40];
|
|
var GRID_SIZES = [true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
|
|
|
function generateGrid(globalStyles, theme, breakpoint) {
|
|
// For the auto layouting
|
|
var styles = (0, _defineProperty2.default)({}, "grid-".concat(breakpoint), {
|
|
flexBasis: 0,
|
|
flexGrow: 1,
|
|
maxWidth: '100%'
|
|
});
|
|
GRID_SIZES.forEach(function (size) {
|
|
if (typeof size === 'boolean') {
|
|
// Skip the first one as handle above.
|
|
return;
|
|
} // Only keep 6 significant numbers.
|
|
|
|
|
|
var width = "".concat(Math.round(size / 12 * 10e6) / 10e4, "%");
|
|
/* eslint-disable max-len */
|
|
// Close to the bootstrap implementation:
|
|
// https://github.com/twbs/bootstrap/blob/8fccaa2439e97ec72a4b7dc42ccc1f649790adb0/scss/mixins/_grid.scss#L41
|
|
|
|
/* eslint-enable max-len */
|
|
|
|
styles["grid-".concat(breakpoint, "-").concat(size)] = {
|
|
flexBasis: width,
|
|
maxWidth: width
|
|
};
|
|
}); // No need for a media query for the first size.
|
|
|
|
if (breakpoint === 'xs') {
|
|
(0, _extends2.default)(globalStyles, styles);
|
|
} else {
|
|
globalStyles[theme.breakpoints.up(breakpoint)] = styles;
|
|
}
|
|
}
|
|
|
|
function generateGutter(theme, breakpoint) {
|
|
var styles = {};
|
|
GUTTERS.forEach(function (spacing, index) {
|
|
if (index === 0) {
|
|
// Skip the default style.
|
|
return;
|
|
}
|
|
|
|
styles["spacing-".concat(breakpoint, "-").concat(spacing)] = {
|
|
margin: -spacing / 2,
|
|
width: "calc(100% + ".concat(spacing, "px)"),
|
|
'& > $item': {
|
|
padding: spacing / 2
|
|
}
|
|
};
|
|
});
|
|
return styles;
|
|
} // Default CSS values
|
|
// flex: '0 1 auto',
|
|
// flexDirection: 'row',
|
|
// alignItems: 'flex-start',
|
|
// flexWrap: 'nowrap',
|
|
// justifyContent: 'flex-start',
|
|
|
|
|
|
var styles = function styles(theme) {
|
|
return (0, _objectSpread2.default)({
|
|
container: {
|
|
boxSizing: 'border-box',
|
|
display: 'flex',
|
|
flexWrap: 'wrap',
|
|
width: '100%'
|
|
},
|
|
item: {
|
|
boxSizing: 'border-box',
|
|
flex: '0 0 auto',
|
|
margin: '0' // For instance, it's useful when used with a `figure` element.
|
|
|
|
},
|
|
zeroMinWidth: {
|
|
minWidth: 0
|
|
},
|
|
'direction-xs-column': {
|
|
flexDirection: 'column'
|
|
},
|
|
'direction-xs-column-reverse': {
|
|
flexDirection: 'column-reverse'
|
|
},
|
|
'direction-xs-row-reverse': {
|
|
flexDirection: 'row-reverse'
|
|
},
|
|
'wrap-xs-nowrap': {
|
|
flexWrap: 'nowrap'
|
|
},
|
|
'wrap-xs-wrap-reverse': {
|
|
flexWrap: 'wrap-reverse'
|
|
},
|
|
'align-items-xs-center': {
|
|
alignItems: 'center'
|
|
},
|
|
'align-items-xs-flex-start': {
|
|
alignItems: 'flex-start'
|
|
},
|
|
'align-items-xs-flex-end': {
|
|
alignItems: 'flex-end'
|
|
},
|
|
'align-items-xs-baseline': {
|
|
alignItems: 'baseline'
|
|
},
|
|
'align-content-xs-center': {
|
|
alignContent: 'center'
|
|
},
|
|
'align-content-xs-flex-start': {
|
|
alignContent: 'flex-start'
|
|
},
|
|
'align-content-xs-flex-end': {
|
|
alignContent: 'flex-end'
|
|
},
|
|
'align-content-xs-space-between': {
|
|
alignContent: 'space-between'
|
|
},
|
|
'align-content-xs-space-around': {
|
|
alignContent: 'space-around'
|
|
},
|
|
'justify-xs-center': {
|
|
justifyContent: 'center'
|
|
},
|
|
'justify-xs-flex-end': {
|
|
justifyContent: 'flex-end'
|
|
},
|
|
'justify-xs-space-between': {
|
|
justifyContent: 'space-between'
|
|
},
|
|
'justify-xs-space-around': {
|
|
justifyContent: 'space-around'
|
|
}
|
|
}, generateGutter(theme, 'xs'), _createBreakpoints.keys.reduce(function (accumulator, key) {
|
|
// Use side effect over immutability for better performance.
|
|
generateGrid(accumulator, theme, key);
|
|
return accumulator;
|
|
}, {}));
|
|
};
|
|
|
|
exports.styles = styles;
|
|
|
|
function Grid(props) {
|
|
var _classNames;
|
|
|
|
var alignContent = props.alignContent,
|
|
alignItems = props.alignItems,
|
|
classes = props.classes,
|
|
classNameProp = props.className,
|
|
Component = props.component,
|
|
container = props.container,
|
|
direction = props.direction,
|
|
hidden = props.hidden,
|
|
item = props.item,
|
|
justify = props.justify,
|
|
lg = props.lg,
|
|
md = props.md,
|
|
sm = props.sm,
|
|
spacing = props.spacing,
|
|
wrap = props.wrap,
|
|
xl = props.xl,
|
|
xs = props.xs,
|
|
zeroMinWidth = props.zeroMinWidth,
|
|
other = (0, _objectWithoutProperties2.default)(props, ["alignContent", "alignItems", "classes", "className", "component", "container", "direction", "hidden", "item", "justify", "lg", "md", "sm", "spacing", "wrap", "xl", "xs", "zeroMinWidth"]);
|
|
var className = (0, _classnames.default)((_classNames = {}, (0, _defineProperty2.default)(_classNames, classes.container, container), (0, _defineProperty2.default)(_classNames, classes.item, item), (0, _defineProperty2.default)(_classNames, classes.zeroMinWidth, zeroMinWidth), (0, _defineProperty2.default)(_classNames, classes["spacing-xs-".concat(String(spacing))], container && spacing !== 0), (0, _defineProperty2.default)(_classNames, classes["direction-xs-".concat(String(direction))], direction !== Grid.defaultProps.direction), (0, _defineProperty2.default)(_classNames, classes["wrap-xs-".concat(String(wrap))], wrap !== Grid.defaultProps.wrap), (0, _defineProperty2.default)(_classNames, classes["align-items-xs-".concat(String(alignItems))], alignItems !== Grid.defaultProps.alignItems), (0, _defineProperty2.default)(_classNames, classes["align-content-xs-".concat(String(alignContent))], alignContent !== Grid.defaultProps.alignContent), (0, _defineProperty2.default)(_classNames, classes["justify-xs-".concat(String(justify))], justify !== Grid.defaultProps.justify), (0, _defineProperty2.default)(_classNames, classes['grid-xs'], xs === true), (0, _defineProperty2.default)(_classNames, classes["grid-xs-".concat(String(xs))], xs && xs !== true), (0, _defineProperty2.default)(_classNames, classes['grid-sm'], sm === true), (0, _defineProperty2.default)(_classNames, classes["grid-sm-".concat(String(sm))], sm && sm !== true), (0, _defineProperty2.default)(_classNames, classes['grid-md'], md === true), (0, _defineProperty2.default)(_classNames, classes["grid-md-".concat(String(md))], md && md !== true), (0, _defineProperty2.default)(_classNames, classes['grid-lg'], lg === true), (0, _defineProperty2.default)(_classNames, classes["grid-lg-".concat(String(lg))], lg && lg !== true), (0, _defineProperty2.default)(_classNames, classes['grid-xl'], xl === true), (0, _defineProperty2.default)(_classNames, classes["grid-xl-".concat(String(xl))], xl && xl !== true), _classNames), classNameProp);
|
|
var gridProps = (0, _objectSpread2.default)({
|
|
className: className
|
|
}, other);
|
|
|
|
if (hidden) {
|
|
return _react.default.createElement(_Hidden.default, hidden, _react.default.createElement(Component, gridProps));
|
|
}
|
|
|
|
return _react.default.createElement(Component, gridProps);
|
|
}
|
|
|
|
Grid.propTypes = process.env.NODE_ENV !== "production" ? {
|
|
/**
|
|
* Defines the `align-content` style property.
|
|
* It's applied for all screen sizes.
|
|
*/
|
|
alignContent: _propTypes.default.oneOf(['stretch', 'center', 'flex-start', 'flex-end', 'space-between', 'space-around']),
|
|
|
|
/**
|
|
* Defines the `align-items` style property.
|
|
* It's applied for all screen sizes.
|
|
*/
|
|
alignItems: _propTypes.default.oneOf(['flex-start', 'center', 'flex-end', 'stretch', 'baseline']),
|
|
|
|
/**
|
|
* The content of the component.
|
|
*/
|
|
children: _propTypes.default.node,
|
|
|
|
/**
|
|
* Useful to extend the style applied to components.
|
|
*/
|
|
classes: _propTypes.default.object.isRequired,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
|
|
/**
|
|
* The component used for the root node.
|
|
* Either a string to use a DOM element or a component.
|
|
*/
|
|
component: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.func]),
|
|
|
|
/**
|
|
* If `true`, the component will have the flex *container* behavior.
|
|
* You should be wrapping *items* with a *container*.
|
|
*/
|
|
container: _propTypes.default.bool,
|
|
|
|
/**
|
|
* Defines the `flex-direction` style property.
|
|
* It is applied for all screen sizes.
|
|
*/
|
|
direction: _propTypes.default.oneOf(['row', 'row-reverse', 'column', 'column-reverse']),
|
|
|
|
/**
|
|
* If provided, will wrap with [Hidden](/api/hidden) component and given properties.
|
|
*/
|
|
hidden: _propTypes.default.object,
|
|
|
|
/**
|
|
* If `true`, the component will have the flex *item* behavior.
|
|
* You should be wrapping *items* with a *container*.
|
|
*/
|
|
item: _propTypes.default.bool,
|
|
|
|
/**
|
|
* Defines the `justify-content` style property.
|
|
* It is applied for all screen sizes.
|
|
*/
|
|
justify: _propTypes.default.oneOf(['flex-start', 'center', 'flex-end', 'space-between', 'space-around']),
|
|
|
|
/**
|
|
* Defines the number of grids the component is going to use.
|
|
* It's applied for the `lg` breakpoint and wider screens if not overridden.
|
|
*/
|
|
lg: _propTypes.default.oneOf([false, true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
|
|
/**
|
|
* Defines the number of grids the component is going to use.
|
|
* It's applied for the `md` breakpoint and wider screens if not overridden.
|
|
*/
|
|
md: _propTypes.default.oneOf([false, true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
|
|
/**
|
|
* Defines the number of grids the component is going to use.
|
|
* It's applied for the `sm` breakpoint and wider screens if not overridden.
|
|
*/
|
|
sm: _propTypes.default.oneOf([false, true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
|
|
/**
|
|
* Defines the space between the type `item` component.
|
|
* It can only be used on a type `container` component.
|
|
*/
|
|
spacing: _propTypes.default.oneOf(GUTTERS),
|
|
|
|
/**
|
|
* Defines the `flex-wrap` style property.
|
|
* It's applied for all screen sizes.
|
|
*/
|
|
wrap: _propTypes.default.oneOf(['nowrap', 'wrap', 'wrap-reverse']),
|
|
|
|
/**
|
|
* Defines the number of grids the component is going to use.
|
|
* It's applied for the `xl` breakpoint and wider screens.
|
|
*/
|
|
xl: _propTypes.default.oneOf([false, true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
|
|
/**
|
|
* Defines the number of grids the component is going to use.
|
|
* It's applied for all the screen sizes with the lowest priority.
|
|
*/
|
|
xs: _propTypes.default.oneOf([false, true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
|
|
/**
|
|
* If `true`, it sets `min-width: 0` on the item.
|
|
* Refer to the limitations section of the documentation to better understand the use case.
|
|
*/
|
|
zeroMinWidth: _propTypes.default.bool
|
|
} : {};
|
|
Grid.defaultProps = {
|
|
alignContent: 'stretch',
|
|
alignItems: 'stretch',
|
|
component: 'div',
|
|
container: false,
|
|
direction: 'row',
|
|
item: false,
|
|
justify: 'flex-start',
|
|
lg: false,
|
|
md: false,
|
|
sm: false,
|
|
spacing: 0,
|
|
wrap: 'wrap',
|
|
xl: false,
|
|
xs: false,
|
|
zeroMinWidth: false
|
|
}; // Add a wrapper component to generate some helper messages in the development
|
|
// environment.
|
|
|
|
/* eslint-disable react/no-multi-comp */
|
|
// eslint-disable-next-line import/no-mutable-exports
|
|
|
|
var GridWrapper = Grid;
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
GridWrapper = function GridWrapper(props) {
|
|
return _react.default.createElement(Grid, props);
|
|
};
|
|
|
|
var requireProp = (0, _requirePropFactory.default)('Grid');
|
|
GridWrapper.propTypes = {
|
|
alignContent: requireProp('container'),
|
|
alignItems: requireProp('container'),
|
|
direction: requireProp('container'),
|
|
justify: requireProp('container'),
|
|
lg: requireProp('item'),
|
|
md: requireProp('item'),
|
|
sm: requireProp('item'),
|
|
spacing: requireProp('container'),
|
|
wrap: requireProp('container'),
|
|
xs: requireProp('item'),
|
|
zeroMinWidth: requireProp('zeroMinWidth')
|
|
};
|
|
}
|
|
|
|
var _default = (0, _withStyles.default)(styles, {
|
|
name: 'MuiGrid'
|
|
})(GridWrapper);
|
|
|
|
exports.default = _default; |