@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
305 lines (304 loc) • 10.7 kB
JavaScript
"use client";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["sizeAsString"],
_excluded2 = ["icon", "size", "width", "height", "border", "color", "inheritColor", "inherit_color", "modifier", "alt", "title", "skeleton", "className"];
var _Fragment;
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 "core-js/modules/es.string.replace.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useContext } from 'react';
import classnames from 'classnames';
import { ErrorHandler } from '../../shared/error-helper';
import { validateDOMAttributes, processChildren, extendPropsWithContext } from '../../shared/component-helper';
import Context from '../../shared/Context';
import { createSpacingClasses } from '../space/SpacingHelper';
import { createSkeletonClass } from '../skeleton/SkeletonHelper';
import { iconCase } from './IconHelpers';
export const DefaultIconSize = 16;
export const DefaultIconSizes = {
default: 16,
medium: 24
};
export const ListDefaultIconSizes = [['default', 16], ['medium', 24]];
export const ValidIconType = ['small', 'default', 'medium', 'large', 'x-large', 'xx-large'];
export default function Icon(localProps) {
const context = useContext(Context);
const props = 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 _Fragment || (_Fragment = React.createElement(React.Fragment, null));
}
return React.createElement("span", wrapperParams, React.createElement(IconContainer, iconParams));
}
export function getIconNameFromComponent(icon) {
var _icon;
if (React.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;
}
export 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, _ListDefaultIconSizes2;
const lastPartOfIconName = nameParts.reverse()[0];
const potentialSize = (_ListDefaultIconSizes = ListDefaultIconSizes.filter(_ref => {
let [key] = _ref;
return key === lastPartOfIconName;
})) === null || _ListDefaultIconSizes === void 0 ? void 0 : (_ListDefaultIconSizes2 = _ListDefaultIconSizes[0]) === null || _ListDefaultIconSizes2 === void 0 ? void 0 : _ListDefaultIconSizes2[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 _ListDefaultIconSizes3, _ListDefaultIconSizes4, _ListDefaultIconSizes5;
sizeAsInt = (_ListDefaultIconSizes3 = (_ListDefaultIconSizes4 = ListDefaultIconSizes.filter(_ref2 => {
let [key] = _ref2;
return key === size;
})) === null || _ListDefaultIconSizes4 === void 0 ? void 0 : (_ListDefaultIconSizes5 = _ListDefaultIconSizes4[0]) === null || _ListDefaultIconSizes5 === void 0 ? void 0 : _ListDefaultIconSizes5[1]) !== null && _ListDefaultIconSizes3 !== void 0 ? _ListDefaultIconSizes3 : -1;
if (ValidIconType.includes(size)) {
sizeAsString = size;
}
} else if (parseFloat(String(size)) > 0) {
var _ListDefaultIconSizes6, _ListDefaultIconSizes7, _ListDefaultIconSizes8;
sizeAsInt = (_ListDefaultIconSizes6 = (_ListDefaultIconSizes7 = ListDefaultIconSizes.filter(_ref3 => {
let [key, value] = _ref3;
return key && value === parseFloat(String(size));
})) === null || _ListDefaultIconSizes7 === void 0 ? void 0 : (_ListDefaultIconSizes8 = _ListDefaultIconSizes7[0]) === null || _ListDefaultIconSizes8 === void 0 ? void 0 : _ListDefaultIconSizes8[1]) !== null && _ListDefaultIconSizes6 !== void 0 ? _ListDefaultIconSizes6 : -1;
if (sizeAsInt === -1) {
sizeAsInt = parseFloat(String(size));
sizeAsString = 'custom-size';
}
}
if (!sizeAsString && sizeAsInt > 0) {
const potentialSizeAsString = ListDefaultIconSizes.reduce((acc, _ref4) => {
let [key, value] = _ref4;
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(_ref5) {
let {
sizeAsString
} = _ref5,
rest = _objectWithoutProperties(_ref5, _excluded);
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));
}
validateDOMAttributes({}, params);
return {
params,
sizeAsString
};
}
export function prepareIcon(props, context) {
const {
icon,
size,
width,
height,
border,
color,
inheritColor,
inherit_color,
modifier,
alt,
title,
skeleton,
className
} = props,
attributes = _objectWithoutProperties(props, _excluded2);
const {
sizeAsString,
iconParams
} = calcSize({
icon,
size,
width,
height
});
if (color) {
iconParams.color = color;
}
const label = icon ? getIconNameFromComponent(icon) : null;
const wrapperParams = validateDOMAttributes(props, _objectSpread({
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 = classnames('dnb-icon', sizeAsString ? `dnb-icon--${sizeAsString}` : 'dnb-icon--default', createSkeletonClass(null, skeleton, context), 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.createElement(iconToRender, validateDOMAttributes({}, {
color,
icon,
size,
width,
height
}));
}
return _objectSpread(_objectSpread({}, props), {}, {
icon: iconToRender,
alt,
iconParams,
wrapperParams
});
}
export function prerenderIcon(props) {
const {
size = null,
listOfIcons = null,
alt = null
} = props;
let {
icon
} = props;
if (typeof icon === 'string' && /^data:image\//.test(icon)) {
return () => React.createElement("img", {
src: String(icon),
alt: alt || 'no-alt'
});
}
if (typeof icon === 'function') {
const elem = icon();
if (React.isValidElement(elem)) {
return icon;
}
return elem;
}
if (React.isValidElement(icon) || Array.isArray(icon)) {
return () => icon;
}
try {
icon = 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) {
ErrorHandler(`Icon '${icon}' did not exist!`);
return null;
}
}
function getIcon(props) {
if (props.icon) {
return props.icon;
}
return processChildren(props);
}
Icon._supportsSpacingProps = true;
//# sourceMappingURL=Icon.js.map