UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

299 lines (298 loc) 10.5 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidIconType = exports.ListDefaultIconSizes = exports.DefaultIconSizes = exports.DefaultIconSize = void 0; exports.calcSize = calcSize; exports.default = Icon; exports.getIconNameFromComponent = getIconNameFromComponent; exports.prepareIcon = prepareIcon; exports.prerenderIcon = prerenderIcon; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); var _errorHelper = require("../../shared/error-helper.js"); var _componentHelper = require("../../shared/component-helper.js"); var _Context = _interopRequireDefault(require("../../shared/Context.js")); var _SpacingHelper = require("../space/SpacingHelper.js"); var _SkeletonHelper = require("../skeleton/SkeletonHelper.js"); var _IconHelpers = require("./IconHelpers.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 DefaultIconSize = exports.DefaultIconSize = 16; const DefaultIconSizes = exports.DefaultIconSizes = { default: 16, medium: 24 }; const ListDefaultIconSizes = exports.ListDefaultIconSizes = [['default', 16], ['medium', 24]]; const ValidIconType = exports.ValidIconType = ['small', 'default', 'medium', 'large', 'x-large', 'xx-large']; function Icon(localProps) { const context = (0, _react.useContext)(_Context.default); const props = (0, _componentHelper.extendPropsWithContext)(localProps, {}, { skeleton: context === null || context === void 0 ? void 0 : context.skeleton }, context.Icon); const { icon: iconProp, size, wrapperParams, iconParams, alt, children } = prepareIcon(props, context); const icon = iconProp !== null && iconProp !== void 0 ? iconProp : children; if (!icon) { return null; } const IconContainer = prerenderIcon({ icon, size, alt }); if (!IconContainer) { return null; } return _react.default.createElement("span", wrapperParams, _react.default.createElement(IconContainer, iconParams)); } function getIconNameFromComponent(icon) { var _icon; if (_react.default.isValidElement(icon) && (_icon = icon) !== null && _icon !== void 0 && _icon.type) { var _icon2; icon = (_icon2 = icon) === null || _icon2 === void 0 ? void 0 : _icon2.type; } const name = typeof icon === 'function' ? icon.name : String(icon); if (/^data:image\//.test(name)) { return null; } return name; } function calcSize(props) { const { icon, size, width, height } = props; let sizeAsInt = null; let sizeAsString = null; if (!size || size === DefaultIconSize) { const name = getIconNameFromComponent(icon); const nameParts = String(name || '').split('_'); if (nameParts.length > 1) { var _ListDefaultIconSizes; const lastPartOfIconName = nameParts.reverse()[0]; const potentialSize = (_ListDefaultIconSizes = ListDefaultIconSizes.filter(([key]) => key === lastPartOfIconName)) === null || _ListDefaultIconSizes === void 0 || (_ListDefaultIconSizes = _ListDefaultIconSizes[0]) === null || _ListDefaultIconSizes === void 0 ? void 0 : _ListDefaultIconSizes[1]; if (potentialSize) { sizeAsInt = potentialSize; } if (ValidIconType.includes(lastPartOfIconName)) { sizeAsString = lastPartOfIconName; } } else { if (typeof icon === 'function') { const elem = icon(); if (elem.props) { let potentialSize = null; if (elem.props.width) { potentialSize = elem.props.width; } if (!potentialSize && elem.props.viewBox) { potentialSize = parseFloat(/[0-9]+ [0-9]+ ([0-9]+)/.exec(elem.props.viewBox)[1]); } if (!isNaN(potentialSize)) { sizeAsInt = potentialSize; } } } } } else if (typeof size === 'string' && !(parseFloat(size) > 0)) { var _ListDefaultIconSizes2, _ListDefaultIconSizes3; sizeAsInt = (_ListDefaultIconSizes2 = (_ListDefaultIconSizes3 = ListDefaultIconSizes.filter(([key]) => key === size)) === null || _ListDefaultIconSizes3 === void 0 || (_ListDefaultIconSizes3 = _ListDefaultIconSizes3[0]) === null || _ListDefaultIconSizes3 === void 0 ? void 0 : _ListDefaultIconSizes3[1]) !== null && _ListDefaultIconSizes2 !== void 0 ? _ListDefaultIconSizes2 : -1; if (ValidIconType.includes(size)) { sizeAsString = size; } } else if (parseFloat(String(size)) > 0) { var _ListDefaultIconSizes4, _ListDefaultIconSizes5; sizeAsInt = (_ListDefaultIconSizes4 = (_ListDefaultIconSizes5 = ListDefaultIconSizes.filter(([key, value]) => key && value === parseFloat(String(size)))) === null || _ListDefaultIconSizes5 === void 0 || (_ListDefaultIconSizes5 = _ListDefaultIconSizes5[0]) === null || _ListDefaultIconSizes5 === void 0 ? void 0 : _ListDefaultIconSizes5[1]) !== null && _ListDefaultIconSizes4 !== void 0 ? _ListDefaultIconSizes4 : -1; if (sizeAsInt === -1) { sizeAsInt = parseFloat(String(size)); sizeAsString = 'custom-size'; } } if (!sizeAsString && sizeAsInt > 0) { const potentialSizeAsString = ListDefaultIconSizes.reduce((acc, [key, value]) => { if (key && value === sizeAsInt) { return key; } return acc; }, null); if (potentialSizeAsString) { sizeAsString = potentialSizeAsString; } } const { sizeAsString: isCustomSize, params: iconParams } = prepareIconParams({ sizeAsString, sizeAsInt, size, width, height }); if (isCustomSize) { sizeAsString = isCustomSize; } if (!(sizeAsInt > 0)) { sizeAsInt = DefaultIconSize; } if (size === 'auto') { iconParams.width = '100%'; iconParams.height = '100%'; sizeAsString = 'auto'; } return { iconParams, sizeAsInt, sizeAsString }; } function prepareIconParams({ sizeAsString, ...rest }) { const { size, width, height, sizeAsInt } = rest; const params = {}; if (!sizeAsString && !(sizeAsInt > 0) && parseFloat(String(size)) > -1) { params.width = params.height = parseFloat(String(size)); } else if (sizeAsString === 'custom-size') { params.width = params.height = parseFloat(String(sizeAsInt)); } if (parseFloat(String(width)) > -1) { sizeAsString = 'custom-size'; params.width = parseFloat(String(width)); } if (parseFloat(String(height)) > -1) { sizeAsString = 'custom-size'; params.height = parseFloat(String(height)); } (0, _componentHelper.validateDOMAttributes)({}, params); return { params, sizeAsString }; } function prepareIcon(props, context) { const { icon, size, width, height, border, color, inheritColor, inherit_color, modifier, alt, title, skeleton, className, ...attributes } = props; const { sizeAsString, iconParams } = calcSize({ icon, size, width, height }); if (color) { iconParams.color = color; } const label = icon ? getIconNameFromComponent(icon) : null; const wrapperParams = (0, _componentHelper.validateDOMAttributes)(props, { role: alt ? 'img' : 'presentation', alt, 'aria-label': label && !label.includes('default') ? label.replace(/_/g, ' ') + ' icon' : null, title, ...attributes }); if (!alt && typeof wrapperParams['aria-hidden'] === 'undefined') { wrapperParams['aria-hidden'] = true; } if (wrapperParams['aria-hidden']) { if (!wrapperParams['data-testid'] && typeof process !== 'undefined' && process.env.NODE_ENV === 'test') { wrapperParams['data-testid'] = wrapperParams['aria-label']; } delete wrapperParams['aria-label']; } wrapperParams.className = (0, _classnames.default)('dnb-icon', sizeAsString ? `dnb-icon--${sizeAsString}` : 'dnb-icon--default', (0, _SkeletonHelper.createSkeletonClass)(null, skeleton, context), (0, _SpacingHelper.createSpacingClasses)(props), className, modifier && `dnb-icon--${modifier}`, border && 'dnb-icon--border', (inheritColor !== null && inheritColor !== void 0 ? inheritColor : inherit_color) !== false && 'dnb-icon--inherit-color'); let iconToRender = getIcon(props); if (iconToRender && typeof iconToRender.defaultProps !== 'undefined') { iconToRender = _react.default.createElement(iconToRender, (0, _componentHelper.validateDOMAttributes)({}, { color, icon, size, width, height })); } return { ...props, icon: iconToRender, alt, iconParams, wrapperParams }; } function prerenderIcon(props) { const { size = null, listOfIcons = null, alt = null } = props; let { icon } = props; if (typeof icon === 'string' && /^data:image\//.test(icon)) { return () => _react.default.createElement("img", { src: String(icon), alt: alt || 'no-alt' }); } if (typeof icon === 'function') { const elem = icon(); if (_react.default.isValidElement(elem)) { return icon; } return elem; } if (_react.default.isValidElement(icon) || Array.isArray(icon)) { return () => icon; } try { icon = (0, _IconHelpers.iconCase)(icon); if (size && DefaultIconSizes[size] && size !== 'basis' && size !== 'default' && !(parseFloat(String(size)) > 0) && !icon.includes(size)) { icon = `${icon}_${size}`; } const mod = (listOfIcons.dnbIcons ? listOfIcons.dnbIcons : listOfIcons)[icon]; return mod && mod.default ? mod.default : mod; } catch (e) { (0, _errorHelper.ErrorHandler)(`Icon '${icon}' did not exist!`); return null; } } function getIcon(props) { if (props.icon) { return props.icon; } return (0, _componentHelper.processChildren)(props); } Icon._supportsSpacingProps = true; //# sourceMappingURL=Icon.js.map