@ledgerhq/lumen-ui-rnative 0.1.73 → 0.1.74

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.
Files changed (26) hide show
  1. package/dist/module/lib/Components/core/DotSymbol/DotSymbol.js +1 -1
  2. package/dist/module/lib/Components/core/Spot/Spot.js +1 -1
  3. package/dist/module/lib/Components/core/TabBar/TabBar.js +293 -132
  4. package/dist/module/lib/Components/core/TabBar/TabBar.js.map +1 -1
  5. package/dist/module/lib/Components/core/TabBar/TabBarContext.js.map +1 -1
  6. package/dist/module/lib/Components/internal/BaseTag/BaseTag.js.map +1 -1
  7. package/dist/typescript/src/lib/Components/core/TabBar/TabBar.d.ts +3 -2
  8. package/dist/typescript/src/lib/Components/core/TabBar/TabBar.d.ts.map +1 -1
  9. package/dist/typescript/src/lib/Components/core/TabBar/TabBarContext.d.ts +3 -0
  10. package/dist/typescript/src/lib/Components/core/TabBar/TabBarContext.d.ts.map +1 -1
  11. package/dist/typescript/src/lib/Components/core/TabBar/types.d.ts +8 -0
  12. package/dist/typescript/src/lib/Components/core/TabBar/types.d.ts.map +1 -1
  13. package/dist/typescript/src/lib/Components/core/Tag/types.d.ts +3 -22
  14. package/dist/typescript/src/lib/Components/core/Tag/types.d.ts.map +1 -1
  15. package/dist/typescript/src/lib/Components/internal/BaseTag/BaseTag.d.ts.map +1 -1
  16. package/dist/typescript/src/lib/Components/internal/BaseTag/types.d.ts +23 -2
  17. package/dist/typescript/src/lib/Components/internal/BaseTag/types.d.ts.map +1 -1
  18. package/package.json +1 -1
  19. package/src/lib/Components/core/DotSymbol/DotSymbol.tsx +1 -1
  20. package/src/lib/Components/core/Spot/Spot.tsx +1 -1
  21. package/src/lib/Components/core/TabBar/TabBar.tsx +213 -107
  22. package/src/lib/Components/core/TabBar/TabBarContext.tsx +3 -0
  23. package/src/lib/Components/core/TabBar/types.ts +9 -0
  24. package/src/lib/Components/core/Tag/types.ts +3 -30
  25. package/src/lib/Components/internal/BaseTag/BaseTag.tsx +2 -3
  26. package/src/lib/Components/internal/BaseTag/types.ts +32 -2
@@ -1,16 +1,17 @@
1
1
  import { BlurView } from '@sbaiahmed1/react-native-blur';
2
2
  import type { ReactElement, ReactNode } from 'react';
3
- import { Children, isValidElement, useEffect, useMemo, useRef } from 'react';
4
- import type { LayoutChangeEvent } from 'react-native';
5
- import { StyleSheet, Text, View } from 'react-native';
3
+ import { Children, isValidElement, useEffect } from 'react';
4
+ import { StyleSheet, Text, useWindowDimensions, View } from 'react-native';
6
5
  import Animated, {
7
6
  cancelAnimation,
7
+ interpolate,
8
8
  useAnimatedStyle,
9
9
  useSharedValue,
10
10
  withDelay,
11
11
  withSequence,
12
12
  withTiming,
13
13
  } from 'react-native-reanimated';
14
+ import type { SharedValue } from 'react-native-reanimated';
14
15
  import { useStyleSheet, useTheme } from '../../../../styles';
15
16
  import { triggerHapticFeedback } from '../../../Haptics';
16
17
  import { RuntimeConstants } from '../../../utils';
@@ -20,10 +21,30 @@ import { Placeholder } from '../../symbols';
20
21
  import { TabBarContextProvider, useTabBarContext } from './TabBarContext';
21
22
  import type { TabBarItemProps, TabBarProps, TabBarValue } from './types';
22
23
 
23
- export const TAB_BAR_HEIGHT = 60;
24
+ export const TAB_BAR_EXPANDED_HEIGHT = 60;
25
+ export const TAB_BAR_COMPACT_HEIGHT = 44;
24
26
  const PILL_INSET = 4;
27
+ const ICON_SIZE = 20;
28
+ const ICON_LABEL_GAP = 4;
29
+
30
+ const useLabelLineHeight = (): { base: number; scaled: number } => {
31
+ const { theme } = useTheme();
32
+ const { fontScale } = useWindowDimensions();
33
+ const base = theme.typographies.body4.lineHeight ?? 0;
34
+ return { base, scaled: base * fontScale };
35
+ };
25
36
 
26
- const useTabBarItemAnimations = ({ isActive }: { isActive: boolean }) => {
37
+ const useTabBarItemAnimations = ({
38
+ isActive,
39
+ densityProgress,
40
+ labelOpacity,
41
+ iconShift,
42
+ }: {
43
+ isActive: boolean;
44
+ densityProgress: SharedValue<number>;
45
+ labelOpacity: SharedValue<number>;
46
+ iconShift: number;
47
+ }) => {
27
48
  const activeProgress = useSharedValue(isActive ? 1 : 0);
28
49
  const pressProgress = useSharedValue(1);
29
50
 
@@ -84,83 +105,136 @@ const useTabBarItemAnimations = ({ isActive }: { isActive: boolean }) => {
84
105
  [activeProgress],
85
106
  );
86
107
 
108
+ const contentStyle = useAnimatedStyle(
109
+ () => ({
110
+ transform: [{ translateY: -iconShift * densityProgress.value }],
111
+ }),
112
+ [densityProgress, iconShift],
113
+ );
114
+
115
+ const labelStyle = useAnimatedStyle(
116
+ () => ({
117
+ opacity: labelOpacity.value,
118
+ // Closes the icon/label gap as the bar collapses.
119
+ transform: [
120
+ { translateY: -ICON_LABEL_GAP * (1 - densityProgress.value) },
121
+ ],
122
+ }),
123
+ [labelOpacity, densityProgress],
124
+ );
125
+
87
126
  return {
88
127
  onPressIn,
89
128
  onPressOut,
90
129
  scaleStyle,
91
130
  activeIconStyle,
92
131
  inactiveIconStyle,
132
+ contentStyle,
133
+ labelStyle,
93
134
  };
94
135
  };
95
136
 
96
- const useTabBarPillLayout = ({
137
+ const useTabBarAnimations = ({
97
138
  active,
139
+ density,
98
140
  children,
99
141
  }: {
100
142
  active: string;
143
+ density: TabBarProps['density'];
101
144
  children: ReactNode;
102
145
  }) => {
103
- const pillProgress = useSharedValue(0);
104
- const itemWidth = useSharedValue(0);
105
- const itemHeight = useSharedValue(0);
106
- const hasLayoutRef = useRef(false);
146
+ const { theme } = useTheme();
147
+ const items = Children.toArray(children).filter(
148
+ isValidElement<TabBarItemProps>,
149
+ );
150
+ const count = items.length;
151
+ const activeIndex = items.findIndex((item) => item.props.value === active);
152
+
153
+ const isExpanded = density === 'expanded';
154
+ const pillIndex = useSharedValue(Math.max(activeIndex, 0));
155
+ const densityProgress = useSharedValue(isExpanded ? 1 : 0);
156
+ const labelOpacity = useSharedValue(isExpanded ? 1 : 0);
107
157
 
108
- const timingConfig = useTimingConfig({
158
+ const pillTimingConfig = useTimingConfig({
109
159
  duration: 300,
110
160
  easing: 'easeInOut',
111
161
  });
162
+ const densityTimingConfig = useTimingConfig({
163
+ duration: 300,
164
+ easing: 'easeOut',
165
+ });
166
+ const labelOutTimingConfig = useTimingConfig({
167
+ duration: 150,
168
+ easing: 'easeOut',
169
+ });
170
+ const labelInTimingConfig = useTimingConfig({
171
+ duration: 200,
172
+ easing: 'easeOut',
173
+ });
174
+ const labelInDelay = theme.motion.delays[75];
175
+ const compactInset = theme.spacings.s32;
176
+ const labelLineHeight = useLabelLineHeight();
177
+ const expandedHeight =
178
+ TAB_BAR_EXPANDED_HEIGHT +
179
+ Math.max(labelLineHeight.scaled - labelLineHeight.base, 0);
112
180
 
113
- const activeIndex = useMemo(() => {
114
- return Children.toArray(children).findIndex((child) => {
115
- if (isValidElement<TabBarItemProps>(child)) {
116
- return child.props.value === active;
117
- }
118
- return false;
119
- });
120
- }, [active, children]);
121
-
122
- const activeIndexRef = useRef(activeIndex);
123
- activeIndexRef.current = activeIndex;
124
-
125
- const onLayout = (e: LayoutChangeEvent): void => {
126
- const { width, height } = e.nativeEvent.layout;
127
- const count = Children.count(children);
128
- const slotWidth = (width - PILL_INSET * 2) / count;
129
-
130
- itemWidth.value = slotWidth;
131
- itemHeight.value = height;
132
-
133
- if (!hasLayoutRef.current) {
134
- hasLayoutRef.current = true;
135
- const index = activeIndexRef.current;
136
- if (index >= 0) {
137
- pillProgress.value = index * slotWidth;
138
- }
181
+ useEffect(() => {
182
+ if (activeIndex >= 0) {
183
+ pillIndex.value = withTiming(activeIndex, pillTimingConfig);
139
184
  }
140
- };
185
+ }, [activeIndex, pillIndex, pillTimingConfig]);
141
186
 
142
187
  useEffect(() => {
143
- if (!hasLayoutRef.current) return;
144
- if (activeIndex >= 0 && itemWidth.value > 0) {
145
- pillProgress.value = withTiming(
146
- activeIndex * itemWidth.value,
147
- timingConfig,
148
- );
149
- }
150
- }, [activeIndex, itemWidth, pillProgress, timingConfig]);
188
+ densityProgress.value = withTiming(isExpanded ? 1 : 0, densityTimingConfig);
189
+ // Labels leave quickly, but only come back once the bar has started
190
+ // making room for them.
191
+ labelOpacity.value = isExpanded
192
+ ? withDelay(labelInDelay, withTiming(1, labelInTimingConfig))
193
+ : withTiming(0, labelOutTimingConfig);
194
+ }, [
195
+ isExpanded,
196
+ densityProgress,
197
+ labelOpacity,
198
+ densityTimingConfig,
199
+ labelInTimingConfig,
200
+ labelOutTimingConfig,
201
+ labelInDelay,
202
+ ]);
203
+
204
+ useEffect(
205
+ () => () => {
206
+ cancelAnimation(pillIndex);
207
+ cancelAnimation(densityProgress);
208
+ cancelAnimation(labelOpacity);
209
+ },
210
+ [pillIndex, densityProgress, labelOpacity],
211
+ );
151
212
 
152
- useEffect(() => () => cancelAnimation(pillProgress), [pillProgress]);
213
+ const barStyle = useAnimatedStyle(
214
+ () => ({
215
+ height: interpolate(
216
+ densityProgress.value,
217
+ [0, 1],
218
+ [TAB_BAR_COMPACT_HEIGHT, expandedHeight],
219
+ ),
220
+ marginHorizontal: interpolate(
221
+ densityProgress.value,
222
+ [0, 1],
223
+ [compactInset, 0],
224
+ ),
225
+ }),
226
+ [densityProgress, compactInset, expandedHeight],
227
+ );
153
228
 
154
- const animatedPillStyle = useAnimatedStyle(
229
+ const pillStyle = useAnimatedStyle(
155
230
  () => ({
156
- transform: [{ translateX: pillProgress.value }],
157
- width: itemWidth.value,
158
- height: itemHeight.value - PILL_INSET * 2,
231
+ width: `${100 / count}%`,
232
+ left: `${(pillIndex.value * 100) / count}%`,
159
233
  }),
160
- [pillProgress, itemWidth, itemHeight],
234
+ [pillIndex, count],
161
235
  );
162
236
 
163
- return { onLayout, animatedPillStyle };
237
+ return { densityProgress, labelOpacity, barStyle, pillStyle };
164
238
  };
165
239
 
166
240
  export function TabBarItem({
@@ -172,7 +246,9 @@ export function TabBarItem({
172
246
  ...props
173
247
  }: TabBarItemProps) {
174
248
  const styles = useStyles();
175
- const { active, onTabPress } = useTabBarContext();
249
+ const labelLineHeight = useLabelLineHeight();
250
+ const { active, onTabPress, densityProgress, labelOpacity } =
251
+ useTabBarContext();
176
252
 
177
253
  const isActive = active === value;
178
254
  const Icon = icon ?? Placeholder;
@@ -184,7 +260,14 @@ export function TabBarItem({
184
260
  scaleStyle,
185
261
  activeIconStyle,
186
262
  inactiveIconStyle,
187
- } = useTabBarItemAnimations({ isActive });
263
+ contentStyle,
264
+ labelStyle,
265
+ } = useTabBarItemAnimations({
266
+ isActive,
267
+ densityProgress,
268
+ labelOpacity,
269
+ iconShift: label ? (ICON_LABEL_GAP + labelLineHeight.scaled) / 2 : 0,
270
+ });
188
271
 
189
272
  function onPress() {
190
273
  onTabPress(value);
@@ -201,24 +284,28 @@ export function TabBarItem({
201
284
  accessibilityState={{ selected: isActive }}
202
285
  {...props}
203
286
  >
204
- <Animated.View style={scaleStyle}>
205
- <Box style={styles.itemIconContainer}>
206
- <Animated.View style={[styles.itemIcon, inactiveIconStyle]}>
207
- <Icon size={20} />
287
+ <Animated.View style={[styles.itemContent, contentStyle]}>
288
+ <Animated.View style={scaleStyle}>
289
+ <Box style={styles.itemIconContainer}>
290
+ <Animated.View style={[styles.itemIcon, inactiveIconStyle]}>
291
+ <Icon size={ICON_SIZE} />
292
+ </Animated.View>
293
+ <Animated.View style={[styles.itemIcon, activeIconStyle]}>
294
+ <ActiveIcon size={ICON_SIZE} />
295
+ </Animated.View>
296
+ </Box>
297
+ </Animated.View>
298
+ {label && (
299
+ <Animated.View style={[styles.itemLabel, labelStyle]}>
300
+ <Text
301
+ style={[styles.itemText, isActive && styles.activeItemText]}
302
+ numberOfLines={1}
303
+ >
304
+ {label}
305
+ </Text>
208
306
  </Animated.View>
209
- <Animated.View style={[styles.itemIcon, activeIconStyle]}>
210
- <ActiveIcon size={20} />
211
- </Animated.View>
212
- </Box>
307
+ )}
213
308
  </Animated.View>
214
- {label && (
215
- <Text
216
- style={[styles.itemText, isActive && styles.activeItemText]}
217
- numberOfLines={1}
218
- >
219
- {label}
220
- </Text>
221
- )}
222
309
  </Pressable>
223
310
  );
224
311
  }
@@ -244,49 +331,53 @@ export function TabBarItem({
244
331
  export function TabBar<T extends TabBarValue = TabBarValue>({
245
332
  active,
246
333
  onTabPress,
334
+ density = 'expanded',
247
335
  children,
248
336
  ...props
249
337
  }: TabBarProps<T>) {
250
338
  const styles = useStyles();
251
339
  const { theme, colorScheme } = useTheme();
252
340
 
253
- const { onLayout, animatedPillStyle } = useTabBarPillLayout({
254
- active,
255
- children,
256
- });
341
+ const { densityProgress, labelOpacity, barStyle, pillStyle } =
342
+ useTabBarAnimations({
343
+ active,
344
+ density,
345
+ children,
346
+ });
257
347
 
258
348
  return (
259
349
  <TabBarContextProvider
260
350
  value={{
261
351
  active,
262
352
  onTabPress: (value) => onTabPress(value as T),
353
+ densityProgress,
354
+ labelOpacity,
263
355
  }}
264
356
  >
265
- <Box
266
- style={styles.container}
267
- onLayout={onLayout}
268
- accessibilityRole='tablist'
269
- {...props}
270
- >
271
- {children}
272
- {RuntimeConstants.isAndroid ? (
273
- <View style={styles.fallbackBackground} />
274
- ) : (
275
- <BlurView
276
- style={styles.blur}
277
- blurAmount={theme.blur.lg}
278
- blurType={colorScheme === 'dark' ? 'dark' : 'light'}
279
- overlayColor={
280
- colorScheme === 'dark'
281
- ? 'rgba(0,0,0,0.15)'
282
- : 'rgba(255,255,255,0.2)'
283
- }
284
- reducedTransparencyFallbackColor={
285
- styles.fallbackBackground.backgroundColor
286
- }
287
- />
288
- )}
289
- <Animated.View style={[styles.pill, animatedPillStyle]} />
357
+ <Box accessibilityRole='tablist' {...props}>
358
+ <Animated.View style={[styles.container, barStyle]}>
359
+ {children}
360
+ {RuntimeConstants.isAndroid ? (
361
+ <View style={styles.fallbackBackground} />
362
+ ) : (
363
+ <BlurView
364
+ style={styles.blur}
365
+ blurAmount={theme.blur.lg}
366
+ blurType={colorScheme === 'dark' ? 'dark' : 'light'}
367
+ overlayColor={
368
+ colorScheme === 'dark'
369
+ ? 'rgba(0,0,0,0.15)'
370
+ : 'rgba(255,255,255,0.2)'
371
+ }
372
+ reducedTransparencyFallbackColor={
373
+ styles.fallbackBackground.backgroundColor
374
+ }
375
+ />
376
+ )}
377
+ <View style={styles.pillTrack}>
378
+ <Animated.View style={[styles.pill, pillStyle]} />
379
+ </View>
380
+ </Animated.View>
290
381
  </Box>
291
382
  </TabBarContextProvider>
292
383
  );
@@ -296,7 +387,6 @@ const useStyles = () =>
296
387
  useStyleSheet(
297
388
  (t) => ({
298
389
  container: {
299
- minHeight: TAB_BAR_HEIGHT,
300
390
  flexDirection: 'row',
301
391
  justifyContent: 'center',
302
392
  padding: t.spacings.s4,
@@ -324,10 +414,19 @@ const useStyles = () =>
324
414
  },
325
415
  item: {
326
416
  flex: 1,
327
- paddingVertical: t.spacings.s4,
328
- alignItems: 'center',
417
+ alignSelf: 'stretch',
418
+ marginVertical: -t.spacings.s4,
329
419
  justifyContent: 'center',
330
- gap: t.spacings.s2,
420
+ },
421
+ itemContent: {
422
+ alignItems: 'center',
423
+ },
424
+ itemLabel: {
425
+ position: 'absolute',
426
+ top: ICON_SIZE + ICON_LABEL_GAP,
427
+ left: 0,
428
+ right: 0,
429
+ alignItems: 'center',
331
430
  },
332
431
  itemText: {
333
432
  ...t.typographies.body4,
@@ -338,18 +437,25 @@ const useStyles = () =>
338
437
  position: 'absolute',
339
438
  },
340
439
  itemIconContainer: {
341
- width: 20,
342
- height: 20,
440
+ width: ICON_SIZE,
441
+ height: ICON_SIZE,
343
442
  position: 'relative',
344
443
  },
345
444
  activeItemText: {
346
445
  ...t.typographies.body4SemiBold,
347
446
  },
348
- pill: {
447
+ pillTrack: {
349
448
  position: 'absolute',
350
449
  pointerEvents: 'none',
351
450
  top: PILL_INSET,
451
+ bottom: PILL_INSET,
352
452
  left: PILL_INSET,
453
+ right: PILL_INSET,
454
+ },
455
+ pill: {
456
+ position: 'absolute',
457
+ top: 0,
458
+ bottom: 0,
353
459
  borderRadius: t.borderRadius.full,
354
460
  backgroundColor: t.colors.bg.mutedTransparent,
355
461
  },
@@ -1,8 +1,11 @@
1
1
  import { createSafeContext } from '@ledgerhq/lumen-utils-shared';
2
+ import type { SharedValue } from 'react-native-reanimated';
2
3
 
3
4
  export type TabBarContextValue = {
4
5
  active?: string;
5
6
  onTabPress: (value: string) => void;
7
+ densityProgress: SharedValue<number>;
8
+ labelOpacity: SharedValue<number>;
6
9
  };
7
10
 
8
11
  const [TabBarContextProvider, _useTabBarSafeContext] =
@@ -9,11 +9,20 @@ type IconComponent = ComponentType<{
9
9
 
10
10
  export type TabBarValue = string;
11
11
 
12
+ export type TabBarDensity = 'compact' | 'expanded';
13
+
12
14
  export type TabBarProps<T extends TabBarValue = TabBarValue> = {
13
15
  /**
14
16
  * The value of the currently active tab.
15
17
  */
16
18
  active: T;
19
+ /**
20
+ * The density of the tab bar. `compact` hides the labels, shrinks the bar's
21
+ * size and insets it horizontally within its container.
22
+ * The transition is animated.
23
+ * @default 'expanded'
24
+ */
25
+ density?: TabBarDensity;
17
26
  /**
18
27
  * The callback function called when a tab is pressed.
19
28
  */
@@ -1,37 +1,10 @@
1
1
  import type { ComponentType } from 'react';
2
- import type { StyledViewProps } from '../../../../styles';
2
+ import type { BaseTagSharedProps } from '../../internal/BaseTag';
3
3
  import type { IconProps } from '../../symbols/Icon';
4
4
 
5
- export type TagProps = {
6
- /**
7
- * The appearance of the tag.
8
- */
9
- appearance?:
10
- | 'base'
11
- | 'gray'
12
- | 'accent'
13
- | 'accent-subtle'
14
- | 'success'
15
- | 'error'
16
- | 'warning'
17
- | 'white';
5
+ export type TagProps = BaseTagSharedProps & {
18
6
  /**
19
7
  * The icon of the tag.
20
8
  */
21
9
  icon?: ComponentType<IconProps>;
22
- /**
23
- * The label of the tag.
24
- */
25
- label: string;
26
- /**
27
- * The size of the tag.
28
- * @default 'md'
29
- */
30
- size?: 'sm' | 'md';
31
- /**
32
- * When `true`, prevents the user from interacting with the tag.
33
- *
34
- * @default false
35
- */
36
- disabled?: boolean;
37
- } & StyledViewProps;
10
+ };
@@ -1,12 +1,11 @@
1
1
  import { useDisabledContext } from '@ledgerhq/lumen-utils-shared';
2
2
  import { StyleSheet, Text } from 'react-native';
3
3
  import { useStyleSheet } from '../../../../styles';
4
- import type { TagProps } from '../../core/Tag/types';
5
4
  import { Box } from '../../primitives';
6
5
  import type { BaseTagProps, BaseTagVariant } from './types';
7
6
 
8
- type Appearance = NonNullable<TagProps['appearance']>;
9
- type Size = NonNullable<TagProps['size']>;
7
+ type Appearance = NonNullable<BaseTagProps['appearance']>;
8
+ type Size = NonNullable<BaseTagProps['size']>;
10
9
 
11
10
  const useBaseTagStyles = ({
12
11
  appearance,
@@ -1,10 +1,40 @@
1
1
  import type { ReactNode } from 'react';
2
2
  import type { StyleProp, TextStyle } from 'react-native';
3
- import type { TagProps } from '../../core/Tag/types';
3
+ import type { StyledViewProps } from '../../../../styles';
4
+
5
+ export type BaseTagSharedProps = {
6
+ /**
7
+ * The appearance of the tag.
8
+ */
9
+ appearance?:
10
+ | 'base'
11
+ | 'gray'
12
+ | 'accent'
13
+ | 'accent-subtle'
14
+ | 'success'
15
+ | 'error'
16
+ | 'warning'
17
+ | 'white';
18
+ /**
19
+ * The label of the tag.
20
+ */
21
+ label: string;
22
+ /**
23
+ * The size of the tag.
24
+ * @default 'md'
25
+ */
26
+ size?: 'sm' | 'md';
27
+ /**
28
+ * When `true`, prevents the user from interacting with the tag.
29
+ *
30
+ * @default false
31
+ */
32
+ disabled?: boolean;
33
+ } & StyledViewProps;
4
34
 
5
35
  export type BaseTagVariant = 'tag' | 'media';
6
36
 
7
- export type BaseTagProps = Omit<TagProps, 'icon'> & {
37
+ export type BaseTagProps = BaseTagSharedProps & {
8
38
  variant: BaseTagVariant;
9
39
  consumerName: string;
10
40
  renderIcon?: (style: StyleProp<TextStyle>) => ReactNode;