UNPKG

@onesy/ui-react

Version:
298 lines (297 loc) 10.6 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 _Divider = _interopRequireDefault(require("../Divider")); var _useMediaQuery = _interopRequireDefault(require("../useMediaQuery")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["ref", "display", "align", "justify", "direction", "gap", "rowGap", "columnGap", "divider", "wrap", "flex", "flexNo", "fullWidth", "DividerProps", "Component", "className", "style", "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: { display: 'flex', boxSizing: 'border-box' }, // flex flex: { flex: '1 1 auto' }, // flexNo flexNo: { flex: '0 0 auto' }, // full width fullWidth: { width: '100%' }, // display display_inherit: { display: 'inherit' }, display_flex: { display: 'flex' }, display_inline_flex: { display: 'inline-flex' }, // wrap wrap_inherit: { flexWrap: 'inherit' }, wrap_nowrap: { flexWrap: 'nowrap' }, wrap_wrap: { flexWrap: 'wrap' }, 'wrap_wrap-reverse': { flexWrap: 'wrap-reverse' }, // direction direction_inherit: { flexDirection: 'inherit' }, direction_row: { flexDirection: 'row' }, 'direction_row-reverse': { flexDirection: 'row-reverse' }, direction_column: { flexDirection: 'column' }, 'direction_column-reverse': { flexDirection: 'column-reverse' }, // align 'align_inherit': { alignItems: 'inherit' }, 'align_initial': { alignItems: 'initial' }, 'align_flex-start': { alignItems: 'flex-start' }, align_center: { alignItems: 'center' }, 'align_flex-end': { alignItems: 'flex-end' }, 'align_baseline': { alignItems: 'baseline' }, 'align_stretch': { alignItems: 'stretch' }, // justify justify_inherit: { justifyContent: 'inherit' }, justify_initial: { justifyContent: 'initial' }, 'justify_flex-start': { justifyContent: 'flex-start' }, justify_center: { justifyContent: 'center' }, 'justify_flex-end': { justifyContent: 'flex-end' }, 'justify_space-around': { justifyContent: 'space-around' }, 'justify_space-between': { justifyContent: 'space-between' }, 'justify_space-evenly': { justifyContent: 'space-evenly' }, // 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-Line' }); const Line = 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.onesyLine) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const Divider = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Divider) || _Divider.default; const { ref, display: display_, align: align_, justify: justify_, direction: direction_, gap: gap_, rowGap: rowGap_, columnGap: columnGap_, divider: divider_, wrap: wrap_, flex: flex_, flexNo: flexNo_, fullWidth: fullWidth_, DividerProps, Component = 'div', className, style, 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 = [display_, align_, justify_, direction_, gap_, rowGap_, columnGap_, divider_, wrap_, flex_, flexNo_, fullWidth_]; 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); }, [display_, align_, justify_, direction_, gap_, rowGap_, columnGap_, divider_, wrap_, flex_, flexNo_, fullWidth_]); const breakpoints = {}; keys.forEach(key_1 => { breakpoints[key_1] = (0, _useMediaQuery.default)(theme.breakpoints.media[key_1], { element: refs.root.current }); }); const display = (0, _utils2.valueBreakpoints)(display_, 'flex', breakpoints, theme); const align = (0, _utils2.valueBreakpoints)(align_, 'flex-start', breakpoints, theme); const justify = (0, _utils2.valueBreakpoints)(justify_, 'flex-start', breakpoints, theme); const direction = (0, _utils2.valueBreakpoints)(direction_, 'column', breakpoints, theme); 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 divider = (0, _utils2.valueBreakpoints)(divider_, undefined, breakpoints, theme); const wrap = (0, _utils2.valueBreakpoints)(wrap_, undefined, breakpoints, theme); const flex = (0, _utils2.valueBreakpoints)(flex_, undefined, breakpoints, theme); const flexNo = (0, _utils2.valueBreakpoints)(flexNo_, undefined, breakpoints, theme); const fullWidth = (0, _utils2.valueBreakpoints)(fullWidth_, undefined, breakpoints, theme); const styles = { root: {}, divider: {} }; const valuesGaps = [0, 0.5, 1, 2, 3, 4, 8, 12, 16]; if (rowGap !== undefined || columnGap !== undefined) { if (rowGap !== undefined && !valuesGaps.includes(rowGap)) styles.root.rowGap = (0, _utils.is)('string', rowGap) ? rowGap : `${rowGap * theme.space.unit}px`; if (columnGap !== undefined && !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`; } const Divider_ = /*#__PURE__*/(0, _jsxRuntime.jsx)(Divider, _objectSpread({ flex: true, orientation: direction.includes('row') ? 'vertical' : 'horizontal' }, DividerProps)); return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _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; }, className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('Line', theme) && ['onesy-Line-root', `onesy-Line-direction-${direction}`], className, classes.root, classes[`display_${display.replace('-', '_')}`], classes[`wrap_${wrap}`], classes[`direction_${direction}`], classes[`align_${align}`], classes[`justify_${justify}`], classes[`gap_${String(gap).replace('.', '')}`], classes[`rowGap_${String(rowGap).replace('.', '')}`], classes[`columnGap_${String(columnGap).replace('.', '')}`], flex && classes.flex, flexNo && classes.flexNo, fullWidth && classes.fullWidth]), style: _objectSpread(_objectSpread({}, styles.root), style) }, other), {}, { children: _react.default.Children.toArray(children).flatMap((item_1, index) => !divider || index === children.length - 1 ? [item_1] : [item_1, Divider_]) })); }; Line.displayName = 'onesy-Line'; var _default = exports.default = Line;