@onesy/ui-react
Version:
UI for React
156 lines (155 loc) • 6.82 kB
JavaScript
"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", "alignment", "paddingVertical", "paddingHorizontal", "fullWidth", "maxWidth", "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%'
},
// alignment
alignment_start: {
marginInlineEnd: 'auto'
},
alignment_center: {
marginInline: 'auto'
},
alignment_end: {
marginInlineStart: 'auto'
},
// padding
// vertical
padding_vertical_both: {
paddingBlock: theme.methods.space.value(2, 'px')
},
padding_vertical_start: {
paddingBlockStart: theme.methods.space.value(2, 'px')
},
padding_vertical_end: {
paddingBlockEnd: theme.methods.space.value(2, 'px')
},
padding_vertical_none: {
paddingBlock: '0px'
},
// horizontal
padding_horizontal_both: {
paddingInline: theme.methods.space.value(2, 'px')
},
padding_horizontal_start: {
paddingInlineStart: theme.methods.space.value(2, 'px')
},
padding_horizontal_end: {
paddingInlineEnd: theme.methods.space.value(2, 'px')
},
padding_horizontal_none: {
paddingInline: '0px'
},
// maxWidth
maxWidth_xxs: {
maxWidth: '320px'
},
maxWidth_xs: {
maxWidth: '400px'
},
maxWidth_sm: {
maxWidth: '600px'
},
maxWidth_rg: {
maxWidth: '960px'
},
maxWidth_lg: {
maxWidth: '1240px'
},
maxWidth_xl: {
maxWidth: '1920px'
},
maxWidth_unset: {
maxWidth: 'unset'
},
// full width
fullWidth: {
width: '100%',
maxWidth: 'unset'
}
}), {
name: 'onesy-Container'
});
const Container = 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.onesyContainer) === 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,
alignment: alignment_,
paddingVertical: paddingVertical_,
paddingHorizontal: paddingHorizontal_,
fullWidth: fullWidth_,
maxWidth: maxWidth_,
Component = 'div',
style,
className,
children
} = props,
other = (0, _objectWithoutProperties2.default)(props, _excluded);
const {
classes
} = useStyle();
const refs = {
root: _react.default.useRef(undefined)
};
const keys = _react.default.useMemo(() => {
const result = [];
const items = [alignment_, paddingVertical_, paddingHorizontal_, fullWidth_, maxWidth_];
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);
}, [alignment_, paddingVertical_, paddingHorizontal_, fullWidth_, maxWidth_]);
const breakpoints = {};
keys.forEach(key_1 => {
breakpoints[key_1] = (0, _useMediaQuery.default)(theme.breakpoints.media[key_1], {
element: refs.root.current
});
});
const alignment = (0, _utils2.valueBreakpoints)(alignment_, 'center', breakpoints, theme);
const paddingVertical = (0, _utils2.valueBreakpoints)(paddingVertical_, 'none', breakpoints, theme);
const paddingHorizontal = (0, _utils2.valueBreakpoints)(paddingHorizontal_, 'both', breakpoints, theme);
const fullWidth = (0, _utils2.valueBreakpoints)(fullWidth_, undefined, breakpoints, theme);
const maxWidth = (0, _utils2.valueBreakpoints)(maxWidth_, undefined, breakpoints, theme);
const styles = {
root: {}
};
if (!classes[`maxWidth_${maxWidth}`]) styles.root.maxWidth = maxWidth;
if (!['both', 'start', 'end', 'none'].includes(paddingHorizontal)) styles.root.paddingInline = paddingHorizontal;
if (!['both', 'start', 'end', 'none'].includes(paddingVertical)) styles.root.paddingBlock = paddingVertical;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, _objectSpread(_objectSpread({
ref: item_0 => {
if (ref) {
if ((0, _utils.is)('function', ref)) ref(item_0);else ref.current = item_0;
}
refs.root.current = item_0;
},
gap: 0,
direction: "row",
className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Container', theme) && ['onesy-Container-root'], className, classes.root, classes[`alignment_${alignment}`], classes[`maxWidth_${maxWidth}`], classes[`padding_vertical_${paddingVertical}`], classes[`padding_horizontal_${paddingHorizontal}`], fullWidth && classes.fullWidth]),
style: _objectSpread(_objectSpread({}, styles.root), style)
}, other), {}, {
children: children
}));
};
Container.displayName = 'onesy-Container';
var _default = exports.default = Container;