|
| 1 | +import * as stylex from '@stylexjs/stylex'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { Avatar } from '../components/avatar'; |
| 5 | +import type { ButtonProps } from '../components/button'; |
| 6 | +import { Button } from '../components/button'; |
| 7 | +import { Icon } from '../components/icon'; |
| 8 | +import { mergeStyleProps, themeProps } from '../props'; |
| 9 | +import { colorVars, radiusVars } from '../tokens.stylex'; |
| 10 | + |
| 11 | +export interface AvatarButtonProps extends Omit<ButtonProps, 'children' | 'shape' | 'size'> { |
| 12 | + imageUrl?: string; |
| 13 | + name: string; |
| 14 | + fallback?: React.ReactNode; |
| 15 | +} |
| 16 | + |
| 17 | +const styles = stylex.create({ |
| 18 | + root: { |
| 19 | + borderWidth: 0, |
| 20 | + position: 'relative', |
| 21 | + }, |
| 22 | + editSurface: { |
| 23 | + borderColor: colorVars['--cl-color-border'], |
| 24 | + borderRadius: radiusVars['--cl-radius-full'], |
| 25 | + borderStyle: 'solid', |
| 26 | + borderWidth: '1px', |
| 27 | + alignItems: 'center', |
| 28 | + backgroundColor: colorVars['--cl-color-card'], |
| 29 | + boxSizing: 'border-box', |
| 30 | + display: 'flex', |
| 31 | + insetInlineStart: '-4.5px', |
| 32 | + justifyContent: 'center', |
| 33 | + position: 'absolute', |
| 34 | + height: '20px', |
| 35 | + top: '25px', |
| 36 | + width: '20px', |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +export const AvatarButton = React.forwardRef<HTMLButtonElement, AvatarButtonProps>(function AvatarButton( |
| 41 | + { |
| 42 | + imageUrl, |
| 43 | + name, |
| 44 | + fallback, |
| 45 | + color = 'neutral', |
| 46 | + variant = 'ghost', |
| 47 | + className, |
| 48 | + style, |
| 49 | + 'aria-label': ariaLabel = 'Edit profile picture', |
| 50 | + ...rest |
| 51 | + }, |
| 52 | + ref, |
| 53 | +) { |
| 54 | + const initials = name |
| 55 | + .split(/\s+/) |
| 56 | + .map(part => part[0]) |
| 57 | + .join('') |
| 58 | + .slice(0, 2) |
| 59 | + .toUpperCase(); |
| 60 | + |
| 61 | + return ( |
| 62 | + <Button |
| 63 | + ref={ref} |
| 64 | + aria-label={ariaLabel} |
| 65 | + color={color} |
| 66 | + shape='circle' |
| 67 | + size='lg' |
| 68 | + variant={variant} |
| 69 | + {...mergeStyleProps(themeProps('avatar-button'), stylex.props(styles.root), className, style)} |
| 70 | + {...rest} |
| 71 | + > |
| 72 | + <Avatar.Root |
| 73 | + aria-hidden |
| 74 | + size='md' |
| 75 | + > |
| 76 | + <Avatar.Image |
| 77 | + alt='' |
| 78 | + src={imageUrl} |
| 79 | + /> |
| 80 | + <Avatar.Fallback>{fallback ?? initials}</Avatar.Fallback> |
| 81 | + </Avatar.Root> |
| 82 | + <span |
| 83 | + aria-hidden |
| 84 | + {...mergeStyleProps(themeProps('avatar-button-edit-surface'), stylex.props(styles.editSurface))} |
| 85 | + > |
| 86 | + <Icon |
| 87 | + aria-hidden |
| 88 | + name='pen' |
| 89 | + size='xs' |
| 90 | + /> |
| 91 | + </span> |
| 92 | + </Button> |
| 93 | + ); |
| 94 | +}); |
0 commit comments