UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

149 lines (148 loc) 5.81 kB
"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 _clsx = _interopRequireDefault(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 }; } 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 _elementProps$type; if (!_react.default.isValidElement(element)) { return undefined; } const elementProps = element.props || {}; return (_elementProps$type = elementProps === null || elementProps === void 0 ? void 0 : elementProps[type]) !== null && _elementProps$type !== void 0 ? _elementProps$type : typeof (elementProps === null || elementProps === void 0 ? void 0 : elementProps.space) === 'object' ? elementProps.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, _props; 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 = (_props = element === null || element === void 0 ? void 0 : element.props) !== null && _props !== void 0 ? _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 === '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; const { key: childKey, ...childProps } = (child === null || child === void 0 ? void 0 : child.props) || {}; return _react.default.Children.toArray(children).map((element, i) => { return _react.default.createElement(child.type, { key: childKey || i, ...childProps }, wrapWithSpace({ element: 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: _, key, ...props } = spaceProps; if (resolvedVariant === true) { return _react.default.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 (!_react.default.isValidElement(element)) { return element; } const elementProps = element.props || {}; const spacing = (0, _SpacingUtils.createSpacing)(spaceProps); return _react.default.createElement(element.type, { ...element.props, key: spaceProps.key, className: (0, _clsx.default)(elementProps === null || elementProps === void 0 ? void 0 : elementProps.className, ...spacing.className, className), style: { ...(elementProps === null || elementProps === void 0 ? void 0 : elementProps.style), ...spacing.style, ...style } }); } //# sourceMappingURL=utils.js.map