UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

78 lines 4.02 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; 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; } import React, { Fragment } from 'react'; import Space from '../space/Space'; import { isValidSpaceProp, removeSpaceProps } from '../space/SpacingUtils'; export const omitSpacingProps = removeSpaceProps; export function pickSpacingProps(props) { const obj = {}; for (const key in props) { if (isValidSpaceProp(key) && typeof props[key] !== 'undefined') { obj[key] = props[key]; } } return obj; } export function getSpaceValue(type, element) { var _element$props$type, _element$props, _element$props2; if (!React.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; } export function isHeadingElement(element) { var _element$type; return React.isValidElement(element) && (element === null || element === void 0 ? void 0 : (_element$type = element.type) === null || _element$type === void 0 ? void 0 : _element$type['_isHeadingElement']) === true; } export function getSpaceVariant(element) { if (React.isValidElement(element)) { var _element$type2, _element$props3; if ((element === null || element === void 0 ? void 0 : element.type) === Fragment) { return 'children'; } const check = element === null || element === void 0 ? 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; } export function renderWithSpacing(element, spaceProps) { const variant = getSpaceVariant(element); if (variant === false) { return element; } if (variant === 'children') { return React.Children.toArray(element).map(child => { var _child$props; const children = child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.children; return React.Children.toArray(children).map((element, i) => { return React.cloneElement(child, _objectSpread({ key: i }, child === null || child === void 0 ? void 0 : child.props), wrapWithSpace({ element, spaceProps })); }); }); } return wrapWithSpace({ element, spaceProps, variant }); } function wrapWithSpace({ element, spaceProps, variant = null }) { return (variant !== null && variant !== void 0 ? variant : getSpaceVariant(element) === true) ? (React.cloneElement(element, spaceProps)) : getSpaceVariant(element) === 'children' ? renderWithSpacing(element, spaceProps) : React.createElement(Space, spaceProps, element); } //# sourceMappingURL=utils.js.map