@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
131 lines (130 loc) • 4.17 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {};
exports.default = void 0;
var _hasOwn = _interopRequireDefault(require("core-js-pure/stable/object/has-own.js"));
var _react = require("react");
var _clsx = _interopRequireDefault(require("clsx"));
var _Context = _interopRequireDefault(require("../../shared/Context.js"));
var _componentHelper = require("../../shared/component-helper.js");
var _SpacingUtils = require("../space/SpacingUtils.js");
var _LogoSvg = require("./LogoSvg.js");
Object.keys(_LogoSvg).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _LogoSvg[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _LogoSvg[key];
}
});
});
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const defaultProps = {
inheritSize: false
};
function Logo(localProps) {
const context = (0, _react.useContext)(_Context.default);
const props = (0, _componentHelper.extendPropsWithContext)(localProps, defaultProps, context.Logo);
const {
width,
inheritSize,
height,
color,
inheritColor,
className: classNameProp,
svg: svgProp = _LogoSvg.DnbDefault,
...rest
} = props;
const theme = (0, _react.useMemo)(() => {
if (!(context !== null && context !== void 0 && context.theme)) {
return null;
}
const {
name,
size
} = context.theme;
return {
name,
size,
isUi: name === 'ui',
isSbanken: name === 'sbanken',
isEiendom: name === 'eiendom',
isCarnegie: name === 'carnegie'
};
}, [context === null || context === void 0 ? void 0 : context.theme]);
const svg = (0, _react.useMemo)(() => {
if ((0, _hasOwn.default)(svgProp, 'brand')) {
return svgProp;
}
if (theme && typeof svgProp === 'function' && svgProp.length === 1) {
return svgProp(theme);
}
return svgProp;
}, [svgProp, theme]);
const altText = (0, _react.useMemo)(() => {
const alt = svg === null || svg === void 0 ? void 0 : svg['alt'];
if (alt) {
return alt;
}
return 'logo';
}, [svg]);
const sharedClasses = classNameProp;
const detectedBrand = (0, _react.useMemo)(() => {
if ((0, _hasOwn.default)(svg, 'brand')) {
const brand = svg.brand;
return brand;
}
return (theme === null || theme === void 0 ? void 0 : theme.name) || 'ui';
}, [svg, theme]);
const className = (0, _react.useMemo)(() => {
return (0, _clsx.default)(`dnb-logo dnb-logo--${detectedBrand}`, sharedClasses, (parseFloat(width) > 0 || parseFloat(height) > 0) && `dnb-logo--has-size`, inheritSize && `dnb-logo--inherit-size`, inheritColor && `dnb-logo--inherit-color`);
}, [detectedBrand, sharedClasses, width, height, inheritSize, inheritColor]);
const rootParams = (0, _SpacingUtils.useSpacing)(props, {
role: 'img',
'aria-hidden': true,
className,
...rest,
style: rest.style,
alt: altText
});
const svgParams = (0, _react.useMemo)(() => {
return {
width,
height,
color,
alt: altText
};
}, [altText, color, height, width]);
const remainingDOMProps = (0, _componentHelper.validateDOMAttributes)(props, rootParams);
return (0, _jsxRuntime.jsx)("span", {
...remainingDOMProps,
children: renderCustomSvg(svg, svgParams, theme)
});
}
function renderCustomSvg(SvgComponent, svgParams, theme) {
if ((0, _react.isValidElement)(SvgComponent)) {
const allowedProps = {};
if (theme) {
for (const key in SvgComponent.props) {
if (!(key in theme)) {
allowedProps[key] = SvgComponent.props[key];
}
}
}
return (0, _react.createElement)(SvgComponent.type, {
...allowedProps,
...svgParams
});
}
return (0, _jsxRuntime.jsx)(SvgComponent, {
...svgParams
});
}
var _default = exports.default = Logo;
//# sourceMappingURL=Logo.js.map