-
Notifications
You must be signed in to change notification settings - Fork 929
refactor: replace and remove deprecated Avatar component #15930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 27 commits
dbf1eb9
8f24c9e
e689d42
6f8c280
e72cecb
758f240
22c241c
8ecbfc5
081bb47
83ca623
dff92f7
52ea623
16c194d
1caeffd
4f12e95
76c211d
08fee38
bfd9244
d6fc56b
f47ed9d
7304999
9fdb4fd
0a0b1cf
f079dcd
dc410a7
37a678d
9e03115
6453ce3
bb8e9af
417cab7
ad61871
d57833b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import * as AvatarPrimitive from "@radix-ui/react-avatar"; | ||
import { type VariantProps, cva } from "class-variance-authority"; | ||
/** | ||
* Copied from shadc/ui on 12/16/2024 | ||
* @see {@link https://ui.shadcn.com/docs/components/avatar} | ||
* | ||
* This component was updated to support the variants and match the styles from | ||
* the Figma design: | ||
* @see {@link https://www.figma.com/design/WfqIgsTFXN2BscBSSyXWF8/Coder-kit?node-id=711-383&t=xqxOSUk48GvDsjGK-0} | ||
* | ||
* It was also simplified to make usage easier and reduce boilerplate. | ||
* @see {@link https://github.com/coder/coder/pull/15930#issuecomment-2552292440} | ||
*/ | ||
|
||
import { useTheme } from "@emotion/react"; | ||
import * as AvatarPrimitive from "@radix-ui/react-avatar"; | ||
import { type VariantProps, cva } from "class-variance-authority"; | ||
import * as React from "react"; | ||
import { getExternalImageStylesFromUrl } from "theme/externalImages"; | ||
import { cn } from "utils/cn"; | ||
|
||
const avatarVariants = cva( | ||
"relative flex shrink-0 overflow-hidden rounded border border-solid bg-surface-secondary text-content-secondary", | ||
{ | ||
variants: { | ||
size: { | ||
lg: "h-10 w-10 rounded-[6px] text-sm font-medium", | ||
default: "h-6 w-6 text-2xs", | ||
sm: "h-[18px] w-[18px] text-[8px]", | ||
lg: "h-[--avatar-lg] w-[--avatar-lg] rounded-[6px] text-sm font-medium", | ||
default: "h-[--avatar-default] w-[--avatar-default] text-2xs", | ||
sm: "h-[--avatar-sm] w-[--avatar-sm] text-[8px]", | ||
}, | ||
variant: { | ||
default: "", | ||
|
@@ -32,63 +38,61 @@ const avatarVariants = cva( | |
{ | ||
size: "lg", | ||
variant: "icon", | ||
className: "p-[9px]", | ||
className: "p-2", | ||
}, | ||
{ | ||
size: "default", | ||
variant: "icon", | ||
className: "p-[3px]", | ||
className: "p-1", | ||
}, | ||
{ | ||
size: "sm", | ||
variant: "icon", | ||
className: "p-[2px]", | ||
className: "p-[3px]", | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
export interface AvatarProps | ||
extends React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>, | ||
VariantProps<typeof avatarVariants> {} | ||
export type AvatarProps = AvatarPrimitive.AvatarProps & | ||
VariantProps<typeof avatarVariants> & { | ||
src?: string; | ||
alt?: string; | ||
fallback?: string; | ||
}; | ||
|
||
const Avatar = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Root>, | ||
AvatarProps | ||
>(({ className, size, variant, ...props }, ref) => ( | ||
<AvatarPrimitive.Root | ||
ref={ref} | ||
className={cn(avatarVariants({ size, variant, className }))} | ||
{...props} | ||
/> | ||
)); | ||
Avatar.displayName = AvatarPrimitive.Root.displayName; | ||
|
||
const AvatarImage = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Image>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image> | ||
>(({ className, ...props }, ref) => ( | ||
<AvatarPrimitive.Image | ||
ref={ref} | ||
className={cn("aspect-square h-full w-full", className)} | ||
{...props} | ||
/> | ||
)); | ||
AvatarImage.displayName = AvatarPrimitive.Image.displayName; | ||
>( | ||
( | ||
{ className, size, variant, src, alt, fallback, children, ...props }, | ||
ref, | ||
) => { | ||
const theme = useTheme(); | ||
|
||
const AvatarFallback = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Fallback>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback> | ||
>(({ className, ...props }, ref) => ( | ||
<AvatarPrimitive.Fallback | ||
ref={ref} | ||
className={cn( | ||
"flex h-full w-full items-center justify-center rounded-full", | ||
className, | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName; | ||
return ( | ||
<AvatarPrimitive.Root | ||
ref={ref} | ||
className={cn(avatarVariants({ size, variant, className }))} | ||
{...props} | ||
> | ||
<AvatarPrimitive.Image | ||
src={src} | ||
className="aspect-square h-full w-full object-contain" | ||
css={getExternalImageStylesFromUrl(theme.externalImages, src)} | ||
alt={alt} | ||
/> | ||
{fallback && ( | ||
<AvatarPrimitive.Fallback className="flex h-full w-full items-center justify-center rounded-full"> | ||
{fallback.charAt(0).toUpperCase()} | ||
</AvatarPrimitive.Fallback> | ||
|
||
)} | ||
{children} | ||
</AvatarPrimitive.Root> | ||
); | ||
}, | ||
); | ||
Avatar.displayName = AvatarPrimitive.Root.displayName; | ||
|
||
export { Avatar, AvatarImage, AvatarFallback }; | ||
export { Avatar }; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, do you think default is being used alot more than lg and sm? otherwise I would name this "md"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, I used "default" and "sm" for Button.tsx because I imagined default would be used most of the time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I think about this more, I feel default makes alot more sense for a variant than it does for a size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I think we can use
md
instead.