@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
231 lines (227 loc) • 9.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toFlexAlign = exports["default"] = exports.CellDecorator = exports.CellBase = exports.Cell = void 0;
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _ForwardRef = _interopRequireDefault(require("../ForwardRef"));
var _resolution = _interopRequireDefault(require("../resolution"));
var _LayoutModule = _interopRequireDefault(require("./Layout.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["component", "componentCss", "componentRef"],
_excluded2 = ["key"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
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) { _defineProperty(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; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var toFlexAlign = exports.toFlexAlign = function toFlexAlign(align) {
return align === 'end' && 'flex-end' || align === 'start' && 'flex-start' || align;
};
/**
* A stateless component that provides a space for your content in a
* {@link ui/Layout.Layout|Layout}, without {@link ui/Layout.CellDecorator|CellDecorator} applied.
*
* @class CellBase
* @memberof ui/Layout
* @ui
* @public
*/
var CellBase = exports.CellBase = (0, _kind["default"])({
name: 'Cell',
propTypes: /** @lends ui/Layout.CellBase.prototype */{
/**
* The alignment of `Cell`.
*
* Aligns this `Cell` vertically in the case of a horizontal layout or
* horizontally in the case of a vertical layout. `"start"`, `"center"` and
* `"end"` are the most commonly used, although all values of `align-self` are supported.
* `"start"` refers to the top in a horizontal layout, and left in a vertical LTR layout
* `"end"` refers to the bottom in a horizontal layout, and right in a vertical LTR layout
* `"start"` and `"end"` reverse places when in a vertical layout in a RTL locale.
*
* @type {String}
* @public
*/
align: _propTypes2["default"].string,
/**
* Any valid {@link /docs/developer-guide/glossary/#node|Node} that should be positioned in this `Cell`.
*
* @type {Any}
* @public
*/
children: _propTypes2["default"].any,
/**
* The type of component to use to render as the `Cell`. May be a DOM node name (e.g 'div',
* 'span', etc.) or a custom component.
*
* @type {String|Component}
* @default 'div'
* @public
*/
component: _propTypes["default"].renderable,
/**
* Customizes `Cell` together with `component` prop.
*
* When using {@link ui/Layout.Cell}, the `componentCss` prop is passed to the rendered component
* as `css`.
*
* @type {Object}
* @public
*/
componentCss: _propTypes2["default"].object,
/**
* Called with a reference to the root component.
*
* When using {@link ui/Layout.Cell}, the `ref` prop is forwarded to the rendered component
* as `componentRef`.
*
* @type {Object|Function}
* @public
*/
componentRef: _propTypes["default"].ref,
/**
* Sizes `Cell` to its container.
*
* A `grow`able cell will expand to its maximum size, according to the remaining space of the
* container. This is used when you want to grow the size of this Cell so that it fills the
* container. See the {@link ui/Layout.CellBase.size|size} property for more details.
*
* When combined with {@link ui/Layout.CellBase.shrink|shrink}, `shrink` prop takes precedence over
* `grow` prop and `grow` prop is simply ignored.
*
* @type {Boolean}
* @public
*/
grow: _propTypes2["default"].bool,
/**
* Sizes `Cell` to its contents.
*
* A `shrink`able cell will contract to its minimum size, according to the dimensions of its
* contents. This is used when you want the size of this Cell's content to influence the
* dimensions of this cell. `shrink` will not allow the contents of the Layout to be pushed
* beyond its boundaries (overflowing). See the {@link ui/Layout.CellBase.size|size} property
* for more details.
*
* @type {Boolean}
* @default false
* @public
*/
shrink: _propTypes2["default"].bool,
/**
* Sets the desired size of the Cell using any valid CSS measurement value.
*
* When used in conjunction with {@link ui/Layout.CellBase.shrink|shrink}, the size will be
* the maximum size, shrinking as necessary, to fit the content.
*
* When used in conjunction with {@link ui/Layout.CellBase.grow|grow}, the size will be the
* minimum size, growing as necessary, to fit the container.
*
* E.g.
* * `size="400px"` -> cell will be 400px, regardless of the dimensions of your content
* * `size="400px" shrink` -> cell will be 400px if your content is greater than 400px,
* and will match your contents size if it's smaller
* * `size="400px" grow` -> cell will be 400px if the container has no remaining space.
* Cell can grow larger than `size` to fill the container if there is remaining space
* in the container.
*
* This accepts any valid CSS measurement value string. If a numeric value is used, it will
* be treated as a pixel value and converted to a
* {@link ui/resolution.unit|relative unit} based on the rules of
* {@link ui/resolution|resolution independence}.
*
* @type {String|Number}
* @public
*/
size: _propTypes2["default"].oneOfType([_propTypes2["default"].string, _propTypes2["default"].number])
},
defaultProps: {
component: 'div',
shrink: false
},
styles: {
css: _LayoutModule["default"],
className: 'cell'
},
computed: {
className: function className(_ref) {
var grow = _ref.grow,
shrink = _ref.shrink,
size = _ref.size,
styler = _ref.styler;
return styler.append({
shrink: shrink,
grow: !shrink && (grow || !size),
size: size
});
},
style: function style(_ref2) {
var align = _ref2.align,
shrink = _ref2.shrink,
size = _ref2.size,
_style = _ref2.style;
if (typeof size === 'number') size = _resolution["default"].unit(_resolution["default"].scale(size), 'rem');
var cellSize = size;
if (!size) {
if (shrink) {
cellSize = '100%';
} else {
cellSize = 'none';
}
}
return _objectSpread(_objectSpread({}, _style), {}, {
alignSelf: toFlexAlign(align),
flexBasis: shrink ? null : size,
// Setting 100% below in the presence of `shrink`` and absence of `size` prevents overflow
'--cell-size': cellSize
});
}
},
render: function render(_ref3) {
var Component = _ref3.component,
componentCss = _ref3.componentCss,
componentRef = _ref3.componentRef,
rest = _objectWithoutProperties(_ref3, _excluded);
delete rest.align;
delete rest.grow;
delete rest.shrink;
delete rest.size;
var _rest = _objectSpread({}, rest),
key = _rest.key,
restProps = _objectWithoutProperties(_rest, _excluded2);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, _objectSpread({
css: componentCss,
ref: componentRef
}, restProps), key);
}
});
/**
* Applies Cell behaviors.
*
* @hoc
* @memberof ui/Layout
* @mixes ui/ForwardRef.ForwardRef
* @public
*/
var CellDecorator = exports.CellDecorator = (0, _ForwardRef["default"])({
prop: 'componentRef'
});
/**
* A stateless component that provides a space for your content in a
* {@link ui/Layout.Layout|Layout}.
*
* @class Cell
* @memberof ui/Layout
* @extends ui/Layout.CellBase
* @mixes ui/Layout.CellDecorator
* @omit componentRef
* @ui
* @public
*/
var Cell = exports.Cell = CellDecorator(CellBase);
var _default = exports["default"] = Cell;