@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
134 lines • 7.1 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Avatar = exports.defaultProps = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const react_1 = __importDefault(require("react"));
const defaultImports_1 = require("../_common/defaultImports");
const icons_1 = require("../icons");
const typography_1 = require("../typography");
function circleSizeFactory(size) {
switch (size) {
case 'small':
return {
cxAndCy: '4',
diameter: '8',
r: '2.75',
rUnknown: '8',
rUnknownInner: '5',
};
case 'medium':
return {
cxAndCy: '5',
diameter: '10',
r: '3.75',
rUnknown: '4.5',
rUnknownInner: '2',
};
case 'large':
return {
cxAndCy: '6',
diameter: '12',
r: '5',
rUnknown: '6',
rUnknownInner: '3',
};
case 'x-large':
default:
return {
cxAndCy: '8',
diameter: '16',
r: '7',
rUnknown: '8',
rUnknownInner: '5',
};
}
}
function StatusIndicator({ size, status, }) {
const { cxAndCy, diameter, r, rUnknown, rUnknownInner } = circleSizeFactory(size);
const statusClasses = `ndl-avatar-status ndl-avatar-status-${size}`;
const svgCircleClasses = (0, defaultImports_1.classNames)(`ndl-avatar-status-svg-${size}`, {
[`ndl-avatar-status-svg-unknown-${size}`]: status === 'unknown',
[`ndl-avatar-status-svg-${status}`]: status !== 'unknown',
});
return ((0, jsx_runtime_1.jsx)("div", { "data-testid": "ndl-avatar-status", className: statusClasses, title: status.charAt(0).toUpperCase() + status.slice(1), role: "presentation", children: (0, jsx_runtime_1.jsxs)("svg", { xmlns: "http://www.w3.org/2000/svg", height: diameter, width: diameter, viewBox: `0 0 ${diameter} ${diameter}`, fill: "none", children: [status === 'unknown' && size !== 'small' && ((0, jsx_runtime_1.jsx)("circle", { cx: cxAndCy, cy: cxAndCy, r: rUnknown, className: "ndl-avatar-status-svg-unknown-inner" })), (0, jsx_runtime_1.jsx)("circle", { cx: cxAndCy, cy: cxAndCy, r: status === 'unknown' && size !== 'small' ? rUnknownInner : r, className: svgCircleClasses })] }) }));
}
exports.defaultProps = {
as: 'div',
hasStatus: false,
isDisabled: false,
name: undefined,
shape: 'circle',
size: 'medium',
source: undefined,
status: undefined,
type: 'image',
};
exports.Avatar = react_1.default.forwardRef(function Avatar(_a, ref) {
var { as = exports.defaultProps.as, hasStatus = exports.defaultProps.hasStatus, isDisabled = exports.defaultProps.isDisabled, name = exports.defaultProps.name, shape = exports.defaultProps.shape, size = exports.defaultProps.size, source = exports.defaultProps.source, status = exports.defaultProps.status, type: initialType = exports.defaultProps.type, className, style, htmlAttributes } = _a, restProps = __rest(_a, ["as", "hasStatus", "isDisabled", "name", "shape", "size", "source", "status", "type", "className", "style", "htmlAttributes"]);
// If source is not provided, fallback to icon
const type = initialType === 'image' && !source ? 'icon' : initialType;
const Component = as || exports.defaultProps.as;
const isButton = Component === 'button';
const classes = (0, defaultImports_1.classNames)(`ndl-avatar ndl-avatar-${shape} ndl-avatar-${size}`, className);
const shapeClasses = (0, defaultImports_1.classNames)(`ndl-avatar-shape ndl-avatar-${shape} ndl-avatar-${shape}-${size}`, {
'ndl-avatar-disabled': isDisabled,
});
const typeClass = `ndl-avatar-${type}`;
const typeSizeClass = `ndl-avatar-${type}-${size}`;
const renderImageType = () => ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("img", { "data-testid": "ndl-avatar-image", src: source, alt: name }), (0, jsx_runtime_1.jsx)("div", { "data-testid": "ndl-avatar-image-overlay", className: `${shapeClasses} ndl-avatar-image-overlay` })] }));
const renderIconType = () => ((0, jsx_runtime_1.jsx)(icons_1.UserIconOutline, { "data-testid": "ndl-avatar-icon", className: typeSizeClass }));
const renderLettersType = () => {
const typographyVariant = size === 'small'
? 'body-small'
: size === 'large' || size === 'x-large'
? 'subheading-small'
: 'body-medium';
return ((0, jsx_runtime_1.jsx)(typography_1.Typography, { "data-testid": "ndl-avatar-letters", variant: typographyVariant, className: typeSizeClass, children: name }));
};
const TypeComponent = type === 'image'
? renderImageType()
: type === 'icon'
? renderIconType()
: type === 'letters' && name
? renderLettersType()
: null;
const isStatusIndicatorVisible = hasStatus && !isDisabled && status;
return ((0, jsx_runtime_1.jsxs)(Component, Object.assign({ ref: ref, className: classes, style: style, "aria-hidden": !isButton }, (isButton && {
disabled: isDisabled,
}), { "aria-label": `Avatar ${name}` }, restProps, htmlAttributes, { children: [(0, jsx_runtime_1.jsx)("div", { className: (0, defaultImports_1.classNames)(shapeClasses, typeClass), "data-testid": "ndl-avatar-container", children: TypeComponent }), isStatusIndicatorVisible && ((0, jsx_runtime_1.jsx)(StatusIndicator, { size: size, status: status }))] })));
});
//# sourceMappingURL=Avatar.js.map