@atlaskit/page
Version:
A page layout organizes sections on a page using a grid and grid columns.
58 lines (56 loc) • 2.18 kB
JavaScript
import React, { useContext, useMemo } from 'react';
import { defaultGridColumns, defaultSpacing } from './constants';
import { Grid } from './grid';
import { GridColumnContext } from './grid-column';
import { GridContext } from './grid-context';
/**
* __Grid__
*
* A grid contains one or more `GridColumn` to provide a grid layout.
*
* - [Examples](https://atlaskit.atlassian.com/packages/design-system/page)
*/
const GridWrapper = ({
spacing: spacingProp,
columns: columnsProp,
layout,
testId,
children,
theme
}) => {
var _ref, _ref2, _theme$isNestedGrid;
/**
* isRoot is `true` only in the default context (i.e. no ancestor Grid).
*/
const {
isRoot
} = useContext(GridContext);
const {
medium
} = useContext(GridColumnContext);
/**
* The colspan of the current containing GridColumn provides
* the default amount of columns in a nested grid.
*/
const defaultColumns = medium > 0 ? medium : defaultGridColumns;
/**
* This is to account for the eventual removal of the `theme` prop. In theory, this should not be exposed.
* However, consumers are still using it - there should be a major rerelease with the complete removal of this prop later.
*/
const spacing = (_ref = spacingProp !== null && spacingProp !== void 0 ? spacingProp : theme === null || theme === void 0 ? void 0 : theme.spacing) !== null && _ref !== void 0 ? _ref : defaultSpacing;
const columns = (_ref2 = columnsProp !== null && columnsProp !== void 0 ? columnsProp : theme === null || theme === void 0 ? void 0 : theme.columns) !== null && _ref2 !== void 0 ? _ref2 : defaultColumns;
const isNested = (_theme$isNestedGrid = theme === null || theme === void 0 ? void 0 : theme.isNestedGrid) !== null && _theme$isNestedGrid !== void 0 ? _theme$isNestedGrid : !isRoot;
const contextValue = useMemo(() => ({
isRoot: false,
isNested,
spacing,
columns: columns
}), [spacing, columns, isNested]);
return /*#__PURE__*/React.createElement(GridContext.Provider, {
value: contextValue
}, /*#__PURE__*/React.createElement(Grid, {
layout: layout,
testId: testId
}, children));
};
export default GridWrapper;