@atlaskit/page
Version:
A page layout organizes sections on a page using a grid and grid columns.
57 lines (55 loc) • 2.38 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)
*/
var GridWrapper = function GridWrapper(_ref) {
var _ref2, _ref3, _theme$isNestedGrid;
var spacingProp = _ref.spacing,
columnsProp = _ref.columns,
layout = _ref.layout,
testId = _ref.testId,
children = _ref.children,
theme = _ref.theme;
/**
* isRoot is `true` only in the default context (i.e. no ancestor Grid).
*/
var _useContext = useContext(GridContext),
isRoot = _useContext.isRoot;
var _useContext2 = useContext(GridColumnContext),
medium = _useContext2.medium;
/**
* The colspan of the current containing GridColumn provides
* the default amount of columns in a nested grid.
*/
var 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.
*/
var spacing = (_ref2 = spacingProp !== null && spacingProp !== void 0 ? spacingProp : theme === null || theme === void 0 ? void 0 : theme.spacing) !== null && _ref2 !== void 0 ? _ref2 : defaultSpacing;
var columns = (_ref3 = columnsProp !== null && columnsProp !== void 0 ? columnsProp : theme === null || theme === void 0 ? void 0 : theme.columns) !== null && _ref3 !== void 0 ? _ref3 : defaultColumns;
var isNested = (_theme$isNestedGrid = theme === null || theme === void 0 ? void 0 : theme.isNestedGrid) !== null && _theme$isNestedGrid !== void 0 ? _theme$isNestedGrid : !isRoot;
var contextValue = useMemo(function () {
return {
isRoot: false,
isNested: isNested,
spacing: 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;