Skip to content

Instantly share code, notes, and snippets.

@aaronmcadam
Created May 16, 2017 16:42
Show Gist options
  • Save aaronmcadam/759b8fef266f2039d9e9744ba97eb633 to your computer and use it in GitHub Desktop.
Save aaronmcadam/759b8fef266f2039d9e9744ba97eb633 to your computer and use it in GitHub Desktop.
import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { baseTheme } from '../../../themes/src';
import { sizes, userRoles } from '../constants';
import Image from '../Image';
import Icon from '../Icon';
const getSize = ({ size }) => sizes.values[size];
const getUserRoleColour = ({ user, theme }) => {
if (user.role === 'staff') {
return theme.palette.role.staff;
}
return theme.palette.role.participant;
};
// eslint-disable-next-line react/prop-types
const Wrapper = ({ user, ...props }) => {
if (!user.avatar) {
return <Icon bordered circular name="user-circle" {...props} />;
}
return (
<Image
bordered
circular
src={user.avatar}
{...props}
data-role="avatar"
alt="avatar"
/>
);
};
const Avatar = styled(Wrapper)`
border-color: ${getUserRoleColour};
border-width: 2px;
box-sizing: border-box;
width: ${getSize};
`;
Avatar.propTypes = {
/** An avatar may appear at different sizes. */
size: PropTypes.oneOf(sizes.labels),
/** Specifies the user. */
user: PropTypes.shape({
/** Specifies the URL of the avatar. */
avatar: PropTypes.string,
/** Specifies the user role */
role: PropTypes.oneOf(userRoles),
}),
/** Specifies the theme. */
theme: PropTypes.shape({
/** Specifies the palette. */
palette: PropTypes.object.isRequired,
}).isRequired,
};
Avatar.defaultProps = {
size: 'tiny',
theme: baseTheme,
};
export default Avatar;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment