@expressive-ui/native 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/README.md +42 -2
- package/dist/Button.d.ts +48 -0
- package/dist/Button.d.ts.map +1 -0
- package/dist/Button.js +122 -0
- package/dist/Button.js.map +1 -0
- package/dist/Carousel.d.ts +13 -0
- package/dist/Carousel.d.ts.map +1 -0
- package/dist/Carousel.js +52 -0
- package/dist/Carousel.js.map +1 -0
- package/dist/Chips.d.ts +35 -0
- package/dist/Chips.d.ts.map +1 -0
- package/dist/Chips.js +138 -0
- package/dist/Chips.js.map +1 -0
- package/dist/Dialogs.d.ts +42 -0
- package/dist/Dialogs.d.ts.map +1 -0
- package/dist/Dialogs.js +115 -0
- package/dist/Dialogs.js.map +1 -0
- package/dist/ExpressiveCard.d.ts +20 -0
- package/dist/ExpressiveCard.d.ts.map +1 -0
- package/dist/ExpressiveCard.js +71 -0
- package/dist/ExpressiveCard.js.map +1 -0
- package/dist/Fab.d.ts +16 -0
- package/dist/Fab.d.ts.map +1 -0
- package/dist/Fab.js +104 -0
- package/dist/Fab.js.map +1 -0
- package/dist/Fields.d.ts +90 -0
- package/dist/Fields.d.ts.map +1 -0
- package/dist/Fields.js +211 -0
- package/dist/Fields.js.map +1 -0
- package/dist/Grid.d.ts +8 -0
- package/dist/Grid.d.ts.map +1 -0
- package/dist/Grid.js +16 -0
- package/dist/Grid.js.map +1 -0
- package/dist/Inputs.d.ts +33 -0
- package/dist/Inputs.d.ts.map +1 -0
- package/dist/Inputs.js +172 -0
- package/dist/Inputs.js.map +1 -0
- package/dist/List.d.ts +20 -0
- package/dist/List.d.ts.map +1 -0
- package/dist/List.js +222 -0
- package/dist/List.js.map +1 -0
- package/dist/Menu.d.ts +26 -0
- package/dist/Menu.d.ts.map +1 -0
- package/dist/Menu.js +117 -0
- package/dist/Menu.js.map +1 -0
- package/dist/Navigation.d.ts +34 -0
- package/dist/Navigation.d.ts.map +1 -0
- package/dist/Navigation.js +80 -0
- package/dist/Navigation.js.map +1 -0
- package/dist/Progress.d.ts +20 -0
- package/dist/Progress.d.ts.map +1 -0
- package/dist/Progress.js +93 -0
- package/dist/Progress.js.map +1 -0
- package/dist/Search.d.ts +50 -0
- package/dist/Search.d.ts.map +1 -0
- package/dist/Search.js +121 -0
- package/dist/Search.js.map +1 -0
- package/dist/Sheets.d.ts +44 -0
- package/dist/Sheets.d.ts.map +1 -0
- package/dist/Sheets.js +128 -0
- package/dist/Sheets.js.map +1 -0
- package/dist/Toolbar.d.ts +13 -0
- package/dist/Toolbar.d.ts.map +1 -0
- package/dist/Toolbar.js +38 -0
- package/dist/Toolbar.js.map +1 -0
- package/dist/Transition.d.ts +34 -0
- package/dist/Transition.d.ts.map +1 -0
- package/dist/Transition.js +93 -0
- package/dist/Transition.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +41 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +55 -0
- package/dist/primitives.js.map +1 -0
- package/dist/theme.d.ts +57 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +89 -0
- package/dist/theme.js.map +1 -0
- package/package.json +53 -4
- package/src/Button.tsx +347 -0
- package/src/Carousel.tsx +108 -0
- package/src/Chips.tsx +292 -0
- package/src/Dialogs.tsx +252 -0
- package/src/ExpressiveCard.tsx +205 -0
- package/src/Fab.tsx +187 -0
- package/src/Fields.tsx +556 -0
- package/src/Grid.tsx +29 -0
- package/src/Inputs.tsx +290 -0
- package/src/List.tsx +295 -0
- package/src/Menu.tsx +238 -0
- package/src/Navigation.tsx +198 -0
- package/src/Progress.tsx +192 -0
- package/src/Search.tsx +351 -0
- package/src/Sheets.tsx +273 -0
- package/src/Toolbar.tsx +88 -0
- package/src/Transition.tsx +155 -0
- package/src/index.ts +19 -0
- package/src/primitives.tsx +110 -0
- package/src/theme.tsx +125 -0
package/src/Inputs.tsx
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, PanResponder, Pressable, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { resolveSlider, resolveSwitch, sliderStops, snapValue, useInteraction, valueRatio, type SliderSize, type SwitchIcons } from '@expressive-ui/core';
|
|
4
|
+
import { tokens } from '@expressive-ui/tokens';
|
|
5
|
+
import { Icon, T, withAlpha } from './primitives.js';
|
|
6
|
+
import { useExpressiveTheme, useSpringValue } from './theme.js';
|
|
7
|
+
|
|
8
|
+
export type SwitchProps = {
|
|
9
|
+
checked: boolean;
|
|
10
|
+
onChange?: (checked: boolean) => void;
|
|
11
|
+
label: string;
|
|
12
|
+
icons?: SwitchIcons;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
style?: StyleProp<ViewStyle>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Switch do M3 (mobile-app-rhosse): a alça cresce ao ligar e ao pressionar, e desliza com a mola spatial. */
|
|
18
|
+
export function Switch({ checked, onChange, label, icons = 'checked', disabled = false, style }: SwitchProps) {
|
|
19
|
+
const { colors } = useExpressiveTheme();
|
|
20
|
+
const spec = resolveSwitch({ checked, disabled, icons });
|
|
21
|
+
const { state, handlers } = useInteraction(disabled);
|
|
22
|
+
const size = useSpringValue(state.pressed ? spec.handleSize.pressed : spec.handleSize.rest, 'spatialFast');
|
|
23
|
+
// Centro da alça medido da borda externa; a borda de 2px do desligado é descontada.
|
|
24
|
+
const center = useSpringValue(spec.handleX - spec.border, 'spatialDefault');
|
|
25
|
+
const layer = useSpringValue(state.pressed ? tokens.state.pressed : 0, 'effectsDefault');
|
|
26
|
+
const half = Animated.multiply(size, 0.5);
|
|
27
|
+
return (
|
|
28
|
+
<Pressable
|
|
29
|
+
accessibilityRole="switch"
|
|
30
|
+
accessibilityLabel={label}
|
|
31
|
+
accessibilityState={{ checked, disabled }}
|
|
32
|
+
disabled={disabled}
|
|
33
|
+
onPress={() => onChange?.(!checked)}
|
|
34
|
+
hitSlop={8}
|
|
35
|
+
{...handlers}
|
|
36
|
+
style={[
|
|
37
|
+
{
|
|
38
|
+
width: spec.width,
|
|
39
|
+
height: spec.height,
|
|
40
|
+
borderRadius: spec.height / 2,
|
|
41
|
+
borderWidth: spec.border,
|
|
42
|
+
borderColor: withAlpha(colors[spec.trackBorder], spec.trackOpacity),
|
|
43
|
+
backgroundColor: withAlpha(colors[spec.track], spec.trackOpacity),
|
|
44
|
+
},
|
|
45
|
+
style,
|
|
46
|
+
]}
|
|
47
|
+
>
|
|
48
|
+
<Animated.View
|
|
49
|
+
pointerEvents="none"
|
|
50
|
+
style={{
|
|
51
|
+
position: 'absolute',
|
|
52
|
+
top: (spec.height - spec.border * 2) / 2 - 20,
|
|
53
|
+
left: Animated.subtract(center, 20),
|
|
54
|
+
width: 40,
|
|
55
|
+
height: 40,
|
|
56
|
+
borderRadius: 20,
|
|
57
|
+
backgroundColor: colors[checked ? 'primary' : 'onSurface'],
|
|
58
|
+
opacity: layer,
|
|
59
|
+
}}
|
|
60
|
+
/>
|
|
61
|
+
<Animated.View
|
|
62
|
+
style={{
|
|
63
|
+
position: 'absolute',
|
|
64
|
+
top: Animated.subtract((spec.height - spec.border * 2) / 2, half),
|
|
65
|
+
left: Animated.subtract(center, half),
|
|
66
|
+
width: size,
|
|
67
|
+
height: size,
|
|
68
|
+
borderRadius: 14,
|
|
69
|
+
alignItems: 'center',
|
|
70
|
+
justifyContent: 'center',
|
|
71
|
+
backgroundColor: withAlpha(colors[spec.handle], checked && disabled ? 1 : spec.contentOpacity),
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
{spec.iconName ? <Icon name={spec.iconName} size={16} tint={withAlpha(colors[spec.icon], spec.contentOpacity)} /> : null}
|
|
75
|
+
</Animated.View>
|
|
76
|
+
</Pressable>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export type SliderProps = {
|
|
81
|
+
value: number;
|
|
82
|
+
min?: number;
|
|
83
|
+
max?: number;
|
|
84
|
+
step?: number;
|
|
85
|
+
/** Chamado ao soltar: o valor é enviado só no fim, como no mobile-app-rhosse. */
|
|
86
|
+
onChange?: (value: number) => void;
|
|
87
|
+
/** Valor provisório durante o arraste; null ao soltar. */
|
|
88
|
+
onPreview?: (value: number | null) => void;
|
|
89
|
+
label: string;
|
|
90
|
+
size?: SliderSize;
|
|
91
|
+
icon?: string;
|
|
92
|
+
stops?: boolean;
|
|
93
|
+
format?: (value: number) => string;
|
|
94
|
+
showValue?: boolean;
|
|
95
|
+
disabled?: boolean;
|
|
96
|
+
style?: StyleProp<ViewStyle>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** Slider do M3 Expressive, portado do mobile-app-rhosse (PanResponder, alça vertical, ícone na parte ativa). */
|
|
100
|
+
export function Slider({
|
|
101
|
+
value,
|
|
102
|
+
min = 0,
|
|
103
|
+
max = 100,
|
|
104
|
+
step = 1,
|
|
105
|
+
onChange,
|
|
106
|
+
onPreview,
|
|
107
|
+
label,
|
|
108
|
+
size = 'sm',
|
|
109
|
+
icon,
|
|
110
|
+
stops = false,
|
|
111
|
+
format = String,
|
|
112
|
+
showValue = true,
|
|
113
|
+
disabled = false,
|
|
114
|
+
style,
|
|
115
|
+
}: SliderProps) {
|
|
116
|
+
const { colors } = useExpressiveTheme();
|
|
117
|
+
const spec = resolveSlider({ size, disabled });
|
|
118
|
+
const [width, setWidth] = useState(0);
|
|
119
|
+
const [drag, setDrag] = useState<number | null>(null);
|
|
120
|
+
const start = useRef({ local: 0, page: 0 });
|
|
121
|
+
const latest = useRef({ min, max, step, width, disabled, onChange, onPreview });
|
|
122
|
+
latest.current = { min, max, step, width, disabled, onChange, onPreview };
|
|
123
|
+
|
|
124
|
+
// Posição = toque inicial (locationX, relativo ao slider) + deslocamento do dedo. Não depende de
|
|
125
|
+
// medir a tela, o que falharia durante animações de entrada (mesma solução do mobile-app-rhosse).
|
|
126
|
+
const toValue = (pageX: number) => {
|
|
127
|
+
const p = latest.current;
|
|
128
|
+
const local = start.current.local + (pageX - start.current.page);
|
|
129
|
+
const ratio = Math.max(0, Math.min(1, local / Math.max(1, p.width)));
|
|
130
|
+
return snapValue(p.min + ratio * (p.max - p.min), p.min, p.max, p.step);
|
|
131
|
+
};
|
|
132
|
+
const responder = useRef(
|
|
133
|
+
PanResponder.create({
|
|
134
|
+
onStartShouldSetPanResponder: () => !latest.current.disabled,
|
|
135
|
+
onMoveShouldSetPanResponder: () => !latest.current.disabled,
|
|
136
|
+
onPanResponderTerminationRequest: () => false,
|
|
137
|
+
onPanResponderGrant: (event) => {
|
|
138
|
+
start.current = { local: event.nativeEvent.locationX, page: event.nativeEvent.pageX };
|
|
139
|
+
const next = toValue(event.nativeEvent.pageX);
|
|
140
|
+
setDrag(next);
|
|
141
|
+
latest.current.onPreview?.(next);
|
|
142
|
+
},
|
|
143
|
+
onPanResponderMove: (event) => {
|
|
144
|
+
const next = toValue(event.nativeEvent.pageX);
|
|
145
|
+
setDrag(next);
|
|
146
|
+
latest.current.onPreview?.(next);
|
|
147
|
+
},
|
|
148
|
+
onPanResponderRelease: (event) => {
|
|
149
|
+
const next = toValue(event.nativeEvent.pageX);
|
|
150
|
+
setDrag(null);
|
|
151
|
+
latest.current.onPreview?.(null);
|
|
152
|
+
latest.current.onChange?.(next);
|
|
153
|
+
},
|
|
154
|
+
onPanResponderTerminate: () => {
|
|
155
|
+
setDrag(null);
|
|
156
|
+
latest.current.onPreview?.(null);
|
|
157
|
+
},
|
|
158
|
+
}),
|
|
159
|
+
).current;
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
if (disabled) setDrag(null);
|
|
162
|
+
}, [disabled]);
|
|
163
|
+
|
|
164
|
+
const shown = drag ?? snapValue(value, min, max, step);
|
|
165
|
+
const ratio = valueRatio(shown, min, max);
|
|
166
|
+
const handle = useSpringValue(drag === null ? spec.handleWidth.rest : spec.handleWidth.pressed, 'spatialFast');
|
|
167
|
+
const bubble = useSpringValue(drag === null ? 0 : 1, 'effectsFast');
|
|
168
|
+
const x = width * ratio;
|
|
169
|
+
const hw = drag === null ? spec.handleWidth.rest : spec.handleWidth.pressed;
|
|
170
|
+
const activeW = Math.max(0, x - spec.gap - hw / 2);
|
|
171
|
+
const inactiveW = Math.max(0, width - x - spec.gap - hw / 2);
|
|
172
|
+
const { outer, inner } = spec.radius;
|
|
173
|
+
const active = withAlpha(colors[spec.active], 1);
|
|
174
|
+
const track = { position: 'absolute' as const, height: spec.track, top: (spec.handleHeight - spec.track) / 2 };
|
|
175
|
+
|
|
176
|
+
return (
|
|
177
|
+
<View
|
|
178
|
+
accessible
|
|
179
|
+
accessibilityRole="adjustable"
|
|
180
|
+
accessibilityLabel={label}
|
|
181
|
+
accessibilityState={{ disabled }}
|
|
182
|
+
accessibilityValue={{ min, max, now: shown, text: format(shown) }}
|
|
183
|
+
accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}
|
|
184
|
+
onAccessibilityAction={(event) => {
|
|
185
|
+
if (disabled) return;
|
|
186
|
+
const delta = event.nativeEvent.actionName === 'increment' ? step : -step;
|
|
187
|
+
onChange?.(snapValue(value + delta, min, max, step));
|
|
188
|
+
}}
|
|
189
|
+
onLayout={(event) => setWidth(event.nativeEvent.layout.width)}
|
|
190
|
+
{...responder.panHandlers}
|
|
191
|
+
style={[{ height: spec.handleHeight, opacity: spec.disabledOpacity }, style]}
|
|
192
|
+
>
|
|
193
|
+
{width > 0 ? (
|
|
194
|
+
<View pointerEvents="none" style={{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0 }}>
|
|
195
|
+
<View
|
|
196
|
+
style={[
|
|
197
|
+
track,
|
|
198
|
+
{
|
|
199
|
+
left: 0,
|
|
200
|
+
width: activeW,
|
|
201
|
+
backgroundColor: active,
|
|
202
|
+
borderTopLeftRadius: outer,
|
|
203
|
+
borderBottomLeftRadius: outer,
|
|
204
|
+
borderTopRightRadius: inner,
|
|
205
|
+
borderBottomRightRadius: inner,
|
|
206
|
+
justifyContent: 'center',
|
|
207
|
+
paddingLeft: 12,
|
|
208
|
+
overflow: 'hidden',
|
|
209
|
+
},
|
|
210
|
+
]}
|
|
211
|
+
>
|
|
212
|
+
{icon && spec.iconSize && activeW > 52 ? <Icon name={icon} size={spec.iconSize} color={spec.onActive} filled /> : null}
|
|
213
|
+
</View>
|
|
214
|
+
<View
|
|
215
|
+
style={[
|
|
216
|
+
track,
|
|
217
|
+
{
|
|
218
|
+
right: 0,
|
|
219
|
+
width: inactiveW,
|
|
220
|
+
backgroundColor: withAlpha(colors[spec.inactive], disabled ? 0.12 : 1),
|
|
221
|
+
borderTopRightRadius: outer,
|
|
222
|
+
borderBottomRightRadius: outer,
|
|
223
|
+
borderTopLeftRadius: inner,
|
|
224
|
+
borderBottomLeftRadius: inner,
|
|
225
|
+
},
|
|
226
|
+
]}
|
|
227
|
+
/>
|
|
228
|
+
{stops
|
|
229
|
+
? sliderStops(min, max, step).map((s) => (
|
|
230
|
+
<View
|
|
231
|
+
key={s}
|
|
232
|
+
style={{
|
|
233
|
+
position: 'absolute',
|
|
234
|
+
left: width * s - spec.stopSize / 2,
|
|
235
|
+
top: (spec.handleHeight - spec.stopSize) / 2,
|
|
236
|
+
width: spec.stopSize,
|
|
237
|
+
height: spec.stopSize,
|
|
238
|
+
borderRadius: spec.stopSize / 2,
|
|
239
|
+
backgroundColor: colors[s <= ratio ? spec.stopOnActive : spec.stopOnInactive],
|
|
240
|
+
}}
|
|
241
|
+
/>
|
|
242
|
+
))
|
|
243
|
+
: null}
|
|
244
|
+
{ratio < 0.98 ? (
|
|
245
|
+
<View
|
|
246
|
+
style={{
|
|
247
|
+
position: 'absolute',
|
|
248
|
+
right: Math.min((spec.track - spec.stopSize) / 2, 12),
|
|
249
|
+
top: (spec.handleHeight - spec.stopSize) / 2,
|
|
250
|
+
width: spec.stopSize,
|
|
251
|
+
height: spec.stopSize,
|
|
252
|
+
borderRadius: spec.stopSize / 2,
|
|
253
|
+
backgroundColor: colors[spec.stopOnInactive],
|
|
254
|
+
}}
|
|
255
|
+
/>
|
|
256
|
+
) : null}
|
|
257
|
+
<Animated.View
|
|
258
|
+
style={{
|
|
259
|
+
position: 'absolute',
|
|
260
|
+
left: Math.max(0, Math.min(width - hw, x - hw / 2)),
|
|
261
|
+
width: handle,
|
|
262
|
+
height: spec.handleHeight,
|
|
263
|
+
borderRadius: 2,
|
|
264
|
+
backgroundColor: active,
|
|
265
|
+
}}
|
|
266
|
+
/>
|
|
267
|
+
{showValue ? (
|
|
268
|
+
<Animated.View
|
|
269
|
+
style={{
|
|
270
|
+
position: 'absolute',
|
|
271
|
+
bottom: spec.handleHeight + 4,
|
|
272
|
+
left: x - 32,
|
|
273
|
+
width: 64,
|
|
274
|
+
alignItems: 'center',
|
|
275
|
+
opacity: bubble,
|
|
276
|
+
transform: [{ translateY: bubble.interpolate({ inputRange: [0, 1], outputRange: [4, 0] }) }],
|
|
277
|
+
}}
|
|
278
|
+
>
|
|
279
|
+
<View style={{ paddingHorizontal: 16, paddingVertical: 12, borderRadius: 999, backgroundColor: colors[spec.valueIndicator.container] }}>
|
|
280
|
+
<T variant="labelLarge" color={spec.valueIndicator.content} style={{ fontVariant: ['tabular-nums'] }}>
|
|
281
|
+
{format(shown)}
|
|
282
|
+
</T>
|
|
283
|
+
</View>
|
|
284
|
+
</Animated.View>
|
|
285
|
+
) : null}
|
|
286
|
+
</View>
|
|
287
|
+
) : null}
|
|
288
|
+
</View>
|
|
289
|
+
);
|
|
290
|
+
}
|
package/src/List.tsx
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { AccessibilityInfo, Animated, LayoutAnimation, PanResponder, StyleSheet, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { LIST_GAP, springConfig, moveListItem, resolveListDrag, resolveListItem, SiblingIndexContext, useListOrder, useInteraction, useSiblingIndex, useSurface, type ListReorderProps, type ListRowGeometry, type RuntimeProps } from '@expressive-ui/core';
|
|
4
|
+
import type { ListGroupData, ListItemData } from '@expressive-ui/schema';
|
|
5
|
+
import { tokens } from '@expressive-ui/tokens';
|
|
6
|
+
import { Avatar, Icon, ShapeIcon, T } from './primitives.js';
|
|
7
|
+
import { useExpressiveTheme, useSpringValue } from './theme.js';
|
|
8
|
+
import { Pressable } from 'react-native';
|
|
9
|
+
|
|
10
|
+
const ListPositionContext = createContext({ first: true, last: true, reorderable: false, dragging: false });
|
|
11
|
+
export type ListGroupProps = ListGroupData & RuntimeProps & ListReorderProps & { style?: StyleProp<ViewStyle> };
|
|
12
|
+
type NativeDrag = { key: string; dy: number; rows: ListRowGeometry[]; keys: string[] };
|
|
13
|
+
|
|
14
|
+
function NativeReorderRow({ children, label, index, count, reorderable, disabled, active, offset, dragValue, settle, onLayout, onStart, onMove, onFinish, onStep }: {
|
|
15
|
+
children: ReactNode; label: string; index: number; count: number; reorderable: boolean; disabled: boolean; active: boolean; offset: number; dragValue: Animated.Value; settle?: { revision: number; offset: number };
|
|
16
|
+
onLayout: (y: number, height: number) => void; onStart: () => void; onMove: (dy: number) => void; onFinish: (cancelled: boolean) => void; onStep: (step: number) => void;
|
|
17
|
+
}) {
|
|
18
|
+
const { labels } = useExpressiveTheme();
|
|
19
|
+
const latest = useRef({ disabled, onStart, onMove, onFinish });
|
|
20
|
+
latest.current = { disabled, onStart, onMove, onFinish };
|
|
21
|
+
const { colors, reduceMotion, motion } = useExpressiveTheme();
|
|
22
|
+
const translated = useSpringValue(active ? 0 : offset, 'spatialFast');
|
|
23
|
+
useLayoutEffect(() => {
|
|
24
|
+
if (!settle) return;
|
|
25
|
+
translated.setValue(reduceMotion ? 0 : settle.offset);
|
|
26
|
+
if (reduceMotion) return;
|
|
27
|
+
const animation = Animated.spring(translated, { toValue: 0, ...springConfig('spatialFast', motion), useNativeDriver: false });
|
|
28
|
+
animation.start();
|
|
29
|
+
return () => animation.stop();
|
|
30
|
+
}, [settle?.revision]);
|
|
31
|
+
const responder = useMemo(() => PanResponder.create({
|
|
32
|
+
onStartShouldSetPanResponder: () => !latest.current.disabled,
|
|
33
|
+
onMoveShouldSetPanResponder: () => !latest.current.disabled,
|
|
34
|
+
onPanResponderGrant: () => latest.current.onStart(),
|
|
35
|
+
onPanResponderMove: (_, gesture) => latest.current.onMove(gesture.dy),
|
|
36
|
+
onPanResponderRelease: () => latest.current.onFinish(false),
|
|
37
|
+
onPanResponderTerminate: () => latest.current.onFinish(true),
|
|
38
|
+
onPanResponderTerminationRequest: () => false,
|
|
39
|
+
}), []);
|
|
40
|
+
return <Animated.View onLayout={event => onLayout(event.nativeEvent.layout.y, event.nativeEvent.layout.height)} style={{ transform: [{ translateY: active ? dragValue : translated }], zIndex: active ? 2 : 0, elevation: active ? 6 : 0 }}>
|
|
41
|
+
{children}
|
|
42
|
+
{reorderable && <View {...responder.panHandlers} accessible accessibilityRole="adjustable" accessibilityLabel={labels.reorder(label)} accessibilityHint={labels.reorderHint} accessibilityState={{ disabled }} accessibilityValue={{ min: 1, max: count, now: index + 1 }} accessibilityActions={disabled ? [] : [{ name: 'decrement', label: labels.moveUp }, { name: 'increment', label: labels.moveDown }]} onAccessibilityAction={event => { if (!disabled) onStep(event.nativeEvent.actionName === 'increment' ? 1 : -1); }} style={{ position: 'absolute', right: 8, top: '50%', marginTop: -22, width: 44, height: 44, alignItems: 'center', justifyContent: 'center', opacity: disabled ? .38 : 1 }}><Icon name="drag_handle" color="onSurfaceVariant"/></View>}
|
|
43
|
+
</Animated.View>;
|
|
44
|
+
}
|
|
45
|
+
export function ListGroup({ title, reorderable = false, order, onReorder, style, children }: ListGroupProps) {
|
|
46
|
+
const { labels } = useExpressiveTheme();
|
|
47
|
+
const { items, keys, commit } = useListOrder(children, { order, onReorder }, reorderable);
|
|
48
|
+
const { reduceMotion, colors } = useExpressiveTheme();
|
|
49
|
+
const measured = useRef(new Map<string, { top: number; height: number }>());
|
|
50
|
+
const current = useRef<NativeDrag | null>(null);
|
|
51
|
+
const value = useRef(new Animated.Value(0)).current;
|
|
52
|
+
const [drag, setDrag] = useState<NativeDrag | null>(null);
|
|
53
|
+
const [settle, setSettle] = useState<{ revision: number; offsets: Record<string, number> } | null>(null);
|
|
54
|
+
const keyString = JSON.stringify(keys);
|
|
55
|
+
const result = drag ? resolveListDrag(drag.rows, drag.key, drag.dy) : null;
|
|
56
|
+
const animateLayout = () => { if (!reduceMotion) LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); };
|
|
57
|
+
const announce = (key: string, to: number) => AccessibilityInfo.announceForAccessibility(labels.position(items.find(item => item.key === key)?.label ?? labels.item, to + 1, items.length));
|
|
58
|
+
const start = (key: string) => {
|
|
59
|
+
if (current.current) return;
|
|
60
|
+
let top = 0;
|
|
61
|
+
const rows = keys.map(key => {
|
|
62
|
+
const box = measured.current.get(key) ?? { top, height: 72 };
|
|
63
|
+
top = box.top + box.height + LIST_GAP;
|
|
64
|
+
return { key, ...box };
|
|
65
|
+
});
|
|
66
|
+
value.setValue(0);
|
|
67
|
+
current.current = { key, dy: 0, rows, keys: [...keys] };
|
|
68
|
+
setDrag(current.current);
|
|
69
|
+
};
|
|
70
|
+
const move = (dy: number) => {
|
|
71
|
+
const active = current.current;
|
|
72
|
+
if (!active) return;
|
|
73
|
+
const row = active.rows.find(row => row.key === active.key)!;
|
|
74
|
+
const last = active.rows[active.rows.length - 1]!;
|
|
75
|
+
dy = Math.max(active.rows[0]!.top - row.top - 12, Math.min(last.top + last.height - row.top - row.height + 12, dy));
|
|
76
|
+
value.setValue(dy);
|
|
77
|
+
current.current = { ...active, dy };
|
|
78
|
+
setDrag(current.current);
|
|
79
|
+
};
|
|
80
|
+
const finish = (cancelled: boolean) => {
|
|
81
|
+
const active = current.current;
|
|
82
|
+
if (!active) return;
|
|
83
|
+
current.current = null;
|
|
84
|
+
cancelled ||= !reorderable || JSON.stringify(active.keys) !== keyString;
|
|
85
|
+
const result = resolveListDrag(active.rows, active.key, active.dy);
|
|
86
|
+
const next = cancelled ? active.keys : moveListItem(active.keys, result.from, result.to);
|
|
87
|
+
let top = active.rows[0]?.top ?? 0;
|
|
88
|
+
const offsets: Record<string, number> = {};
|
|
89
|
+
for (const key of next) {
|
|
90
|
+
const row = active.rows.find(row => row.key === key)!;
|
|
91
|
+
offsets[key] = row.top + (result.offsets[key] ?? 0) - top;
|
|
92
|
+
top += row.height + LIST_GAP;
|
|
93
|
+
}
|
|
94
|
+
setSettle(previous => ({ revision: (previous?.revision ?? 0) + 1, offsets }));
|
|
95
|
+
setDrag(null);
|
|
96
|
+
if (!cancelled) { commit(next); announce(active.key, result.to); }
|
|
97
|
+
};
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (current.current && (!reorderable || JSON.stringify(current.current.keys) !== keyString)) finish(true);
|
|
100
|
+
}, [keyString, reorderable]);
|
|
101
|
+
return <View style={style}>
|
|
102
|
+
{title ? <T variant="titleSmall" color="primary" accessibilityRole="header" style={styles.title}>{title}</T> : null}
|
|
103
|
+
<View style={{ gap: LIST_GAP }}>
|
|
104
|
+
{drag && result && <View pointerEvents="none" style={{ position: 'absolute', left: 0, right: 0, top: result.dropTop, height: drag.rows.find(row => row.key === drag.key)!.height, backgroundColor: colors.secondaryContainer, opacity: .35, borderRadius: tokens.shape.largeIncreased }}/>}
|
|
105
|
+
{items.map((item, index) => <NativeReorderRow key={item.key} label={item.label} index={index} count={items.length} reorderable={reorderable} disabled={item.disabled || items.length < 2} active={drag?.key === item.key} offset={result?.offsets[item.key] ?? 0} dragValue={value} settle={settle ? { revision: settle.revision, offset: settle.offsets[item.key] ?? 0 } : undefined} onLayout={(top, height) => measured.current.set(item.key, { top, height })} onStart={() => start(item.key)} onMove={move} onFinish={finish} onStep={step => {
|
|
106
|
+
const to = Math.max(0, Math.min(keys.length - 1, index + step));
|
|
107
|
+
animateLayout(); commit(moveListItem(keys, index, to)); announce(item.key, to);
|
|
108
|
+
}}><ListPositionContext.Provider value={{ first: index === 0, last: index === items.length - 1, reorderable, dragging: drag?.key === item.key }}><SiblingIndexContext.Provider value={index}>{item.node}</SiblingIndexContext.Provider></ListPositionContext.Provider></NativeReorderRow>)}
|
|
109
|
+
</View>
|
|
110
|
+
</View>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type ListItemProps = ListItemData &
|
|
114
|
+
RuntimeProps & {
|
|
115
|
+
onPress?: () => void;
|
|
116
|
+
expanded?: boolean;
|
|
117
|
+
onExpandedChange?: (expanded: boolean) => void;
|
|
118
|
+
onLongPress?: () => void;
|
|
119
|
+
/** Controle à direita (ex.: <Switch />). Só runtime; substitui o trailing dos dados. */
|
|
120
|
+
trailing?: ReactNode;
|
|
121
|
+
style?: StyleProp<ViewStyle>;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
function ListItemBody({ onPress, onLongPress, onAction, index, trailing: trailingSlot, style, children: _, expanded, onExpandedChange: __, defaultExpanded: ___, swipeActions: ____, fullSwipe: _____, ...data }: ListItemProps) {
|
|
125
|
+
const { colors } = useExpressiveTheme();
|
|
126
|
+
const { first, last, reorderable, dragging } = useContext(ListPositionContext);
|
|
127
|
+
const position = useSiblingIndex(index);
|
|
128
|
+
const card = useSurface();
|
|
129
|
+
const spec = resolveListItem(data, { index: position, card });
|
|
130
|
+
const press = onPress ?? (data.pressAction && onAction ? () => onAction(data.pressAction!) : undefined);
|
|
131
|
+
const interactive = Boolean(press || onLongPress);
|
|
132
|
+
const { state, handlers } = useInteraction(!interactive || spec.disabled);
|
|
133
|
+
const { radius, leading, trailing } = spec;
|
|
134
|
+
|
|
135
|
+
// Pontas grandes, meio pequeno; hover cresce os cantos internos, pressionar/selecionar arredonda tudo.
|
|
136
|
+
const corner = (edge: boolean) => {
|
|
137
|
+
const base = edge ? radius.outer : radius.inner;
|
|
138
|
+
if (spec.selected || dragging) return radius.selected;
|
|
139
|
+
if (state.pressed) return radius.pressed;
|
|
140
|
+
return state.hovered ? Math.max(base, radius.hover) : base;
|
|
141
|
+
};
|
|
142
|
+
const top = useSpringValue(corner(first), 'spatialDefault');
|
|
143
|
+
const bottom = useSpringValue(corner(last), 'spatialDefault');
|
|
144
|
+
const layer = useSpringValue(state.pressed ? tokens.state.pressed : state.hovered ? tokens.state.hover : 0, 'effectsDefault');
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<Pressable
|
|
148
|
+
accessibilityRole={interactive ? 'button' : undefined}
|
|
149
|
+
accessibilityLabel={spec.accessibilityLabel}
|
|
150
|
+
accessibilityState={{ disabled: spec.disabled, selected: spec.selected, expanded }}
|
|
151
|
+
disabled={!interactive || spec.disabled}
|
|
152
|
+
onPress={press}
|
|
153
|
+
onLongPress={onLongPress}
|
|
154
|
+
{...handlers}
|
|
155
|
+
>
|
|
156
|
+
<Animated.View
|
|
157
|
+
style={[
|
|
158
|
+
styles.item,
|
|
159
|
+
{
|
|
160
|
+
minHeight: spec.minHeight,
|
|
161
|
+
backgroundColor: colors[dragging ? 'secondaryContainer' : spec.container],
|
|
162
|
+
paddingRight: reorderable ? 64 : 16,
|
|
163
|
+
borderTopLeftRadius: top,
|
|
164
|
+
borderTopRightRadius: top,
|
|
165
|
+
borderBottomLeftRadius: bottom,
|
|
166
|
+
borderBottomRightRadius: bottom,
|
|
167
|
+
opacity: spec.disabled ? tokens.state.disabledContent : 1,
|
|
168
|
+
},
|
|
169
|
+
style,
|
|
170
|
+
]}
|
|
171
|
+
>
|
|
172
|
+
{interactive ? <Animated.View pointerEvents="none" style={[StyleSheet.absoluteFill, { backgroundColor: colors[spec.stateLayer], opacity: layer }]} /> : null}
|
|
173
|
+
{leading?.kind === 'icon' ? (
|
|
174
|
+
<ShapeIcon shape={leading.shape} icon={leading.name} size={leading.size} container={leading.container} content={leading.content} />
|
|
175
|
+
) : leading?.kind === 'avatar' ? (
|
|
176
|
+
<Avatar name={data.leadingAvatar!} size={leading.size} />
|
|
177
|
+
) : null}
|
|
178
|
+
<View style={styles.text}>
|
|
179
|
+
{data.overline ? (
|
|
180
|
+
<T variant="labelSmall" emphasized color={spec.supporting} style={styles.overline}>
|
|
181
|
+
{data.overline}
|
|
182
|
+
</T>
|
|
183
|
+
) : null}
|
|
184
|
+
<T variant="bodyLarge" color={spec.headline} numberOfLines={2}>
|
|
185
|
+
{data.headline}
|
|
186
|
+
</T>
|
|
187
|
+
{data.supporting ? (
|
|
188
|
+
<T variant="bodyMedium" color={spec.supporting} numberOfLines={2}>
|
|
189
|
+
{data.supporting}
|
|
190
|
+
</T>
|
|
191
|
+
) : null}
|
|
192
|
+
</View>
|
|
193
|
+
{trailingSlot ? (
|
|
194
|
+
<View style={styles.trailing}>{trailingSlot}</View>
|
|
195
|
+
) : trailing.text || trailing.badge || trailing.icon ? (
|
|
196
|
+
<View style={styles.trailing}>
|
|
197
|
+
{trailing.text ? (
|
|
198
|
+
<T variant="labelMedium" color={trailing.color} style={styles.meta}>
|
|
199
|
+
{trailing.text}
|
|
200
|
+
</T>
|
|
201
|
+
) : null}
|
|
202
|
+
{trailing.badge ? (
|
|
203
|
+
<View style={[styles.badge, { backgroundColor: colors.error }]}>
|
|
204
|
+
<T variant="labelSmall" emphasized color="onError">
|
|
205
|
+
{trailing.badge}
|
|
206
|
+
</T>
|
|
207
|
+
</View>
|
|
208
|
+
) : null}
|
|
209
|
+
{trailing.icon ? <Icon name={trailing.icon} size={24} color={trailing.color} /> : null}
|
|
210
|
+
</View>
|
|
211
|
+
) : null}
|
|
212
|
+
</Animated.View>
|
|
213
|
+
</Pressable>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const styles = StyleSheet.create({
|
|
218
|
+
title: { paddingHorizontal: 4, marginBottom: 8 },
|
|
219
|
+
item: { flexDirection: 'row', alignItems: 'center', gap: 16, paddingVertical: 10, paddingLeft: 16, paddingRight: 16, overflow: 'hidden' },
|
|
220
|
+
text: { flex: 1, minWidth: 0 },
|
|
221
|
+
overline: { textTransform: 'uppercase' },
|
|
222
|
+
trailing: { flexDirection: 'row', alignItems: 'center', gap: 8 },
|
|
223
|
+
meta: { fontVariant: ['tabular-nums'] },
|
|
224
|
+
badge: { minWidth: 16, height: 16, paddingHorizontal: 4, borderRadius: 8, alignItems: 'center', justifyContent: 'center' },
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
/** Ações também estão disponíveis por botão, sem exigir gesto horizontal. */
|
|
228
|
+
export function ListItem(props: ListItemProps) {
|
|
229
|
+
const { labels } = useExpressiveTheme();
|
|
230
|
+
const { colors, reduceMotion, motion } = useExpressiveTheme();
|
|
231
|
+
const { reorderable } = useContext(ListPositionContext);
|
|
232
|
+
const { children, expanded: controlled, defaultExpanded = false, onExpandedChange, swipeActions = [], fullSwipe = false, disabled } = props;
|
|
233
|
+
const [inner, setInner] = useState(defaultExpanded);
|
|
234
|
+
const expanded = controlled ?? inner;
|
|
235
|
+
const expandable = children !== undefined && children !== null && children !== false;
|
|
236
|
+
const [open, setOpen] = useState(false);
|
|
237
|
+
const [dismissed, setDismissed] = useState(false);
|
|
238
|
+
const width = useRef(320);
|
|
239
|
+
const offset = useRef(0);
|
|
240
|
+
const start = useRef(0);
|
|
241
|
+
const translated = useRef(new Animated.Value(0)).current;
|
|
242
|
+
const trayWidth = Math.min(swipeActions.length * 56, 168);
|
|
243
|
+
const animate = (target: number) => {
|
|
244
|
+
offset.current = target;
|
|
245
|
+
setOpen(target < 0);
|
|
246
|
+
if (reduceMotion) translated.setValue(target);
|
|
247
|
+
else Animated.spring(translated, { toValue: target, ...springConfig('spatialFast', motion), useNativeDriver: true }).start();
|
|
248
|
+
};
|
|
249
|
+
const perform = (action: NonNullable<ListItemData['swipeActions']>[number], primary: boolean) => {
|
|
250
|
+
if (disabled) return;
|
|
251
|
+
animate(0);
|
|
252
|
+
if (primary && fullSwipe) {
|
|
253
|
+
if (!reduceMotion) LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
|
|
254
|
+
setDismissed(true);
|
|
255
|
+
}
|
|
256
|
+
props.onAction?.(action);
|
|
257
|
+
};
|
|
258
|
+
const latest = useRef({ disabled, swipeActions, fullSwipe, trayWidth, animate, perform });
|
|
259
|
+
latest.current = { disabled, swipeActions, fullSwipe, trayWidth, animate, perform };
|
|
260
|
+
const responder = useMemo(() => PanResponder.create({
|
|
261
|
+
onMoveShouldSetPanResponder: (_, gesture) => !latest.current.disabled && !!latest.current.swipeActions.length && Math.abs(gesture.dx) > 10 && Math.abs(gesture.dx) > Math.abs(gesture.dy) * 1.3,
|
|
262
|
+
onPanResponderGrant: () => { translated.stopAnimation(); start.current = offset.current; },
|
|
263
|
+
onPanResponderMove: (_, gesture) => {
|
|
264
|
+
offset.current = Math.max(-(latest.current.fullSwipe ? width.current : latest.current.trayWidth), Math.min(0, start.current + gesture.dx));
|
|
265
|
+
translated.setValue(offset.current);
|
|
266
|
+
},
|
|
267
|
+
onPanResponderRelease: () => {
|
|
268
|
+
const current = latest.current;
|
|
269
|
+
if (current.fullSwipe && -offset.current >= width.current * .82) current.perform(current.swipeActions[current.swipeActions.length - 1]!, true);
|
|
270
|
+
else current.animate(offset.current < -current.trayWidth / 2 ? -current.trayWidth : 0);
|
|
271
|
+
},
|
|
272
|
+
onPanResponderTerminate: () => latest.current.animate(start.current),
|
|
273
|
+
}), []);
|
|
274
|
+
if (!expandable && !swipeActions.length) return <ListItemBody {...props}/>;
|
|
275
|
+
if (dismissed) return null;
|
|
276
|
+
const toggle = () => {
|
|
277
|
+
if (disabled) return;
|
|
278
|
+
if (!reduceMotion) LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
|
|
279
|
+
setInner(!expanded); onExpandedChange?.(!expanded);
|
|
280
|
+
};
|
|
281
|
+
return <View>
|
|
282
|
+
<View onLayout={event => { width.current = event.nativeEvent.layout.width; }} style={{ overflow: 'hidden', borderRadius: 20 }}>
|
|
283
|
+
{!!swipeActions.length && <View accessibilityElementsHidden={!open} importantForAccessibility={open ? 'auto' : 'no-hide-descendants'} pointerEvents={open ? 'auto' : 'none'} style={{ position: 'absolute', right: 0, top: 0, bottom: 0, width: trayWidth, flexDirection: 'row', gap: 2 }}>
|
|
284
|
+
{swipeActions.map((action, i) => <Pressable key={action.id} disabled={disabled} accessibilityRole="button" accessibilityLabel={action.label} onPress={() => perform(action, i === swipeActions.length - 1)} style={{ flex: 1, minWidth: 44, borderRadius: 999, alignItems: 'center', justifyContent: 'center', backgroundColor: colors[i === swipeActions.length - 1 ? 'primary' : 'secondaryContainer'] }}>
|
|
285
|
+
<Icon name={action.icon ?? 'more_vert'} size={24} color={i === swipeActions.length - 1 ? 'onPrimary' : 'onSecondaryContainer'}/>
|
|
286
|
+
</Pressable>)}
|
|
287
|
+
</View>}
|
|
288
|
+
<Animated.View {...responder.panHandlers} style={{ transform: [{ translateX: translated }] }}>
|
|
289
|
+
<ListItemBody {...props} expanded={expandable ? expanded : undefined} onPress={expandable ? toggle : props.onPress} trailing={expandable ? <Icon name={expanded ? 'expand_less' : 'expand_more'}/> : props.trailing} style={[!!swipeActions.length && { paddingRight: reorderable ? 108 : 64 }, expanded && expandable && { backgroundColor: colors.secondaryContainer }, props.style]}/>
|
|
290
|
+
{!!swipeActions.length && <Pressable accessibilityRole="button" accessibilityLabel={labels.itemActions(props.headline)} accessibilityState={{ expanded: open, disabled }} disabled={disabled} onPress={() => animate(open ? 0 : -trayWidth)} style={{ position: 'absolute', right: reorderable ? 52 : 8, top: '50%', marginTop: -22, width: 44, height: 44, alignItems: 'center', justifyContent: 'center' }}><Icon name="more_vert"/></Pressable>}
|
|
291
|
+
</Animated.View>
|
|
292
|
+
</View>
|
|
293
|
+
{expandable && expanded && <View style={{ paddingTop: 2 }}>{children}</View>}
|
|
294
|
+
</View>;
|
|
295
|
+
}
|