@material-ui/core
Version:
React components that implement Google's Material Design.
169 lines (139 loc) • 4.77 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.styles = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _clsx = _interopRequireDefault(require("clsx"));
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
var styles = function styles(theme) {
return {
/* Styles applied to the root element. */
root: {
position: 'relative',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
width: 40,
height: 40,
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.pxToRem(20),
lineHeight: 1,
borderRadius: '50%',
overflow: 'hidden',
userSelect: 'none'
},
/* Styles applied to the root element if there are children and not `src` or `srcSet`. */
colorDefault: {
color: theme.palette.background.default,
backgroundColor: theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[600]
},
/* Styles applied to the root element if `variant="circle"`. */
circle: {},
/* Styles applied to the root element if `variant="rounded"`. */
rounded: {
borderRadius: theme.shape.borderRadius
},
/* Styles applied to the root element if `variant="square"`. */
square: {
borderRadius: 0
},
/* Styles applied to the img element if either `src` or `srcSet` is defined. */
img: {
width: '100%',
height: '100%',
textAlign: 'center',
// Handle non-square image. The property isn't supported by IE 11.
objectFit: 'cover'
}
};
};
exports.styles = styles;
var Avatar = _react.default.forwardRef(function Avatar(props, ref) {
var alt = props.alt,
childrenProp = props.children,
classes = props.classes,
className = props.className,
_props$component = props.component,
Component = _props$component === void 0 ? 'div' : _props$component,
imgProps = props.imgProps,
sizes = props.sizes,
src = props.src,
srcSet = props.srcSet,
_props$variant = props.variant,
variant = _props$variant === void 0 ? 'circle' : _props$variant,
other = (0, _objectWithoutProperties2.default)(props, ["alt", "children", "classes", "className", "component", "imgProps", "sizes", "src", "srcSet", "variant"]);
var children = null;
var img = src || srcSet;
if (img) {
children = _react.default.createElement("img", (0, _extends2.default)({
alt: alt,
src: src,
srcSet: srcSet,
sizes: sizes,
className: classes.img
}, imgProps));
} else {
children = childrenProp;
}
return _react.default.createElement(Component, (0, _extends2.default)({
className: (0, _clsx.default)(classes.root, classes.system, classes[variant], className, !img && classes.colorDefault),
ref: ref
}, other), children);
});
process.env.NODE_ENV !== "production" ? Avatar.propTypes = {
/**
* Used in combination with `src` or `srcSet` to
* provide an alt attribute for the rendered `img` element.
*/
alt: _propTypes.default.string,
/**
* Used to render icon or text elements inside the Avatar if `src` is not set.
* This can be an element, or just a string.
*/
children: _propTypes.default.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: _propTypes.default.object.isRequired,
/**
* @ignore
*/
className: _propTypes.default.string,
/**
* The component used for the root node.
* Either a string to use a DOM element or a component.
*/
component: _propTypes.default.elementType,
/**
* Attributes applied to the `img` element if the component
* is used to display an image.
*/
imgProps: _propTypes.default.object,
/**
* The `sizes` attribute for the `img` element.
*/
sizes: _propTypes.default.string,
/**
* The `src` attribute for the `img` element.
*/
src: _propTypes.default.string,
/**
* The `srcSet` attribute for the `img` element.
*/
srcSet: _propTypes.default.string,
/**
* The shape of the avatar.
*/
variant: _propTypes.default.oneOf(['circle', 'rounded', 'square'])
} : void 0;
var _default = (0, _withStyles.default)(styles, {
name: 'MuiAvatar'
})(Avatar);
exports.default = _default;