UNPKG

@atlaskit/page

Version:

A page layout organizes sections on a page using a grid and grid columns.

116 lines (111 loc) 3.15 kB
/* grid-column.tsx generated by @compiled/babel-plugin v0.39.1 */ 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. */ const varGridSpacing = '--ds-grid-spacing'; /** * The number of columns that a `GridColumn` covers. */ const 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. */ const singleColumnWidth = `(99.9999% / var(--ds-columns-num))`; const styles = { gridColumn: "_19pkidpf _2hwxwx3y _otyridpf _18u0wx3y _1ul9hrtj _16jlkb7n _1o9zidpf _slp31hna" }; const gridColumnWidthMapStyles = { auto: "_p12f1ho1 _i0dl1wug", bounded: "_p12fpg3g _i0dl1osq", fullWidth: "_p12f1ho1 _i0dl1osq" }; const getVariant = ({ medium, 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) */ const GridColumn = ({ medium = defaultMedium, children, testId }) => { const { columns } = useContext(GridContext); const contextValue = useMemo(() => ({ medium }), [medium]); /** * The real column span, * obtained by clamping the passed `medium` value within the allowed range. */ const colSpan = Math.max(1, Math.min(medium, columns)); /** * How we should calculate the column width. */ const variant = getVariant({ medium, columns }); return /*#__PURE__*/React.createElement(GridColumnContext.Provider, { value: contextValue }, /*#__PURE__*/React.createElement("div", { style: { /** * The 'auto' value here isn't actually consumed anywhere and is * just to better reflect what is happening when inspecting CSS. */ [varColumnSpan]: variant === ColumnVariant.Auto ? 'auto' : colSpan }, "data-testid": testId, className: ax([styles.gridColumn, gridColumnWidthMapStyles[variant]]) }, children)); }; export default GridColumn;