@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
128 lines • 6.43 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;
};
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "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/>.
*/
import React from 'react';
import { classNames } from '../_common/defaultImports';
import { UserIconOutline } from '../icons';
import { Typography } from '../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 = classNames(`ndl-avatar-status-svg-${size}`, {
[`ndl-avatar-status-svg-unknown-${size}`]: status === 'unknown',
[`ndl-avatar-status-svg-${status}`]: status !== 'unknown',
});
return (_jsx("div", { "data-testid": "ndl-avatar-status", className: statusClasses, title: status.charAt(0).toUpperCase() + status.slice(1), role: "presentation", children: _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' && (_jsx("circle", { cx: cxAndCy, cy: cxAndCy, r: rUnknown, className: "ndl-avatar-status-svg-unknown-inner" })), _jsx("circle", { cx: cxAndCy, cy: cxAndCy, r: status === 'unknown' && size !== 'small' ? rUnknownInner : r, className: svgCircleClasses })] }) }));
}
export const defaultProps = {
as: 'div',
hasStatus: false,
isDisabled: false,
name: undefined,
shape: 'circle',
size: 'medium',
source: undefined,
status: undefined,
type: 'image',
};
export const Avatar = React.forwardRef(function Avatar(_a, ref) {
var { as = defaultProps.as, hasStatus = defaultProps.hasStatus, isDisabled = defaultProps.isDisabled, name = defaultProps.name, shape = defaultProps.shape, size = defaultProps.size, source = defaultProps.source, status = defaultProps.status, type: initialType = 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 || defaultProps.as;
const isButton = Component === 'button';
const classes = classNames(`ndl-avatar ndl-avatar-${shape} ndl-avatar-${size}`, className);
const shapeClasses = 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 = () => (_jsxs(_Fragment, { children: [_jsx("img", { "data-testid": "ndl-avatar-image", src: source, alt: name }), _jsx("div", { "data-testid": "ndl-avatar-image-overlay", className: `${shapeClasses} ndl-avatar-image-overlay` })] }));
const renderIconType = () => (_jsx(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 (_jsx(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 (_jsxs(Component, Object.assign({ ref: ref, className: classes, style: style, "aria-hidden": !isButton }, (isButton && {
disabled: isDisabled,
}), { "aria-label": `Avatar ${name}` }, restProps, htmlAttributes, { children: [_jsx("div", { className: classNames(shapeClasses, typeClass), "data-testid": "ndl-avatar-container", children: TypeComponent }), isStatusIndicatorVisible && (_jsx(StatusIndicator, { size: size, status: status }))] })));
});
//# sourceMappingURL=Avatar.js.map