UNPKG

@onesy/ui-react

Version:
233 lines (230 loc) 9.63 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _utils = require("@onesy/utils"); var _styleReact = require("@onesy/style-react"); var _Line = _interopRequireDefault(require("../Line")); var _useMediaQuery = _interopRequireDefault(require("../useMediaQuery")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["ref", "auto", "wrap", "columns", "gap", "rowGap", "columnGap", "parentGap", "parentRowGap", "parentColumnGap", "direction", "offsets", "values", "responsive", "RootProps", "Component", "style", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const useStyle = (0, _styleReact.style)(theme => ({ root: { width: '100%', flex: '0 1 auto' }, auto: { flex: '1 1 auto', width: 'auto' }, // gaps // gap gap_0: { gap: '0' }, 'gap_05': { gap: `${0.5 * theme.space.unit}px` }, 'gap_1': { gap: `${1 * theme.space.unit}px` }, 'gap_2': { gap: `${2 * theme.space.unit}px` }, 'gap_3': { gap: `${3 * theme.space.unit}px` }, 'gap_4': { gap: `${4 * theme.space.unit}px` }, 'gap_8': { gap: `${8 * theme.space.unit}px` }, 'gap_12': { gap: `${12 * theme.space.unit}px` }, 'gap_16': { gap: `${16 * theme.space.unit}px` }, // rowGap rowGap_0: { rowGap: '0' }, 'rowGap_05': { rowGap: `${0.5 * theme.space.unit}px` }, 'rowGap_1': { rowGap: `${1 * theme.space.unit}px` }, 'rowGap_2': { rowGap: `${2 * theme.space.unit}px` }, 'rowGap_3': { rowGap: `${3 * theme.space.unit}px` }, 'rowGap_4': { rowGap: `${4 * theme.space.unit}px` }, 'rowGap_8': { rowGap: `${8 * theme.space.unit}px` }, 'rowGap_12': { rowGap: `${12 * theme.space.unit}px` }, 'rowGap_16': { rowGap: `${16 * theme.space.unit}px` }, // columnGap columnGap_0: { columnGap: '0' }, 'columnGap_05': { columnGap: `${0.5 * theme.space.unit}px` }, 'columnGap_1': { columnGap: `${1 * theme.space.unit}px` }, 'columnGap_2': { columnGap: `${2 * theme.space.unit}px` }, 'columnGap_3': { columnGap: `${3 * theme.space.unit}px` }, 'columnGap_4': { columnGap: `${4 * theme.space.unit}px` }, 'columnGap_8': { columnGap: `${8 * theme.space.unit}px` }, 'columnGap_12': { columnGap: `${12 * theme.space.unit}px` }, 'columnGap_16': { columnGap: `${16 * theme.space.unit}px` } }), { name: 'onesy-Grid' }); const Grid = props_ => { var _theme$ui, _theme$ui2, _theme$elements; const theme = (0, _styleReact.useOnesyTheme)(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyGrid) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default; const { ref, auto, wrap, columns = 12, gap: gap_, rowGap: rowGap_, columnGap: columnGap_, parentGap, parentRowGap, parentColumnGap, direction: direction_, offsets, values, responsive: responsive_, RootProps, Component = 'div', style, className, children } = props, otherProps = (0, _objectWithoutProperties2.default)(props, _excluded); const other = otherProps; const { classes } = useStyle(); const refs = { root: _react.default.useRef(undefined), responsiveIFrame: _react.default.useRef({}) }; const keys = _react.default.useMemo(() => { const result = []; const items = [gap_, rowGap_, columnGap_, direction_, responsive_]; items.forEach(item => { if ((0, _utils.is)('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key_0 => result.push(key_0)); }); return (0, _utils.unique)(result); }, [gap_, rowGap_, columnGap_, direction_, responsive_]); const breakpoints = {}; keys.forEach(key_1 => { breakpoints[key_1] = (0, _useMediaQuery.default)(theme.breakpoints.media[key_1], { element: refs.root.current }); }); const gap = (0, _utils2.valueBreakpoints)(gap_, 2, breakpoints, theme); const rowGap = (0, _utils2.valueBreakpoints)(rowGap_, undefined, breakpoints, theme); const columnGap = (0, _utils2.valueBreakpoints)(columnGap_, undefined, breakpoints, theme); const direction = (0, _utils2.valueBreakpoints)(direction_, 'column', breakpoints, theme); const responsive = (0, _utils2.valueBreakpoints)(responsive_, undefined, breakpoints, theme); const responsiveValues = { mobile: (0, _useMediaQuery.default)(`(max-width: 767px)`, { element: refs.root.current }) }; const styles = { root: {}, divider: {} }; const valuesGaps = [0, 0.5, 1, 2, 3, 4, 8, 12, 16]; if (rowGap !== undefined || columnGap !== undefined) { if (!valuesGaps.includes(rowGap)) styles.root.rowGap = (0, _utils.is)('string', rowGap) ? rowGap : `${rowGap * theme.space.unit}px`; if (!valuesGaps.includes(columnGap)) styles.root.columnGap = (0, _utils.is)('string', columnGap) ? columnGap : `${columnGap * theme.space.unit}px`; } else { if (!valuesGaps.includes(gap)) styles.root.gap = (0, _utils.is)('string', gap) ? gap : `${gap * theme.space.unit}px`; } other.wrap = wrap; other.gap = gap; other.columnGap = columnGap; other.direction = direction; // Width // first smallest to largest // that is true const breakpoint = (0, _utils.is)('object', values) && ['xxs', 'xs', 'sm', 'md', 'lg', 'xl', 'xxl'].find(item_0 => !!values[item_0] && breakpoints[item_0]); let width = (values === null || values === void 0 ? void 0 : values[breakpoint]) || (values === null || values === void 0 ? void 0 : values.default) || (0, _utils.is)('number', values) && values || columns; // responsive // value provided, override const isResponsive = responsive && responsiveValues.mobile; const offset = (offsets === null || offsets === void 0 ? void 0 : offsets[breakpoint]) || (offsets === null || offsets === void 0 ? void 0 : offsets.default) || 0; const valueGap = (parentColumnGap !== undefined ? parentColumnGap : parentGap) || 0; const part = width / columns; const partGap = valueGap * theme.space.unit - valueGap * theme.space.unit * part; if (auto) width = undefined;else if (isResponsive) width = '100%';else if (width === columns) width = '100%';else width = `calc(${part * 100}% - ${partGap}px)`; if (isResponsive) { other.direction = 'column'; } styles.root.width = width; if (offset > 0) styles.root.marginInlineStart = `${offset / columns * 100}%`; return /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread(_objectSpread({ ref: item_1 => { if (ref) { if ((0, _utils.is)('function', ref)) ref(item_1);else ref.current = item_1; } refs.root.current = item_1; }, Component: Component, className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Grid', theme) && ['onesy-Grid-root'], className, classes.root, auto && classes.auto, classes[`gap_${String(gap).replace('.', '')}`], classes[`rowGap_${String(rowGap).replace('.', '')}`], classes[`columnGap_${String(columnGap).replace('.', '')}`]]), style: _objectSpread(_objectSpread({}, styles.root), style) }, other), RootProps), {}, { children: _react.default.Children.toArray(children).map(item_2 => { var _item_2$type; return /*#__PURE__*/_react.default.cloneElement(item_2, !((_item_2$type = item_2.type) !== null && _item_2$type !== void 0 && (_item_2$type = _item_2$type.displayName) !== null && _item_2$type !== void 0 && _item_2$type.includes('Grid')) ? {} : _objectSpread({}, other.direction === 'row' && { parentGap: gap, parentRowGap: rowGap, parentColumnGap: columnGap })); }) })); }; Grid.displayName = 'onesy-Grid'; var _default = exports.default = Grid;