| 1 |
import styles from '@patternfly/react-styles/css/layouts/Grid/grid'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import { DeviceSizes } from '../../styles/sizes'; |
| 4 |
import * as gridToken from '@patternfly/react-tokens/dist/esm/l_grid_item_Order'; |
| 5 |
|
| 6 |
import { setBreakpointCssVars } from '../../helpers/util'; |
| 7 |
|
| 8 |
export type gridItemSpanValueShape = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; |
| 9 |
|
| 10 |
export interface GridProps extends React.HTMLProps<HTMLDivElement> { |
| 11 |
/** content rendered inside the Grid layout */ |
| 12 |
children?: React.ReactNode; |
| 13 |
/** additional classes added to the Grid layout */ |
| 14 |
className?: string; |
| 15 |
/** Adds space between children. */ |
| 16 |
hasGutter?: boolean; |
| 17 |
/** The number of rows a column in the grid should span. Value should be a number 1-12 */ |
| 18 |
span?: gridItemSpanValueShape; |
| 19 |
/** the number of columns all grid items should span on a small device */ |
| 20 |
sm?: gridItemSpanValueShape; |
| 21 |
/** the number of columns all grid items should span on a medium device */ |
| 22 |
md?: gridItemSpanValueShape; |
| 23 |
/** the number of columns all grid items should span on a large device */ |
| 24 |
lg?: gridItemSpanValueShape; |
| 25 |
/** the number of columns all grid items should span on a xLarge device */ |
| 26 |
xl?: gridItemSpanValueShape; |
| 27 |
/** the number of columns all grid items should span on a 2xLarge device */ |
| 28 |
xl2?: gridItemSpanValueShape; |
| 29 |
/** Modifies the flex layout element order property */ |
| 30 |
order?: { |
| 31 |
default?: string; |
| 32 |
md?: string; |
| 33 |
lg?: string; |
| 34 |
xl?: string; |
| 35 |
'2xl'?: string; |
| 36 |
}; |
| 37 |
/** Sets the base component to render. defaults to div */ |
| 38 |
component?: React.ElementType<any> | React.ComponentType<any>; |
| 39 |
} |
| 40 |
|
| 41 |
export const Grid: React.FunctionComponent<GridProps> = ({ |
| 42 |
children = null, |
| 43 |
className = '', |
| 44 |
component = 'div', |
| 45 |
hasGutter, |
| 46 |
span = null, |
| 47 |
order, |
| 48 |
style, |
| 49 |
...props |
| 50 |
}: GridProps) => { |
| 51 |
const classes = [styles.grid, span && styles.modifiers[`all_${span}Col` as keyof typeof styles.modifiers]]; |
| 52 |
const Component: any = component; |
| 53 |
|
| 54 |
Object.entries(DeviceSizes).forEach(([propKey, gridSpanModifier]) => { |
| 55 |
const key = propKey as keyof typeof DeviceSizes; |
| 56 |
const propValue = props[key] as gridItemSpanValueShape; |
| 57 |
if (propValue) { |
| 58 |
classes.push(styles.modifiers[`all_${propValue}ColOn${gridSpanModifier}` as keyof typeof styles.modifiers]); |
| 59 |
} |
| 60 |
delete props[key]; |
| 61 |
}); |
| 62 |
|
| 63 |
return ( |
| 64 |
<Component |
| 65 |
className={css(...classes, hasGutter && styles.modifiers.gutter, className)} |
| 66 |
style={ |
| 67 |
style || order ? { ...style, ...setBreakpointCssVars(order, gridToken.l_grid_item_Order.name) } : undefined |
| 68 |
} |
| 69 |
{...props} |
| 70 |
> |
| 71 |
{children} |
| 72 |
</Component> |
| 73 |
); |
| 74 |
}; |
| 75 |
Grid.displayName = 'Grid'; |