| 1 |
import { arrayToClassName, pxToRem } from 'elementor-app/utils/utils.js'; |
| 2 |
|
| 3 |
import './grid.scss'; |
| 4 |
|
| 5 |
export default function Grid( props ) { |
| 6 |
const propsMap = { |
| 7 |
direction: '--direction{{ -VALUE }}', |
| 8 |
justify: '--justify{{ -VALUE }}', |
| 9 |
alignContent: '--align-content{{ -VALUE }}', |
| 10 |
alignItems: '--align-items{{ -VALUE }}', |
| 11 |
container: '-container', |
| 12 |
item: '-item', |
| 13 |
noWrap: '-container--no-wrap', |
| 14 |
wrapReverse: '-container--wrap-reverse', |
| 15 |
zeroMinWidth: '-item--zero-min-width', |
| 16 |
spacing: '-container--spacing', |
| 17 |
xs: '-item-xs{{ -VALUE }}', |
| 18 |
sm: '-item-sm{{ -VALUE }}', |
| 19 |
md: '-item-md{{ -VALUE }}', |
| 20 |
lg: '-item-lg{{ -VALUE }}', |
| 21 |
xl: '-item-xl{{ -VALUE }}', |
| 22 |
xxl: '-item-xxl{{ -VALUE }}', |
| 23 |
}, |
| 24 |
getStyle = () => isValidPropValue( props.spacing ) ? { '--grid-spacing-gutter': pxToRem( props.spacing ) } : {}, |
| 25 |
classes = [ getBaseClassName(), props.className, ...getPropsClasses( propsMap, props ) ]; |
| 26 |
|
| 27 |
return ( |
| 28 |
<div style={ getStyle() } className={ arrayToClassName( classes ) }> |
| 29 |
{ props.children } |
| 30 |
</div> |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
function getPropsClasses( propsMap, props ) { |
| 35 |
const classes = []; |
| 36 |
|
| 37 |
for ( const prop in propsMap ) { |
| 38 |
if ( props[ prop ] ) { |
| 39 |
const propValue = isValidPropValue( props[ prop ] ) ? props[ prop ] : ''; |
| 40 |
|
| 41 |
classes.push( getBaseClassName() + renderPropValueBrackets( propsMap[ prop ], propValue ) ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return classes; |
| 46 |
} |
| 47 |
|
| 48 |
function renderPropValueBrackets( propClass, propValue ) { |
| 49 |
const brackets = propClass.match( /{{.*?}}/ ); |
| 50 |
|
| 51 |
if ( brackets ) { |
| 52 |
const bracketsValue = propValue ? brackets[ 0 ].replace( /[{ }]/g, '' ).replace( /value/i, propValue ) : ''; |
| 53 |
|
| 54 |
propClass = propClass.replace( brackets[ 0 ], bracketsValue ); |
| 55 |
} |
| 56 |
|
| 57 |
return propClass; |
| 58 |
} |
| 59 |
|
| 60 |
function getBaseClassName() { |
| 61 |
return 'eps-grid'; |
| 62 |
} |
| 63 |
|
| 64 |
function isValidPropValue( propValue ) { |
| 65 |
return propValue && 'boolean' !== typeof propValue; |
| 66 |
} |
| 67 |
|
| 68 |
Grid.propTypes = { |
| 69 |
className: PropTypes.string, |
| 70 |
direction: PropTypes.oneOf( [ 'row', 'column', 'row-reverse', 'column-reverse' ] ), |
| 71 |
justify: PropTypes.oneOf( [ 'start', 'center', 'end', 'space-between', 'space-evenly', 'space-around', 'stretch' ] ), |
| 72 |
alignContent: PropTypes.oneOf( [ 'start', 'center', 'end', 'space-between', 'stretch' ] ), |
| 73 |
alignItems: PropTypes.oneOf( [ 'start', 'center', 'end', 'baseline', 'stretch' ] ), |
| 74 |
container: PropTypes.bool, |
| 75 |
item: PropTypes.bool, |
| 76 |
noWrap: PropTypes.bool, |
| 77 |
wrapReverse: PropTypes.bool, |
| 78 |
zeroMinWidth: PropTypes.bool, |
| 79 |
spacing: PropTypes.number, |
| 80 |
xs: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 81 |
sm: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 82 |
md: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 83 |
lg: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 84 |
xl: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 85 |
xxl: PropTypes.oneOfType( [ PropTypes.number, PropTypes.bool ] ), |
| 86 |
children: PropTypes.any.isRequired, |
| 87 |
}; |
| 88 |
|
| 89 |
Grid.defaultProps = { |
| 90 |
className: '', |
| 91 |
}; |
| 92 |
|