@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
318 lines (317 loc) • 9.97 kB
JavaScript
"use client";
import React, { useContext, useMemo } from 'react';
import clsx from 'clsx';
import { warn, validateDOMAttributes, processChildren, extendPropsWithContext } from "../../shared/component-helper.js";
import Context from "../../shared/Context.js";
import { applySpacing } from "../space/SpacingUtils.js";
import { createSkeletonClass } from "../skeleton/SkeletonHelper.js";
import { iconCase } from "./IconHelpers.js";
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
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
} = 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 _jsx("span", {
...wrapperParams,
children: _jsx(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;
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.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));
}
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 = 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, applySpacing(props, {
className: clsx('dnb-icon', sizeAsString ? `dnb-icon--${sizeAsString}` : 'dnb-icon--default', 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.createElement(iconToRender, 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 = useMemo(() => icon ? getIconNameFromComponent(icon) : null, [icon]);
return useMemo(() => prepareIconCore(props, context, {
...cachedCalcSize,
label
}), [props, context, cachedCalcSize, label]);
}
export function prepareIcon(props, context) {
return prepareIconCore(props, context);
}
export function prerenderIcon(props) {
const {
size = null,
listOfIcons = null,
alt = null
} = props;
let {
icon
} = props;
if (typeof icon === 'string' && /^data:image\//.test(icon)) {
return () => _jsx("img", {
src: String(icon),
alt: alt || ''
});
}
if (typeof icon === 'function') {
return props => {
const IconComponent = icon;
return _jsx(IconComponent, {
...props
});
};
}
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) {
warn(`Icon '${icon}' did not exist!`);
return null;
}
}
function getIcon(props) {
if (props.icon) {
return props.icon;
}
return processChildren(props);
}
withComponentMarkers(Icon, {
_supportsSpacingProps: true
});
//# sourceMappingURL=Icon.js.map