@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
133 lines (132 loc) • 5.69 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSpaceValue = getSpaceValue;
exports.getSpaceVariant = getSpaceVariant;
exports.isHeadingElement = isHeadingElement;
exports.omitSpacingProps = void 0;
exports.pickSpacingProps = pickSpacingProps;
exports.renderWithSpacing = renderWithSpacing;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Space = _interopRequireDefault(require("../space/Space.js"));
var _SpacingUtils = require("../space/SpacingUtils.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const omitSpacingProps = exports.omitSpacingProps = _SpacingUtils.removeSpaceProps;
function pickSpacingProps(props) {
const obj = {};
for (const key in props) {
if ((0, _SpacingUtils.isValidSpaceProp)(key) && typeof props[key] !== 'undefined') {
obj[key] = props[key];
}
}
return obj;
}
function getSpaceValue(type, element) {
var _element$props$type, _element$props, _element$props2;
if (!_react.default.isValidElement(element)) {
return;
}
return (_element$props$type = (_element$props = element.props) === null || _element$props === void 0 ? void 0 : _element$props[type]) !== null && _element$props$type !== void 0 ? _element$props$type : typeof ((_element$props2 = element.props) === null || _element$props2 === void 0 ? void 0 : _element$props2.space) === 'object' ? element.props.space[type] : undefined;
}
function isHeadingElement(element) {
var _element$type;
return _react.default.isValidElement(element) && (element === null || element === void 0 || (_element$type = element.type) === null || _element$type === void 0 ? void 0 : _element$type['_isHeadingElement']) === true;
}
function getSpaceVariant(element) {
if (_react.default.isValidElement(element)) {
var _element$type2, _element$props3;
if ((element === null || element === void 0 ? void 0 : element.type) === _react.Fragment) {
return 'children';
}
const check = element === null || element === void 0 || (_element$type2 = element.type) === null || _element$type2 === void 0 ? void 0 : _element$type2['_supportsSpacingProps'];
if (typeof check !== 'undefined') {
return check;
}
const keys = ['space', 'top', 'right', 'bottom', 'left'];
const props = (_element$props3 = element === null || element === void 0 ? void 0 : element.props) !== null && _element$props3 !== void 0 ? _element$props3 : {};
if (keys.some(key => key in props)) {
return true;
}
}
return undefined;
}
function renderWithSpacing(element, spaceProps) {
const variant = getSpaceVariant(element);
const {
wrapInSpace = true
} = spaceProps;
if (variant === false) {
return element;
}
if (variant === 'children') {
return _react.default.Children.toArray(element).map(child => {
var _child$props;
const children = child === null || child === void 0 || (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.children;
return _react.default.Children.toArray(children).map((element, i) => {
return _react.default.cloneElement(child, {
key: i,
...(child === null || child === void 0 ? void 0 : child.props)
}, wrapWithSpace({
element,
spaceProps,
wrapInSpace
}));
});
});
}
return wrapWithSpace({
element,
spaceProps,
variant,
wrapInSpace
});
}
function isIntrinsicElement(element) {
return _react.default.isValidElement(element) && typeof element.type === 'string';
}
function wrapWithSpace({
element,
spaceProps,
variant = null,
wrapInSpace = true
}) {
const resolvedVariant = variant !== null && variant !== void 0 ? variant : getSpaceVariant(element);
const {
wrapInSpace: _,
...props
} = spaceProps;
if (resolvedVariant === true) {
return _react.default.cloneElement(element, props);
}
if (resolvedVariant === 'children') {
return renderWithSpacing(element, spaceProps);
}
if (!wrapInSpace && isIntrinsicElement(element)) {
return cloneIntrinsicElementWithSpacing(element, spaceProps);
}
return _react.default.createElement(_Space.default, props, element);
}
function cloneIntrinsicElementWithSpacing(element, {
wrapInSpace,
className,
style,
...spaceProps
}) {
var _element$props4, _element$props5;
if (!_react.default.isValidElement(element)) {
return element;
}
return _react.default.cloneElement(element, {
key: spaceProps.key,
className: (0, _classnames.default)((_element$props4 = element.props) === null || _element$props4 === void 0 ? void 0 : _element$props4.className, ...(0, _SpacingUtils.createSpacingClasses)(spaceProps), className),
style: {
...((_element$props5 = element.props) === null || _element$props5 === void 0 ? void 0 : _element$props5.style),
...(0, _SpacingUtils.createSpacingProperties)(spaceProps),
...style
}
});
}
//# sourceMappingURL=utils.js.map