UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

285 lines (284 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.translateSpace = exports.sumTypes = exports.splitTypes = exports.spacingDefaultProps = exports.spacePatterns = exports.removeSpaceProps = exports.isValidSpaceProp = exports.isInline = exports.findTypeAll = exports.findType = exports.findNearestTypes = exports.createTypeModifiers = exports.createSpacingProperties = exports.createSpacingClasses = exports.calc = void 0; require("core-js/modules/es.string.replace.js"); require("core-js/modules/web.dom-collections.iterator.js"); var _componentHelper = require("../../shared/component-helper"); const _excluded = ["space", "innerSpace", "top", "bottom", "left", "right"]; 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); } const spacingDefaultProps = exports.spacingDefaultProps = { space: null, innerSpace: null, top: null, right: null, bottom: null, left: null }; const spacePatterns = exports.spacePatterns = { 'xx-small': 0.25, 'x-small': 0.5, small: 1, medium: 1.5, large: 2, 'x-large': 3, 'xx-large': 3.5, 'xx-large-x2': 7 }; globalThis.CALC_CACHE = {}; const calc = function () { for (var _len = arguments.length, types = new Array(_len), _key = 0; _key < _len; _key++) { types[_key] = arguments[_key]; } const hash = types.join('|'); if (globalThis.CALC_CACHE[hash]) { return globalThis.CALC_CACHE[hash]; } const result = []; types.forEach(rawTypes => { createTypeModifiers(rawTypes).forEach(type => { result.push(`var(--spacing-${type})`); }); }); return result.length ? globalThis.CALC_CACHE[hash] = `calc(${result.join(' + ')})` : null; }; exports.calc = calc; const createSpacingProperties = props => { if (props !== null && props !== void 0 && props.innerSpace) { return computeProperties(props.innerSpace); } return {}; }; exports.createSpacingProperties = createSpacingProperties; function hasMediaSize(media) { const keys = Object.keys(media); return keys.includes('small') || keys.includes('medium') || keys.includes('large'); } function hasSize(space) { const keys = Object.keys(space); return keys.includes('top') || keys.includes('right') || keys.includes('bottom') || keys.includes('left'); } function computeProperties(space) { if (!hasMediaSize(space)) { space = { small: space, medium: space, large: space }; } const result = {}; for (const size in space) { var _space; const value = (_space = space) === null || _space === void 0 ? void 0 : _space[size]; const props = transformToAll(value); for (const key in props) { if (isValidSpaceProp(key)) { const cur = props[key]; const name = `--space-${key[0]}-${size[0]}`; if (String(cur) === '0' || String(cur) === 'false') { result[name] = '0'; } else if (cur) { const typeModifiers = createTypeModifiers(cur); const sum = sumTypes(typeModifiers); result[name] = `${sum}rem`; } } } } return result; } function transformToAll(value) { let result = value; if (!hasSize(value)) { result = { top: value, right: value, bottom: value, left: value }; } return result; } const createSpacingClasses = function (props) { let Element = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; const p = Object.isFrozen(props) ? _objectSpread({}, props) : props; if (typeof p.space !== 'undefined') { if (typeof p.space === 'string' || typeof p.space === 'number' || typeof p.space === 'boolean' && p.space) { var _p$left, _p$bottom, _p$right, _p$top; p.left = (_p$left = p.left) !== null && _p$left !== void 0 ? _p$left : p.space; p.bottom = (_p$bottom = p.bottom) !== null && _p$bottom !== void 0 ? _p$bottom : p.space; p.right = (_p$right = p.right) !== null && _p$right !== void 0 ? _p$right : p.space; p.top = (_p$top = p.top) !== null && _p$top !== void 0 ? _p$top : p.space; } if (typeof p.space === 'object') { for (const i in p.space) { if (!p[i] && isValidSpaceProp(i)) { p[i] = p.space[i]; } } } delete p.space; } return Object.entries(p).reduce((acc, _ref) => { let [direction, cur] = _ref; if (isValidSpaceProp(direction) && direction !== 'innerSpace') { if (String(cur) === '0' || String(cur) === 'false') { acc.push(`dnb-space__${direction}--zero`); } else if (cur) { const typeModifiers = createTypeModifiers(cur); const sum = sumTypes(typeModifiers); if (sum > 10) { (0, _componentHelper.warn)(`Spacing of more than 10rem is not supported! You used ${sum} / (${typeModifiers.join(',')})`); } else { const nearestTypes = findNearestTypes(sum, true); acc = [...acc, ...nearestTypes.map(type => `dnb-space__${direction}--${type}`)]; } } } else if (direction === 'no_collapse') { acc.push('dnb-space--no-collapse'); if (Element && isInline(Element)) { acc.push('dnb-space--inline'); } } return acc; }, []); }; exports.createSpacingClasses = createSpacingClasses; const translateSpace = type => { if (/-x2$/.test(String(type))) { return spacePatterns[String(type).replace(/-x2$/, '')] * 2; } return spacePatterns[String(type)] || 0; }; exports.translateSpace = translateSpace; const splitTypes = types => { if (typeof types === 'string') { const test = types.split(/ /g); return clean(test); } else if (typeof types === 'boolean') { return [types ? 'small' : 0]; } else if (typeof types === 'number') { return [types]; } return clean(types) || null; function clean(t) { return t === null || t === void 0 ? void 0 : t.filter(r => r && String(r).length > 0); } }; exports.splitTypes = splitTypes; const sumTypes = types => splitTypes(types).map(type => translateSpace(type)).reduce((acc, cur) => { if (cur > 0) { acc += cur; } else if (cur < 0) { acc -= cur; } return acc; }, 0); exports.sumTypes = sumTypes; const createTypeModifiers = types => { return (splitTypes(types) || []).reduce((acc, type) => { if (type) { const firstLetter = String(type)[0]; if (parseFloat(firstLetter) > -1) { let num = parseFloat(String(type)); if (num >= 8 && /[0-9]px/.test(String(type))) { num = num / 16; } const foundType = findType(num); if (foundType) { type = foundType; } else { findNearestTypes(num).forEach(type => { if (type) { acc.push(type); } }); } } if (!(parseFloat(String(type)) > 0)) { acc.push(type); } } return acc; }, []); }; exports.createTypeModifiers = createTypeModifiers; const findType = num => { const found = findTypeAll(num); if (found) { return found[0]; } return null; }; exports.findType = findType; const findTypeAll = num => { const listOfSpacePatterns = Object.entries(spacePatterns); const found = listOfSpacePatterns.find(_ref2 => { let [k, v] = _ref2; return k && v === num; }) || null; return found; }; exports.findTypeAll = findTypeAll; const findNearestTypes = function (num) { let multiply = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; let res = []; const near = Object.entries(spacePatterns).reverse().filter(k => multiply ? true : !k[0].includes('-x')).find(_ref3 => { let [k, v] = _ref3; return k && num >= v; }); const nearNum = near && near[1] || num; const types = findTypeAll(nearNum); if (types) { const nearType = types[0]; res.push(nearType); const leftOver = num - parseFloat(String(types[1])); const foundMoreTypes = findNearestTypes(leftOver, multiply); foundMoreTypes.forEach(type => { const index = res.indexOf(type); if (index !== -1) { res[index] = multiply ? `${type}-x2` : type; } }); res = [...res, ...foundMoreTypes]; } return res; }; exports.findNearestTypes = findNearestTypes; const isValidSpaceProp = propName => propName && ['top', 'right', 'bottom', 'left', 'space', 'innerSpace'].includes(propName); exports.isValidSpaceProp = isValidSpaceProp; const removeSpaceProps = props => { const { space, innerSpace, top, bottom, left, right } = props, restProps = _objectWithoutProperties(props, _excluded); return restProps; }; exports.removeSpaceProps = removeSpaceProps; const isInline = elementName => { switch (elementName) { case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6': case 'p': return true; } return false; }; exports.isInline = isInline; //# sourceMappingURL=SpacingUtils.js.map