Skip to content

Avatar and AvatarGroup with color support for fallback#793

Merged
kodiakhq[bot] merged 2 commits intorad-ui:mainfrom
GoldGroove06:avatar-and-avatarGroup-color-prop
Jan 19, 2025
Merged

Avatar and AvatarGroup with color support for fallback#793
kodiakhq[bot] merged 2 commits intorad-ui:mainfrom
GoldGroove06:avatar-and-avatarGroup-color-prop

Conversation

@GoldGroove06
Copy link
Copy Markdown
Collaborator

@GoldGroove06 GoldGroove06 commented Jan 18, 2025

Added the color prop for fallback with tests and storybook example. Updated the docs too will work once we do a npm release.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added optional color property to Avatar and AvatarGroup components.
    • Introduced color customization for avatar fallback states.
  • Improvements

    • Enhanced avatar rendering flexibility with the new color attribute.
    • Updated documentation to reflect the new color property and its usage.
  • Testing

    • Expanded test coverage for avatar color functionality.
    • Added new Storybook stories demonstrating color variations for both Avatar and AvatarGroup components.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 18, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 eslint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

docs/app/docs/components/avatar/docs/codeUsage.js

Oops! Something went wrong! :(

ESLint: 8.56.0

ESLint couldn't find the config "next/core-web-vitals" to extend from. Please check that the name of the config is correct.

The config "next/core-web-vitals" was referenced from the config file in "/docs/.eslintrc.json".

If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.

Walkthrough

This pull request introduces a color prop to the Avatar and AvatarGroup components, allowing for customization of fallback avatar appearances. Changes are made across various files, including the main component implementations, stories, tests, and the primitive fallback component. The new color prop enables the setting of a specific color for avatar fallbacks when no image source is provided, enhancing the visual flexibility of these UI components.

Changes

File Change Summary
src/components/ui/Avatar/Avatar.tsx Added optional color prop to AvatarProps and updated component signature
src/components/ui/Avatar/stories/Avatar.stories.js Added new withColor story demonstrating color prop usage
src/components/ui/Avatar/tests/Avatar.test.tsx Added test case to verify color prop functionality for fallback
src/components/ui/AvatarGroup/AvatarGroup.tsx Added optional color prop to AvatarGroupProps and updated component signature
src/components/ui/AvatarGroup/stories/AvatarGroup.stories.js Added withFallback and withColor stories for AvatarGroup
src/components/ui/AvatarGroup/tests/AvatarGroup.test.js Added test case to verify color prop in AvatarGroup fallbacks
src/core/primitives/Avatar/fragments/AvatarPrimitiveFallback.tsx Updated interface and component to support color prop
docs/app/docs/components/avatar/docs/codeUsage.js Updated Avatar usage example to include color prop
docs/app/docs/components/avatar/page.js Added color property to AvatarDocs data and updated Avatar usage example

Sequence Diagram

sequenceDiagram
    participant Avatar
    participant AvatarPrimitiveFallback
    
    Avatar->>AvatarPrimitiveFallback: Pass color prop
    AvatarPrimitiveFallback-->>Avatar: Render with data-accent-color
Loading

Possibly related PRs

  • Docs Improvements #676: The changes in this PR involve documentation improvements, which may relate to the enhancements made to the Avatar component's functionality and usage of the color prop.
  • AvatarGroup Tests #707: This PR introduces tests for the AvatarGroup component, which is relevant as it also utilizes the color prop in its functionality.
  • AvatarGroup Docs #716: This PR adds documentation for the AvatarGroup, which is pertinent to the changes made in the main PR regarding the Avatar component and its new color prop.

Suggested labels

automerge

Suggested reviewers

  • kotAPI

Poem

🐰 A rabbit's tale of colors bright,
Avatar's fallback now shines with might!
Blue and bold, no image in sight,
Customization brings pure delight!
Code hops forward with colorful glee 🎨

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (7)
src/core/primitives/Avatar/fragments/AvatarPrimitiveFallback.tsx (1)

12-12: Fix spacing in function parameters.

There's an extra space before the closing curly brace in the destructured parameters.

-const AvatarPrimitiveFallback = ({ children, className = '' , color = ''}: AvatarPrimitiveFallback
+const AvatarPrimitiveFallback = ({ children, className = '', color = '' }: AvatarPrimitiveFallback
🧰 Tools
🪛 eslint

[error] 12-12: A space is required before '}'.

(object-curly-spacing)

src/components/ui/Avatar/tests/Avatar.test.tsx (1)

24-27: Remove unnecessary async keyword.

The test doesn't contain any asynchronous operations, so the async keyword can be removed.

-    test('renders color for fallback when src is not provided', async() => {
+    test('renders color for fallback when src is not provided', () => {

Otherwise, the test case looks good and properly verifies the color prop functionality.

src/components/ui/Avatar/Avatar.tsx (1)

14-14: Fix TypeScript style: Add space after colon.

Add a space after the colon in the type definition for consistency.

-    color?:string,
+    color?: string,
src/components/ui/Avatar/stories/Avatar.stories.js (1)

45-50: Add newline at end of file.

Add a newline at the end of the file to satisfy ESLint requirements.

    }
-};
+};
+

The story implementation looks good and provides a clear example of the color prop usage.

🧰 Tools
🪛 eslint

[error] 50-50: Newline required at end of file but not found.

(eol-last)

src/components/ui/AvatarGroup/stories/AvatarGroup.stories.js (1)

37-46: Fix object literal spacing and add color variants.

The story demonstrates color support well, but has some formatting issues.

Apply these fixes:

-            { src: '', fallback: 'RU'},
+            { src: '', fallback: 'RU' },
-            { src: '', fallback: 'PK'},
+            { src: '', fallback: 'PK' },

Consider adding more color variants to demonstrate the full range of supported colors.

🧰 Tools
🪛 eslint

[error] 40-40: A space is required before '}'.

(object-curly-spacing)


[error] 41-41: A space is required before '}'.

(object-curly-spacing)

src/components/ui/AvatarGroup/AvatarGroup.tsx (1)

17-17: Consider using a union type for color prop.

Instead of using a generic string type, consider defining specific color options for better type safety and documentation.

-    color?: string;
+    color?: 'blue' | 'green' | 'red' | 'yellow' | 'purple';  // add supported colors
src/components/ui/AvatarGroup/tests/AvatarGroup.test.js (1)

72-76: Enhance color testing coverage.

While the current test verifies basic color functionality, consider adding these test cases:

  1. Test with different color values
  2. Test color inheritance from AvatarGroup to individual Avatars
  3. Test behavior when color prop is undefined

Example addition:

test('inherits color prop correctly to all avatars', () => {
    const colors = ['blue', 'green', 'red'];
    colors.forEach(color => {
        render(<AvatarGroup avatars={avatarsWithFallback} color={color} />);
        expect(screen.getByText('A')).toHaveAttribute('data-accent-color', color);
        expect(screen.getByText('B')).toHaveAttribute('data-accent-color', color);
        cleanup();
    });
});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a8d10dc and 50ebdd4.

📒 Files selected for processing (7)
  • src/components/ui/Avatar/Avatar.tsx (2 hunks)
  • src/components/ui/Avatar/stories/Avatar.stories.js (1 hunks)
  • src/components/ui/Avatar/tests/Avatar.test.tsx (1 hunks)
  • src/components/ui/AvatarGroup/AvatarGroup.tsx (1 hunks)
  • src/components/ui/AvatarGroup/stories/AvatarGroup.stories.js (1 hunks)
  • src/components/ui/AvatarGroup/tests/AvatarGroup.test.js (1 hunks)
  • src/core/primitives/Avatar/fragments/AvatarPrimitiveFallback.tsx (1 hunks)
🧰 Additional context used
🪛 eslint
src/components/ui/AvatarGroup/stories/AvatarGroup.stories.js

[error] 30-30: A space is required before '}'.

(object-curly-spacing)


[error] 31-31: A space is required before '}'.

(object-curly-spacing)


[error] 40-40: A space is required before '}'.

(object-curly-spacing)


[error] 41-41: A space is required before '}'.

(object-curly-spacing)

src/core/primitives/Avatar/fragments/AvatarPrimitiveFallback.tsx

[error] 12-12: A space is required before '}'.

(object-curly-spacing)

src/components/ui/Avatar/stories/Avatar.stories.js

[error] 50-50: Newline required at end of file but not found.

(eol-last)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (4)
src/core/primitives/Avatar/fragments/AvatarPrimitiveFallback.tsx (1)

9-9: LGTM! Clean implementation of the color prop.

The implementation follows React best practices by:

  • Using an optional prop with proper TypeScript typing
  • Using a data attribute for styling customization

Also applies to: 19-19

src/components/ui/Avatar/Avatar.tsx (1)

18-18: LGTM! Clean prop forwarding implementation.

The color prop is correctly forwarded to the Fallback component while maintaining the component's structure.

Also applies to: 27-29

src/components/ui/AvatarGroup/stories/AvatarGroup.stories.js (1)

27-35: LGTM! Good test coverage for fallback behavior.

The story effectively demonstrates the fallback behavior when src is empty.

🧰 Tools
🪛 eslint

[error] 30-30: A space is required before '}'.

(object-curly-spacing)


[error] 31-31: A space is required before '}'.

(object-curly-spacing)

src/components/ui/AvatarGroup/AvatarGroup.tsx (1)

21-21: LGTM! Color prop is correctly passed to fallback component.

The implementation properly handles the color prop flow from AvatarGroup to AvatarPrimitiveFallback.

Also applies to: 26-26

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
docs/app/docs/components/avatar/docs/codeUsage.js (1)

9-9: Consider documenting color values and CSS variable relationship.

While the example demonstrates the new color prop usage, it would be helpful to document:

  1. The list of supported color values
  2. How these values map to CSS variables in the fallback implementation
-        <Avatar fallback="AA" color='pink'/>
+        {/* Colors map to CSS variables (--rad-ui-color-{color}-500/900) */}
+        <Avatar fallback="AA" color="pink" />
docs/app/docs/components/avatar/page.js (1)

21-21: Enhance color prop documentation.

The description could be more specific about:

  1. The supported color values
  2. Whether custom colors are supported
  3. The actual default value (string 'null' vs JavaScript null)
-        {prop: 'color', type: 'string', default: 'null', description: 'Accent Color of the text initials or placeholder displayed when the image fails to load or if no src is provided.', id: 'color'},
+        {prop: 'color', type: 'string', default: null, description: 'Accent color for the fallback. Supports theme colors like "pink", "blue", etc. Custom colors are not supported.', id: 'color'},
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 50ebdd4 and b88832d.

📒 Files selected for processing (2)
  • docs/app/docs/components/avatar/docs/codeUsage.js (1 hunks)
  • docs/app/docs/components/avatar/page.js (2 hunks)
🔇 Additional comments (1)
docs/app/docs/components/avatar/page.js (1)

31-31: LGTM! Example demonstrates the new color prop effectively.

The example in the hero section provides a clear visual demonstration of the color prop usage.

@kotAPI
Copy link
Copy Markdown
Collaborator

kotAPI commented Jan 19, 2025

Merging, note that the docs changes wont update til we do another npm release!

From next PRs lets do core library updates in one PR, and docs updates after an NPM release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants