mobile-design 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/LICENSE-Apache-2.0.txt +55 -0
- package/NOTICE +11 -0
- package/README.md +96 -2
- package/assets/fonts/MaterialSymbolsRounded.ttf +0 -0
- package/assets/fonts/MaterialSymbolsRounded.woff2 +0 -0
- package/assets/fonts/roboto/OFL.txt +93 -0
- package/assets/fonts/roboto/Roboto_400Regular.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_400Regular_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_500Medium_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_600SemiBold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_700Bold_Italic.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold.woff2 +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.ttf +0 -0
- package/assets/fonts/roboto/Roboto_800ExtraBold_Italic.woff2 +0 -0
- package/assets/logo.svg +13 -0
- package/package.json +62 -3
- package/src/catalog.ts +683 -0
- package/src/color-scheme.ts +68 -0
- package/src/color.ts +35 -0
- package/src/components/action-surface.tsx +83 -0
- package/src/components/appbar-layout.ts +85 -0
- package/src/components/appbar-scroll-behavior.tsx +386 -0
- package/src/components/appbar.tsx +474 -0
- package/src/components/avatar.tsx +135 -0
- package/src/components/bottom-sheet-text-field.tsx +13 -0
- package/src/components/bottom-sheet-text-input.tsx +2 -0
- package/src/components/bottom-sheet-text-input.web.tsx +2 -0
- package/src/components/button-group.tsx +996 -0
- package/src/components/button.tsx +504 -0
- package/src/components/card.tsx +284 -0
- package/src/components/carousel-full-screen.tsx +374 -0
- package/src/components/carousel-layout.ts +418 -0
- package/src/components/carousel-scroll-controller.tsx +303 -0
- package/src/components/carousel-vertical-layout.ts +92 -0
- package/src/components/carousel-vertical.tsx +505 -0
- package/src/components/carousel.tsx +678 -0
- package/src/components/choice-motion.tsx +108 -0
- package/src/components/choice-types.ts +52 -0
- package/src/components/choice.tsx +400 -0
- package/src/components/choice.web.ts +1 -0
- package/src/components/circular-progress-status.tsx +218 -0
- package/src/components/container-transform.tsx +304 -0
- package/src/components/content.tsx +1450 -0
- package/src/components/date-picker-calendar.tsx +933 -0
- package/src/components/date-picker-months.tsx +102 -0
- package/src/components/date-picker-utils.ts +96 -0
- package/src/components/date-picker.tsx +641 -0
- package/src/components/dialog.tsx +340 -0
- package/src/components/docked-date-picker.tsx +525 -0
- package/src/components/drawer.tsx +248 -0
- package/src/components/fab.tsx +1221 -0
- package/src/components/focus-order.ts +40 -0
- package/src/components/full-screen-dialog.tsx +234 -0
- package/src/components/icon-button.tsx +72 -0
- package/src/components/list-focus.tsx +182 -0
- package/src/components/list-reorder.tsx +349 -0
- package/src/components/loading-indicator.tsx +378 -0
- package/src/components/menu.tsx +1091 -0
- package/src/components/navigation-bar.tsx +390 -0
- package/src/components/navigation-focus.ts +45 -0
- package/src/components/navigation-rail-layout.ts +53 -0
- package/src/components/navigation.tsx +682 -0
- package/src/components/popup.tsx +624 -0
- package/src/components/press-scope.ts +17 -0
- package/src/components/progress-indicator-paths.ts +254 -0
- package/src/components/progress-indicator.tsx +570 -0
- package/src/components/progress.tsx +39 -0
- package/src/components/pull-to-refresh.tsx +379 -0
- package/src/components/rich-tooltip.tsx +183 -0
- package/src/components/search-icon-button.tsx +8 -0
- package/src/components/search-trigger.tsx +28 -0
- package/src/components/search-view.tsx +790 -0
- package/src/components/searchbar.tsx +190 -0
- package/src/components/segmented-buttons.tsx +456 -0
- package/src/components/selection-controls.tsx +760 -0
- package/src/components/sheets.tsx +1027 -0
- package/src/components/slider.tsx +1290 -0
- package/src/components/snackbar.tsx +430 -0
- package/src/components/split-button.tsx +525 -0
- package/src/components/swipe-list-item.tsx +793 -0
- package/src/components/switch.tsx +319 -0
- package/src/components/tabs-pager.tsx +241 -0
- package/src/components/tabs-pager.web.tsx +110 -0
- package/src/components/tabs.tsx +759 -0
- package/src/components/text-field.tsx +673 -0
- package/src/components/time-picker.tsx +1248 -0
- package/src/components/toolbar.tsx +590 -0
- package/src/components/tooltip.tsx +273 -0
- package/src/components/web-choice.tsx +572 -0
- package/src/core.tsx +169 -0
- package/src/font-assets.ts +28 -0
- package/src/font-assets.web.ts +25 -0
- package/src/index.ts +139 -0
- package/src/material-symbol-codepoints.ts +67 -0
- package/src/material-symbol-paths.ts +340 -0
- package/src/material-symbols.tsx +155 -0
- package/src/native-modal.tsx +50 -0
- package/src/outside-interaction.tsx +39 -0
- package/src/paper-icon-aliases.ts +147 -0
- package/src/paper-internals.d.ts +15 -0
- package/src/paper-motion-bridge.tsx +11 -0
- package/src/paper-motion.tsx +24 -0
- package/src/primitives.tsx +13 -0
- package/src/ripple.tsx +7 -0
- package/src/ripple.web.tsx +108 -0
- package/src/scroll-indicators.ts +12 -0
- package/src/typography.ts +9 -0
- package/src/web-accessibility.ts +19 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type ComponentProps } from 'react';
|
|
2
|
+
import { Animated, Platform, Pressable, StyleSheet, View, type GestureResponderEvent } from 'react-native';
|
|
3
|
+
import { Card as PaperCard, Surface, ThemeProvider } from 'react-native-paper';
|
|
4
|
+
import { useMobileTheme } from '../core';
|
|
5
|
+
import { withAlpha } from '../color';
|
|
6
|
+
import { TouchableRipple } from '../ripple';
|
|
7
|
+
import { usePressScope } from './press-scope';
|
|
8
|
+
|
|
9
|
+
type PressProps = ComponentProps<typeof Pressable>;
|
|
10
|
+
export type CardProps = Omit<ComponentProps<typeof PaperCard>, 'onPressIn' | 'onPressOut' | 'elevation'> & {
|
|
11
|
+
/** Link destination in a browser. Native navigation remains in onPress. */
|
|
12
|
+
href?: string;
|
|
13
|
+
/** Browser attributes for the link destination. */
|
|
14
|
+
hrefAttrs?: { rel?: string };
|
|
15
|
+
/** Material names; Paper's contained mode corresponds to the filled variant. */
|
|
16
|
+
variant?: 'elevated' | 'filled' | 'outlined';
|
|
17
|
+
elevation?: 0 | 1 | 2 | 3 | 4 | 5 | Animated.Value;
|
|
18
|
+
/** Drag state comes from the application's actual drag controller. */
|
|
19
|
+
dragged?: boolean;
|
|
20
|
+
onPressIn?: (event: GestureResponderEvent) => void;
|
|
21
|
+
onPressOut?: (event: GestureResponderEvent) => void;
|
|
22
|
+
onHoverIn?: PressProps['onHoverIn'];
|
|
23
|
+
onHoverOut?: PressProps['onHoverOut'];
|
|
24
|
+
onFocus?: PressProps['onFocus'];
|
|
25
|
+
onBlur?: PressProps['onBlur'];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Paper content slots are retained; current M3 container/state tokens are applied locally. */
|
|
29
|
+
function CardRoot({
|
|
30
|
+
variant,
|
|
31
|
+
mode: suppliedMode = 'elevated',
|
|
32
|
+
elevation: suppliedElevation,
|
|
33
|
+
dragged = false,
|
|
34
|
+
href,
|
|
35
|
+
hrefAttrs,
|
|
36
|
+
disabled = false,
|
|
37
|
+
onPress,
|
|
38
|
+
onLongPress,
|
|
39
|
+
onPressIn,
|
|
40
|
+
onPressOut,
|
|
41
|
+
onHoverIn,
|
|
42
|
+
onHoverOut,
|
|
43
|
+
onFocus,
|
|
44
|
+
onBlur,
|
|
45
|
+
delayLongPress,
|
|
46
|
+
children,
|
|
47
|
+
style,
|
|
48
|
+
contentStyle,
|
|
49
|
+
theme: overrides,
|
|
50
|
+
testID = 'mobile-card',
|
|
51
|
+
accessible,
|
|
52
|
+
accessibilityRole,
|
|
53
|
+
accessibilityLabel,
|
|
54
|
+
accessibilityHint,
|
|
55
|
+
accessibilityState,
|
|
56
|
+
ref,
|
|
57
|
+
...props
|
|
58
|
+
}: CardProps) {
|
|
59
|
+
const mode = variant === 'filled' ? 'contained' : (variant ?? suppliedMode);
|
|
60
|
+
const theme = useMobileTheme(overrides);
|
|
61
|
+
const canPress = usePressScope(disabled);
|
|
62
|
+
const [pressed, setPressed] = useState(false);
|
|
63
|
+
const [hovered, setHovered] = useState(false);
|
|
64
|
+
const [focused, setFocused] = useState(false);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (disabled) {
|
|
67
|
+
setPressed(false);
|
|
68
|
+
setHovered(false);
|
|
69
|
+
setFocused(false);
|
|
70
|
+
}
|
|
71
|
+
}, [disabled]);
|
|
72
|
+
const base = mode === 'elevated' ? 1 : 0;
|
|
73
|
+
const target = disabled
|
|
74
|
+
? base
|
|
75
|
+
: dragged
|
|
76
|
+
? mode === 'elevated'
|
|
77
|
+
? 4
|
|
78
|
+
: 3
|
|
79
|
+
: pressed || focused
|
|
80
|
+
? base
|
|
81
|
+
: hovered
|
|
82
|
+
? mode === 'elevated'
|
|
83
|
+
? 2
|
|
84
|
+
: 1
|
|
85
|
+
: base;
|
|
86
|
+
const elevation = useRef(new Animated.Value(typeof suppliedElevation === 'number' ? suppliedElevation : base)).current;
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
elevation.stopAnimation();
|
|
89
|
+
if (suppliedElevation instanceof Animated.Value) return;
|
|
90
|
+
const value = typeof suppliedElevation === 'number' ? suppliedElevation : target;
|
|
91
|
+
if (theme.animation.scale === 0) {
|
|
92
|
+
elevation.setValue(value);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
// Paper Surface consumes an RN Animated.Value for shadow/color interpolation.
|
|
96
|
+
// Shadow elevation is not a native-driver property in Paper's renderer.
|
|
97
|
+
const spring = theme.motion.spring.default.effects;
|
|
98
|
+
const animation = Animated.spring(elevation, {
|
|
99
|
+
toValue: value,
|
|
100
|
+
stiffness: spring.stiffness,
|
|
101
|
+
damping: 2 * spring.damping * Math.sqrt(spring.stiffness),
|
|
102
|
+
mass: 1,
|
|
103
|
+
useNativeDriver: false,
|
|
104
|
+
isInteraction: false,
|
|
105
|
+
});
|
|
106
|
+
animation.start();
|
|
107
|
+
return () => animation.stop();
|
|
108
|
+
}, [elevation, suppliedElevation, target, theme]);
|
|
109
|
+
const interactive = !!(onPress || onLongPress || onPressIn || onPressOut || (Platform.OS === 'web' && href));
|
|
110
|
+
const CardPressable = (Platform.OS === 'web' ? TouchableRipple : Pressable) as typeof Pressable;
|
|
111
|
+
const background =
|
|
112
|
+
mode === 'contained'
|
|
113
|
+
? theme.colors.surfaceContainerHighest
|
|
114
|
+
: mode === 'outlined'
|
|
115
|
+
? theme.colors.surface
|
|
116
|
+
: theme.colors.surfaceContainerLow;
|
|
117
|
+
const disabledLayer = mode === 'contained' ? theme.colors.surfaceVariant : theme.colors.surface;
|
|
118
|
+
const border = disabled ? theme.colors.outline : focused ? theme.colors.onSurface : theme.colors.outlineVariant;
|
|
119
|
+
const stateOpacity = disabled
|
|
120
|
+
? 0
|
|
121
|
+
: dragged
|
|
122
|
+
? 0.16
|
|
123
|
+
: pressed
|
|
124
|
+
? Platform.OS === 'android' || (Platform.OS === 'web' && theme.animation.scale !== 0)
|
|
125
|
+
? 0
|
|
126
|
+
: 0.1
|
|
127
|
+
: focused
|
|
128
|
+
? 0.1
|
|
129
|
+
: hovered
|
|
130
|
+
? 0.08
|
|
131
|
+
: 0;
|
|
132
|
+
const card = (
|
|
133
|
+
<View pointerEvents={disabled ? 'none' : 'auto'} style={{ borderRadius: 12, overflow: 'hidden' }}>
|
|
134
|
+
{disabled && mode !== 'outlined' ? (
|
|
135
|
+
<View pointerEvents="none" style={[StyleSheet.absoluteFill, { backgroundColor: disabledLayer, opacity: 0.38 }]} />
|
|
136
|
+
) : null}
|
|
137
|
+
<PaperCard
|
|
138
|
+
mode="contained"
|
|
139
|
+
theme={theme}
|
|
140
|
+
style={{ backgroundColor: 'transparent', borderRadius: 12 }}
|
|
141
|
+
contentStyle={contentStyle}
|
|
142
|
+
testID={`${testID}-content`}
|
|
143
|
+
>
|
|
144
|
+
{children}
|
|
145
|
+
</PaperCard>
|
|
146
|
+
{stateOpacity ? (
|
|
147
|
+
<View
|
|
148
|
+
pointerEvents="none"
|
|
149
|
+
style={[StyleSheet.absoluteFill, { backgroundColor: theme.colors.onSurface, opacity: stateOpacity }]}
|
|
150
|
+
/>
|
|
151
|
+
) : null}
|
|
152
|
+
</View>
|
|
153
|
+
);
|
|
154
|
+
return (
|
|
155
|
+
<ThemeProvider theme={theme}>
|
|
156
|
+
<Surface
|
|
157
|
+
{...props}
|
|
158
|
+
ref={ref}
|
|
159
|
+
elevation={suppliedElevation instanceof Animated.Value ? suppliedElevation : elevation}
|
|
160
|
+
style={[{ borderRadius: 12, backgroundColor: disabled && mode === 'contained' ? theme.colors.surface : background }, style]}
|
|
161
|
+
testID={`${testID}-container`}
|
|
162
|
+
>
|
|
163
|
+
{interactive ? (
|
|
164
|
+
<CardPressable
|
|
165
|
+
key={disabled ? 'inactive' : 'interactive'}
|
|
166
|
+
accessible={accessible}
|
|
167
|
+
accessibilityRole={accessibilityRole ?? (href ? 'link' : 'button')}
|
|
168
|
+
accessibilityLabel={accessibilityLabel}
|
|
169
|
+
accessibilityHint={accessibilityHint}
|
|
170
|
+
accessibilityState={{ ...accessibilityState, disabled }}
|
|
171
|
+
{...(Platform.OS === 'web'
|
|
172
|
+
? {
|
|
173
|
+
href: disabled ? undefined : href,
|
|
174
|
+
hrefAttrs,
|
|
175
|
+
'aria-disabled': disabled,
|
|
176
|
+
'aria-selected': accessibilityState?.selected,
|
|
177
|
+
}
|
|
178
|
+
: {})}
|
|
179
|
+
disabled={disabled}
|
|
180
|
+
delayLongPress={delayLongPress}
|
|
181
|
+
onPress={
|
|
182
|
+
onPress
|
|
183
|
+
? (event) => {
|
|
184
|
+
if (canPress()) onPress(event);
|
|
185
|
+
}
|
|
186
|
+
: undefined
|
|
187
|
+
}
|
|
188
|
+
onLongPress={
|
|
189
|
+
onLongPress
|
|
190
|
+
? () => {
|
|
191
|
+
if (canPress()) onLongPress();
|
|
192
|
+
}
|
|
193
|
+
: undefined
|
|
194
|
+
}
|
|
195
|
+
onPressIn={(event) => {
|
|
196
|
+
if (!canPress()) return;
|
|
197
|
+
if (Platform.OS === 'web') setFocused(typeof (event.nativeEvent as { key?: unknown }).key === 'string');
|
|
198
|
+
setPressed(true);
|
|
199
|
+
onPressIn?.(event);
|
|
200
|
+
}}
|
|
201
|
+
onPressOut={(event) => {
|
|
202
|
+
setPressed(false);
|
|
203
|
+
onPressOut?.(event);
|
|
204
|
+
}}
|
|
205
|
+
onHoverIn={(event) => {
|
|
206
|
+
setHovered(true);
|
|
207
|
+
onHoverIn?.(event);
|
|
208
|
+
}}
|
|
209
|
+
onHoverOut={(event) => {
|
|
210
|
+
setHovered(false);
|
|
211
|
+
onHoverOut?.(event);
|
|
212
|
+
}}
|
|
213
|
+
onFocus={(event) => {
|
|
214
|
+
if (!canPress()) return;
|
|
215
|
+
const target = event.currentTarget as unknown as { matches?: (selector: string) => boolean };
|
|
216
|
+
setFocused(Platform.OS !== 'web' || (target.matches?.(':focus-visible') ?? true));
|
|
217
|
+
onFocus?.(event);
|
|
218
|
+
}}
|
|
219
|
+
onBlur={(event) => {
|
|
220
|
+
setFocused(false);
|
|
221
|
+
setPressed(false);
|
|
222
|
+
onBlur?.(event);
|
|
223
|
+
}}
|
|
224
|
+
android_ripple={{
|
|
225
|
+
color:
|
|
226
|
+
typeof theme.colors.onSurface === 'string'
|
|
227
|
+
? withAlpha(theme.colors.onSurface, 0.1)
|
|
228
|
+
: theme.colors.onSurface,
|
|
229
|
+
foreground: true,
|
|
230
|
+
}}
|
|
231
|
+
{...(Platform.OS === 'web'
|
|
232
|
+
? {
|
|
233
|
+
rippleColor:
|
|
234
|
+
typeof theme.colors.onSurface === 'string'
|
|
235
|
+
? withAlpha(theme.colors.onSurface, 0.1)
|
|
236
|
+
: theme.colors.onSurface,
|
|
237
|
+
}
|
|
238
|
+
: {})}
|
|
239
|
+
style={{ borderRadius: 12, overflow: 'hidden', backgroundColor: 'transparent' }}
|
|
240
|
+
testID={testID}
|
|
241
|
+
>
|
|
242
|
+
{card}
|
|
243
|
+
</CardPressable>
|
|
244
|
+
) : (
|
|
245
|
+
<View
|
|
246
|
+
accessible={accessible}
|
|
247
|
+
accessibilityLabel={accessibilityLabel}
|
|
248
|
+
accessibilityHint={accessibilityHint}
|
|
249
|
+
accessibilityRole={accessibilityRole}
|
|
250
|
+
accessibilityState={accessibilityState}
|
|
251
|
+
testID={testID}
|
|
252
|
+
>
|
|
253
|
+
{card}
|
|
254
|
+
</View>
|
|
255
|
+
)}
|
|
256
|
+
{mode === 'outlined' ? (
|
|
257
|
+
<View
|
|
258
|
+
pointerEvents="none"
|
|
259
|
+
style={[
|
|
260
|
+
StyleSheet.absoluteFill,
|
|
261
|
+
{ borderRadius: 12, borderWidth: 1, borderColor: border, opacity: disabled ? 0.12 : 1 },
|
|
262
|
+
]}
|
|
263
|
+
/>
|
|
264
|
+
) : null}
|
|
265
|
+
{focused && !disabled ? (
|
|
266
|
+
<View
|
|
267
|
+
pointerEvents="none"
|
|
268
|
+
style={[
|
|
269
|
+
StyleSheet.absoluteFill,
|
|
270
|
+
{ margin: -5, borderWidth: 3, borderColor: theme.colors.secondary, borderRadius: 17 },
|
|
271
|
+
]}
|
|
272
|
+
/>
|
|
273
|
+
) : null}
|
|
274
|
+
</Surface>
|
|
275
|
+
</ThemeProvider>
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export const Card = Object.assign(CardRoot, {
|
|
280
|
+
Title: PaperCard.Title,
|
|
281
|
+
Content: PaperCard.Content,
|
|
282
|
+
Cover: PaperCard.Cover,
|
|
283
|
+
Actions: PaperCard.Actions,
|
|
284
|
+
});
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { getScrollIndicatorProps, type ScrollIndicatorOptions } from '../scroll-indicators';
|
|
2
|
+
import { Image } from 'expo-image';
|
|
3
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
4
|
+
import { Platform, StyleSheet, View, useWindowDimensions, type ViewInstance } from 'react-native';
|
|
5
|
+
import Animated, { useAnimatedReaction } from 'react-native-reanimated';
|
|
6
|
+
import { scheduleOnRN } from 'react-native-worklets';
|
|
7
|
+
|
|
8
|
+
import { Icon } from '../primitives';
|
|
9
|
+
import { IconButton } from '../primitives';
|
|
10
|
+
import { Text } from '../primitives';
|
|
11
|
+
import { TouchableRipple } from '../primitives';
|
|
12
|
+
import type { CarouselItem, CarouselProps } from './carousel';
|
|
13
|
+
import { useCarouselScrollController } from './carousel-scroll-controller';
|
|
14
|
+
import { useMobileTheme } from '../core';
|
|
15
|
+
import { usePressScope } from './press-scope';
|
|
16
|
+
|
|
17
|
+
function FullScreenItem({
|
|
18
|
+
item,
|
|
19
|
+
index,
|
|
20
|
+
count,
|
|
21
|
+
height,
|
|
22
|
+
onPress,
|
|
23
|
+
renderItemContent,
|
|
24
|
+
onFocus,
|
|
25
|
+
onNavigate,
|
|
26
|
+
showText,
|
|
27
|
+
disabled,
|
|
28
|
+
tabStop,
|
|
29
|
+
textInsetBottom,
|
|
30
|
+
registerFocus,
|
|
31
|
+
keyboardNavigate,
|
|
32
|
+
}: {
|
|
33
|
+
item: CarouselItem;
|
|
34
|
+
index: number;
|
|
35
|
+
count: number;
|
|
36
|
+
height: number;
|
|
37
|
+
onPress?: () => void;
|
|
38
|
+
renderItemContent?: CarouselProps['renderItemContent'];
|
|
39
|
+
onFocus: () => void;
|
|
40
|
+
onNavigate: (index: number) => void;
|
|
41
|
+
showText: boolean;
|
|
42
|
+
disabled: boolean;
|
|
43
|
+
tabStop: boolean;
|
|
44
|
+
textInsetBottom: number;
|
|
45
|
+
registerFocus: (index: number, node: ViewInstance | null) => void;
|
|
46
|
+
keyboardNavigate: (index: number, step: number) => void;
|
|
47
|
+
}) {
|
|
48
|
+
const theme = useMobileTheme();
|
|
49
|
+
const { width } = useWindowDimensions();
|
|
50
|
+
const [failed, setFailed] = useState(false);
|
|
51
|
+
const [focused, setFocused] = useState(false);
|
|
52
|
+
const customContent = renderItemContent?.({ item, index, width, height });
|
|
53
|
+
const customMedia = customContent != null;
|
|
54
|
+
const hasImage = customMedia || (Boolean(item.image) && !failed);
|
|
55
|
+
const Container = onPress ? TouchableRipple : View;
|
|
56
|
+
const inactive = Boolean(disabled || item.disabled);
|
|
57
|
+
const canPress = usePressScope(inactive, item.id);
|
|
58
|
+
const label = item.accessibilityLabel ?? `${item.title}${item.description ? `. ${item.description}` : ''}`;
|
|
59
|
+
useEffect(() => setFailed(false), [item.image]);
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (inactive) setFocused(false);
|
|
62
|
+
}, [inactive]);
|
|
63
|
+
return (
|
|
64
|
+
<Container
|
|
65
|
+
ref={(node: ViewInstance | null) => registerFocus(index, node)}
|
|
66
|
+
{...(Platform.OS === 'web'
|
|
67
|
+
? {
|
|
68
|
+
tabIndex: !inactive && (onPress || tabStop) ? (0 as const) : (-1 as const),
|
|
69
|
+
onKeyDown: (event: { nativeEvent: { key?: string }; preventDefault: () => void; stopPropagation: () => void }) => {
|
|
70
|
+
const key = event.nativeEvent.key;
|
|
71
|
+
if (!canPress() || (key !== 'ArrowUp' && key !== 'ArrowDown')) return;
|
|
72
|
+
event.preventDefault();
|
|
73
|
+
event.stopPropagation();
|
|
74
|
+
keyboardNavigate(index, key === 'ArrowDown' ? 1 : -1);
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
: {})}
|
|
78
|
+
accessible
|
|
79
|
+
accessibilityLabel={`${label}. ${index + 1} de ${count}`}
|
|
80
|
+
accessibilityRole={onPress ? 'button' : 'image'}
|
|
81
|
+
accessibilityState={{ disabled: inactive }}
|
|
82
|
+
accessibilityActions={
|
|
83
|
+
inactive
|
|
84
|
+
? []
|
|
85
|
+
: [
|
|
86
|
+
...(index > 0 ? [{ name: 'previous', label: 'Elemento anterior' }] : []),
|
|
87
|
+
...(index < count - 1 ? [{ name: 'next', label: 'Elemento siguiente' }] : []),
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
onAccessibilityAction={(event) => {
|
|
91
|
+
if (!canPress()) return;
|
|
92
|
+
if (event.nativeEvent.actionName === 'next') onNavigate(Math.min(count - 1, index + 1));
|
|
93
|
+
if (event.nativeEvent.actionName === 'previous') onNavigate(Math.max(0, index - 1));
|
|
94
|
+
}}
|
|
95
|
+
disabled={inactive}
|
|
96
|
+
focusable={Boolean(onPress && !inactive)}
|
|
97
|
+
onBlur={() => setFocused(false)}
|
|
98
|
+
onFocus={(event) => {
|
|
99
|
+
const target = event.currentTarget as unknown as HTMLElement;
|
|
100
|
+
if (!canPress() || (Platform.OS === 'web' && !target.matches(':focus-visible'))) return;
|
|
101
|
+
setFocused(true);
|
|
102
|
+
onFocus();
|
|
103
|
+
}}
|
|
104
|
+
onPress={
|
|
105
|
+
onPress
|
|
106
|
+
? () => {
|
|
107
|
+
if (canPress()) onPress();
|
|
108
|
+
}
|
|
109
|
+
: undefined
|
|
110
|
+
}
|
|
111
|
+
style={{
|
|
112
|
+
height,
|
|
113
|
+
backgroundColor: theme.colors.secondaryContainer,
|
|
114
|
+
opacity: inactive ? 0.38 : 1,
|
|
115
|
+
outlineColor: theme.colors.secondary,
|
|
116
|
+
outlineStyle: 'solid',
|
|
117
|
+
outlineWidth: focused && !inactive ? 3 : 0,
|
|
118
|
+
outlineOffset: -4,
|
|
119
|
+
}}
|
|
120
|
+
>
|
|
121
|
+
<View pointerEvents="none" style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
122
|
+
{customMedia ? (
|
|
123
|
+
<View
|
|
124
|
+
pointerEvents="none"
|
|
125
|
+
accessibilityElementsHidden
|
|
126
|
+
importantForAccessibility="no-hide-descendants"
|
|
127
|
+
{...(Platform.OS === 'web' ? { 'aria-hidden': true, inert: true } : {})}
|
|
128
|
+
style={StyleSheet.absoluteFill}
|
|
129
|
+
>
|
|
130
|
+
{customContent}
|
|
131
|
+
</View>
|
|
132
|
+
) : hasImage ? (
|
|
133
|
+
<Image
|
|
134
|
+
accessible={false}
|
|
135
|
+
contentFit="cover"
|
|
136
|
+
onError={() => setFailed(true)}
|
|
137
|
+
recyclingKey={item.id}
|
|
138
|
+
source={item.image}
|
|
139
|
+
style={StyleSheet.absoluteFill}
|
|
140
|
+
/>
|
|
141
|
+
) : (
|
|
142
|
+
<Icon color={theme.colors.onSecondaryContainer} name={item.icon ?? 'image-outline'} size={72} />
|
|
143
|
+
)}
|
|
144
|
+
{showText && hasImage ? (
|
|
145
|
+
<View
|
|
146
|
+
style={[
|
|
147
|
+
StyleSheet.absoluteFill,
|
|
148
|
+
{ backgroundImage: 'linear-gradient(180deg, transparent 35%, rgba(0,0,0,0.8) 100%)' },
|
|
149
|
+
]}
|
|
150
|
+
/>
|
|
151
|
+
) : null}
|
|
152
|
+
{focused && !inactive ? (
|
|
153
|
+
<View style={[StyleSheet.absoluteFill, { backgroundColor: theme.colors.onSurface, opacity: 0.1 }]} />
|
|
154
|
+
) : null}
|
|
155
|
+
{showText ? (
|
|
156
|
+
<View style={{ position: 'absolute', bottom: 32 + Math.max(0, textInsetBottom), left: 24, right: 24, gap: 8 }}>
|
|
157
|
+
<Text
|
|
158
|
+
numberOfLines={width < 600 ? 1 : 2}
|
|
159
|
+
variant="headlineLarge"
|
|
160
|
+
style={{ color: hasImage ? '#FFFFFF' : theme.colors.onSecondaryContainer }}
|
|
161
|
+
>
|
|
162
|
+
{item.title}
|
|
163
|
+
</Text>
|
|
164
|
+
{item.description ? (
|
|
165
|
+
<Text
|
|
166
|
+
numberOfLines={width < 600 ? 1 : 2}
|
|
167
|
+
variant="bodyLarge"
|
|
168
|
+
style={{ color: hasImage ? '#FFFFFF' : theme.colors.onSecondaryContainer }}
|
|
169
|
+
>
|
|
170
|
+
{item.description}
|
|
171
|
+
</Text>
|
|
172
|
+
) : null}
|
|
173
|
+
</View>
|
|
174
|
+
) : null}
|
|
175
|
+
</View>
|
|
176
|
+
</Container>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A vertical, paged feed. Use an available portrait compact/medium viewport and zero outer padding. */
|
|
181
|
+
export function FullScreenCarousel({
|
|
182
|
+
scrollIndicators,
|
|
183
|
+
items,
|
|
184
|
+
height = 640,
|
|
185
|
+
initialIndex = 0,
|
|
186
|
+
onIndexChange,
|
|
187
|
+
onItemPress,
|
|
188
|
+
renderItemContent,
|
|
189
|
+
showControls = false,
|
|
190
|
+
showText = true,
|
|
191
|
+
disabled = false,
|
|
192
|
+
textInsetBottom = 0,
|
|
193
|
+
accessibilityLabel = 'Carrusel vertical',
|
|
194
|
+
style,
|
|
195
|
+
testID = 'carousel',
|
|
196
|
+
}: CarouselProps) {
|
|
197
|
+
const theme = useMobileTheme();
|
|
198
|
+
const firstIndex = useRef(Math.max(0, Math.min(items.length - 1, Number.isFinite(initialIndex) ? Math.trunc(initialIndex) : 0)));
|
|
199
|
+
const [index, setIndex] = useState(firstIndex.current);
|
|
200
|
+
const current = useRef(index);
|
|
201
|
+
const currentId = useRef(items[index]?.id);
|
|
202
|
+
const focusTargets = useRef(new Map<number, ViewInstance>()),
|
|
203
|
+
pendingFocus = useRef<number | undefined>(undefined);
|
|
204
|
+
const registerFocus = useCallback((index: number, node: ViewInstance | null) => {
|
|
205
|
+
if (node) {
|
|
206
|
+
focusTargets.current.set(index, node);
|
|
207
|
+
if (pendingFocus.current === index) {
|
|
208
|
+
pendingFocus.current = undefined;
|
|
209
|
+
(node as unknown as HTMLElement).focus({ preventScroll: true });
|
|
210
|
+
}
|
|
211
|
+
} else focusTargets.current.delete(index);
|
|
212
|
+
}, []);
|
|
213
|
+
const requested = useRef<{ index: number; revision: number } | undefined>(undefined);
|
|
214
|
+
const pendingRebase = useRef(true);
|
|
215
|
+
const itemHeight = Math.max(1, Number.isFinite(height) ? height : 640);
|
|
216
|
+
const step = itemHeight + 16;
|
|
217
|
+
const maxOffset = Math.max(0, (items.length - 1) * step);
|
|
218
|
+
const ids = JSON.stringify(items.map((item) => item.id));
|
|
219
|
+
const geometry = useMemo(() => ({}), [ids, itemHeight]);
|
|
220
|
+
const currentGeometry = useRef(geometry);
|
|
221
|
+
const mounted = useRef(true);
|
|
222
|
+
useEffect(() => {
|
|
223
|
+
mounted.current = true;
|
|
224
|
+
return () => {
|
|
225
|
+
mounted.current = false;
|
|
226
|
+
};
|
|
227
|
+
}, []);
|
|
228
|
+
const clearRequest = useCallback(() => {
|
|
229
|
+
requested.current = undefined;
|
|
230
|
+
}, []);
|
|
231
|
+
const { ref, offset, onScroll, goTo, isCurrentRequest } = useCarouselScrollController<CarouselItem>({
|
|
232
|
+
maxOffset,
|
|
233
|
+
snapOffsets: items.map((_, index) => index * step),
|
|
234
|
+
vertical: true,
|
|
235
|
+
interactive: !disabled && items.length > 1,
|
|
236
|
+
animationScale: theme.animation.scale,
|
|
237
|
+
onInterrupted: clearRequest,
|
|
238
|
+
onSettled: clearRequest,
|
|
239
|
+
});
|
|
240
|
+
const reportIndex = useCallback(
|
|
241
|
+
(next: number) => {
|
|
242
|
+
if (!mounted.current || currentGeometry.current !== geometry) return;
|
|
243
|
+
if (current.current === next) return;
|
|
244
|
+
current.current = next;
|
|
245
|
+
currentId.current = items[next]?.id;
|
|
246
|
+
setIndex(next);
|
|
247
|
+
onIndexChange?.(next);
|
|
248
|
+
},
|
|
249
|
+
[geometry, onIndexChange]
|
|
250
|
+
);
|
|
251
|
+
useAnimatedReaction(
|
|
252
|
+
() => Math.max(0, Math.min(items.length - 1, Math.round(offset.get() / step))),
|
|
253
|
+
(next, previous) => {
|
|
254
|
+
if (next !== previous) scheduleOnRN(reportIndex, next);
|
|
255
|
+
}
|
|
256
|
+
);
|
|
257
|
+
useLayoutEffect(() => {
|
|
258
|
+
currentGeometry.current = geometry;
|
|
259
|
+
pendingFocus.current = undefined;
|
|
260
|
+
if (items.length === 0) return;
|
|
261
|
+
const retained = items.findIndex((item) => item.id === currentId.current);
|
|
262
|
+
const next = retained >= 0 ? retained : Math.max(0, Math.min(items.length - 1, current.current));
|
|
263
|
+
if (next !== current.current) reportIndex(next);
|
|
264
|
+
currentId.current = items[next]?.id;
|
|
265
|
+
clearRequest();
|
|
266
|
+
pendingRebase.current = true;
|
|
267
|
+
offset.set(next * step);
|
|
268
|
+
goTo(next * step, false);
|
|
269
|
+
}, [ids, itemHeight]);
|
|
270
|
+
const go = useCallback(
|
|
271
|
+
(next: number) => {
|
|
272
|
+
const bounded = Math.max(0, Math.min(items.length - 1, next));
|
|
273
|
+
requested.current = { index: bounded, revision: goTo(bounded * step) };
|
|
274
|
+
},
|
|
275
|
+
[goTo, items.length, step]
|
|
276
|
+
);
|
|
277
|
+
const buttonIndex = () =>
|
|
278
|
+
requested.current && isCurrentRequest(requested.current.revision)
|
|
279
|
+
? requested.current.index
|
|
280
|
+
: Math.max(0, Math.min(items.length - 1, Math.round(offset.get() / step)));
|
|
281
|
+
const keyboardNavigate = (index: number, step: number) => {
|
|
282
|
+
if (disabled) return;
|
|
283
|
+
let next = index + step;
|
|
284
|
+
while (next >= 0 && next < items.length && items[next].disabled) next += step;
|
|
285
|
+
if (next < 0 || next >= items.length) return;
|
|
286
|
+
go(next);
|
|
287
|
+
const target = focusTargets.current.get(next);
|
|
288
|
+
if (target) (target as unknown as HTMLElement).focus({ preventScroll: true });
|
|
289
|
+
else pendingFocus.current = next;
|
|
290
|
+
};
|
|
291
|
+
if (!items.length) return null;
|
|
292
|
+
const tabIndex = items[index]?.disabled ? items.findIndex((item) => !item.disabled) : index;
|
|
293
|
+
return (
|
|
294
|
+
<View style={style} testID={testID}>
|
|
295
|
+
<Animated.FlatList
|
|
296
|
+
accessibilityLabel={accessibilityLabel}
|
|
297
|
+
accessible={false}
|
|
298
|
+
focusable={false}
|
|
299
|
+
bounces={false}
|
|
300
|
+
data={items}
|
|
301
|
+
decelerationRate="fast"
|
|
302
|
+
disableIntervalMomentum
|
|
303
|
+
getItemLayout={(_, i) => ({ length: itemHeight, offset: i * step, index: i })}
|
|
304
|
+
ItemSeparatorComponent={() => <View style={{ height: 16 }} />}
|
|
305
|
+
keyExtractor={(item) => item.id}
|
|
306
|
+
initialScrollIndex={firstIndex.current}
|
|
307
|
+
onScroll={onScroll}
|
|
308
|
+
onContentSizeChange={() => {
|
|
309
|
+
if (pendingRebase.current) {
|
|
310
|
+
pendingRebase.current = false;
|
|
311
|
+
goTo(current.current * step, false);
|
|
312
|
+
}
|
|
313
|
+
}}
|
|
314
|
+
ref={ref}
|
|
315
|
+
renderItem={({ item, index: i }) => (
|
|
316
|
+
<FullScreenItem
|
|
317
|
+
item={item}
|
|
318
|
+
index={i}
|
|
319
|
+
count={items.length}
|
|
320
|
+
height={itemHeight}
|
|
321
|
+
showText={showText}
|
|
322
|
+
renderItemContent={renderItemContent}
|
|
323
|
+
disabled={disabled}
|
|
324
|
+
tabStop={i === tabIndex}
|
|
325
|
+
textInsetBottom={textInsetBottom}
|
|
326
|
+
onFocus={() => {
|
|
327
|
+
if (buttonIndex() !== i) go(i);
|
|
328
|
+
}}
|
|
329
|
+
onNavigate={go}
|
|
330
|
+
registerFocus={registerFocus}
|
|
331
|
+
keyboardNavigate={keyboardNavigate}
|
|
332
|
+
onPress={onItemPress ? () => onItemPress(item, i) : undefined}
|
|
333
|
+
/>
|
|
334
|
+
)}
|
|
335
|
+
scrollEnabled={!disabled && items.length > 1}
|
|
336
|
+
scrollEventThrottle={16}
|
|
337
|
+
{...getScrollIndicatorProps(scrollIndicators)}
|
|
338
|
+
snapToInterval={step}
|
|
339
|
+
overScrollMode="never"
|
|
340
|
+
windowSize={3}
|
|
341
|
+
initialNumToRender={2}
|
|
342
|
+
maxToRenderPerBatch={2}
|
|
343
|
+
removeClippedSubviews={false}
|
|
344
|
+
style={{ height: itemHeight, flexGrow: 0 }}
|
|
345
|
+
testID={`${testID}-scroll`}
|
|
346
|
+
/>
|
|
347
|
+
{showControls && items.length > 1 ? (
|
|
348
|
+
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 8 }}>
|
|
349
|
+
<Text accessibilityLiveRegion="polite" testID={`${testID}-position`}>
|
|
350
|
+
{index + 1} / {items.length}
|
|
351
|
+
</Text>
|
|
352
|
+
<View style={{ flexDirection: 'row' }}>
|
|
353
|
+
<IconButton
|
|
354
|
+
accessibilityLabel="Elemento anterior"
|
|
355
|
+
icon="arrow-up"
|
|
356
|
+
disabled={disabled || index === 0}
|
|
357
|
+
onPress={() => go(buttonIndex() - 1)}
|
|
358
|
+
style={{ height: 48, width: 48, margin: 0 }}
|
|
359
|
+
testID={`${testID}-previous`}
|
|
360
|
+
/>
|
|
361
|
+
<IconButton
|
|
362
|
+
accessibilityLabel="Elemento siguiente"
|
|
363
|
+
icon="arrow-down"
|
|
364
|
+
disabled={disabled || index === items.length - 1}
|
|
365
|
+
onPress={() => go(buttonIndex() + 1)}
|
|
366
|
+
style={{ height: 48, width: 48, margin: 0 }}
|
|
367
|
+
testID={`${testID}-next`}
|
|
368
|
+
/>
|
|
369
|
+
</View>
|
|
370
|
+
</View>
|
|
371
|
+
) : null}
|
|
372
|
+
</View>
|
|
373
|
+
);
|
|
374
|
+
}
|