@ledgerhq/lumen-ui-rnative 0.1.51 → 0.1.52
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module/lib/Components/Avatar/Avatar.figma.js +3 -6
- package/dist/module/lib/Components/Avatar/Avatar.figma.js.map +1 -1
- package/dist/module/lib/Components/Avatar/Avatar.js +92 -39
- package/dist/module/lib/Components/Avatar/Avatar.js.map +1 -1
- package/dist/module/lib/Components/Avatar/Avatar.mdx +36 -191
- package/dist/module/lib/Components/Avatar/Avatar.stories.js +80 -125
- package/dist/module/lib/Components/Avatar/Avatar.stories.js.map +1 -1
- package/dist/module/lib/Components/Avatar/Avatar.test.js +56 -45
- package/dist/module/lib/Components/Avatar/Avatar.test.js.map +1 -1
- package/dist/module/lib/Components/Avatar/index.js +1 -0
- package/dist/module/lib/Components/Avatar/index.js.map +1 -1
- package/dist/module/lib/Components/Avatar/resolveAvatarColor.js +18 -0
- package/dist/module/lib/Components/Avatar/resolveAvatarColor.js.map +1 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.js +77 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.js.map +1 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.mdx +62 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.stories.js +59 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.stories.js.map +1 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.test.js +100 -0
- package/dist/module/lib/Components/AvatarButton/AvatarButton.test.js.map +1 -0
- package/dist/module/lib/Components/AvatarButton/index.js +5 -0
- package/dist/module/lib/Components/AvatarButton/index.js.map +1 -0
- package/dist/module/lib/Components/AvatarButton/types.js +4 -0
- package/dist/module/lib/Components/AvatarButton/types.js.map +1 -0
- package/dist/module/lib/Components/DotIndicator/getDotIndicatorProps.js +16 -1
- package/dist/module/lib/Components/DotIndicator/getDotIndicatorProps.js.map +1 -1
- package/dist/module/lib/Components/DotIndicator/getDotIndicatorProps.test.js +16 -0
- package/dist/module/lib/Components/DotIndicator/getDotIndicatorProps.test.js.map +1 -1
- package/dist/module/lib/Components/MediaBanner/MediaBanner.js +55 -37
- package/dist/module/lib/Components/MediaBanner/MediaBanner.js.map +1 -1
- package/dist/module/lib/Components/MediaBanner/MediaBanner.mdx +25 -3
- package/dist/module/lib/Components/MediaBanner/MediaBanner.stories.js +23 -0
- package/dist/module/lib/Components/MediaBanner/MediaBanner.stories.js.map +1 -1
- package/dist/module/lib/Components/MediaBanner/MediaBanner.test.js +17 -0
- package/dist/module/lib/Components/MediaBanner/MediaBanner.test.js.map +1 -1
- package/dist/module/lib/Components/index.js +1 -0
- package/dist/module/lib/Components/index.js.map +1 -1
- package/dist/typescript/src/lib/Components/Avatar/Avatar.d.ts +3 -3
- package/dist/typescript/src/lib/Components/Avatar/Avatar.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/Avatar/index.d.ts +1 -0
- package/dist/typescript/src/lib/Components/Avatar/index.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/Avatar/resolveAvatarColor.d.ts +8 -0
- package/dist/typescript/src/lib/Components/Avatar/resolveAvatarColor.d.ts.map +1 -0
- package/dist/typescript/src/lib/Components/Avatar/types.d.ts +22 -7
- package/dist/typescript/src/lib/Components/Avatar/types.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/AvatarButton/AvatarButton.d.ts +13 -0
- package/dist/typescript/src/lib/Components/AvatarButton/AvatarButton.d.ts.map +1 -0
- package/dist/typescript/src/lib/Components/AvatarButton/index.d.ts +3 -0
- package/dist/typescript/src/lib/Components/AvatarButton/index.d.ts.map +1 -0
- package/dist/typescript/src/lib/Components/AvatarButton/types.d.ts +11 -0
- package/dist/typescript/src/lib/Components/AvatarButton/types.d.ts.map +1 -0
- package/dist/typescript/src/lib/Components/DotIndicator/getDotIndicatorProps.d.ts +32 -0
- package/dist/typescript/src/lib/Components/DotIndicator/getDotIndicatorProps.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/MediaBanner/MediaBanner.d.ts +1 -1
- package/dist/typescript/src/lib/Components/MediaBanner/MediaBanner.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/MediaBanner/types.d.ts +1 -1
- package/dist/typescript/src/lib/Components/MediaBanner/types.d.ts.map +1 -1
- package/dist/typescript/src/lib/Components/index.d.ts +1 -0
- package/dist/typescript/src/lib/Components/index.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/lib/Components/Avatar/Avatar.figma.tsx +2 -5
- package/src/lib/Components/Avatar/Avatar.mdx +36 -191
- package/src/lib/Components/Avatar/Avatar.stories.tsx +58 -83
- package/src/lib/Components/Avatar/Avatar.test.tsx +58 -58
- package/src/lib/Components/Avatar/Avatar.tsx +100 -32
- package/src/lib/Components/Avatar/index.ts +1 -0
- package/src/lib/Components/Avatar/resolveAvatarColor.ts +19 -0
- package/src/lib/Components/Avatar/types.ts +23 -7
- package/src/lib/Components/AvatarButton/AvatarButton.mdx +62 -0
- package/src/lib/Components/AvatarButton/AvatarButton.stories.tsx +58 -0
- package/src/lib/Components/AvatarButton/AvatarButton.test.tsx +92 -0
- package/src/lib/Components/AvatarButton/AvatarButton.tsx +77 -0
- package/src/lib/Components/AvatarButton/index.ts +2 -0
- package/src/lib/Components/AvatarButton/types.ts +15 -0
- package/src/lib/Components/DotIndicator/getDotIndicatorProps.test.ts +16 -0
- package/src/lib/Components/DotIndicator/getDotIndicatorProps.ts +6 -2
- package/src/lib/Components/MediaBanner/MediaBanner.mdx +25 -3
- package/src/lib/Components/MediaBanner/MediaBanner.stories.tsx +25 -0
- package/src/lib/Components/MediaBanner/MediaBanner.test.tsx +18 -0
- package/src/lib/Components/MediaBanner/MediaBanner.tsx +54 -32
- package/src/lib/Components/MediaBanner/types.ts +1 -1
- package/src/lib/Components/index.ts +1 -0
|
@@ -1,40 +1,53 @@
|
|
|
1
|
+
import type { TypographyDefinition } from '@ledgerhq/lumen-design-core';
|
|
1
2
|
import { useEffect, useState } from 'react';
|
|
2
|
-
import { Image, StyleSheet } from 'react-native';
|
|
3
|
+
import { Image, StyleSheet, Text, View } from 'react-native';
|
|
3
4
|
import { useCommonTranslation } from '../../../i18n';
|
|
4
5
|
import { useStyleSheet } from '../../../styles';
|
|
5
6
|
import { User } from '../../Symbols';
|
|
7
|
+
import type { IconSize } from '../Icon';
|
|
6
8
|
import { Box } from '../Utility';
|
|
7
9
|
import type { AvatarProps } from './types';
|
|
8
10
|
|
|
9
|
-
type Appearance = NonNullable<AvatarProps['appearance']>;
|
|
10
11
|
type Size = NonNullable<AvatarProps['size']>;
|
|
11
12
|
|
|
12
|
-
const
|
|
13
|
+
const fallbackIconSizes: Record<Size, IconSize> = {
|
|
14
|
+
xs: 12,
|
|
13
15
|
sm: 16,
|
|
14
16
|
md: 24,
|
|
15
17
|
lg: 32,
|
|
16
18
|
xl: 40,
|
|
17
|
-
|
|
19
|
+
'2xl': 56,
|
|
20
|
+
};
|
|
18
21
|
|
|
19
22
|
const useStyles = ({
|
|
20
|
-
appearance,
|
|
21
23
|
size,
|
|
24
|
+
fallbackColor,
|
|
25
|
+
shouldFallback,
|
|
26
|
+
appearance,
|
|
22
27
|
}: {
|
|
23
|
-
appearance: Appearance;
|
|
24
28
|
size: Size;
|
|
29
|
+
fallbackColor?: string;
|
|
30
|
+
shouldFallback: boolean;
|
|
31
|
+
appearance?: 'thin' | 'thick';
|
|
25
32
|
}) => {
|
|
26
33
|
return useStyleSheet(
|
|
27
34
|
(t) => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
const sizeMap: Record<Size, { size: number }> = {
|
|
36
|
+
xs: { size: t.sizes.s24 },
|
|
37
|
+
sm: { size: t.sizes.s40 },
|
|
38
|
+
md: { size: t.sizes.s48 },
|
|
39
|
+
lg: { size: t.sizes.s56 },
|
|
40
|
+
xl: { size: t.sizes.s72 },
|
|
41
|
+
'2xl': { size: t.sizes.s128 },
|
|
31
42
|
};
|
|
32
43
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
const fallbackTextTypography: Record<Size, TypographyDefinition> = {
|
|
45
|
+
xs: t.typographies.body4SemiBold,
|
|
46
|
+
sm: t.typographies.body1SemiBold,
|
|
47
|
+
md: t.typographies.heading5SemiBold,
|
|
48
|
+
lg: t.typographies.heading4SemiBold,
|
|
49
|
+
xl: t.typographies.heading2SemiBold,
|
|
50
|
+
'2xl': t.typographies.heading1SemiBold,
|
|
38
51
|
};
|
|
39
52
|
|
|
40
53
|
return {
|
|
@@ -43,28 +56,76 @@ const useStyles = ({
|
|
|
43
56
|
width: sizeMap[size].size,
|
|
44
57
|
height: sizeMap[size].size,
|
|
45
58
|
borderRadius: 9999,
|
|
46
|
-
|
|
59
|
+
overflow: 'hidden',
|
|
60
|
+
backgroundColor:
|
|
61
|
+
shouldFallback && fallbackColor
|
|
62
|
+
? fallbackColor
|
|
63
|
+
: t.colors.bg.baseTransparentHover,
|
|
47
64
|
alignItems: 'center',
|
|
48
65
|
justifyContent: 'center',
|
|
49
|
-
|
|
66
|
+
},
|
|
67
|
+
fallbackText: {
|
|
68
|
+
...fallbackTextTypography[size],
|
|
69
|
+
color: fallbackColor ? t.colors.text.black : t.colors.text.base,
|
|
50
70
|
},
|
|
51
71
|
image: {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
position: 'absolute',
|
|
73
|
+
top: 0,
|
|
74
|
+
left: 0,
|
|
75
|
+
right: 0,
|
|
76
|
+
bottom: 0,
|
|
77
|
+
},
|
|
78
|
+
insetBorder: {
|
|
79
|
+
position: 'absolute',
|
|
80
|
+
top: 0,
|
|
81
|
+
left: 0,
|
|
82
|
+
right: 0,
|
|
83
|
+
bottom: 0,
|
|
55
84
|
borderRadius: 9999,
|
|
85
|
+
...(appearance === 'thin' && {
|
|
86
|
+
borderWidth: 1,
|
|
87
|
+
borderColor: t.colors.border.icon,
|
|
88
|
+
}),
|
|
89
|
+
...(appearance === 'thick' && {
|
|
90
|
+
borderWidth: 2,
|
|
91
|
+
borderColor: t.colors.border.mutedSubtleTransparent,
|
|
92
|
+
}),
|
|
56
93
|
},
|
|
57
94
|
};
|
|
58
95
|
},
|
|
59
|
-
[
|
|
96
|
+
[size, fallbackColor, shouldFallback, appearance],
|
|
60
97
|
);
|
|
61
98
|
};
|
|
62
99
|
|
|
100
|
+
const FallbackContent = ({
|
|
101
|
+
size,
|
|
102
|
+
fallbackText,
|
|
103
|
+
fallbackColor,
|
|
104
|
+
styles,
|
|
105
|
+
}: {
|
|
106
|
+
size: Size;
|
|
107
|
+
fallbackText?: string;
|
|
108
|
+
fallbackColor?: string;
|
|
109
|
+
styles: ReturnType<typeof useStyles>;
|
|
110
|
+
}) =>
|
|
111
|
+
fallbackText ? (
|
|
112
|
+
<Text style={styles.fallbackText} selectable={false}>
|
|
113
|
+
{fallbackText}
|
|
114
|
+
</Text>
|
|
115
|
+
) : (
|
|
116
|
+
<User
|
|
117
|
+
size={fallbackIconSizes[size]}
|
|
118
|
+
color={fallbackColor ? 'black' : 'base'}
|
|
119
|
+
accessible={false}
|
|
120
|
+
testID='avatar-fallback-icon'
|
|
121
|
+
/>
|
|
122
|
+
);
|
|
123
|
+
|
|
63
124
|
/**
|
|
64
125
|
* A circular avatar component that displays a user image or fallback icon.
|
|
65
126
|
*
|
|
66
|
-
* When the image fails to load or no src is provided, displays
|
|
67
|
-
*
|
|
127
|
+
* When the image fails to load or no src is provided, displays either the
|
|
128
|
+
* `fallbackText` (e.g. initials) or a User icon fallback.
|
|
68
129
|
*
|
|
69
130
|
* @see {@link https://ldls-react-native.vercel.app/?path=/docs/rnative-avatar--docs Storybook}
|
|
70
131
|
*
|
|
@@ -77,16 +138,23 @@ export const Avatar = ({
|
|
|
77
138
|
lx,
|
|
78
139
|
style,
|
|
79
140
|
src,
|
|
80
|
-
alt
|
|
81
|
-
appearance = 'transparent',
|
|
141
|
+
alt,
|
|
82
142
|
size = 'md',
|
|
143
|
+
fallbackText,
|
|
144
|
+
fallbackColor,
|
|
145
|
+
appearance,
|
|
83
146
|
ref,
|
|
84
147
|
...props
|
|
85
148
|
}: AvatarProps) => {
|
|
86
149
|
const { t } = useCommonTranslation();
|
|
87
150
|
const [error, setError] = useState<boolean>(false);
|
|
88
151
|
const shouldFallback = !src || error;
|
|
89
|
-
const styles = useStyles({
|
|
152
|
+
const styles = useStyles({
|
|
153
|
+
size,
|
|
154
|
+
fallbackColor,
|
|
155
|
+
shouldFallback,
|
|
156
|
+
appearance,
|
|
157
|
+
});
|
|
90
158
|
|
|
91
159
|
const resolvedAlt = alt || t('components.avatar.defaultAriaLabel');
|
|
92
160
|
|
|
@@ -94,7 +162,7 @@ export const Avatar = ({
|
|
|
94
162
|
setError(false);
|
|
95
163
|
}, [src]);
|
|
96
164
|
|
|
97
|
-
|
|
165
|
+
return (
|
|
98
166
|
<Box
|
|
99
167
|
ref={ref}
|
|
100
168
|
lx={lx}
|
|
@@ -104,10 +172,11 @@ export const Avatar = ({
|
|
|
104
172
|
{...props}
|
|
105
173
|
>
|
|
106
174
|
{shouldFallback ? (
|
|
107
|
-
<
|
|
108
|
-
size={
|
|
109
|
-
|
|
110
|
-
|
|
175
|
+
<FallbackContent
|
|
176
|
+
size={size}
|
|
177
|
+
fallbackText={fallbackText}
|
|
178
|
+
fallbackColor={fallbackColor}
|
|
179
|
+
styles={styles}
|
|
111
180
|
/>
|
|
112
181
|
) : (
|
|
113
182
|
<Image
|
|
@@ -118,8 +187,7 @@ export const Avatar = ({
|
|
|
118
187
|
testID='avatar-image'
|
|
119
188
|
/>
|
|
120
189
|
)}
|
|
190
|
+
{appearance && <View style={styles.insetBorder} pointerEvents='none' />}
|
|
121
191
|
</Box>
|
|
122
192
|
);
|
|
123
|
-
|
|
124
|
-
return avatarContent;
|
|
125
193
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { primitiveColorTokens } from '@ledgerhq/lumen-design-core';
|
|
2
|
+
import {
|
|
3
|
+
resolveAvatarColorKey,
|
|
4
|
+
type AvatarColorKey,
|
|
5
|
+
} from '@ledgerhq/lumen-utils-shared';
|
|
6
|
+
|
|
7
|
+
// either light or dark works, they're the same regardless of theme
|
|
8
|
+
const AVATAR_COLOR_TOKENS: Record<AvatarColorKey, string> =
|
|
9
|
+
primitiveColorTokens.light.avatar;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Resolves a stable identifier (e.g. a user id) to one of the avatar pastel
|
|
13
|
+
* colors. The same identifier always resolves to the same color.
|
|
14
|
+
*
|
|
15
|
+
* Pass the result directly to `<Avatar fallbackColor={resolveAvatarColor(id)} />`.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveAvatarColor(identifier: string): string {
|
|
18
|
+
return AVATAR_COLOR_TOKENS[resolveAvatarColorKey(identifier)];
|
|
19
|
+
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { StyledViewProps } from '../../../styles';
|
|
2
2
|
|
|
3
|
+
export type AvatarSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
|
|
4
|
+
|
|
3
5
|
export type AvatarProps = {
|
|
4
6
|
/**
|
|
5
|
-
* Image source URL. When undefined or on load error, displays a fallback
|
|
7
|
+
* Image source URL. When undefined or on load error, displays a fallback.
|
|
6
8
|
* @optional
|
|
7
9
|
*/
|
|
8
10
|
src?: string;
|
|
@@ -12,15 +14,29 @@ export type AvatarProps = {
|
|
|
12
14
|
*/
|
|
13
15
|
alt?: string;
|
|
14
16
|
/**
|
|
15
|
-
* The
|
|
17
|
+
* The size variant of the avatar.
|
|
16
18
|
* @optional
|
|
17
|
-
* @default
|
|
19
|
+
* @default md
|
|
18
20
|
*/
|
|
19
|
-
|
|
21
|
+
size?: AvatarSize;
|
|
20
22
|
/**
|
|
21
|
-
*
|
|
23
|
+
* Background color behind the fallback when no image is displayed.
|
|
24
|
+
* Use `resolveAvatarColor` from `@ledgerhq/lumen-ui-rnative` to derive
|
|
25
|
+
* a stable pastel color from an identifier such as a user id.
|
|
26
|
+
* @optional
|
|
27
|
+
*/
|
|
28
|
+
fallbackColor?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Short text shown in place of the fallback icon when no image is displayed,
|
|
31
|
+
* typically a user's initials.
|
|
32
|
+
* @optional
|
|
33
|
+
*/
|
|
34
|
+
fallbackText?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Appearance for the avatar.
|
|
37
|
+
* - `thin`: 1px ring using the icon border color
|
|
38
|
+
* - `thick`: 2px ring using the muted subtle transparent border color
|
|
22
39
|
* @optional
|
|
23
|
-
* @default md
|
|
24
40
|
*/
|
|
25
|
-
|
|
41
|
+
appearance?: 'thin' | 'thick';
|
|
26
42
|
} & Omit<StyledViewProps, 'children'>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Meta, Canvas, Controls } from '@storybook/addon-docs/blocks';
|
|
2
|
+
import * as AvatarButtonStories from './AvatarButton.stories';
|
|
3
|
+
import { CustomTabs, Tab } from '../../../../.storybook/components';
|
|
4
|
+
|
|
5
|
+
<Meta title='Core/AvatarButton' of={AvatarButtonStories} />
|
|
6
|
+
|
|
7
|
+
# AvatarButton
|
|
8
|
+
|
|
9
|
+
<CustomTabs>
|
|
10
|
+
<Tab label="Overview">
|
|
11
|
+
|
|
12
|
+
## Introduction
|
|
13
|
+
|
|
14
|
+
`AvatarButton` wraps an [`Avatar`](/docs/rnative-avatar--docs) in a `Pressable`, for use as an interactive trigger such as opening a user menu or navigating to a profile. It accepts every `Avatar` prop plus `onPress` and `onLongPress`.
|
|
15
|
+
|
|
16
|
+
## Anatomy
|
|
17
|
+
|
|
18
|
+
<Canvas of={AvatarButtonStories.Base} />
|
|
19
|
+
<Controls of={AvatarButtonStories.Base} />
|
|
20
|
+
|
|
21
|
+
## Fallback
|
|
22
|
+
|
|
23
|
+
Like `Avatar`, it falls back to a User icon when no image is available.
|
|
24
|
+
|
|
25
|
+
<Canvas of={AvatarButtonStories.Fallback} />
|
|
26
|
+
|
|
27
|
+
## Disabled
|
|
28
|
+
|
|
29
|
+
Pass `disabled` to prevent interaction. The pressable is not activatable, and the avatar renders at reduced opacity.
|
|
30
|
+
|
|
31
|
+
<Canvas of={AvatarButtonStories.Disabled} />
|
|
32
|
+
|
|
33
|
+
## Accessibility
|
|
34
|
+
|
|
35
|
+
- Renders with `accessibilityRole="button"` so it is announced as a button by screen readers.
|
|
36
|
+
- Provide a meaningful `alt` describing the action (e.g. `"Open user menu"`); it is exposed as the avatar's accessible label.
|
|
37
|
+
|
|
38
|
+
</Tab>
|
|
39
|
+
<Tab label="Implementation">
|
|
40
|
+
|
|
41
|
+
## Setup
|
|
42
|
+
|
|
43
|
+
Install and set up the library with our [Setup Guide →](?path=/docs/getting-started-setup--docs).
|
|
44
|
+
|
|
45
|
+
## Basic Usage
|
|
46
|
+
|
|
47
|
+
```tsx
|
|
48
|
+
import { AvatarButton } from '@ledgerhq/lumen-ui-rnative';
|
|
49
|
+
|
|
50
|
+
function MyComponent() {
|
|
51
|
+
return (
|
|
52
|
+
<AvatarButton
|
|
53
|
+
src="https://example.com/photo.jpg"
|
|
54
|
+
alt="Open user menu"
|
|
55
|
+
onPress={openMenu}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
</Tab>
|
|
62
|
+
</CustomTabs>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react-native-web-vite';
|
|
2
|
+
import { AvatarButton } from './AvatarButton';
|
|
3
|
+
|
|
4
|
+
const meta = {
|
|
5
|
+
component: AvatarButton,
|
|
6
|
+
id: 'rnative-avatar-button',
|
|
7
|
+
title: 'Core/AvatarButton',
|
|
8
|
+
parameters: {
|
|
9
|
+
docs: {
|
|
10
|
+
source: {
|
|
11
|
+
language: 'tsx',
|
|
12
|
+
format: true,
|
|
13
|
+
type: 'code',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
} satisfies Meta<typeof AvatarButton>;
|
|
18
|
+
|
|
19
|
+
export default meta;
|
|
20
|
+
type Story = StoryObj<typeof meta>;
|
|
21
|
+
|
|
22
|
+
const exampleSrc =
|
|
23
|
+
'https://plus.unsplash.com/premium_photo-1689551670902-19b441a6afde?q=80&w=774&auto=format&fit=crop&ixlib=rb-4.1.0&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D';
|
|
24
|
+
|
|
25
|
+
export const Base: Story = {
|
|
26
|
+
args: {
|
|
27
|
+
src: exampleSrc,
|
|
28
|
+
size: 'md',
|
|
29
|
+
alt: 'Open user menu',
|
|
30
|
+
onPress: () => ({}),
|
|
31
|
+
},
|
|
32
|
+
render: (args) => <AvatarButton {...args} />,
|
|
33
|
+
parameters: {
|
|
34
|
+
docs: {
|
|
35
|
+
source: {
|
|
36
|
+
code: `<AvatarButton src="https://example.com" alt="Open user menu" onPress={openMenu} />`,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const Fallback: Story = {
|
|
43
|
+
args: {
|
|
44
|
+
alt: 'Open user menu',
|
|
45
|
+
},
|
|
46
|
+
render: (args) => <AvatarButton {...args} />,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const Disabled: Story = {
|
|
50
|
+
args: {
|
|
51
|
+
src: exampleSrc,
|
|
52
|
+
size: 'md',
|
|
53
|
+
alt: 'Open user menu',
|
|
54
|
+
disabled: true,
|
|
55
|
+
onPress: () => ({}),
|
|
56
|
+
},
|
|
57
|
+
render: (args) => <AvatarButton {...args} />,
|
|
58
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, it, expect, jest } from '@jest/globals';
|
|
2
|
+
import { ledgerLiveThemes } from '@ledgerhq/lumen-design-core';
|
|
3
|
+
import { render, fireEvent } from '@testing-library/react-native';
|
|
4
|
+
import { type ReactNode, createRef } from 'react';
|
|
5
|
+
import type { View } from 'react-native';
|
|
6
|
+
import { ThemeProvider } from '../ThemeProvider/ThemeProvider';
|
|
7
|
+
import { AvatarButton } from './AvatarButton';
|
|
8
|
+
|
|
9
|
+
const TestWrapper = ({ children }: { children: ReactNode }) => (
|
|
10
|
+
<ThemeProvider themes={ledgerLiveThemes} colorScheme='dark' locale='en'>
|
|
11
|
+
{children}
|
|
12
|
+
</ThemeProvider>
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
describe('AvatarButton Component', () => {
|
|
16
|
+
const validSrc = 'https://example.com/photo.jpg';
|
|
17
|
+
|
|
18
|
+
it('should render a pressable wrapping the avatar', () => {
|
|
19
|
+
const { getByRole, getByLabelText } = render(
|
|
20
|
+
<TestWrapper>
|
|
21
|
+
<AvatarButton src={validSrc} alt='Open menu' />
|
|
22
|
+
</TestWrapper>,
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(getByRole('button')).toBeTruthy();
|
|
26
|
+
expect(getByLabelText('Open menu')).toBeTruthy();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should call onPress when pressed', () => {
|
|
30
|
+
const onPress = jest.fn();
|
|
31
|
+
const { getByRole } = render(
|
|
32
|
+
<TestWrapper>
|
|
33
|
+
<AvatarButton src={validSrc} onPress={onPress} />
|
|
34
|
+
</TestWrapper>,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
fireEvent.press(getByRole('button'));
|
|
38
|
+
expect(onPress).toHaveBeenCalledTimes(1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should forward alt to the underlying Avatar', () => {
|
|
42
|
+
const { getByLabelText } = render(
|
|
43
|
+
<TestWrapper>
|
|
44
|
+
<AvatarButton src={validSrc} alt='User profile' />
|
|
45
|
+
</TestWrapper>,
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
expect(getByLabelText('User profile')).toBeTruthy();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should render fallback icon when no src is provided', () => {
|
|
52
|
+
const { getByTestId } = render(
|
|
53
|
+
<TestWrapper>
|
|
54
|
+
<AvatarButton />
|
|
55
|
+
</TestWrapper>,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
expect(getByTestId('avatar-fallback-icon')).toBeTruthy();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should render image when src is provided', () => {
|
|
62
|
+
const { getByTestId } = render(
|
|
63
|
+
<TestWrapper>
|
|
64
|
+
<AvatarButton src={validSrc} />
|
|
65
|
+
</TestWrapper>,
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
const image = getByTestId('avatar-image');
|
|
69
|
+
expect(image.props.source).toEqual({ uri: validSrc });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should forward testID to the pressable', () => {
|
|
73
|
+
const { getByTestId } = render(
|
|
74
|
+
<TestWrapper>
|
|
75
|
+
<AvatarButton src={validSrc} testID='avatar-btn' />
|
|
76
|
+
</TestWrapper>,
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
expect(getByTestId('avatar-btn')).toBeTruthy();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should forward ref to the pressable element', () => {
|
|
83
|
+
const ref = createRef<View>();
|
|
84
|
+
render(
|
|
85
|
+
<TestWrapper>
|
|
86
|
+
<AvatarButton src={validSrc} ref={ref} />
|
|
87
|
+
</TestWrapper>,
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
expect(ref.current).toBeTruthy();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useDisabledContext } from '@ledgerhq/lumen-utils-shared';
|
|
2
|
+
import { StyleSheet, View } from 'react-native';
|
|
3
|
+
import { useStyleSheet } from '../../../styles';
|
|
4
|
+
import { Avatar } from '../Avatar/Avatar';
|
|
5
|
+
import { Pressable } from '../Utility';
|
|
6
|
+
import type { AvatarButtonProps } from './types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An {@link Avatar} wrapped in a Pressable, for use as an interactive trigger.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://ldls-react-native.vercel.app/?path=/docs/rnative-avatar-button--docs Storybook}
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { AvatarButton } from '@ledgerhq/lumen-ui-rnative';
|
|
15
|
+
*
|
|
16
|
+
* <AvatarButton src="https://example.com/photo.jpg" alt="Open user menu" onPress={openMenu} />
|
|
17
|
+
*/
|
|
18
|
+
export const AvatarButton = ({
|
|
19
|
+
src,
|
|
20
|
+
alt,
|
|
21
|
+
size,
|
|
22
|
+
fallbackText,
|
|
23
|
+
fallbackColor,
|
|
24
|
+
appearance,
|
|
25
|
+
disabled: disabledProp = false,
|
|
26
|
+
style,
|
|
27
|
+
...props
|
|
28
|
+
}: AvatarButtonProps) => {
|
|
29
|
+
const disabled = useDisabledContext({
|
|
30
|
+
consumerName: 'AvatarButton',
|
|
31
|
+
mergeWith: { disabled: disabledProp },
|
|
32
|
+
});
|
|
33
|
+
const styles = useStyles({ disabled });
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<Pressable
|
|
37
|
+
accessibilityRole='button'
|
|
38
|
+
disabled={disabled}
|
|
39
|
+
style={[styles.container, style]}
|
|
40
|
+
{...props}
|
|
41
|
+
>
|
|
42
|
+
{({ pressed }) => (
|
|
43
|
+
<>
|
|
44
|
+
<Avatar
|
|
45
|
+
src={src}
|
|
46
|
+
alt={alt}
|
|
47
|
+
size={size}
|
|
48
|
+
fallbackText={fallbackText}
|
|
49
|
+
fallbackColor={fallbackColor}
|
|
50
|
+
appearance={appearance}
|
|
51
|
+
/>
|
|
52
|
+
<View
|
|
53
|
+
style={[styles.overlay, pressed && styles.overlayPressed]}
|
|
54
|
+
pointerEvents='none'
|
|
55
|
+
/>
|
|
56
|
+
</>
|
|
57
|
+
)}
|
|
58
|
+
</Pressable>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const useStyles = ({ disabled }: { disabled: boolean }) =>
|
|
63
|
+
useStyleSheet(
|
|
64
|
+
(t) => ({
|
|
65
|
+
container: {
|
|
66
|
+
...(disabled && { opacity: 0.3 }),
|
|
67
|
+
},
|
|
68
|
+
overlay: {
|
|
69
|
+
...StyleSheet.absoluteFillObject,
|
|
70
|
+
borderRadius: 9999,
|
|
71
|
+
},
|
|
72
|
+
overlayPressed: {
|
|
73
|
+
backgroundColor: t.colors.bg.baseTransparentPressed,
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
[disabled],
|
|
77
|
+
);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StyledPressableProps } from '../../../styles';
|
|
2
|
+
import type { AvatarProps } from '../Avatar/types';
|
|
3
|
+
|
|
4
|
+
export type AvatarButtonProps = Pick<
|
|
5
|
+
AvatarProps,
|
|
6
|
+
'src' | 'alt' | 'size' | 'fallbackText' | 'fallbackColor' | 'appearance'
|
|
7
|
+
> &
|
|
8
|
+
StyledPressableProps & {
|
|
9
|
+
/**
|
|
10
|
+
* When `true`, prevents the user from interacting with the avatar.
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
};
|
|
@@ -3,6 +3,10 @@ import { getDotIndicatorProps } from './getDotIndicatorProps';
|
|
|
3
3
|
|
|
4
4
|
describe('getDotIndicatorProps', () => {
|
|
5
5
|
it('resolves avatar dot indicator props', () => {
|
|
6
|
+
expect(getDotIndicatorProps('avatar', 'xs')).toEqual({
|
|
7
|
+
size: 'sm',
|
|
8
|
+
appearance: 'red',
|
|
9
|
+
});
|
|
6
10
|
expect(getDotIndicatorProps('avatar', 'sm')).toEqual({
|
|
7
11
|
size: 'lg',
|
|
8
12
|
appearance: 'red',
|
|
@@ -11,5 +15,17 @@ describe('getDotIndicatorProps', () => {
|
|
|
11
15
|
size: 'xl',
|
|
12
16
|
appearance: 'red',
|
|
13
17
|
});
|
|
18
|
+
expect(getDotIndicatorProps('avatar', 'lg')).toEqual({
|
|
19
|
+
size: 'xl',
|
|
20
|
+
appearance: 'red',
|
|
21
|
+
});
|
|
22
|
+
expect(getDotIndicatorProps('avatar', 'xl')).toEqual({
|
|
23
|
+
size: 'xl',
|
|
24
|
+
appearance: 'red',
|
|
25
|
+
});
|
|
26
|
+
expect(getDotIndicatorProps('avatar', '2xl')).toEqual({
|
|
27
|
+
size: 'xl',
|
|
28
|
+
appearance: 'red',
|
|
29
|
+
});
|
|
14
30
|
});
|
|
15
31
|
});
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { createPropsResolver } from '@ledgerhq/lumen-utils-shared';
|
|
2
|
+
import type { AvatarSize } from '../Avatar/types';
|
|
2
3
|
import type { DotIndicatorProps } from './types';
|
|
3
4
|
|
|
4
5
|
export const getDotIndicatorProps = createPropsResolver({
|
|
5
|
-
// larger avatars (`lg`, `xl`) don't render a dot indicator
|
|
6
6
|
avatar: {
|
|
7
|
+
xs: { size: 'sm', appearance: 'red' },
|
|
7
8
|
sm: { size: 'lg', appearance: 'red' },
|
|
8
9
|
md: { size: 'xl', appearance: 'red' },
|
|
9
|
-
|
|
10
|
+
lg: { size: 'xl', appearance: 'red' },
|
|
11
|
+
xl: { size: 'xl', appearance: 'red' },
|
|
12
|
+
'2xl': { size: 'xl', appearance: 'red' },
|
|
13
|
+
} satisfies Record<AvatarSize, DotIndicatorProps>,
|
|
10
14
|
});
|