@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
330 lines (329 loc) • 11.5 kB
JavaScript
;
"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 _clsx = _interopRequireDefault(require("clsx"));
var _componentHelper = require("../../shared/component-helper.js");
var _Context = _interopRequireDefault(require("../../shared/Context.js"));
var _SpacingUtils = require("../space/SpacingUtils.js");
var _SkeletonHelper = require("../skeleton/SkeletonHelper.js");
var _IconHelpers = require("./IconHelpers.js");
var _withComponentMarkers = _interopRequireDefault(require("../../shared/helpers/withComponentMarkers.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 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
} = usePrepareIcon(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 (0, _jsxRuntime.jsx)("span", {
...wrapperParams,
children: (0, _jsxRuntime.jsx)(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 {
const iconFn = typeof icon === 'function' ? icon : _react.default.isValidElement(icon) && typeof icon.type === 'function' ? icon.type : null;
const hasHooks = iconFn ? /\buse[A-Z][A-Za-z0-9_]*\b/.test(iconFn.toString()) : false;
if (iconFn && !hasHooks) {
try {
var _elem$props;
const elem = iconFn();
const potentialSize = elem === null || elem === void 0 || (_elem$props = elem.props) === null || _elem$props === void 0 ? void 0 : _elem$props.width;
if (potentialSize && !isNaN(potentialSize)) {
sizeAsInt = potentialSize;
}
} catch {}
}
}
} 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 prepareIconCore(props, context, cachedValues) {
var _cachedValues$label;
const {
icon,
size,
width,
height,
border,
color,
inheritColor,
modifier,
alt,
title,
skeleton,
className,
...attributes
} = props;
const {
sizeAsString,
iconParams
} = cachedValues || calcSize({
icon,
size,
width,
height
});
if (color) {
iconParams.color = color;
}
const label = (_cachedValues$label = cachedValues === null || cachedValues === void 0 ? void 0 : cachedValues.label) !== null && _cachedValues$label !== void 0 ? _cachedValues$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'];
}
Object.assign(wrapperParams, (0, _SpacingUtils.applySpacing)(props, {
className: (0, _clsx.default)('dnb-icon', sizeAsString ? `dnb-icon--${sizeAsString}` : 'dnb-icon--default', (0, _SkeletonHelper.createSkeletonClass)(null, skeleton, context), className, modifier && `dnb-icon--${modifier}`, border && 'dnb-icon--border', inheritColor !== false && 'dnb-icon--inherit-color'),
style: wrapperParams.style
}));
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 usePrepareIcon(props, context) {
const {
icon,
size,
width,
height
} = props;
const cachedCalcSize = calcSize({
icon,
size,
width,
height
});
const label = (0, _react.useMemo)(() => icon ? getIconNameFromComponent(icon) : null, [icon]);
return (0, _react.useMemo)(() => prepareIconCore(props, context, {
...cachedCalcSize,
label
}), [props, context, cachedCalcSize, label]);
}
function prepareIcon(props, context) {
return prepareIconCore(props, context);
}
function prerenderIcon(props) {
const {
size = null,
listOfIcons = null,
alt = null
} = props;
let {
icon
} = props;
if (typeof icon === 'string' && /^data:image\//.test(icon)) {
return () => (0, _jsxRuntime.jsx)("img", {
src: String(icon),
alt: alt || ''
});
}
if (typeof icon === 'function') {
return props => {
const IconComponent = icon;
return (0, _jsxRuntime.jsx)(IconComponent, {
...props
});
};
}
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, _componentHelper.warn)(`Icon '${icon}' did not exist!`);
return null;
}
}
function getIcon(props) {
if (props.icon) {
return props.icon;
}
return (0, _componentHelper.processChildren)(props);
}
(0, _withComponentMarkers.default)(Icon, {
_supportsSpacingProps: true
});
//# sourceMappingURL=Icon.js.map