@carbon/react
Version:
React components for the Carbon Design System
145 lines (137 loc) • 4.39 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var cx = require('classnames');
var PropTypes = require('prop-types');
var React = require('react');
var usePrefix = require('../../internal/usePrefix.js');
var GridContext = require('./GridContext.js');
// eslint-disable-next-line react/display-name -- https://github.com/carbon-design-system/carbon/issues/20452
const CSSGrid = /*#__PURE__*/React.forwardRef(({
align,
as,
children,
className: customClassName,
condensed = false,
fullWidth = false,
narrow = false,
...rest
}, ref) => {
const prefix = usePrefix.usePrefix();
const {
subgrid
} = GridContext.useGridSettings();
let mode = 'wide';
if (narrow) {
mode = 'narrow';
} else if (condensed) {
mode = 'condensed';
}
if (subgrid) {
return /*#__PURE__*/React.createElement(GridContext.GridSettings, {
mode: "css-grid",
subgrid: true
}, /*#__PURE__*/React.createElement(Subgrid, _rollupPluginBabelHelpers.extends({
ref: ref,
as: as,
className: customClassName,
mode: mode
}, rest), children));
}
const className = cx(customClassName, {
[`${prefix}--css-grid`]: true,
[`${prefix}--css-grid--condensed`]: mode === 'condensed',
[`${prefix}--css-grid--narrow`]: mode === 'narrow',
[`${prefix}--css-grid--full-width`]: fullWidth,
[`${prefix}--css-grid--start`]: align === 'start',
[`${prefix}--css-grid--end`]: align === 'end'
});
// cast as any to let TypeScript allow passing in attributes to base component
const BaseComponent = as || 'div';
return /*#__PURE__*/React.createElement(GridContext.GridSettings, {
mode: "css-grid",
subgrid: true
}, /*#__PURE__*/React.createElement(BaseComponent, _rollupPluginBabelHelpers.extends({
className: className,
ref: ref
}, rest), children));
});
CSSGrid.propTypes = {
/**
* Provide a custom element to render instead of the default <div>
*/
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
/**
* Specify grid alignment. Default is center
*/
align: PropTypes.oneOf(['start', 'center', 'end']),
/**
* Pass in content that will be rendered within the `Grid`
*/
children: PropTypes.node,
/**
* Specify a custom className to be applied to the `Grid`
*/
className: PropTypes.string,
/**
* Collapse the gutter to 1px. Useful for fluid layouts.
* Rows have 1px of margin between them to match gutter.
*/
condensed: PropTypes.bool,
/**
* Remove the default max width that the grid has set
*/
fullWidth: PropTypes.bool,
/**
* Container hangs 16px into the gutter. Useful for
* typographic alignment with and without containers.
*/
narrow: PropTypes.bool
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- https://github.com/carbon-design-system/carbon/issues/20452
// eslint-disable-next-line react/display-name -- https://github.com/carbon-design-system/carbon/issues/20452
const Subgrid = /*#__PURE__*/React.forwardRef(({
as,
className: customClassName,
children,
mode,
...rest
}, ref) => {
const prefix = usePrefix.usePrefix();
const className = cx(customClassName, {
[`${prefix}--subgrid`]: true,
[`${prefix}--subgrid--condensed`]: mode === 'condensed',
[`${prefix}--subgrid--narrow`]: mode === 'narrow',
[`${prefix}--subgrid--wide`]: mode === 'wide'
});
const BaseComponent = as || 'div';
return /*#__PURE__*/React.createElement(BaseComponent, _rollupPluginBabelHelpers.extends({}, rest, {
ref: ref,
className: className
}), children);
});
Subgrid.propTypes = {
/**
* Provide a custom element to render instead of the default <div>
*/
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
/**
* Pass in content that will be rendered within the `Subgrid`
*/
children: PropTypes.node,
/**
* Specify a custom className to be applied to the `Subgrid`
*/
className: PropTypes.string,
/**
* Specify the gutter mode for the subgrid
*/
mode: PropTypes.oneOf(['wide', 'narrow', 'condensed'])
};
const CSSGridComponent = CSSGrid;
exports.CSSGrid = CSSGridComponent;