-
-
Notifications
You must be signed in to change notification settings - Fork 53
Fix Avatar + AvatarGroup composable API + other fixes #1080
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd83a7c
refactor: avatar primitive into avatar API
kotAPI 9b1bc8a
refactor: contexts
kotAPI eb57af7
fix: tests and behaviour
kotAPI 3b4c158
fix: load image issue persists
kotAPI c1967ea
fix: group
kotAPI ce79e10
add: tests
kotAPI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,20 @@ | ||
| import React from 'react'; | ||
| import { clsx } from 'clsx'; | ||
| import AvatarPrimitive from '~/core/primitives/Avatar'; | ||
| import { useCreateDataAttribute, useComposeAttributes, useCreateDataAccentColorAttribute } from '~/core/hooks/createDataAttribute'; | ||
|
|
||
| const COMPONENT_NAME = 'Avatar'; | ||
|
|
||
| export type AvatarProps = { | ||
| import AvatarRoot from './fragments/AvatarRoot'; | ||
| import AvatarImage from './fragments/AvatarImage'; | ||
| import AvatarFallback from './fragments/AvatarFallback'; | ||
|
|
||
| customRootClass?: string, | ||
| fallback?: string, | ||
| className?: string, | ||
| variant?: string; | ||
| size?:string; | ||
| src?: string, | ||
| alt?: string, | ||
| color?:string, | ||
| asChild?: boolean, | ||
| props?: Record<string, any>[] | ||
| } | ||
| const COMPONENT_NAME = 'Avatar'; | ||
|
|
||
| const Avatar = ({ customRootClass = '', fallback, className, src, alt, variant = '', size = '', asChild = false, color = '', ...props }: AvatarProps) => { | ||
| const dataAttributes = useCreateDataAttribute('avatar', { variant, size }); | ||
| const accentAttributes = useCreateDataAccentColorAttribute(color); | ||
| const composedAttributes = useComposeAttributes(dataAttributes(), accentAttributes()); | ||
| return ( | ||
| <AvatarPrimitive.Root customRootClass={customRootClass} asChild={asChild} {...composedAttributes()}> | ||
| <AvatarPrimitive.Image | ||
| src={src} | ||
| alt={alt} | ||
| className={clsx(className)} | ||
| {...props} | ||
| /> | ||
| <AvatarPrimitive.Fallback color={color}> | ||
| {fallback} | ||
| </AvatarPrimitive.Fallback> | ||
| </AvatarPrimitive.Root> | ||
| ); | ||
| const Avatar = () => { | ||
| console.warn('Direct usage of Avatar is not supported. Please use Avatar.Root, Avatar.Image, Avatar.Fallback instead.'); | ||
| return null; | ||
| }; | ||
|
|
||
| Avatar.displayName = COMPONENT_NAME; | ||
| Avatar.Root = AvatarPrimitive.Root; | ||
| Avatar.Image = AvatarPrimitive.Image; | ||
| Avatar.Fallback = AvatarPrimitive.Fallback; | ||
| Avatar.Root = AvatarRoot; | ||
| Avatar.Image = AvatarImage; | ||
| Avatar.Fallback = AvatarFallback; | ||
|
|
||
| export default Avatar; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { createContext } from 'react'; | ||
|
|
||
| type AvatarContextType = { | ||
| size?: string; | ||
| variant?: string; | ||
| color?: string; | ||
| rootClass?: string; | ||
| } | ||
| export const AvatarContext = createContext<AvatarContextType>({ | ||
| size: '', | ||
| variant: '', | ||
| color: '', | ||
| rootClass: '' | ||
| } as AvatarContextType); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { useContext } from 'react'; | ||
|
|
||
| import AvatarPrimitiveFallback, { AvatarPrimitiveFallbackProps } from '~/core/primitives/Avatar/fragments/AvatarPrimitiveFallback'; | ||
| import { AvatarContext } from '../contexts/AvatarContext'; | ||
| import { clsx } from 'clsx'; | ||
|
|
||
| const AvatarFallback = ({ children, ...props }: AvatarPrimitiveFallbackProps) => { | ||
| const { rootClass } = useContext(AvatarContext); | ||
| return <AvatarPrimitiveFallback className={clsx(`${rootClass}-fallback`)} {...props}>{children}</AvatarPrimitiveFallback>; | ||
| }; | ||
|
|
||
| export default AvatarFallback; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { useContext } from 'react'; | ||
| import AvatarPrimitiveImage, { AvatarRootImageProps } from '~/core/primitives/Avatar/fragments/AvatarPrimitiveImage'; | ||
| import { AvatarContext } from '../contexts/AvatarContext'; | ||
| import { clsx } from 'clsx'; | ||
|
|
||
| type AvatarImageProps = AvatarRootImageProps & { | ||
| src?: string; | ||
| alt?: string; | ||
| } | ||
|
|
||
| const AvatarImage = ({ src = '', alt = '', children, ...props }: AvatarImageProps) => { | ||
| const { rootClass } = useContext(AvatarContext); | ||
| return <AvatarPrimitiveImage className={clsx(`${rootClass}-image`)} src={src} alt={alt} {...props} />; | ||
| }; | ||
|
|
||
| export default AvatarImage; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use client'; | ||
| import React from 'react'; | ||
| import AvatarPrimitiveRoot, { AvatarPrimitiveRootProps } from '~/core/primitives/Avatar/fragments/AvatarPrimitiveRoot'; | ||
| import { clsx } from 'clsx'; | ||
| import { customClassSwitcher } from '~/core'; | ||
| import { AvatarContext } from '../contexts/AvatarContext'; | ||
| import { useCreateDataAttribute, useComposeAttributes, useCreateDataAccentColorAttribute } from '~/core/hooks/createDataAttribute'; | ||
| const COMPONENT_NAME = 'Avatar'; | ||
|
|
||
| export type AvatarRootProps = AvatarPrimitiveRootProps & { | ||
| customRootClass?: string; | ||
| className?: string; | ||
| size?: string; | ||
| variant?: string; | ||
| color?: string; | ||
| } | ||
|
|
||
| const AvatarRoot = ({ children, customRootClass = '', className = '', size = '', variant = '', color = '', ...props }: AvatarRootProps) => { | ||
| const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); | ||
|
|
||
| const dataAttributes = useCreateDataAttribute('avatar', { variant, size }); | ||
| const accentAttributes = useCreateDataAccentColorAttribute(color); | ||
| const composedAttributes = useComposeAttributes(dataAttributes(), accentAttributes()); | ||
|
|
||
| return <AvatarContext.Provider value={{ size, variant, color, rootClass }}> | ||
| <AvatarPrimitiveRoot className={clsx(rootClass, className)} {...composedAttributes()} {...props} >{children}</AvatarPrimitiveRoot> | ||
| </AvatarContext.Provider>; | ||
| }; | ||
|
|
||
| export default AvatarRoot; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,16 @@ | ||
| import React from 'react'; | ||
| import { clsx } from 'clsx'; | ||
| import AvatarGroupRoot from './fragments/AvatarGroupRoot'; | ||
| import AvatarPrimitiveRoot from '~/core/primitives/Avatar/fragments/AvatarPrimitiveRoot'; | ||
| import AvatarPrimitiveFallback from '~/core/primitives/Avatar/fragments/AvatarPrimitiveFallback'; | ||
| import AvatarPrimitiveImage from '~/core/primitives/Avatar/fragments/AvatarPrimitiveImage'; | ||
| import AvatarGroupItem from './fragments/AvatarGroupItem'; | ||
| import AvatarGroupAvatar from './fragments/AvatarGroupAvatar'; | ||
| import AvatarGroupFallback from './fragments/AvatarGroupFallback'; | ||
|
|
||
| const COMPONENT_NAME = 'AvatarGroup'; | ||
|
|
||
| // contexts | ||
|
|
||
| export type AvatarGroupProps = { | ||
| avatars: { fallback: string, src: string, alt: string }[]; | ||
| size: 'sm' | 'md' | 'lg'; | ||
| customRootClass?: string; | ||
| className?: string; | ||
| color?:string; | ||
| props?: Record<string, any>; | ||
| } | ||
|
|
||
| const AvatarGroup = ({ avatars = [], size, customRootClass = '', className, color, ...props }: AvatarGroupProps) => { | ||
| return <AvatarGroupRoot customRootClass={customRootClass} className={clsx(className)} {...props} > | ||
| {avatars.map((avatar, index) => ( | ||
| <AvatarPrimitiveRoot key={index}> | ||
| <AvatarPrimitiveImage src={avatar.src} alt={avatar.alt} /> | ||
| <AvatarPrimitiveFallback color={color}>{avatar.fallback}</AvatarPrimitiveFallback> | ||
| </AvatarPrimitiveRoot> | ||
| ))} | ||
| </AvatarGroupRoot>; | ||
| const AvatarGroup = () => { | ||
| console.warn('Direct usage of AvatarGroup is not supported. Please use AvatarGroup.Root, AvatarGroup.Item, AvatarGroup.Avatar, AvatarGroup.Fallback instead.'); | ||
| return null; | ||
| }; | ||
|
|
||
| AvatarGroup.displayName = COMPONENT_NAME; | ||
| AvatarGroup.Root = AvatarGroupRoot; | ||
| AvatarGroup.AvatarRoot = AvatarPrimitiveRoot; | ||
| AvatarGroup.AvatarImage = AvatarPrimitiveImage; | ||
| AvatarGroup.AvatarFallback = AvatarPrimitiveFallback; | ||
| AvatarGroup.Item = AvatarGroupItem; | ||
| AvatarGroup.Avatar = AvatarGroupAvatar; | ||
| AvatarGroup.Fallback = AvatarGroupFallback; | ||
|
|
||
| export default AvatarGroup; | ||
7 changes: 7 additions & 0 deletions
7
src/components/ui/AvatarGroup/contexts/AvatarGroupContext.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { createContext } from 'react'; | ||
|
|
||
| export const AvatarGroupContext = createContext({ | ||
| rootClass: '', | ||
| size: '', | ||
| variant: '' | ||
| }); |
10 changes: 10 additions & 0 deletions
10
src/components/ui/AvatarGroup/fragments/AvatarGroupAvatar.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import AvatarPrimitiveImage from '~/core/primitives/Avatar/fragments/AvatarPrimitiveImage'; | ||
|
|
||
| const AvatarGroupAvatar = ({ src, alt }: { src: string, alt: string }) => { | ||
| return <AvatarPrimitiveImage src={src} alt={alt} />; | ||
| }; | ||
|
|
||
| export default AvatarGroupAvatar; |
14 changes: 14 additions & 0 deletions
14
src/components/ui/AvatarGroup/fragments/AvatarGroupFallback.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { useContext } from 'react'; | ||
| import AvatarPrimitiveFallback from '~/core/primitives/Avatar/fragments/AvatarPrimitiveFallback'; | ||
| import { AvatarGroupContext } from '../contexts/AvatarGroupContext'; | ||
|
|
||
| const AvatarGroupFallback = ({ children }: { fallback?: string, children: React.ReactNode }) => { | ||
| const { rootClass } = useContext(AvatarGroupContext); | ||
| return <AvatarPrimitiveFallback className={`${rootClass}-fallback`}> | ||
| {children} | ||
| </AvatarPrimitiveFallback>; | ||
| }; | ||
|
|
||
| export default AvatarGroupFallback; |
19 changes: 19 additions & 0 deletions
19
src/components/ui/AvatarGroup/fragments/AvatarGroupItem.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use client'; | ||
|
|
||
| import React, { useContext } from 'react'; | ||
| import AvatarPrimitiveRoot from '~/core/primitives/Avatar/fragments/AvatarPrimitiveRoot'; | ||
| import { AvatarGroupContext } from '../contexts/AvatarGroupContext'; | ||
| import { useCreateDataAccentColorAttribute, useComposeAttributes } from '~/core/hooks/createDataAttribute'; | ||
|
|
||
| const AvatarGroupItem = ({ color = '', children }: { color?: string, children: React.ReactNode }) => { | ||
| const { rootClass } = useContext(AvatarGroupContext); | ||
|
|
||
| const accentAttributes = useCreateDataAccentColorAttribute(color); | ||
| const composedAttributes = useComposeAttributes(accentAttributes()); | ||
|
|
||
| return <AvatarPrimitiveRoot className={`${rootClass}-item`} {...composedAttributes()}> | ||
| {children} | ||
| </AvatarPrimitiveRoot>; | ||
| }; | ||
|
|
||
| export default AvatarGroupItem; |
22 changes: 16 additions & 6 deletions
22
src/components/ui/AvatarGroup/fragments/AvatarGroupRoot.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
💡 Verification agent
🧩 Analysis chain
API breaking change detected.
The component API has changed significantly. The previous API used AvatarGroup.AvatarRoot, AvatarGroup.AvatarImage, and AvatarGroup.AvatarFallback (as seen in the relevant code snippet), while the new API exports AvatarGroup.Item, AvatarGroup.Avatar, and AvatarGroup.Fallback.
This change will require updates to existing code that uses the AvatarGroup component.
Given this breaking change:
🏁 Script executed:
Length of output: 608
🏁 Script executed:
Length of output: 982
Update documentation and migration support for AvatarGroup API changes
The documentation still references the old API in
docs/app/docs/components/avatar-group/docs/avatarGroup_anatomy.tsx. Please:AvatarRoot,AvatarImage,AvatarFallback) to the new one (Item,Avatar,Fallback).