-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,95 @@ | ||
import * as React from 'react'; | ||
import { | ||
ImageErrorEventData, | ||
ImageLoadEventData, | ||
NativeSyntheticEvent, | ||
Image as RNImage, | ||
View, | ||
} from 'react-native'; | ||
import * as Slot from '~/components/primitives/slot'; | ||
import { ComponentPropsWithAsChild, SlottableViewProps, ViewRef } from '~/components/primitives/types'; | ||
import { AvatarImageProps, AvatarRootProps } from './types'; | ||
|
||
type AvatarState = 'loading' | 'error' | 'loaded'; | ||
|
||
interface IRootContext extends AvatarRootProps { | ||
status: AvatarState; | ||
setStatus: (status: AvatarState) => void; | ||
} | ||
|
||
const RootContext = React.createContext<IRootContext | null>(null); | ||
|
||
const Root = React.forwardRef<ViewRef, SlottableViewProps & AvatarRootProps>( | ||
({ asChild, alt, ...viewProps }, ref) => { | ||
const [status, setStatus] = React.useState<AvatarState>('loading'); | ||
const Component = asChild ? Slot.View : View; | ||
return ( | ||
<RootContext.Provider value={{ alt, status, setStatus }}> | ||
<Component ref={ref} {...viewProps} /> | ||
</RootContext.Provider> | ||
); | ||
} | ||
); | ||
|
||
Root.displayName = 'RootAvatar'; | ||
|
||
function useRootContext() { | ||
const context = React.useContext(RootContext); | ||
if (!context) { | ||
throw new Error('Avatar compound components cannot be rendered outside the Avatar component'); | ||
} | ||
return context; | ||
} | ||
|
||
const Image = React.forwardRef< | ||
React.ElementRef<typeof RNImage>, | ||
Omit<ComponentPropsWithAsChild<typeof RNImage>, 'alt'> & AvatarImageProps | ||
>( | ||
( | ||
{ asChild, onLoad: onLoadProps, onError: onErrorProps, onLoadingStatusChange, ...props }, | ||
ref | ||
) => { | ||
const { alt, setStatus, status } = useRootContext(); | ||
|
||
const onLoad = React.useCallback( | ||
(e: NativeSyntheticEvent<ImageLoadEventData>) => { | ||
setStatus('loaded'); | ||
onLoadingStatusChange?.('loaded'); | ||
onLoadProps?.(e); | ||
}, | ||
[onLoadProps] | ||
); | ||
|
||
const onError = React.useCallback( | ||
(e: NativeSyntheticEvent<ImageErrorEventData>) => { | ||
setStatus('error'); | ||
onLoadingStatusChange?.('error'); | ||
onErrorProps?.(e); | ||
}, | ||
[onErrorProps] | ||
); | ||
|
||
if (status === 'error') { | ||
return null; | ||
} | ||
|
||
const Component = asChild ? Slot.Image : RNImage; | ||
return <Component ref={ref} alt={alt} onLoad={onLoad} onError={onError} {...props} />; | ||
} | ||
); | ||
|
||
Image.displayName = 'ImageAvatar'; | ||
|
||
const Fallback = React.forwardRef<ViewRef, SlottableViewProps>(({ asChild, ...props }, ref) => { | ||
const { alt, status } = useRootContext(); | ||
|
||
if (status !== 'error') { | ||
return null; | ||
} | ||
const Component = asChild ? Slot.View : View; | ||
return <Component ref={ref} role={'img'} aria-label={alt} {...props} />; | ||
}); | ||
|
||
Fallback.displayName = 'FallbackAvatar'; | ||
|
||
export { Fallback, Image, Root }; |
This file contains 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 @@ | ||
interface AvatarRootProps { | ||
alt: string; | ||
} | ||
|
||
interface AvatarImageProps { | ||
children?: React.ReactNode; | ||
onLoadingStatusChange?: (status: 'error' | 'loaded') => void; | ||
} | ||
|
||
export type { AvatarRootProps, AvatarImageProps }; |
This file contains 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,44 @@ | ||
import * as React from 'react'; | ||
import * as AvatarPrimitive from '~/components/primitives/avatar'; | ||
import { cn } from '~/lib/utils'; | ||
|
||
const Avatar = React.forwardRef< | ||
React.ElementRef<typeof AvatarPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root> | ||
>(({ className, ...props }, ref) => ( | ||
<AvatarPrimitive.Root | ||
ref={ref} | ||
className={cn('relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full', 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; | ||
|
||
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 bg-muted', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName; | ||
|
||
export { Avatar, AvatarFallback, AvatarImage }; |
This file contains 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,4 @@ | ||
import { PanelLeft } from "lucide-react-native"; | ||
import { iconWithClassName } from "./iconWithClassName"; | ||
iconWithClassName(PanelLeft); | ||
export { PanelLeft }; |