@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/Menu.tsx
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Fragment, useEffect, useRef, useState, type ReactNode } from 'react';
|
|
2
|
+
import { Animated, Modal, Pressable, ScrollView, StyleSheet, useWindowDimensions, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { isMenuSlot, placeMenu, resolveMenu, type MenuColor, type MenuEntry, type MenuGroup, type MenuItem, type MenuPlacement, type MenuSeparator } from '@expressive-ui/core';
|
|
4
|
+
import { tokens } from '@expressive-ui/tokens';
|
|
5
|
+
import { Icon, T, withAlpha } from './primitives.js';
|
|
6
|
+
import { useExpressiveTheme, usePresence } from './theme.js';
|
|
7
|
+
|
|
8
|
+
export type { MenuEntry, MenuGroup, MenuItem };
|
|
9
|
+
|
|
10
|
+
export type MenuTriggerProps = { open: boolean; toggle: () => void };
|
|
11
|
+
|
|
12
|
+
export type MenuProps = {
|
|
13
|
+
trigger: (props: MenuTriggerProps) => ReactNode;
|
|
14
|
+
groups?: readonly MenuGroup[];
|
|
15
|
+
items?: readonly MenuEntry[];
|
|
16
|
+
open?: boolean;
|
|
17
|
+
onOpenChange?: (open: boolean) => void;
|
|
18
|
+
onSelect?: (key: string, item: MenuItem) => void;
|
|
19
|
+
color?: MenuColor;
|
|
20
|
+
separator?: MenuSeparator;
|
|
21
|
+
maxHeight?: number;
|
|
22
|
+
placement?: Partial<MenuPlacement>;
|
|
23
|
+
matchTriggerWidth?: boolean;
|
|
24
|
+
closeOnSelect?: boolean;
|
|
25
|
+
style?: StyleProp<ViewStyle>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type Frame = { x: number; y: number; width: number; height: number };
|
|
29
|
+
|
|
30
|
+
/** Menu ancorado ao gatilho; submenus trocam o nível (com voltar), o padrão em telas de toque. */
|
|
31
|
+
export function Menu({
|
|
32
|
+
trigger,
|
|
33
|
+
groups,
|
|
34
|
+
items,
|
|
35
|
+
open: controlled,
|
|
36
|
+
onOpenChange,
|
|
37
|
+
onSelect,
|
|
38
|
+
color = 'standard',
|
|
39
|
+
separator = 'gap',
|
|
40
|
+
maxHeight,
|
|
41
|
+
placement: forced,
|
|
42
|
+
matchTriggerWidth = false,
|
|
43
|
+
closeOnSelect = true,
|
|
44
|
+
style,
|
|
45
|
+
}: MenuProps) {
|
|
46
|
+
const { labels } = useExpressiveTheme();
|
|
47
|
+
const { colors, mode } = useExpressiveTheme();
|
|
48
|
+
const window = useWindowDimensions();
|
|
49
|
+
const spec = resolveMenu(color);
|
|
50
|
+
const [inner, setInner] = useState(false);
|
|
51
|
+
const open = controlled ?? inner;
|
|
52
|
+
const setOpen = (next: boolean) => {
|
|
53
|
+
if (controlled === undefined) setInner(next);
|
|
54
|
+
onOpenChange?.(next);
|
|
55
|
+
};
|
|
56
|
+
const anchor = useRef<View>(null);
|
|
57
|
+
const [frame, setFrame] = useState<Frame | null>(null);
|
|
58
|
+
const [size, setSize] = useState<{ width: number; height: number } | null>(null);
|
|
59
|
+
// Pilha de níveis: [raiz, submenu, sub-submenu…].
|
|
60
|
+
const [stack, setStack] = useState<{ title?: string; groups: readonly MenuGroup[] }[]>([]);
|
|
61
|
+
const { progress: reveal, opacity, mounted } = usePresence(open && !!size);
|
|
62
|
+
const root = groups ?? [{ items: items ?? [] }];
|
|
63
|
+
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!open) return;
|
|
66
|
+
setStack([{ groups: root }]);
|
|
67
|
+
anchor.current?.measureInWindow((x, y, width, height) => setFrame({ x, y, width, height }));
|
|
68
|
+
}, [open]);
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!open && !mounted) {
|
|
71
|
+
setFrame(null);
|
|
72
|
+
setSize(null);
|
|
73
|
+
}
|
|
74
|
+
}, [open, mounted]);
|
|
75
|
+
|
|
76
|
+
const level = stack[stack.length - 1];
|
|
77
|
+
const choose = (item: MenuItem) => {
|
|
78
|
+
if (item.submenu?.length) return setStack([...stack, { title: item.label, groups: item.submenu }]);
|
|
79
|
+
item.onSelect?.();
|
|
80
|
+
onSelect?.(item.key, item);
|
|
81
|
+
if (closeOnSelect) setOpen(false);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
let position: ViewStyle = { opacity: 0, left: 0, top: 0 };
|
|
85
|
+
let vertical: MenuPlacement['vertical'] = 'bottom';
|
|
86
|
+
if (frame && size) {
|
|
87
|
+
const auto = placeMenu(frame, size, window);
|
|
88
|
+
vertical = forced?.vertical ?? auto.vertical;
|
|
89
|
+
const horizontal = forced?.horizontal ?? auto.horizontal;
|
|
90
|
+
position = {
|
|
91
|
+
top: vertical === 'bottom' ? frame.y + frame.height + 4 : frame.y - size.height - 4,
|
|
92
|
+
left: Math.max(8, Math.min(window.width - size.width - 8, horizontal === 'start' ? frame.x : frame.x + frame.width - size.width)),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const surface = { backgroundColor: colors[spec.container], boxShadow: tokens.elevation[mode][spec.elevation] };
|
|
97
|
+
const panel = level ? (
|
|
98
|
+
<Animated.View
|
|
99
|
+
onLayout={(e) => !size && setSize({ width: e.nativeEvent.layout.width, height: e.nativeEvent.layout.height })}
|
|
100
|
+
style={[
|
|
101
|
+
styles.panel,
|
|
102
|
+
{ minWidth: Math.max(spec.minWidth, matchTriggerWidth && frame ? frame.width : 0), maxWidth: spec.maxWidth },
|
|
103
|
+
position,
|
|
104
|
+
size && {
|
|
105
|
+
opacity,
|
|
106
|
+
transform: [{ scaleY: reveal.interpolate({ inputRange: [0, 1], outputRange: [0.8, 1] }) }],
|
|
107
|
+
transformOrigin: vertical === 'bottom' ? 'top' : 'bottom',
|
|
108
|
+
},
|
|
109
|
+
separator === 'gap' ? { gap: spec.groupGap } : [surface, { borderRadius: spec.radius.outer, padding: spec.padding }],
|
|
110
|
+
style,
|
|
111
|
+
]}
|
|
112
|
+
>
|
|
113
|
+
<ScrollView style={maxHeight ? { maxHeight } : undefined} scrollEnabled={Boolean(maxHeight)} contentContainerStyle={separator === 'gap' ? { gap: spec.groupGap } : undefined}>
|
|
114
|
+
{stack.length > 1 ? (
|
|
115
|
+
<View style={[separator === 'gap' && [surface, { borderRadius: spec.radius.outer, padding: spec.padding }]]}>
|
|
116
|
+
<Row spec={spec} item={{ key: '__back', label: level.title ?? '', icon: 'arrow_back' }} onPress={() => setStack(stack.slice(0, -1))} header />
|
|
117
|
+
</View>
|
|
118
|
+
) : null}
|
|
119
|
+
{level.groups.map((group, g) => {
|
|
120
|
+
const first = g === 0 && stack.length === 1;
|
|
121
|
+
const last = g === level.groups.length - 1;
|
|
122
|
+
return (
|
|
123
|
+
<Fragment key={group.key ?? g}>
|
|
124
|
+
{separator === 'divider' && g > 0 ? <View style={[styles.divider, { backgroundColor: colors[spec.divider] }]} /> : null}
|
|
125
|
+
<View
|
|
126
|
+
accessibilityRole="menu"
|
|
127
|
+
style={
|
|
128
|
+
separator === 'gap'
|
|
129
|
+
? [
|
|
130
|
+
surface,
|
|
131
|
+
{
|
|
132
|
+
padding: spec.padding,
|
|
133
|
+
borderTopLeftRadius: first ? spec.radius.outer : spec.radius.inner,
|
|
134
|
+
borderTopRightRadius: first ? spec.radius.outer : spec.radius.inner,
|
|
135
|
+
borderBottomLeftRadius: last ? spec.radius.outer : spec.radius.inner,
|
|
136
|
+
borderBottomRightRadius: last ? spec.radius.outer : spec.radius.inner,
|
|
137
|
+
},
|
|
138
|
+
]
|
|
139
|
+
: undefined
|
|
140
|
+
}
|
|
141
|
+
>
|
|
142
|
+
{group.label ? (
|
|
143
|
+
<T variant="labelMedium" emphasized color={spec.supporting} style={styles.label}>
|
|
144
|
+
{group.label}
|
|
145
|
+
</T>
|
|
146
|
+
) : null}
|
|
147
|
+
{group.items.map((entry) =>
|
|
148
|
+
isMenuSlot(entry) ? (
|
|
149
|
+
<View key={entry.key} style={styles.slot}>
|
|
150
|
+
{entry.slot}
|
|
151
|
+
</View>
|
|
152
|
+
) : (
|
|
153
|
+
<Row key={entry.key} spec={spec} item={entry} onPress={() => choose(entry)} />
|
|
154
|
+
),
|
|
155
|
+
)}
|
|
156
|
+
</View>
|
|
157
|
+
</Fragment>
|
|
158
|
+
);
|
|
159
|
+
})}
|
|
160
|
+
</ScrollView>
|
|
161
|
+
</Animated.View>
|
|
162
|
+
) : null;
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<View ref={anchor} collapsable={false} style={styles.anchor}>
|
|
166
|
+
{trigger({ open, toggle: () => setOpen(!open) })}
|
|
167
|
+
{open || mounted ? (
|
|
168
|
+
<Modal visible transparent animationType="none" onRequestClose={() => (stack.length > 1 ? setStack(stack.slice(0, -1)) : setOpen(false))}>
|
|
169
|
+
<Pressable accessibilityRole="button" accessibilityLabel={labels.closeMenu} style={StyleSheet.absoluteFill} onPress={() => setOpen(false)} />
|
|
170
|
+
{panel}
|
|
171
|
+
</Modal>
|
|
172
|
+
) : null}
|
|
173
|
+
</View>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function Row({ spec, item, onPress, header = false }: { spec: ReturnType<typeof resolveMenu>; item: MenuItem; onPress: () => void; header?: boolean }) {
|
|
178
|
+
const { colors } = useExpressiveTheme();
|
|
179
|
+
const selected = item.selected === true;
|
|
180
|
+
const fg = item.destructive ? 'error' : selected ? spec.selected.content : spec.content;
|
|
181
|
+
const muted = selected ? spec.selected.content : spec.supporting;
|
|
182
|
+
const hasSub = Boolean(item.submenu?.length);
|
|
183
|
+
return (
|
|
184
|
+
<Pressable
|
|
185
|
+
accessibilityRole="menuitem"
|
|
186
|
+
accessibilityLabel={header ? `Voltar: ${item.label}` : item.label}
|
|
187
|
+
accessibilityState={{ disabled: item.disabled, checked: item.selected, expanded: hasSub ? false : undefined }}
|
|
188
|
+
disabled={item.disabled}
|
|
189
|
+
onPress={onPress}
|
|
190
|
+
style={({ pressed }) => [
|
|
191
|
+
styles.item,
|
|
192
|
+
{
|
|
193
|
+
minHeight: item.supporting ? spec.itemHeightWithSupporting : spec.itemHeight,
|
|
194
|
+
borderRadius: pressed ? tokens.shape.large : spec.radius.item,
|
|
195
|
+
backgroundColor: selected ? colors[spec.selected.container] : pressed ? withAlpha(colors[spec.content], tokens.state.pressed) : 'transparent',
|
|
196
|
+
opacity: item.disabled ? tokens.state.disabledContent : 1,
|
|
197
|
+
},
|
|
198
|
+
]}
|
|
199
|
+
>
|
|
200
|
+
{selected ? <Icon name="check" size={20} color={fg} /> : item.icon ? <Icon name={item.icon} size={20} color={item.destructive ? 'error' : selected ? fg : spec.icon} /> : null}
|
|
201
|
+
<View style={styles.text}>
|
|
202
|
+
<T variant="labelLarge" emphasized={header} color={fg} numberOfLines={1}>
|
|
203
|
+
{item.label}
|
|
204
|
+
</T>
|
|
205
|
+
{item.supporting ? (
|
|
206
|
+
<T variant="bodySmall" color={muted}>
|
|
207
|
+
{item.supporting}
|
|
208
|
+
</T>
|
|
209
|
+
) : null}
|
|
210
|
+
</View>
|
|
211
|
+
{item.badge ? (
|
|
212
|
+
<View style={[styles.badge, { backgroundColor: colors[spec.badge.container] }]}>
|
|
213
|
+
<T variant="labelSmall" emphasized color={spec.badge.content}>
|
|
214
|
+
{item.badge}
|
|
215
|
+
</T>
|
|
216
|
+
</View>
|
|
217
|
+
) : null}
|
|
218
|
+
{item.trailingText ? (
|
|
219
|
+
<T variant="labelLarge" color={muted} style={styles.trailingText}>
|
|
220
|
+
{item.trailingText}
|
|
221
|
+
</T>
|
|
222
|
+
) : null}
|
|
223
|
+
{hasSub ? <Icon name="arrow_right" size={20} color={spec.icon} /> : item.trailingIcon ? <Icon name={item.trailingIcon} size={20} color={selected ? fg : spec.icon} /> : null}
|
|
224
|
+
</Pressable>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const styles = StyleSheet.create({
|
|
229
|
+
anchor: { alignSelf: 'flex-start' },
|
|
230
|
+
panel: { position: 'absolute' },
|
|
231
|
+
divider: { height: 1, marginVertical: 4, marginHorizontal: 12 },
|
|
232
|
+
label: { paddingHorizontal: 12, paddingTop: 12, paddingBottom: 4 },
|
|
233
|
+
slot: { paddingVertical: 4 },
|
|
234
|
+
item: { flexDirection: 'row', alignItems: 'center', gap: 12, paddingHorizontal: 12 },
|
|
235
|
+
text: { flex: 1, minWidth: 0 },
|
|
236
|
+
badge: { paddingHorizontal: 8, paddingVertical: 2, borderRadius: 8 },
|
|
237
|
+
trailingText: { marginLeft: 12, fontVariant: ['tabular-nums'] },
|
|
238
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import { Animated, Pressable, StyleSheet, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { NAV_BAR, NAV_RAIL, resolveNavItem, useInteraction, type NavItem } from '@expressive-ui/core';
|
|
4
|
+
import { tokens } from '@expressive-ui/tokens';
|
|
5
|
+
import { Fab } from './Fab.js';
|
|
6
|
+
import { Icon, T, withAlpha } from './primitives.js';
|
|
7
|
+
import { useExpressiveTheme, useSpringValue } from './theme.js';
|
|
8
|
+
|
|
9
|
+
type NavProps<K extends string> = {
|
|
10
|
+
items: readonly NavItem<K>[];
|
|
11
|
+
/** Chave do destino ativo (a rota atual). */
|
|
12
|
+
value: K;
|
|
13
|
+
onChange?: (key: K) => void;
|
|
14
|
+
style?: StyleProp<ViewStyle>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// stacked: barra; rail: rail recolhido (mesmo visual, sem flex: 1 na coluna); horizontal/expanded: rótulo na pílula.
|
|
18
|
+
type Layout = 'stacked' | 'rail' | 'horizontal' | 'expanded';
|
|
19
|
+
|
|
20
|
+
// No rail expandido o badge continua preso ao ícone (anatomia do M3); só a barra horizontal o põe em linha.
|
|
21
|
+
function Badge({ badge, layout }: { badge: ReturnType<typeof resolveNavItem>['badge']; layout: Layout }) {
|
|
22
|
+
const { colors } = useExpressiveTheme();
|
|
23
|
+
if (!badge) return null;
|
|
24
|
+
const position = layout === 'horizontal' ? null : [badge.dot ? styles.dotFloating : styles.badgeFloating, layout === 'expanded' && { marginTop: 12 }];
|
|
25
|
+
return (
|
|
26
|
+
<View style={[badge.dot ? styles.dot : styles.badge, { backgroundColor: colors.error }, position]}>
|
|
27
|
+
{badge.text ? (
|
|
28
|
+
<T variant="labelSmall" emphasized color="onError">
|
|
29
|
+
{badge.text}
|
|
30
|
+
</T>
|
|
31
|
+
) : null}
|
|
32
|
+
</View>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Um destino. A pílula cresce do centro (metade → inteira) com a mola spatial, como no mobile-app-rhosse.
|
|
37
|
+
function NavDestination<K extends string>({ item, active, layout, onChange }: { item: NavItem<K>; active: boolean; layout: Layout; onChange?: (key: K) => void }) {
|
|
38
|
+
const { colors } = useExpressiveTheme();
|
|
39
|
+
const spec = resolveNavItem(item, active);
|
|
40
|
+
const { state, handlers } = useInteraction(item.disabled);
|
|
41
|
+
const grow = useSpringValue(active ? 1 : 0.5, 'spatialDefault');
|
|
42
|
+
const shown = useSpringValue(active ? 1 : 0, 'effectsFast');
|
|
43
|
+
const layer = useSpringValue(state.pressed ? tokens.state.pressed : state.hovered ? tokens.state.hover : 0, 'effectsDefault');
|
|
44
|
+
const inline = layout === 'horizontal' || layout === 'expanded';
|
|
45
|
+
const height = layout === 'expanded' ? NAV_RAIL.indicator.expandedHeight : layout === 'horizontal' ? NAV_BAR.indicator.horizontalHeight : NAV_BAR.indicator.height;
|
|
46
|
+
const content = colors[spec.icon];
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<Pressable
|
|
50
|
+
accessibilityRole="tab"
|
|
51
|
+
accessibilityLabel={spec.accessibilityLabel}
|
|
52
|
+
accessibilityState={{ selected: active, disabled: item.disabled }}
|
|
53
|
+
disabled={item.disabled}
|
|
54
|
+
onPress={() => onChange?.(item.key)}
|
|
55
|
+
style={[styles.item, layout === 'stacked' && styles.itemStacked, layout === 'rail' && styles.itemRail, item.disabled && { opacity: tokens.state.disabledContent }]}
|
|
56
|
+
{...handlers}
|
|
57
|
+
>
|
|
58
|
+
<View
|
|
59
|
+
style={[
|
|
60
|
+
styles.indicator,
|
|
61
|
+
{ height, borderRadius: height / 2 },
|
|
62
|
+
inline ? { paddingLeft: 16, paddingRight: 16, gap: layout === 'expanded' ? 8 : 4 } : { width: NAV_BAR.indicator.width },
|
|
63
|
+
]}
|
|
64
|
+
>
|
|
65
|
+
<Animated.View pointerEvents="none" style={[StyleSheet.absoluteFill, { borderRadius: height / 2, backgroundColor: colors[spec.indicator], opacity: shown, transform: [{ scaleX: grow }] }]} />
|
|
66
|
+
<Animated.View pointerEvents="none" style={[StyleSheet.absoluteFill, { borderRadius: height / 2, backgroundColor: content, opacity: layer }]} />
|
|
67
|
+
<Icon name={item.icon} size={NAV_BAR.iconSize} tint={content} filled={spec.iconFilled} />
|
|
68
|
+
{inline ? (
|
|
69
|
+
<T variant={layout === 'expanded' ? NAV_RAIL.labelType : NAV_BAR.labelType} emphasized={spec.labelEmphasized} color={spec.icon} numberOfLines={1}>
|
|
70
|
+
{item.label}
|
|
71
|
+
</T>
|
|
72
|
+
) : null}
|
|
73
|
+
<Badge badge={spec.badge} layout={layout} />
|
|
74
|
+
</View>
|
|
75
|
+
{inline ? null : (
|
|
76
|
+
<T variant={NAV_BAR.labelType} color={spec.label} numberOfLines={1} style={{ fontWeight: spec.labelEmphasized ? '700' : '500' }}>
|
|
77
|
+
{item.shortLabel ?? item.label}
|
|
78
|
+
</T>
|
|
79
|
+
)}
|
|
80
|
+
</Pressable>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type NavigationBarProps<K extends string> = NavProps<K> & {
|
|
85
|
+
layout?: 'stacked' | 'horizontal';
|
|
86
|
+
/** Área segura inferior (ex.: useSafeAreaInsets().bottom); a biblioteca não depende de safe-area-context. */
|
|
87
|
+
bottomInset?: number;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** Barra inferior do M3, portada do mobile-app-rhosse e do Elo mobile. Use de 3 a 5 destinos. */
|
|
91
|
+
export function NavigationBar<K extends string>({ items, value, onChange, layout = 'stacked', bottomInset = 0, style }: NavigationBarProps<K>) {
|
|
92
|
+
const { colors } = useExpressiveTheme();
|
|
93
|
+
return (
|
|
94
|
+
<View
|
|
95
|
+
accessibilityRole="tablist"
|
|
96
|
+
style={[
|
|
97
|
+
styles.bar,
|
|
98
|
+
{ backgroundColor: colors[NAV_BAR.container], paddingTop: NAV_BAR.paddingTop, paddingBottom: Math.max(16, bottomInset + 4) },
|
|
99
|
+
layout === 'horizontal' && styles.barHorizontal,
|
|
100
|
+
style,
|
|
101
|
+
]}
|
|
102
|
+
>
|
|
103
|
+
{items.map((item) => (
|
|
104
|
+
<NavDestination key={item.key} item={item} active={item.key === value} layout={layout} onChange={onChange} />
|
|
105
|
+
))}
|
|
106
|
+
</View>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export type NavigationRailProps<K extends string> = NavProps<K> & {
|
|
111
|
+
expanded?: boolean;
|
|
112
|
+
onToggle?: () => void;
|
|
113
|
+
header?: ReactNode;
|
|
114
|
+
fab?: { icon: string; label: string; onPress?: () => void };
|
|
115
|
+
footer?: ReactNode | ((expanded: boolean) => ReactNode);
|
|
116
|
+
/** Expandido por cima do conteúdo, com scrim; o espaço recolhido continua reservado. */
|
|
117
|
+
modal?: boolean;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** Navigation rail para tablets, com a mesma anatomia do rail dos admins. */
|
|
121
|
+
export function NavigationRail<K extends string>({ items, value, onChange, expanded = false, onToggle, header, fab, footer, modal = false, style }: NavigationRailProps<K>) {
|
|
122
|
+
const { labels } = useExpressiveTheme();
|
|
123
|
+
const { colors, mode } = useExpressiveTheme();
|
|
124
|
+
const width = useSpringValue(expanded ? NAV_RAIL.expandedWidth : NAV_RAIL.collapsedWidth, 'spatialDefault');
|
|
125
|
+
const scrim = useSpringValue(modal && expanded ? 1 : 0, 'effectsDefault');
|
|
126
|
+
const rail = (
|
|
127
|
+
<Animated.View
|
|
128
|
+
style={[
|
|
129
|
+
styles.rail,
|
|
130
|
+
{ width, backgroundColor: colors[NAV_RAIL.container], paddingHorizontal: expanded ? 20 : 0, alignItems: expanded ? 'stretch' : 'center' },
|
|
131
|
+
modal && styles.railModal,
|
|
132
|
+
modal && expanded && { borderTopRightRadius: 28, borderBottomRightRadius: 28, boxShadow: tokens.elevation[mode][3] },
|
|
133
|
+
]}
|
|
134
|
+
>
|
|
135
|
+
{onToggle || header ? (
|
|
136
|
+
<View style={styles.header}>
|
|
137
|
+
{onToggle ? (
|
|
138
|
+
<Pressable
|
|
139
|
+
accessibilityRole="button"
|
|
140
|
+
accessibilityLabel={expanded ? labels.collapseMenu : labels.expandMenu}
|
|
141
|
+
accessibilityState={{ expanded }}
|
|
142
|
+
onPress={onToggle}
|
|
143
|
+
style={({ pressed }) => [styles.toggle, pressed && { backgroundColor: withAlpha(colors.onSurfaceVariant, tokens.state.pressed) }]}
|
|
144
|
+
>
|
|
145
|
+
<Icon name={expanded ? 'menu_open' : 'menu'} color="onSurfaceVariant" />
|
|
146
|
+
</Pressable>
|
|
147
|
+
) : null}
|
|
148
|
+
{header && expanded ? <View style={styles.brand}>{header}</View> : null}
|
|
149
|
+
</View>
|
|
150
|
+
) : null}
|
|
151
|
+
{fab ? (
|
|
152
|
+
<View style={[styles.fab, { alignSelf: expanded ? 'flex-start' : 'center' }]}>
|
|
153
|
+
<Fab action={{ id: 'rail.fab', label: fab.label }} icon={fab.icon} extended expanded={expanded} onPress={fab.onPress} />
|
|
154
|
+
</View>
|
|
155
|
+
) : null}
|
|
156
|
+
<View accessibilityRole="tablist" style={styles.nav}>
|
|
157
|
+
{items.map((item) => (
|
|
158
|
+
<NavDestination key={item.key} item={item} active={item.key === value} layout={expanded ? 'expanded' : 'rail'} onChange={onChange} />
|
|
159
|
+
))}
|
|
160
|
+
</View>
|
|
161
|
+
{footer ? <View style={styles.footer}>{typeof footer === 'function' ? footer(expanded) : footer}</View> : null}
|
|
162
|
+
</Animated.View>
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
if (!modal) return <View style={[{ height: '100%' }, style]}>{rail}</View>;
|
|
166
|
+
return (
|
|
167
|
+
<View style={[{ width: NAV_RAIL.collapsedWidth, height: '100%', zIndex: 20 }, style]}>
|
|
168
|
+
{expanded ? (
|
|
169
|
+
<Animated.View style={[styles.scrim, { backgroundColor: withAlpha(colors.scrim, 0.32), opacity: scrim }]}>
|
|
170
|
+
<Pressable accessibilityRole="button" accessibilityLabel={labels.collapseMenu} style={StyleSheet.absoluteFill} onPress={onToggle} />
|
|
171
|
+
</Animated.View>
|
|
172
|
+
) : null}
|
|
173
|
+
{rail}
|
|
174
|
+
</View>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const styles = StyleSheet.create({
|
|
179
|
+
bar: { flexDirection: 'row', alignItems: 'flex-start' },
|
|
180
|
+
barHorizontal: { justifyContent: 'center', alignItems: 'center', gap: 8 },
|
|
181
|
+
item: { alignItems: 'center', gap: 4 },
|
|
182
|
+
itemStacked: { flex: 1 },
|
|
183
|
+
itemRail: { paddingTop: 4, paddingBottom: 6 },
|
|
184
|
+
indicator: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center' },
|
|
185
|
+
badge: { minWidth: 16, height: 16, paddingHorizontal: 4, borderRadius: 8, alignItems: 'center', justifyContent: 'center' },
|
|
186
|
+
dot: { width: 6, height: 6, borderRadius: 3 },
|
|
187
|
+
badgeFloating: { position: 'absolute', top: -2, left: 32 },
|
|
188
|
+
dotFloating: { position: 'absolute', top: 2, left: 36 },
|
|
189
|
+
rail: { height: '100%', paddingVertical: 16, gap: NAV_RAIL.gap, overflow: 'hidden' },
|
|
190
|
+
railModal: { position: 'absolute', top: 0, bottom: 0, left: 0 },
|
|
191
|
+
header: { flexDirection: 'row', alignItems: 'center', gap: 8, minHeight: 56 },
|
|
192
|
+
toggle: { width: 56, height: 56, borderRadius: 28, alignItems: 'center', justifyContent: 'center' },
|
|
193
|
+
brand: { flex: 1, minWidth: 0 },
|
|
194
|
+
fab: { marginTop: 4, marginBottom: 16 },
|
|
195
|
+
nav: { gap: 2, alignSelf: 'stretch' },
|
|
196
|
+
footer: { marginTop: 'auto', alignSelf: 'stretch' },
|
|
197
|
+
scrim: { position: 'absolute', top: 0, bottom: 0, left: 0, width: 4000 },
|
|
198
|
+
});
|
package/src/Progress.tsx
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, Easing, View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import Svg, { Path } from 'react-native-svg';
|
|
4
|
+
import {
|
|
5
|
+
circularDash,
|
|
6
|
+
circularGeometry,
|
|
7
|
+
LOADING_CYCLE_MS,
|
|
8
|
+
loadingFrame,
|
|
9
|
+
resolveLoading,
|
|
10
|
+
resolveProgress,
|
|
11
|
+
waveLinePath,
|
|
12
|
+
type ProgressSpec,
|
|
13
|
+
type RuntimeProps,
|
|
14
|
+
} from '@expressive-ui/core';
|
|
15
|
+
import type { LoadingIndicatorData, ProgressIndicatorData } from '@expressive-ui/schema';
|
|
16
|
+
import { tokens, type ColorRole } from '@expressive-ui/tokens';
|
|
17
|
+
import { useExpressiveTheme, useSpringValue } from './theme.js';
|
|
18
|
+
|
|
19
|
+
// --- LoadingIndicator ---------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
export type LoadingIndicatorProps = LoadingIndicatorData &
|
|
22
|
+
RuntimeProps & {
|
|
23
|
+
color?: ColorRole;
|
|
24
|
+
/** Cor já resolvida (ex.: a do conteúdo de um Button); tem prioridade sobre `color`. */
|
|
25
|
+
tint?: string;
|
|
26
|
+
style?: StyleProp<ViewStyle>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Mesma sequência de formas do sprite do mobile-app-rhosse, agora em vetor: o quadro é calculado
|
|
31
|
+
* pelo core a cada frame (forma + rotação no mesmo path). Com "reduzir movimento" gira na metade da velocidade.
|
|
32
|
+
*/
|
|
33
|
+
export function LoadingIndicator({ contained, size, label: labelProp, color, tint, style }: LoadingIndicatorProps) {
|
|
34
|
+
const { labels } = useExpressiveTheme();
|
|
35
|
+
const label = labelProp ?? labels.loading;
|
|
36
|
+
const { colors, reduceMotion } = useExpressiveTheme();
|
|
37
|
+
const spec = resolveLoading({ contained, size });
|
|
38
|
+
const [t, setT] = useState(0);
|
|
39
|
+
const cycle = LOADING_CYCLE_MS * (reduceMotion ? 2 : 1);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const start = Date.now();
|
|
42
|
+
let frame = requestAnimationFrame(function tick() {
|
|
43
|
+
setT(((Date.now() - start) % cycle) / cycle);
|
|
44
|
+
frame = requestAnimationFrame(tick);
|
|
45
|
+
});
|
|
46
|
+
return () => cancelAnimationFrame(frame);
|
|
47
|
+
}, [cycle]);
|
|
48
|
+
return (
|
|
49
|
+
<View
|
|
50
|
+
accessibilityRole="progressbar"
|
|
51
|
+
accessibilityLabel={label}
|
|
52
|
+
accessibilityState={{ busy: true }}
|
|
53
|
+
style={[
|
|
54
|
+
{
|
|
55
|
+
width: spec.size,
|
|
56
|
+
height: spec.size,
|
|
57
|
+
borderRadius: spec.size / 2,
|
|
58
|
+
backgroundColor: spec.container ? colors[spec.container] : undefined,
|
|
59
|
+
alignItems: 'center',
|
|
60
|
+
justifyContent: 'center',
|
|
61
|
+
},
|
|
62
|
+
style,
|
|
63
|
+
]}
|
|
64
|
+
>
|
|
65
|
+
<Svg width={spec.shapeSize} height={spec.shapeSize} viewBox="0 0 100 100">
|
|
66
|
+
<Path d={loadingFrame(t)} fill={tint ?? colors[color ?? spec.content]} />
|
|
67
|
+
</Svg>
|
|
68
|
+
</View>
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// --- ProgressIndicator --------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
export type ProgressIndicatorProps = ProgressIndicatorData & RuntimeProps & { style?: StyleProp<ViewStyle> };
|
|
75
|
+
|
|
76
|
+
export function ProgressIndicator({ onAction: _, index: __, children: ___, style, ...data }: ProgressIndicatorProps) {
|
|
77
|
+
const spec = resolveProgress(data);
|
|
78
|
+
return spec.type === 'circular' ? <CircularProgress spec={spec} style={style} /> : <LinearProgress spec={spec} style={style} />;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const accessibility = (spec: ProgressSpec) => ({
|
|
82
|
+
accessibilityRole: 'progressbar' as const,
|
|
83
|
+
accessibilityLabel: spec.label,
|
|
84
|
+
accessibilityValue: spec.value === null ? undefined : { min: spec.accessibility.min, max: spec.accessibility.max, now: spec.accessibility.now },
|
|
85
|
+
accessibilityState: spec.value === null ? { busy: true } : undefined,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Laço contínuo 0→1 no driver nativo (só transform).
|
|
89
|
+
function useLoop(duration: number, easing: (t: number) => number, enabled = true) {
|
|
90
|
+
const value = useRef(new Animated.Value(0)).current;
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!enabled) return;
|
|
93
|
+
const loop = Animated.loop(Animated.timing(value, { toValue: 1, duration, easing, useNativeDriver: true }));
|
|
94
|
+
loop.start();
|
|
95
|
+
return () => loop.stop();
|
|
96
|
+
}, [value, duration, easing, enabled]);
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const linear = Easing.linear;
|
|
101
|
+
const emphasized = Easing.bezier(...tokens.motion.easing.emphasized);
|
|
102
|
+
|
|
103
|
+
/** Onda que "corre": desenhada com um comprimento a mais e deslocada em laço (driver nativo). */
|
|
104
|
+
function Wave({ spec, length, color }: { spec: ProgressSpec; length: number; color: string }) {
|
|
105
|
+
const { reduceMotion } = useExpressiveTheme();
|
|
106
|
+
const phase = useLoop(spec.value === null ? 500 : 1000, linear, !reduceMotion);
|
|
107
|
+
const translateX = phase.interpolate({ inputRange: [0, 1], outputRange: [-spec.wavelength, 0] });
|
|
108
|
+
return (
|
|
109
|
+
<Animated.View style={{ width: length + spec.wavelength, height: spec.waveHeight, transform: [{ translateX }] }}>
|
|
110
|
+
<Svg width={length + spec.wavelength} height={spec.waveHeight}>
|
|
111
|
+
<Path d={waveLinePath(length + spec.wavelength, spec.wavelength, spec.amplitude, spec.waveHeight)} fill="none" stroke={color} strokeWidth={spec.track} />
|
|
112
|
+
</Svg>
|
|
113
|
+
</Animated.View>
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function LinearProgress({ spec, style }: { spec: ProgressSpec; style?: StyleProp<ViewStyle> }) {
|
|
118
|
+
const { colors } = useExpressiveTheme();
|
|
119
|
+
const [width, setWidth] = useState(0);
|
|
120
|
+
const active = colors[spec.active];
|
|
121
|
+
const height = spec.wavy ? spec.waveHeight : spec.track;
|
|
122
|
+
const usable = Math.max(0, width - spec.stop - spec.gap);
|
|
123
|
+
const fill = useSpringValue((spec.value ?? 0) * usable, 'effectsSlow');
|
|
124
|
+
const slide = useLoop(1800, emphasized, spec.value === null && width > 0);
|
|
125
|
+
const bar = width * 0.4;
|
|
126
|
+
const segment = (length: number | Animated.Value) => (
|
|
127
|
+
<Animated.View style={{ width: length, height, overflow: 'hidden', justifyContent: 'center', borderRadius: spec.wavy ? 0 : spec.track / 2 }}>
|
|
128
|
+
{spec.wavy ? <Wave spec={spec} length={typeof length === 'number' ? length : usable} color={active} /> : <View style={{ height: spec.track, backgroundColor: active }} />}
|
|
129
|
+
</Animated.View>
|
|
130
|
+
);
|
|
131
|
+
const trackStyle = { height: spec.track, borderRadius: spec.track / 2, backgroundColor: colors[spec.trackColor] };
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<View
|
|
135
|
+
onLayout={(e) => setWidth(e.nativeEvent.layout.width)}
|
|
136
|
+
style={[{ height, flexDirection: 'row', alignItems: 'center', overflow: spec.value === null ? 'hidden' : 'visible' }, style]}
|
|
137
|
+
{...accessibility(spec)}
|
|
138
|
+
>
|
|
139
|
+
{!width ? null : spec.value === null ? (
|
|
140
|
+
<>
|
|
141
|
+
<View style={[trackStyle, { position: 'absolute', left: 0, right: 0 }]} />
|
|
142
|
+
<Animated.View style={{ position: 'absolute', left: 0, transform: [{ translateX: slide.interpolate({ inputRange: [0, 1], outputRange: [-bar, width] }) }] }}>
|
|
143
|
+
{segment(bar)}
|
|
144
|
+
</Animated.View>
|
|
145
|
+
</>
|
|
146
|
+
) : (
|
|
147
|
+
<>
|
|
148
|
+
{segment(fill)}
|
|
149
|
+
{spec.value < 0.995 ? <View style={[trackStyle, { flex: 1, marginLeft: spec.gap }]} /> : null}
|
|
150
|
+
<View style={{ position: 'absolute', right: 0, width: spec.stop, height: spec.stop, borderRadius: spec.stop / 2, backgroundColor: active }} />
|
|
151
|
+
</>
|
|
152
|
+
)}
|
|
153
|
+
</View>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Strokes com comprimento absoluto (react-native-svg não garante pathLength).
|
|
158
|
+
function CircularProgress({ spec, style }: { spec: ProgressSpec; style?: StyleProp<ViewStyle> }) {
|
|
159
|
+
const { colors } = useExpressiveTheme();
|
|
160
|
+
const geo = circularGeometry(spec);
|
|
161
|
+
const indeterminate = spec.value === null;
|
|
162
|
+
const dash = circularDash(spec.value ?? 0.28, geo.gapFraction);
|
|
163
|
+
const spin = useLoop(1600, linear, indeterminate);
|
|
164
|
+
const rotate = spin.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '360deg'] });
|
|
165
|
+
return (
|
|
166
|
+
<Animated.View style={[{ width: spec.size, height: spec.size, transform: indeterminate ? [{ rotate }] : [] }, style]} {...accessibility(spec)}>
|
|
167
|
+
<Svg width={spec.size} height={spec.size}>
|
|
168
|
+
{!indeterminate && dash.showTrack ? (
|
|
169
|
+
<Path
|
|
170
|
+
d={geo.track}
|
|
171
|
+
fill="none"
|
|
172
|
+
stroke={colors[spec.trackColor]}
|
|
173
|
+
strokeWidth={spec.track}
|
|
174
|
+
strokeLinecap="round"
|
|
175
|
+
strokeDasharray={[dash.trackLength * geo.trackLength, geo.trackLength]}
|
|
176
|
+
strokeDashoffset={-dash.trackStart * geo.trackLength}
|
|
177
|
+
/>
|
|
178
|
+
) : null}
|
|
179
|
+
{indeterminate || dash.showActive ? (
|
|
180
|
+
<Path
|
|
181
|
+
d={geo.active}
|
|
182
|
+
fill="none"
|
|
183
|
+
stroke={colors[spec.active]}
|
|
184
|
+
strokeWidth={spec.track}
|
|
185
|
+
strokeLinecap="round"
|
|
186
|
+
strokeDasharray={[dash.active * geo.activeLength, geo.activeLength]}
|
|
187
|
+
/>
|
|
188
|
+
) : null}
|
|
189
|
+
</Svg>
|
|
190
|
+
</Animated.View>
|
|
191
|
+
);
|
|
192
|
+
}
|