Skip to content
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

LeafyGreen UI Avatar [LG-3584] #2332

Merged
merged 38 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
050f72e
adds LiveExample & generated stories
TheSonOfThomp May 3, 2024
5f60a30
Create nervous-suits-thank.md
TheSonOfThomp May 3, 2024
65e7554
disable snapshot
TheSonOfThomp May 3, 2024
d10f9c4
mv avatar changelog
TheSonOfThomp May 1, 2024
b25f29e
init avatar
TheSonOfThomp May 1, 2024
164f6d7
Basic Avatar replacement
TheSonOfThomp May 1, 2024
9e7cf9d
export GlyphName
TheSonOfThomp May 1, 2024
79c820f
Update Avatar contents logic
TheSonOfThomp May 1, 2024
6e5d064
updates getInitials function
TheSonOfThomp May 2, 2024
c10d4bb
if no text is provided, default to icon format
TheSonOfThomp May 2, 2024
a5e2644
updates Avatar sizing
TheSonOfThomp May 2, 2024
d51a94b
Add sizeOverride. Improve SB demos
TheSonOfThomp May 3, 2024
c917c1a
Update many-camels-think.md
TheSonOfThomp May 3, 2024
53ad4c8
update omit import
TheSonOfThomp May 3, 2024
dc525ee
consume lgui-avatar in chat
TheSonOfThomp May 3, 2024
923cd8b
rm deprecated subcomponents
TheSonOfThomp May 3, 2024
0651daa
update exported var names
TheSonOfThomp May 3, 2024
703d8f6
Update TitleBar.tsx
TheSonOfThomp May 3, 2024
ea266ed
adds changesets
TheSonOfThomp May 3, 2024
e1f989b
mv chatAvatarSizeMap
TheSonOfThomp May 3, 2024
a49d922
spread rest on Avatar
TheSonOfThomp May 3, 2024
ced2d16
Update index.ts
TheSonOfThomp May 3, 2024
b1793da
Update AvatarContents.tsx
TheSonOfThomp May 3, 2024
110ce2b
update avatar text sizing for single/multi char
TheSonOfThomp May 3, 2024
84c047b
adds style overrides for chat avatar
TheSonOfThomp May 3, 2024
906a4e4
rename files to ChatAvatar
TheSonOfThomp May 3, 2024
f0d13e8
update pkg json
TheSonOfThomp May 3, 2024
a8e7c54
Update ChatAvatar.tsx
TheSonOfThomp May 3, 2024
a524455
Merge branch 'main' into adam/avatar
TheSonOfThomp May 20, 2024
0ea5963
Merge branch 'main' into adam/avatar
TheSonOfThomp May 21, 2024
2d804ee
updates testids
TheSonOfThomp May 21, 2024
0936fa2
Merge branch 'adam/avatar' of https://github.com/mongodb/leafygreen-u…
TheSonOfThomp May 21, 2024
d45f98f
Update many-camels-think.md
TheSonOfThomp May 21, 2024
bfbeff0
Merge branch 'main' into adam/avatar
TheSonOfThomp May 22, 2024
7f0214e
Addresses SA review comments
TheSonOfThomp May 22, 2024
68bb779
Update README.md
TheSonOfThomp May 22, 2024
292eeb6
Update README.md
TheSonOfThomp May 22, 2024
6c4fe03
Adds Avatar type tests
TheSonOfThomp May 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-radios-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/icon': minor
---

Exports type `GlyphName`, a union of all default LeafyGreen icon names
5 changes: 5 additions & 0 deletions .changeset/good-elephants-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lg-chat/avatar': minor
---

Imports Avatar from '@leafygreen-ui', and removes duplicated code
5 changes: 5 additions & 0 deletions .changeset/green-queens-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lg-chat/title-bar': patch
---

Removes `color` prop from Avatar
9 changes: 9 additions & 0 deletions .changeset/many-camels-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@leafygreen-ui/avatar': major
---

Moves base Avatar component to `@leafygreen-ui` from `@lg-chat`.

Updates supported sizes, and renames `variant` prop to `format`.

Avatar no longer calls `getInitials` internally, and will render the first two characters of the `text` prop. `getInitials` must now be called external to Avatar, and one of its results (`initials`, `givenInitial` or `surnameInitial`) passed into the `text` prop.
5 changes: 5 additions & 0 deletions .changeset/nervous-suits-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lg-chat/avatar': patch
---

Adds Generated stories
3 changes: 1 addition & 2 deletions chat/avatar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
"access": "public"
},
"dependencies": {
"@leafygreen-ui/avatar": "^0.1.0",
"@leafygreen-ui/emotion": "^4.0.8",
"@leafygreen-ui/icon": "^12.2.0",
"@leafygreen-ui/lib": "^13.3.0",
"@leafygreen-ui/logo": "^9.0.2",
"@leafygreen-ui/palette": "^4.0.9",
"@leafygreen-ui/tokens": "^2.7.0"
},
Expand Down
23 changes: 0 additions & 23 deletions chat/avatar/src/Avatar.types.ts

This file was deleted.

35 changes: 0 additions & 35 deletions chat/avatar/src/Avatar/Avatar.tsx

This file was deleted.

30 changes: 0 additions & 30 deletions chat/avatar/src/Avatar/Avatar.types.ts

This file was deleted.

24 changes: 24 additions & 0 deletions chat/avatar/src/Avatar/ChatAvatar.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { css } from '@leafygreen-ui/emotion';
import { Theme } from '@leafygreen-ui/lib';
import { palette } from '@leafygreen-ui/palette';

export const textAvatarStyleOverrides = css`
background-color: ${palette.gray.dark1};
border: unset;
`;

export const iconAvatarStyleOverrides = (theme: Theme) => css`
background-color: ${theme === Theme.Dark
? palette.gray.dark2
: palette.gray.base};

color: ${theme === Theme.Dark ? palette.gray.light1 : palette.white};

border: unset;
`;

export const logoAvatarStyleOverrides = (theme: Theme) => css`
background-color: ${theme === Theme.Dark
? palette.green.dark3
: palette.black};
`;
86 changes: 86 additions & 0 deletions chat/avatar/src/Avatar/ChatAvatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, { ForwardedRef, forwardRef } from 'react';
import { useLeafyGreenChatContext } from '@lg-chat/leafygreen-chat-provider';

import { Avatar, Format, getInitials } from '@leafygreen-ui/avatar';
import { cx } from '@leafygreen-ui/emotion';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { breakpoints } from '@leafygreen-ui/tokens';

import {
iconAvatarStyleOverrides,
logoAvatarStyleOverrides,
textAvatarStyleOverrides,
} from './ChatAvatar.styles';
import {
type ChatAvatarProps,
ChatAvatarSize,
ChatAvatarVariant,
} from './ChatAvatar.types';

export const chatAvatarSizeMap: Record<ChatAvatarSize, number> = {
[ChatAvatarSize.Default]: 52,
[ChatAvatarSize.Small]: 40,
};

export const variantToAvatarFormatMap: Record<ChatAvatarVariant, Format> = {
[ChatAvatarVariant.Default]: Format.Icon,
[ChatAvatarVariant.Mongo]: Format.MongoDB,
[ChatAvatarVariant.User]: Format.Text,
};

export const ChatAvatar = forwardRef(
(
{
variant = ChatAvatarVariant.Default,
size: sizeProp,
sizeOverride: sizeOverrideProp,
darkMode: darkModeProp,
name,
...rest
}: ChatAvatarProps,
fwdRef: ForwardedRef<HTMLDivElement>,
) => {
const { theme } = useDarkMode(darkModeProp);
const { containerWidth } = useLeafyGreenChatContext();
const size =
sizeProp || (containerWidth && containerWidth < breakpoints.Tablet)
TheSonOfThomp marked this conversation as resolved.
Show resolved Hide resolved
? ChatAvatarSize.Small
: ChatAvatarSize.Default;

const sizeOverride = sizeOverrideProp ?? chatAvatarSizeMap[size];
const format = variantToAvatarFormatMap[variant];
const { initials } = getInitials(name);
const testid = (() => {
switch (variant) {
case 'mongo':
return 'mongo-avatar';
case 'user':
return 'user-avatar';
case 'default':
default:
return 'fallback-avatar';
}
})();

return (
<Avatar
ref={fwdRef}
format={format}
text={initials ?? undefined}
glyph="Person"
sizeOverride={sizeOverride}
data-testid={testid}
className={cx({
[textAvatarStyleOverrides]: variant === ChatAvatarVariant.User,
[iconAvatarStyleOverrides(theme)]:
variant === ChatAvatarVariant.Default,
[logoAvatarStyleOverrides(theme)]:
variant === ChatAvatarVariant.Mongo,
})}
{...rest}
/>
);
},
);

ChatAvatar.displayName = 'Avatar';
47 changes: 47 additions & 0 deletions chat/avatar/src/Avatar/ChatAvatar.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { DarkModeProps } from '@leafygreen-ui/lib';

export const ChatAvatarVariant = {
/**
* Renders a MongoDB logo mark
*/
Mongo: 'mongo',
/**
* Renders the user's name
*/
User: 'user',
/**
* Renders a person icon
*/
Default: 'default',
} as const;

export type ChatAvatarVariant =
(typeof ChatAvatarVariant)[keyof typeof ChatAvatarVariant];

export const ChatAvatarSize = {
Small: 'small',
Default: 'default',
} as const;

export type ChatAvatarSize =
(typeof ChatAvatarSize)[keyof typeof ChatAvatarSize];

export interface ChatAvatarProps extends DarkModeProps {
/**
* If provided, overrides the size prop to a customizable number (in px)
*/
sizeOverride?: number;
/**
* Determines the size of the avatar
* @default Size.Default
*/
size?: ChatAvatarSize;
/**
* The name of the user who is represented by the avatar. The rendered text will be the initials of the text passed to this prop.
*/
name?: string;
/**
* Determines the Avatar component's variant.
*/
variant?: ChatAvatarVariant;
}
12 changes: 10 additions & 2 deletions chat/avatar/src/Avatar/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
export { Avatar } from './Avatar';
export { type AvatarProps, Variant } from './Avatar.types';
export {
ChatAvatar,
chatAvatarSizeMap,
variantToAvatarFormatMap,
} from './ChatAvatar';
export {
type ChatAvatarProps,
ChatAvatarSize,
ChatAvatarVariant,
} from './ChatAvatar.types';
17 changes: 0 additions & 17 deletions chat/avatar/src/AvatarWrapper/AvatarWrapper.styles.ts

This file was deleted.

41 changes: 0 additions & 41 deletions chat/avatar/src/AvatarWrapper/AvatarWrapper.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions chat/avatar/src/AvatarWrapper/AvatarWrapper.types.ts

This file was deleted.

3 changes: 0 additions & 3 deletions chat/avatar/src/AvatarWrapper/index.ts

This file was deleted.

Loading
Loading