-
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
1 parent
e0dec30
commit a89eaa8
Showing
17 changed files
with
342 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { HStack, Avatar } from '@amalgama/embassy-ui' | ||
import CodePreview from '@site/src/components/CodePreview'; | ||
|
||
# Avatar | ||
|
||
This component is used to display a user's profile picture. | ||
|
||
To add the `Avatar` component to your project you can import it as follows: | ||
|
||
```tsx | ||
import { Avatar } from '@amalgama/embassy-ui'; | ||
``` | ||
|
||
## Example | ||
|
||
<CodePreview> | ||
<Avatar | ||
size="md" | ||
source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} | ||
/> | ||
</CodePreview> | ||
|
||
```tsx | ||
<Avatar size="md" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
``` | ||
|
||
## Props | ||
|
||
### source | ||
The source of the image to display. | ||
|
||
| Type | Required | | ||
| ---- | -------- | | ||
| string | Yes | | ||
|
||
### size | ||
The size of the avatar. | ||
|
||
| Type | Required | Default | | ||
| ---- | -------- | ------- | | ||
| string | No | "md" | | ||
|
||
Available sizes: | ||
|
||
| Size | Value | | ||
| ---- | ----- | | ||
| "xs" | 40px | | ||
| "sm" | 64px | | ||
| "md" | 96px | | ||
| "lg" | 128px | | ||
| "xl" | 160px | | ||
|
||
<CodePreview> | ||
<HStack space="4"> | ||
<Avatar size="xs" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
<Avatar size="sm" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
<Avatar size="md" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
<Avatar size="lg" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
<Avatar size="xl" source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} /> | ||
</HStack> | ||
</CodePreview> | ||
|
||
### alt | ||
The alternative text for the image. It will be used for the accessibility label of the image as `{alt}'s avatar`. | ||
|
||
| Type | Required | | ||
| ---- | -------- | | ||
| string | No | - | | ||
|
||
:::info | ||
If you don't provide an `alt` prop, the accessibility label will be `Avatar`. | ||
::: | ||
|
||
## Pseudo Props | ||
|
||
### __image | ||
|
||
Props to be to the internal `Image` component. | ||
|
||
| Type | Required | | ||
| ---------- | -------- | | ||
| `IImageProps`| No | | ||
|
||
<CodePreview> | ||
<Avatar | ||
source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} | ||
size="xl" | ||
__image={{ | ||
borderWidth: "xl", | ||
borderColor: "success.500", | ||
}} | ||
/> | ||
</CodePreview> | ||
|
||
```tsx | ||
<Avatar | ||
source={{uri:"https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80"}} | ||
size="xl" | ||
__image={{ | ||
borderWidth: "xl", | ||
borderColor: "success.500", | ||
}} | ||
/> | ||
|
||
|
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,43 @@ | ||
import * as React from 'react'; | ||
|
||
import { StyleSheet, View } from 'react-native'; | ||
|
||
import { | ||
HStack, Text, VStack, Avatar | ||
} from '@amalgama/embassy-ui'; | ||
|
||
const styles = StyleSheet.create( { | ||
container: { | ||
marginBottom: 20 | ||
}, | ||
separator: { | ||
height: 1, | ||
minWidth: '100%', | ||
marginTop: 2, | ||
marginBottom: 6, | ||
backgroundColor: 'black' | ||
}, | ||
vspace: { | ||
height: 10, | ||
minWidth: '100%' | ||
} | ||
} ); | ||
|
||
const SRC = { uri: 'https://images.unsplash.com/photo-1599566150163-29194dcaad36?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80' }; | ||
|
||
const AvatarExamples = () => ( | ||
<VStack style={styles.container} space="2"> | ||
<Text variant="headline">Avatar Component</Text> | ||
<Text variant="subtitle">Sizes</Text> | ||
<View style={styles.separator} /> | ||
<HStack justifyContent="space-between" flexWrap='wrap'> | ||
<Avatar source={SRC} size="xs" /> | ||
<Avatar source={SRC} size="sm" /> | ||
<Avatar source={SRC} size="md" /> | ||
<Avatar source={SRC} size="lg" /> | ||
<Avatar source={SRC} size="xl" /> | ||
</HStack> | ||
</VStack> | ||
); | ||
|
||
export default AvatarExamples; |
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,22 @@ | ||
import { useTheme } from '../../../core/theme/hooks'; | ||
import { useComponentPropsResolver } from '../../../hooks'; | ||
import type { IAvatarProps } from './types'; | ||
|
||
type IUseAvatarPropsResolver = Omit<IAvatarProps, 'source'>; | ||
const useAvatarPropsResolver = ( props: IUseAvatarPropsResolver ) => { | ||
const theme = useTheme(); | ||
const { | ||
__image: imageProps, | ||
size: sizeProp, | ||
...containerProps | ||
} = useComponentPropsResolver( 'Avatar', props ); | ||
|
||
const size = sizeProp ? theme?.sizeFor( 'Avatar', sizeProp as string ) : undefined; | ||
|
||
containerProps.width = size; | ||
containerProps.height = size; | ||
|
||
return { avatarProps: containerProps, imageProps }; | ||
}; | ||
|
||
export default useAvatarPropsResolver; |
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,25 @@ | ||
import React from 'react'; | ||
import Box from '../Box'; | ||
import Image from '../Image'; | ||
import useAvatarPropsResolver from './hooks'; | ||
import type { IAvatarProps } from './types'; | ||
|
||
const Avatar = ( { | ||
source, alt, testID, ...props | ||
}: IAvatarProps ) => { | ||
const { avatarProps, imageProps } = useAvatarPropsResolver( props ); | ||
const accessibilityLabel = alt ? `${alt}'s avatar` : 'Avatar'; | ||
return ( | ||
<Box | ||
{...avatarProps} | ||
testID={testID} | ||
accessible | ||
accessibilityRole="image" | ||
accessibilityLabel={accessibilityLabel} | ||
> | ||
<Image source={ source } {...imageProps} /> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default Avatar; |
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,8 @@ | ||
import type { ImageProps } from 'react-native'; | ||
import type { IBoxProps } from '../Box/types'; | ||
|
||
export interface IAvatarProps extends Omit<IBoxProps, 'children'> { | ||
source: ImageProps['source'], | ||
size?: string, | ||
alt?: string, | ||
} |
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,20 @@ | ||
export default { | ||
defaultProps: { | ||
rounded: 'full', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
size: 'xs', | ||
__image: { | ||
width: '100%', | ||
height: '100%', | ||
rounded: 'full' | ||
} | ||
}, | ||
sizes: { | ||
'xs': 40, | ||
'sm': 64, | ||
'md': 96, | ||
'lg': 128, | ||
'xl': 160 | ||
} | ||
}; |
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,51 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react-native'; | ||
import WithThemeProvider from '../../support/withThemeProvider'; | ||
import Avatar from '../../../src/components/main/Avatar'; | ||
|
||
const { itBehavesLike } = require( '../../support/sharedExamples' ); | ||
|
||
const TEST_SRC = 'https://avatars.githubusercontent.com/u/1174405?v=4'; | ||
describe( 'Avatar', () => { | ||
const renderAvatar = ( { src, ...props } = {} ) => render( | ||
<Avatar src={src} testID="test-avatar" {...props} />, | ||
{ wrapper: WithThemeProvider } | ||
); | ||
|
||
it( 'renders the provided source image', () => { | ||
const { getByTestId } = renderAvatar( { src: TEST_SRC } ); | ||
expect( getByTestId( 'test-avatar' ) ).not.toBeNull(); | ||
} ); | ||
|
||
it( 'renders the provided size', () => { | ||
const { getByTestId } = renderAvatar( { size: 'xl' } ); | ||
expect( getByTestId( 'test-avatar' ) ).toHaveStyle( { width: 160, height: 160 } ); | ||
} ); | ||
|
||
describe( 'when an alternative text is provided', () => { | ||
it( 'uses it for the accessibilityLabel', () => { | ||
const { getByTestId } = renderAvatar( { alt: 'user' } ); | ||
expect( getByTestId( 'test-avatar' ) ).toHaveProp( 'accessibilityLabel', "user's avatar" ); | ||
} ); | ||
} ); | ||
|
||
itBehavesLike( | ||
'aStyledSystemComponent', | ||
{ | ||
renderComponent: props => renderAvatar( props ), | ||
testId: 'test-avatar', | ||
omitProps: [ 'borderRadius' ] | ||
} | ||
); | ||
|
||
itBehavesLike( | ||
'anAccessibleComponent', | ||
{ | ||
renderComponent: props => renderAvatar( props ), | ||
testID: 'test-avatar', | ||
accessible: true, | ||
accessibilityRole: 'image', | ||
accessibilityLabel: 'Avatar', | ||
omitProps: [ 'accessibilityState' ] | ||
} ); | ||
} ); |
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 |
---|---|---|
@@ -1,18 +1,11 @@ | ||
const testingProps = [ 'accessible', 'accessibilityRole', 'accessibilityState', 'accessibilityLabel' ]; | ||
|
||
module.exports = ( { | ||
renderComponent, testID, accessibilityRole, accessibilityState | ||
renderComponent, testID, omitProps = [], ...accessibilityProps | ||
} ) => { | ||
describe( 'is an accessible component', () => { | ||
it( 'has accessible prop', () => { | ||
const { getByTestId } = renderComponent(); | ||
expect( getByTestId( testID ) ).toHaveProp( 'accessible', true ); | ||
} ); | ||
it( 'has accessibilityRole prop', () => { | ||
const { getByTestId } = renderComponent(); | ||
expect( getByTestId( testID ) ).toHaveProp( 'accessibilityRole', accessibilityRole ); | ||
} ); | ||
it( 'has accessibilityState prop', () => { | ||
const { getByTestId } = renderComponent(); | ||
expect( getByTestId( testID ) ).toHaveProp( 'accessibilityState', accessibilityState ); | ||
} ); | ||
const props = testingProps.filter( prop => !omitProps.includes( prop ) ); | ||
it.each( props )( 'has %s prop', ( prop ) => { | ||
const { getByTestId } = renderComponent(); | ||
expect( getByTestId( testID ) ).toHaveProp( prop, accessibilityProps[ prop ] ); | ||
} ); | ||
}; |
Oops, something went wrong.