Skip to content

Commit

Permalink
Add avatar image and text changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vineethasok committed Oct 17, 2023
1 parent eb264a2 commit 96a7b00
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 7 deletions.
169 changes: 169 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/components/Avatar/Avatar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Avatar } from "./Avatar";
export default {
component: Avatar,
title: "Display/Avatar",
tags: ["avatar","autodocs"],
tags: ["avatar", "autodocs"],
};

export const Playground = {
args: {
text: "CM",
},
};
};
34 changes: 29 additions & 5 deletions src/components/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { Fallback, Root } from "@radix-ui/react-avatar";
import { Fallback, Image, Root } from "@radix-ui/react-avatar";
import { HTMLAttributes } from "react";
import styled from "styled-components";

type TextSize = "md" | "sm";

export type AvatarProps = {
export interface AvatarProps extends HTMLAttributes<HTMLDivElement> {
text: string;
textSize?: TextSize;
};
src?: string;
srcSet?: string;
}

const Avatar = ({ text = "", textSize = "sm", ...delegated }: AvatarProps) => (
const Avatar = ({ text, textSize = "sm", src, srcSet, ...delegated }: AvatarProps) => (
<StyledRoot {...delegated}>
<StyledFallback $textSize={textSize}>{text}</StyledFallback>
<AvatarImage
src={src}
srcSet={srcSet}
alt={text}
/>
<StyledFallback
$textSize={textSize}
delayMs={0}
>
{text
.trim()
.replace(/(^.)([^ ]* )?(.).*/, "$1$3")
.trim()
.toUpperCase()}
</StyledFallback>
</StyledRoot>
);

Expand Down Expand Up @@ -39,6 +56,13 @@ const StyledRoot = styled(Root)`
}
`;

const AvatarImage = styled(Image)`
width: 100%;
height: 100%;
object-fit: cover;
border-radius: inherit;
`;

const StyledFallback = styled(Fallback)<{ $textSize: TextSize }>`
width: ${props => props.theme.click.avatar.size.label.width};
display: inline-flex;
Expand Down

0 comments on commit 96a7b00

Please sign in to comment.