@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
161 lines (160 loc) • 4.9 kB
JavaScript
;
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 = require("react");
var _clsx = require("clsx");
var _Space = _interopRequireDefault(require("../space/Space.js"));
var _SpacingUtils = require("../space/SpacingUtils.js");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 _elementProps$type;
if (!(0, _react.isValidElement)(element)) {
return undefined;
}
const elementProps = element.props || {};
return (_elementProps$type = elementProps?.[type]) !== null && _elementProps$type !== void 0 ? _elementProps$type : typeof elementProps?.space === 'object' ? elementProps.space[type] : undefined;
}
function isHeadingElement(element) {
return (0, _react.isValidElement)(element) && element?.type?._isHeadingElement === true;
}
function getSpaceVariant(element) {
if ((0, _react.isValidElement)(element)) {
var _element$props;
if (element?.type === _react.Fragment) {
return 'children';
}
const check = element?.type?._supportsSpacingProps;
if (typeof check !== 'undefined') {
return check;
}
const keys = ['space', 'top', 'right', 'bottom', 'left'];
const props = (_element$props = element?.props) !== null && _element$props !== void 0 ? _element$props : {};
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 === 'passthrough') {
const childElement = element;
const children = childElement?.props?.children;
const childKey = childElement?.key;
const childProps = childElement?.props || {};
return _react.Children.toArray(children).map((child, i) => {
const wrapped = wrapWithSpace({
element: child,
spaceProps,
wrapInSpace
});
return (0, _react.createElement)(childElement.type, {
...childProps,
key: childKey ? `${childKey}-${i}` : `${i}`
}, wrapped);
});
}
if (variant === 'children') {
return _react.Children.toArray(element).map(child => {
const children = child?.props?.children;
const childKey = child?.key;
const childProps = child?.props || {};
return _react.Children.toArray(children).map((element, i) => {
return (0, _react.createElement)(child.type, {
...childProps,
key: childKey ? `${childKey}-${i}` : i
}, wrapWithSpace({
element: element,
spaceProps,
wrapInSpace
}));
});
});
}
return wrapWithSpace({
element,
spaceProps,
variant,
wrapInSpace
});
}
function isIntrinsicElement(element) {
return (0, _react.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: _,
key,
...props
} = spaceProps;
if (resolvedVariant === true) {
return (0, _react.createElement)(element.type, {
...element.props,
key,
...props
});
}
if (resolvedVariant === 'children') {
return renderWithSpacing(element, spaceProps);
}
if (!wrapInSpace && isIntrinsicElement(element)) {
return cloneIntrinsicElementWithSpacing(element, spaceProps);
}
return (0, _jsxRuntime.jsx)(_Space.default, {
...props,
children: element
}, key);
}
function cloneIntrinsicElementWithSpacing(element, {
wrapInSpace,
className,
style,
...spaceProps
}) {
if (!(0, _react.isValidElement)(element)) {
return element;
}
const elementProps = element.props || {};
const spacing = (0, _SpacingUtils.createSpacing)(spaceProps);
return (0, _react.createElement)(element.type, {
...element.props,
key: spaceProps.key,
className: (0, _clsx.clsx)(elementProps?.className, ...spacing.className, className),
style: {
...elementProps?.style,
...spacing.style,
...style
}
});
}
//# sourceMappingURL=utils.js.map