Completely updated React, fixed #11, (hopefully)

This commit is contained in:
2018-03-04 19:11:49 -05:00
parent 6e0afd6e2a
commit 34e5f5139a
13674 changed files with 333464 additions and 473223 deletions

View File

@@ -1,4 +1,3 @@
// @flow
// A grid component using the following libs as inspiration.
//
// For the implementation:
@@ -11,13 +10,12 @@
// - https://css-tricks.com/snippets/css/a-guide-to-flexbox/
import React from 'react';
import type { ElementType, Node } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import withStyles from '../styles/withStyles';
import { keys as breakpointKeys } from '../styles/createBreakpoints';
import requirePropFactory from '../utils/requirePropFactory';
import Hidden from '../Hidden';
import type { HiddenProps } from '../Hidden/types';
const GUTTERS = [0, 8, 16, 24, 40];
const GRID_SIZES = [true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
@@ -39,11 +37,11 @@ function generateGrid(globalStyles, theme, breakpoint) {
}
// Only keep 6 significant numbers.
const width = `${Math.round(size / 12 * Math.pow(10, 6)) / Math.pow(10, 4)}%`;
const width = `${Math.round(size / 12 * 10e6) / 10e4}%`;
/* eslint-disable max-len */
// Close to the bootstrap implementation:
// https://github.com/twbs/bootstrap/blob/b0508a975d711d6b24c01f57dd5445c22699fac4/scss/mixins/_grid.scss#L69
// https://github.com/twbs/bootstrap/blob/8fccaa2439e97ec72a4b7dc42ccc1f649790adb0/scss/mixins/_grid.scss#L41
/* eslint-enable max-len */
styles[`grid-${breakpoint}-${size}`] = {
flexBasis: width,
@@ -86,7 +84,7 @@ function generateGutter(theme, breakpoint) {
// alignItems: 'flex-start',
// flexWrap: 'nowrap',
// justifyContent: 'flex-start',
export const styles = (theme: Object) => ({
export const styles = theme => ({
typeContainer: {
boxSizing: 'border-box',
display: 'flex',
@@ -98,6 +96,9 @@ export const styles = (theme: Object) => ({
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',
},
@@ -110,6 +111,9 @@ export const styles = (theme: Object) => ({
'wrap-xs-nowrap': {
flexWrap: 'nowrap',
},
'wrap-xs-wrap-reverse': {
flexWrap: 'wrap-reverse',
},
'align-items-xs-center': {
alignItems: 'center',
},
@@ -157,127 +161,26 @@ export const styles = (theme: Object) => ({
}, {}),
});
export type GridSizes = boolean | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type ProvidedProps = {
classes: Object,
component: ElementType,
};
export type Props = {
/**
* The content of the component.
*/
children?: Node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* @ignore
*/
className?: string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component?: ElementType,
/**
* If `true`, the component will have the flex *container* behavior.
* You should be wrapping *items* with a *container*.
*/
container?: boolean,
/**
* If `true`, the component will have the flex *item* behavior.
* You should be wrapping *items* with a *container*.
*/
item?: boolean,
/**
* Defines the `align-content` style property.
* It's applied for all screen sizes.
*/
alignContent?:
| 'stretch'
| 'center'
| 'flex-start'
| 'flex-end'
| 'space-between'
| 'space-around',
/**
* Defines the `align-items` style property.
* It's applied for all screen sizes.
*/
alignItems?: 'flex-start' | 'center' | 'flex-end' | 'stretch' | 'baseline',
/**
* Defines the `flex-direction` style property.
* It is applied for all screen sizes.
*/
direction?: 'row' | 'row-reverse' | 'column' | 'column-reverse',
/**
* Defines the space between the type `item` component.
* It can only be used on a type `container` component.
*/
spacing?: 0 | 8 | 16 | 24 | 40,
/**
* If provided, will wrap with [Hidden](/api/hidden) component and given properties.
*/
hidden?: HiddenProps,
/**
* Defines the `justify-content` style property.
* It is applied for all screen sizes.
*/
justify?: 'flex-start' | 'center' | 'flex-end' | 'space-between' | 'space-around',
/**
* Defines the `flex-wrap` style property.
* It's applied for all screen sizes.
*/
wrap?: 'nowrap' | 'wrap' | 'wrap-reverse',
/**
* Defines the number of grids the component is going to use.
* It's applied for all the screen sizes with the lowest priority.
*/
xs?: GridSizes,
/**
* 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?: GridSizes,
/**
* 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?: GridSizes,
/**
* 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?: GridSizes,
/**
* Defines the number of grids the component is going to use.
* It's applied for the `xl` breakpoint and wider screens.
*/
xl?: GridSizes,
};
function Grid(props: ProvidedProps & Props) {
function Grid(props) {
const {
classes,
className: classNameProp,
component: ComponentProp,
container,
item,
alignContent,
alignItems,
classes,
className: classNameProp,
component: Component,
container,
direction,
spacing,
hidden,
item,
justify,
wrap,
xs,
sm,
md,
lg,
md,
zeroMinWidth,
sm,
spacing,
wrap,
xl,
xs,
...other
} = props;
@@ -285,6 +188,7 @@ function Grid(props: ProvidedProps & Props) {
{
[classes.typeContainer]: container,
[classes.typeItem]: item,
[classes.zeroMinWidth]: zeroMinWidth,
[classes[`spacing-xs-${String(spacing)}`]]: container && spacing !== 0,
[classes[`direction-xs-${String(direction)}`]]: direction !== Grid.defaultProps.direction,
[classes[`wrap-xs-${String(wrap)}`]]: wrap !== Grid.defaultProps.wrap,
@@ -311,38 +215,138 @@ function Grid(props: ProvidedProps & Props) {
if (hidden) {
return (
<Hidden {...hidden}>
<ComponentProp {...gridProps} />
<Component {...gridProps} />
</Hidden>
);
}
return <ComponentProp {...gridProps} />;
return <Component {...gridProps} />;
}
Grid.propTypes = {
/**
* Defines the `align-content` style property.
* It's applied for all screen sizes.
*/
alignContent: PropTypes.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.oneOf(['flex-start', 'center', 'flex-end', 'stretch', 'baseline']),
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Useful to extend the style applied to components.
*/
classes: PropTypes.object.isRequired,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/**
* If `true`, the component will have the flex *container* behavior.
* You should be wrapping *items* with a *container*.
*/
container: PropTypes.bool,
/**
* Defines the `flex-direction` style property.
* It is applied for all screen sizes.
*/
direction: PropTypes.oneOf(['row', 'row-reverse', 'column', 'column-reverse']),
/**
* If provided, will wrap with [Hidden](/api/hidden) component and given properties.
*/
hidden: PropTypes.object,
/**
* If `true`, the component will have the flex *item* behavior.
* You should be wrapping *items* with a *container*.
*/
item: PropTypes.bool,
/**
* Defines the `justify-content` style property.
* It is applied for all screen sizes.
*/
justify: PropTypes.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.oneOf([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.oneOf([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.oneOf([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.oneOf(GUTTERS),
/**
* Defines the `flex-wrap` style property.
* It's applied for all screen sizes.
*/
wrap: PropTypes.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.oneOf([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.oneOf([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.bool,
};
Grid.defaultProps = {
alignContent: 'stretch',
alignItems: 'stretch',
component: 'div',
container: false,
direction: 'row',
hidden: undefined,
item: false,
justify: 'flex-start',
zeroMinWidth: false,
spacing: 16,
wrap: 'wrap',
};
// 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
let GridWrapper = Grid;
if (process.env.NODE_ENV !== 'production') {
GridWrapper = props => <Grid {...props} />;
const requireProp = requirePropFactory('Grid');
GridWrapper = (props: any) => <Grid {...props} />;
// $FlowFixMe - cannot mix legacy propTypes with current HOC pattern - https://github.com/facebook/flow/issues/4644#issuecomment-332530909
GridWrapper.propTypes = {
alignContent: requireProp('container'),
alignItems: requireProp('container'),
@@ -354,6 +358,7 @@ if (process.env.NODE_ENV !== 'production') {
spacing: requireProp('container'),
wrap: requireProp('container'),
xs: requireProp('item'),
zeroMinWidth: requireProp('zeroMinWidth'),
};
}