@atlaskit/page
Version:
A page layout organizes sections on a page using a grid and grid columns.
111 lines (106 loc) • 3.3 kB
JavaScript
/* grid-column.tsx generated by @compiled/babel-plugin v0.39.1 */
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import "./grid-column.compiled.css";
import * as React from 'react';
import { ax, ix } from "@compiled/react/runtime";
import { useContext, useMemo } from 'react';
import { defaultMedium } from './constants';
import { GridColumnContext } from './grid-column-context';
import { GridContext } from './grid-context';
/**
* Determines the method by which column width is calculated.
*/
var ColumnVariant = /*#__PURE__*/function (ColumnVariant) {
/**
* Column occupies available space.
*
* Used when `medium` is 0 (the default).
*/
ColumnVariant["Auto"] = "auto";
/**
* Column occupies specified space.
*
* Used when 0 < `medium` < `columns`.
*/
ColumnVariant["Bounded"] = "bounded";
/**
* Column occupies entire row.
*
* Used when `medium` >= `columns`.
*
* This case is handled separately because of rounding.
*/
ColumnVariant["FullWidth"] = "fullWidth";
return ColumnVariant;
}(ColumnVariant || {});
/**
* The spacing (in `px`) between each column.
*/
var varGridSpacing = '--ds-grid-spacing';
/**
* The number of columns that a `GridColumn` covers.
*/
var varColumnSpan = '--ds-column-span';
/**
* IE11 and Edge both have rounding issues for flexbox which is why a width of
* 99.9999% is used. Using 100% here causes columns to wrap prematurely.
*/
var singleColumnWidth = "(99.9999% / var(--ds-columns-num))";
var styles = {
gridColumn: "_19pkidpf _2hwxwx3y _otyridpf _18u0wx3y _1ul9hrtj _16jlkb7n _1o9zidpf _slp31hna"
};
var gridColumnWidthMapStyles = {
auto: "_p12f1ho1 _i0dl1wug",
bounded: "_p12fpg3g _i0dl1osq",
fullWidth: "_p12f1ho1 _i0dl1osq"
};
var getVariant = function getVariant(_ref) {
var medium = _ref.medium,
columns = _ref.columns;
if (medium === defaultMedium) {
return ColumnVariant.Auto;
} else if (medium < columns) {
return ColumnVariant.Bounded;
}
return ColumnVariant.FullWidth;
};
/**
* __Grid column__
*
* A grid column can span one or more column positions within a grid.
*
* - [Examples](https://atlaskit.atlassian.com/packages/design-system/page)
*/
var GridColumn = function GridColumn(_ref2) {
var _ref2$medium = _ref2.medium,
medium = _ref2$medium === void 0 ? defaultMedium : _ref2$medium,
children = _ref2.children,
testId = _ref2.testId;
var _useContext = useContext(GridContext),
columns = _useContext.columns;
var contextValue = useMemo(function () {
return {
medium: medium
};
}, [medium]);
/**
* The real column span,
* obtained by clamping the passed `medium` value within the allowed range.
*/
var colSpan = Math.max(1, Math.min(medium, columns));
/**
* How we should calculate the column width.
*/
var variant = getVariant({
medium: medium,
columns: columns
});
return /*#__PURE__*/React.createElement(GridColumnContext.Provider, {
value: contextValue
}, /*#__PURE__*/React.createElement("div", {
style: _defineProperty({}, varColumnSpan, variant === ColumnVariant.Auto ? 'auto' : colSpan),
"data-testid": testId,
className: ax([styles.gridColumn, gridColumnWidthMapStyles[variant]])
}, children));
};
export default GridColumn;