@plexui/react-native 0.2.15 → 0.2.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/components/SegmentedControl/SegmentedControl.js +22 -18
- package/dist/es/components/TabBar/TabBar.js +68 -4
- package/dist/types/components/SegmentedControl/SegmentedControl.d.ts +8 -1
- package/package.json +1 -1
- package/src/components/SegmentedControl/SegmentedControl.tsx +25 -10
- package/src/components/TabBar/TabBar.tsx +83 -3
|
@@ -70,7 +70,7 @@ const optionName = (option) => {
|
|
|
70
70
|
* iOS 26 apps (48pt; 40 in a Toolbar.Row), its thumb sliding to the selection; on Android a segmented capsule, white
|
|
71
71
|
* with a sliding pill of the accent. Props follow @plexui/ui (`SegmentedControl` and `SegmentedControl.Tab`).
|
|
72
72
|
*/
|
|
73
|
-
function SegmentedControlRoot({ value, onChange, onClick, "aria-label": ariaLabel, size = "md", disabled = false, block = false, mobileMaterial, mobileSegmentWidth = "equal", showBadges = false, style, testID, children, }) {
|
|
73
|
+
function SegmentedControlRoot({ value, onChange, onClick, "aria-label": ariaLabel, size = "md", disabled = false, block = false, mobileMaterial, mobileSegmentWidth = "equal", showBadges = false, badgeColor = "label", style, testID, children, }) {
|
|
74
74
|
const theme = useTheme();
|
|
75
75
|
const options = childrenOfType(children, SegmentedControlTab).map((child) => child.props);
|
|
76
76
|
const select = (option) => {
|
|
@@ -85,11 +85,11 @@ function SegmentedControlRoot({ value, onChange, onClick, "aria-label": ariaLabe
|
|
|
85
85
|
});
|
|
86
86
|
// Drawn counts differ from tab to tab, so each segment takes its own width with them, as a messenger's folder tabs
|
|
87
87
|
const counted = showBadges && options.some((option) => option.badge != null);
|
|
88
|
-
const common = { options, value, select, keys, disabled, block, equal: mobileSegmentWidth === "equal" && !counted, showBadges };
|
|
88
|
+
const common = { options, value, select, keys, disabled, block, equal: mobileSegmentWidth === "equal" && !counted, showBadges, badgeColor };
|
|
89
89
|
return (_jsx(View, { accessibilityRole: "tablist", "aria-label": ariaLabel, accessibilityState: { disabled }, testID: testID, style: [block ? styles.block : styles.hug, style], children: theme.isAndroid ? _jsx(AndroidControl, { ...common, size: size }) : _jsx(IOSControl, { ...common, size: size, material: mobileMaterial }) }));
|
|
90
90
|
}
|
|
91
91
|
SegmentedControlRoot.displayName = "SegmentedControl";
|
|
92
|
-
function IOSControl({ options, value, select, keys, disabled, block, equal, showBadges, size, material }) {
|
|
92
|
+
function IOSControl({ options, value, select, keys, disabled, block, equal, showBadges, badgeColor, size, material }) {
|
|
93
93
|
const theme = useTheme();
|
|
94
94
|
const inBar = !!useContext(BarItemContext);
|
|
95
95
|
const { inRow } = useContext(SegmentedControlRowContext);
|
|
@@ -161,7 +161,7 @@ function IOSControl({ options, value, select, keys, disabled, block, equal, show
|
|
|
161
161
|
], children: _jsxs(View, { onLayout: (event) => {
|
|
162
162
|
const w = Math.ceil(event.nativeEvent.layout.width);
|
|
163
163
|
setLabels((prev) => (prev[option.value] === w ? prev : { ...prev, [option.value]: w }));
|
|
164
|
-
}, style: [styles.label, held === option.value ? styles.heldLabel : null], children: [symbolOnly ? (_jsx(IconDefaultsProvider, { size: 20, color: color, children: option.icon })) : (_jsx(Text, { numberOfLines: 1, maxFontSizeMultiplier: maxScale, color: color, weight: isSelected ? look.selectedWeight : look.weight, style: { fontSize: look.fontSize, lineHeight: look.lineHeight }, children: option.children })), showBadges ? _jsx(SegmentCount, { badge: option.badge, selected: isSelected, off: off }) : null] }) }, option.value));
|
|
164
|
+
}, style: [styles.label, held === option.value ? styles.heldLabel : null], children: [symbolOnly ? (_jsx(IconDefaultsProvider, { size: 20, color: color, children: option.icon })) : (_jsx(Text, { numberOfLines: 1, maxFontSizeMultiplier: maxScale, color: color, weight: isSelected ? look.selectedWeight : look.weight, style: { fontSize: look.fontSize, lineHeight: look.lineHeight }, children: option.children })), showBadges ? _jsx(SegmentCount, { badge: option.badge, selected: isSelected, off: off, color: badgeColor }) : null] }) }, option.value));
|
|
165
165
|
})] }));
|
|
166
166
|
if (look.glass) {
|
|
167
167
|
// The glass keeps the control's fill over it: one flat layer, no sheen (touch.css)
|
|
@@ -183,7 +183,7 @@ const ANDROID_MOVE = { duration: 320, easing: curves.easeOutQuint };
|
|
|
183
183
|
* its label in the accent, the others in the secondary gray. The pill slides to a new selection and takes its width.
|
|
184
184
|
* In a bar's row of controls the capsule takes the height of the circles beside it (46dp).
|
|
185
185
|
*/
|
|
186
|
-
function AndroidControl({ options, value, select, keys, disabled, block, equal, showBadges, size }) {
|
|
186
|
+
function AndroidControl({ options, value, select, keys, disabled, block, equal, showBadges, badgeColor, size }) {
|
|
187
187
|
const theme = useTheme();
|
|
188
188
|
const m = theme.material;
|
|
189
189
|
const bar = useContext(BarItemContext);
|
|
@@ -219,13 +219,13 @@ function AndroidControl({ options, value, select, keys, disabled, block, equal,
|
|
|
219
219
|
styles.androidRow,
|
|
220
220
|
{ height, borderRadius: height / 2, backgroundColor: barSurface(m, theme.scheme), boxShadow: barShadow(theme.scheme) },
|
|
221
221
|
block ? styles.stretch : null,
|
|
222
|
-
], children: [frame ? (_jsx(Animated.View, { pointerEvents: "none", style: [styles.androidPill, { height: inner, borderRadius: inner / 2, backgroundColor: withAlpha(m.primary, 0.12), left: x, width }] })) : null, options.map((option) => (_jsx(AndroidSegment, { option: option, selected: option.value === value, off: disabled || !!option.disabled, framed: frame, showBadges: showBadges, height: inner,
|
|
222
|
+
], children: [frame ? (_jsx(Animated.View, { pointerEvents: "none", style: [styles.androidPill, { height: inner, borderRadius: inner / 2, backgroundColor: withAlpha(m.primary, 0.12), left: x, width }] })) : null, options.map((option) => (_jsx(AndroidSegment, { option: option, selected: option.value === value, off: disabled || !!option.disabled, framed: frame, showBadges: showBadges, badgeColor: badgeColor, height: inner,
|
|
223
223
|
// In a filled row equal segments share the width and the others take their label and share what is left; a
|
|
224
224
|
// control that hugs its segments gives equal ones the widest one's width
|
|
225
225
|
size: block ? (equal ? styles.grow : styles.share) : widest ? { minWidth: widest } : null, keys: keys, select: select, onWidth: (w) => setWidths((prev) => (prev[option.value] === w ? prev : { ...prev, [option.value]: w })) }, option.value)))] }));
|
|
226
226
|
}
|
|
227
227
|
/** One segment of the capsule: its label turns from the gray to the accent as the pill comes, and back as it goes. */
|
|
228
|
-
function AndroidSegment({ option, selected, off, framed, showBadges, height, size, keys, select, onWidth }) {
|
|
228
|
+
function AndroidSegment({ option, selected, off, framed, showBadges, badgeColor, height, size, keys, select, onWidth }) {
|
|
229
229
|
const theme = useTheme();
|
|
230
230
|
const m = theme.material;
|
|
231
231
|
const [tint] = useState(() => new Animated.Value(selected ? 1 : 0));
|
|
@@ -251,14 +251,15 @@ function AndroidSegment({ option, selected, off, framed, showBadges, height, siz
|
|
|
251
251
|
selected && !framed ? { backgroundColor: withAlpha(m.primary, 0.12) } : null,
|
|
252
252
|
], children: [isRenderable(option.icon) ? (_jsx(IconDefaultsProvider, { size: 20, color: color, children: option.icon })) : null, isRenderable(option.children) ? (
|
|
253
253
|
// An Animated text, so its color can blend: the face, size and tracking of the kit's Text
|
|
254
|
-
_jsx(Animated.Text, { numberOfLines: 1, style: [textStyle(theme, { weight: "medium" }), { fontSize: 14, lineHeight: 20, letterSpacing: theme.tracking(14), color: label }], children: option.children })) : null, showBadges ? _jsx(SegmentCount, { badge: option.badge, selected: selected, off: off }) : null] }));
|
|
254
|
+
_jsx(Animated.Text, { numberOfLines: 1, style: [textStyle(theme, { weight: "medium" }), { fontSize: 14, lineHeight: 20, letterSpacing: theme.tracking(14), color: label }], children: option.children })) : null, showBadges ? _jsx(SegmentCount, { badge: option.badge, selected: selected, off: off, color: badgeColor }) : null] }));
|
|
255
255
|
}
|
|
256
256
|
/**
|
|
257
257
|
* A tab's count drawn after its label (`showBadges`): a capsule 18 tall, the digits 12 semibold in the screen's color.
|
|
258
258
|
* iOS: the label's color on the selected segment, so it reads on the thumb, the secondary label on the others.
|
|
259
|
-
* Android: the accent on the selected segment, as its label, the gray on the others.
|
|
259
|
+
* Android: the accent on the selected segment, as its label, the gray on the others. `accent`: the accent's capsule and
|
|
260
|
+
* white digits on every segment, on both platforms.
|
|
260
261
|
*/
|
|
261
|
-
function SegmentCount({ badge, selected, off }) {
|
|
262
|
+
function SegmentCount({ badge, selected, off, color }) {
|
|
262
263
|
const theme = useTheme();
|
|
263
264
|
const text = badge != null ? badgeText(badge) : undefined;
|
|
264
265
|
if (!text)
|
|
@@ -266,16 +267,19 @@ function SegmentCount({ badge, selected, off }) {
|
|
|
266
267
|
// iOS: the label's color in a capsule, the bar's ink in it; Android: the segment's own ink, the selected one's
|
|
267
268
|
// container in it (onSecondaryContainer on secondaryContainer), the others' onSurfaceVariant on the surface, so the
|
|
268
269
|
// small digits keep their contrast
|
|
270
|
+
const accent = color === "accent";
|
|
269
271
|
const fill = off
|
|
270
272
|
? theme.color("--alpha-20")
|
|
271
|
-
:
|
|
272
|
-
?
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
:
|
|
278
|
-
|
|
273
|
+
: accent
|
|
274
|
+
? theme.color("--color-background-accent-solid")
|
|
275
|
+
: theme.isAndroid
|
|
276
|
+
? selected
|
|
277
|
+
? theme.material.onSecondaryContainer
|
|
278
|
+
: theme.material.onSurfaceVariant
|
|
279
|
+
: selected
|
|
280
|
+
? theme.colors.text
|
|
281
|
+
: theme.colors.labelSecondary;
|
|
282
|
+
const ink = accent && !off ? theme.color("--color-text-accent-solid", "#FFFFFF") : theme.isAndroid ? (selected ? theme.material.secondaryContainer : theme.material.surface) : theme.colors.background;
|
|
279
283
|
return (_jsx(View, { "aria-hidden": true, style: [styles.count, { backgroundColor: fill }], children: _jsx(Text, { numberOfLines: 1, weight: "semibold", color: ink, style: styles.countText, children: text }) }));
|
|
280
284
|
}
|
|
281
285
|
export const SegmentedControl = Object.assign(SegmentedControlRoot, { Tab: SegmentedControlTab, Option: SegmentedControlTab });
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
3
|
import { Animated, Easing, StyleSheet, View } from "react-native";
|
|
4
4
|
import { Pressable } from "../../internal/Pressable";
|
|
5
5
|
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
@@ -70,7 +70,14 @@ function IOSTab({ value, label, icon, badge, badgeColor, disabled = false, onPre
|
|
|
70
70
|
// A minimized tab takes the touches of its whole 48pt circle, not only its 40pt face
|
|
71
71
|
hitSlop: collapsed ? capsulePadding : undefined,
|
|
72
72
|
// A tab bar's labels keep their size, as UIKit's: a large text size shows the tab in the large content viewer
|
|
73
|
-
accessibilityShowsLargeContentViewer: true, accessibilityLargeContentTitle: textOf(label) || undefined,
|
|
73
|
+
accessibilityShowsLargeContentViewer: true, accessibilityLargeContentTitle: textOf(label) || undefined, onLayout:
|
|
74
|
+
// An expanded tab only: a minimized one's 40pt face is not where the pill goes when the bar expands
|
|
75
|
+
!circle && !collapsed && context.onTabLayout
|
|
76
|
+
? (event) => {
|
|
77
|
+
const { y, width, height } = event.nativeEvent.layout;
|
|
78
|
+
context.onTabLayout?.(value, { y, width, height });
|
|
79
|
+
}
|
|
80
|
+
: undefined, style: [styles.iosItem, size, circle ? null : { flexShrink: 1, minWidth: 0 }], children: [selected && !collapsed && (circle || context.slidingPill !== value) ? _jsx(View, { pointerEvents: "none", style: [styles.pill, pill, { backgroundColor: theme.color("--tab-bar-item-background-selected") }] }) : null, _jsx(View, { style: [styles.iosIcon, { height: theme.length("--tab-bar-icon-box", 28) }], children: _jsx(IconDefaultsProvider, { size: theme.length("--tab-bar-icon-size", 28), color: color, children: icon }) }), showLabel ? (_jsx(Text, { numberOfLines: 1, allowFontScaling: false, weight: "semibold", color: color, style: { fontSize: theme.length("--tab-bar-label-font-size", 10), lineHeight: theme.length("--tab-bar-label-line-height", 12), maxWidth: "100%" }, children: label })) : null, hasBadge(badge) && !circle ? (_jsx(View, { pointerEvents: "none", style: [styles.iosBadge, iosBadgeStyle(theme, badgeColor ?? context.badgeColor), { top: badgeTop, marginLeft: badgeStart }], children: badge === true ? null : (_jsx(Text, { color: (badgeColor ?? context.badgeColor) === "accent" ? theme.color("--color-text-accent-solid", "#FFFFFF") : theme.color("--tab-bar-badge-color"), style: iosBadgeText(theme), numberOfLines: 1, allowFontScaling: false, children: badge })) })) : null] }));
|
|
74
81
|
}
|
|
75
82
|
const iosBadgeStyle = (theme, color) => {
|
|
76
83
|
const size = theme.length("--tab-bar-badge-size", 18);
|
|
@@ -346,7 +353,61 @@ function TabBarRoot({ value: valueProp, defaultValue, onValueChange, search, pro
|
|
|
346
353
|
const visible = shown.length ? shown : capsuleTabs.slice(0, 1);
|
|
347
354
|
const reachable = [...visible, ...(circle ? [circle] : [])];
|
|
348
355
|
const keys = useTabKeys(reachable, value, select);
|
|
349
|
-
|
|
356
|
+
// iOS 26 slides the selection to the new tab: one pill in the capsule, under the tabs, moving from tab to tab
|
|
357
|
+
const [frames, setFrames] = useState({});
|
|
358
|
+
const onTabLayout = useCallback((tab, next) => {
|
|
359
|
+
setFrames((current) => {
|
|
360
|
+
const prev = current[tab];
|
|
361
|
+
return prev && prev.y === next.y && prev.width === next.width && prev.height === next.height ? current : { ...current, [tab]: next };
|
|
362
|
+
});
|
|
363
|
+
}, []);
|
|
364
|
+
// The pill's place from the start of the capsule: its padding and the widths of the tabs before it, in their order
|
|
365
|
+
// (a reorder moves it with no resize to report; the start edge, so a right-to-left bar mirrors it as its tabs)
|
|
366
|
+
const capsuleInline = theme.length("--tab-bar-capsule-padding-inline", 10);
|
|
367
|
+
const selectedIndex = value != null && !minimized ? visible.findIndex((tab) => tab.value === value) : -1;
|
|
368
|
+
const before = selectedIndex >= 0 ? visible.slice(0, selectedIndex) : [];
|
|
369
|
+
const frame = selectedIndex >= 0 && theme.isIOS ? frames[visible[selectedIndex].value] : undefined;
|
|
370
|
+
const pillFrame = frame && before.every((tab) => frames[tab.value] != null)
|
|
371
|
+
? { ...frame, x: capsuleInline + before.reduce((sum, tab) => sum + frames[tab.value].width, 0) }
|
|
372
|
+
: undefined;
|
|
373
|
+
const context = {
|
|
374
|
+
value,
|
|
375
|
+
badgeColor,
|
|
376
|
+
minimized,
|
|
377
|
+
select,
|
|
378
|
+
expand,
|
|
379
|
+
keys,
|
|
380
|
+
onTabLayout: theme.isIOS ? onTabLayout : undefined,
|
|
381
|
+
slidingPill: pillFrame ? value : undefined,
|
|
382
|
+
};
|
|
383
|
+
const pillOutset = theme.length("--tab-bar-pill-outset", 6);
|
|
384
|
+
const [pillX] = useState(() => new Animated.Value(0));
|
|
385
|
+
const [pillWidth] = useState(() => new Animated.Value(0));
|
|
386
|
+
const pillShown = useRef(false);
|
|
387
|
+
const pillLeft = pillFrame ? pillFrame.x - pillOutset : undefined;
|
|
388
|
+
const pillSpan = pillFrame ? pillFrame.width + 2 * pillOutset : undefined;
|
|
389
|
+
// Before the paint, so the pill never shows a frame at its old place or at 0
|
|
390
|
+
useLayoutEffect(() => {
|
|
391
|
+
if (pillLeft == null || pillSpan == null) {
|
|
392
|
+
pillShown.current = false;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
// It appears in place (the first frame, back from a minimized bar or the circle's tab), then slides: CSS `ease`
|
|
396
|
+
// over 300ms, as SegmentedControl's thumb
|
|
397
|
+
if (!pillShown.current || theme.reducedMotion) {
|
|
398
|
+
pillX.setValue(pillLeft);
|
|
399
|
+
pillWidth.setValue(pillSpan);
|
|
400
|
+
pillShown.current = true;
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const easing = Easing.bezier(0.25, 0.1, 0.25, 1);
|
|
404
|
+
const animation = Animated.parallel([
|
|
405
|
+
Animated.timing(pillX, { toValue: pillLeft, duration: 300, easing, useNativeDriver: false }),
|
|
406
|
+
Animated.timing(pillWidth, { toValue: pillSpan, duration: 300, easing, useNativeDriver: false }),
|
|
407
|
+
]);
|
|
408
|
+
animation.start();
|
|
409
|
+
return () => animation.stop();
|
|
410
|
+
}, [pillLeft, pillSpan, theme.reducedMotion, pillX, pillWidth]);
|
|
350
411
|
if (theme.isAndroid) {
|
|
351
412
|
return (_jsx(TabBarContext.Provider, { value: context, children: _jsx(AndroidBar, { context: context, tabs: capsuleTabs, circle: circle, accent: !!prominentTab, accessory: accessory, background: background, scrolled: scrolled, canvasColor: canvasColor, safeArea: safeArea, ariaLabel: ariaLabel, rest: rest, style: style }) }));
|
|
352
413
|
}
|
|
@@ -376,7 +437,10 @@ function TabBarRoot({ value: valueProp, defaultValue, onValueChange, search, pro
|
|
|
376
437
|
{ paddingTop: padTop + (accessory ? accessoryHeight + accessoryGap : 0), paddingBottom },
|
|
377
438
|
physicalPadding(sideLeft, sideRight),
|
|
378
439
|
style,
|
|
379
|
-
], children: [backdrop, accessory ? (_jsx(Glass, { radius: accessoryHeight / 2, elevation: "raised", style: [styles.accessory, accessoryPlace], children: accessory }, "accessory")) : null, _jsxs(View, { style: [styles.iosRow, { gap: theme.length("--tab-bar-gap", 8), justifyContent: circle || minimized ? "space-between" : "center" }], children: [
|
|
440
|
+
], children: [backdrop, accessory ? (_jsx(Glass, { radius: accessoryHeight / 2, elevation: "raised", style: [styles.accessory, accessoryPlace], children: accessory }, "accessory")) : null, _jsxs(View, { style: [styles.iosRow, { gap: theme.length("--tab-bar-gap", 8), justifyContent: circle || minimized ? "space-between" : "center" }], children: [_jsxs(Glass, { radius: 999, elevation: "raised", style: [styles.capsule, minimized ? { padding: capsulePadding } : { paddingVertical: capsulePadding, paddingHorizontal: theme.length("--tab-bar-capsule-padding-inline", 10) }], children: [pillFrame ? (_jsx(Animated.View, { pointerEvents: "none", style: [
|
|
441
|
+
styles.pill,
|
|
442
|
+
{ top: pillFrame.y, height: pillFrame.height, start: pillX, width: pillWidth, backgroundColor: theme.color("--tab-bar-item-background-selected") },
|
|
443
|
+
] })) : null, visible.map((tab) => (_jsx(Tab, { ...tab, context: context }, tab.value)))] }), circle ? (_jsx(Glass, { radius: circleSize / 2, elevation: "raised", style: { width: circleSize, height: circleSize }, children: _jsx(Tab, { ...circle, context: { ...context, minimized: false }, kind: "circle", hideLabel: true }) })) : null] })] }) }));
|
|
380
444
|
}
|
|
381
445
|
TabBarRoot.displayName = "TabBar";
|
|
382
446
|
export const TabBar = Object.assign(TabBarRoot, { Item: TabBarItem });
|
|
@@ -40,6 +40,13 @@ export type SegmentedControlProps<T extends string> = {
|
|
|
40
40
|
* count. Off, the count is only read with the segment's name, as the native controls draw none. @default false
|
|
41
41
|
*/
|
|
42
42
|
showBadges?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* The drawn counts' color. `label`: the label's on the selected segment, the secondary one on the others (Android:
|
|
45
|
+
* the selected segment's ink, the gray on the others), so a count changes with the selection. `accent`: one accent
|
|
46
|
+
* capsule with white digits on every segment, as TabBar's `badgeColor="accent"`, the same whichever is selected.
|
|
47
|
+
* @default "label"
|
|
48
|
+
*/
|
|
49
|
+
badgeColor?: "label" | "accent";
|
|
43
50
|
style?: StyleProp<ViewStyle>;
|
|
44
51
|
testID?: string;
|
|
45
52
|
/** `SegmentedControl.Tab` elements. */
|
|
@@ -78,7 +85,7 @@ declare namespace SegmentedControlTab {
|
|
|
78
85
|
* iOS 26 apps (48pt; 40 in a Toolbar.Row), its thumb sliding to the selection; on Android a segmented capsule, white
|
|
79
86
|
* with a sliding pill of the accent. Props follow @plexui/ui (`SegmentedControl` and `SegmentedControl.Tab`).
|
|
80
87
|
*/
|
|
81
|
-
declare function SegmentedControlRoot<T extends string>({ value, onChange, onClick, "aria-label": ariaLabel, size, disabled, block, mobileMaterial, mobileSegmentWidth, showBadges, style, testID, children, }: SegmentedControlProps<T>): import("react").JSX.Element;
|
|
88
|
+
declare function SegmentedControlRoot<T extends string>({ value, onChange, onClick, "aria-label": ariaLabel, size, disabled, block, mobileMaterial, mobileSegmentWidth, showBadges, badgeColor, style, testID, children, }: SegmentedControlProps<T>): import("react").JSX.Element;
|
|
82
89
|
declare namespace SegmentedControlRoot {
|
|
83
90
|
var displayName: string;
|
|
84
91
|
}
|
package/package.json
CHANGED
|
@@ -51,6 +51,13 @@ export type SegmentedControlProps<T extends string> = {
|
|
|
51
51
|
* count. Off, the count is only read with the segment's name, as the native controls draw none. @default false
|
|
52
52
|
*/
|
|
53
53
|
showBadges?: boolean
|
|
54
|
+
/**
|
|
55
|
+
* The drawn counts' color. `label`: the label's on the selected segment, the secondary one on the others (Android:
|
|
56
|
+
* the selected segment's ink, the gray on the others), so a count changes with the selection. `accent`: one accent
|
|
57
|
+
* capsule with white digits on every segment, as TabBar's `badgeColor="accent"`, the same whichever is selected.
|
|
58
|
+
* @default "label"
|
|
59
|
+
*/
|
|
60
|
+
badgeColor?: "label" | "accent"
|
|
54
61
|
style?: StyleProp<ViewStyle>
|
|
55
62
|
testID?: string
|
|
56
63
|
/** `SegmentedControl.Tab` elements. */
|
|
@@ -172,6 +179,7 @@ function SegmentedControlRoot<T extends string>({
|
|
|
172
179
|
mobileMaterial,
|
|
173
180
|
mobileSegmentWidth = "equal",
|
|
174
181
|
showBadges = false,
|
|
182
|
+
badgeColor = "label",
|
|
175
183
|
style,
|
|
176
184
|
testID,
|
|
177
185
|
children,
|
|
@@ -192,7 +200,7 @@ function SegmentedControlRoot<T extends string>({
|
|
|
192
200
|
)
|
|
193
201
|
// Drawn counts differ from tab to tab, so each segment takes its own width with them, as a messenger's folder tabs
|
|
194
202
|
const counted = showBadges && options.some((option) => option.badge != null)
|
|
195
|
-
const common = { options, value, select, keys, disabled, block, equal: mobileSegmentWidth === "equal" && !counted, showBadges }
|
|
203
|
+
const common = { options, value, select, keys, disabled, block, equal: mobileSegmentWidth === "equal" && !counted, showBadges, badgeColor }
|
|
196
204
|
return (
|
|
197
205
|
<View accessibilityRole="tablist" aria-label={ariaLabel} accessibilityState={{ disabled }} testID={testID} style={[block ? styles.block : styles.hug, style]}>
|
|
198
206
|
{theme.isAndroid ? <AndroidControl {...common} size={size} /> : <IOSControl {...common} size={size} material={mobileMaterial} />}
|
|
@@ -210,9 +218,10 @@ type ControlProps = {
|
|
|
210
218
|
block: boolean
|
|
211
219
|
equal: boolean
|
|
212
220
|
showBadges: boolean
|
|
221
|
+
badgeColor: "label" | "accent"
|
|
213
222
|
}
|
|
214
223
|
|
|
215
|
-
function IOSControl({ options, value, select, keys, disabled, block, equal, showBadges, size, material }: ControlProps & { size: ControlSize; material?: "glass" | "flat" }) {
|
|
224
|
+
function IOSControl({ options, value, select, keys, disabled, block, equal, showBadges, badgeColor, size, material }: ControlProps & { size: ControlSize; material?: "glass" | "flat" }) {
|
|
216
225
|
const theme = useTheme()
|
|
217
226
|
const inBar = !!useContext(BarItemContext)
|
|
218
227
|
const { inRow } = useContext(SegmentedControlRowContext)
|
|
@@ -326,7 +335,7 @@ function IOSControl({ options, value, select, keys, disabled, block, equal, show
|
|
|
326
335
|
{option.children}
|
|
327
336
|
</Text>
|
|
328
337
|
)}
|
|
329
|
-
{showBadges ? <SegmentCount badge={option.badge} selected={isSelected} off={off} /> : null}
|
|
338
|
+
{showBadges ? <SegmentCount badge={option.badge} selected={isSelected} off={off} color={badgeColor} /> : null}
|
|
330
339
|
</View>
|
|
331
340
|
</Pressable>
|
|
332
341
|
)
|
|
@@ -365,7 +374,7 @@ const ANDROID_MOVE = { duration: 320, easing: curves.easeOutQuint }
|
|
|
365
374
|
* its label in the accent, the others in the secondary gray. The pill slides to a new selection and takes its width.
|
|
366
375
|
* In a bar's row of controls the capsule takes the height of the circles beside it (46dp).
|
|
367
376
|
*/
|
|
368
|
-
function AndroidControl({ options, value, select, keys, disabled, block, equal, showBadges, size }: ControlProps & { size: ControlSize }) {
|
|
377
|
+
function AndroidControl({ options, value, select, keys, disabled, block, equal, showBadges, badgeColor, size }: ControlProps & { size: ControlSize }) {
|
|
369
378
|
const theme = useTheme()
|
|
370
379
|
const m = theme.material
|
|
371
380
|
const bar = useContext(BarItemContext)
|
|
@@ -418,6 +427,7 @@ function AndroidControl({ options, value, select, keys, disabled, block, equal,
|
|
|
418
427
|
off={disabled || !!option.disabled}
|
|
419
428
|
framed={frame}
|
|
420
429
|
showBadges={showBadges}
|
|
430
|
+
badgeColor={badgeColor}
|
|
421
431
|
height={inner}
|
|
422
432
|
// In a filled row equal segments share the width and the others take their label and share what is left; a
|
|
423
433
|
// control that hugs its segments gives equal ones the widest one's width
|
|
@@ -438,6 +448,7 @@ type AndroidSegmentProps = {
|
|
|
438
448
|
/** The pill is placed: the selected segment draws none of its own. */
|
|
439
449
|
framed: boolean
|
|
440
450
|
showBadges: boolean
|
|
451
|
+
badgeColor: "label" | "accent"
|
|
441
452
|
height: number
|
|
442
453
|
size: ViewStyle | null
|
|
443
454
|
keys: ReturnType<typeof useTabKeys>
|
|
@@ -446,7 +457,7 @@ type AndroidSegmentProps = {
|
|
|
446
457
|
}
|
|
447
458
|
|
|
448
459
|
/** One segment of the capsule: its label turns from the gray to the accent as the pill comes, and back as it goes. */
|
|
449
|
-
function AndroidSegment({ option, selected, off, framed, showBadges, height, size, keys, select, onWidth }: AndroidSegmentProps) {
|
|
460
|
+
function AndroidSegment({ option, selected, off, framed, showBadges, badgeColor, height, size, keys, select, onWidth }: AndroidSegmentProps) {
|
|
450
461
|
const theme = useTheme()
|
|
451
462
|
const m = theme.material
|
|
452
463
|
const [tint] = useState(() => new Animated.Value(selected ? 1 : 0))
|
|
@@ -494,7 +505,7 @@ function AndroidSegment({ option, selected, off, framed, showBadges, height, siz
|
|
|
494
505
|
{option.children}
|
|
495
506
|
</Animated.Text>
|
|
496
507
|
) : null}
|
|
497
|
-
{showBadges ? <SegmentCount badge={option.badge} selected={selected} off={off} /> : null}
|
|
508
|
+
{showBadges ? <SegmentCount badge={option.badge} selected={selected} off={off} color={badgeColor} /> : null}
|
|
498
509
|
</Pressable>
|
|
499
510
|
)
|
|
500
511
|
}
|
|
@@ -502,25 +513,29 @@ function AndroidSegment({ option, selected, off, framed, showBadges, height, siz
|
|
|
502
513
|
/**
|
|
503
514
|
* A tab's count drawn after its label (`showBadges`): a capsule 18 tall, the digits 12 semibold in the screen's color.
|
|
504
515
|
* iOS: the label's color on the selected segment, so it reads on the thumb, the secondary label on the others.
|
|
505
|
-
* Android: the accent on the selected segment, as its label, the gray on the others.
|
|
516
|
+
* Android: the accent on the selected segment, as its label, the gray on the others. `accent`: the accent's capsule and
|
|
517
|
+
* white digits on every segment, on both platforms.
|
|
506
518
|
*/
|
|
507
|
-
function SegmentCount({ badge, selected, off }: { badge: SegmentedControlBadgeProp | undefined; selected: boolean; off: boolean }) {
|
|
519
|
+
function SegmentCount({ badge, selected, off, color }: { badge: SegmentedControlBadgeProp | undefined; selected: boolean; off: boolean; color: "label" | "accent" }) {
|
|
508
520
|
const theme = useTheme()
|
|
509
521
|
const text = badge != null ? badgeText(badge) : undefined
|
|
510
522
|
if (!text) return null
|
|
511
523
|
// iOS: the label's color in a capsule, the bar's ink in it; Android: the segment's own ink, the selected one's
|
|
512
524
|
// container in it (onSecondaryContainer on secondaryContainer), the others' onSurfaceVariant on the surface, so the
|
|
513
525
|
// small digits keep their contrast
|
|
526
|
+
const accent = color === "accent"
|
|
514
527
|
const fill = off
|
|
515
528
|
? theme.color("--alpha-20")
|
|
516
|
-
:
|
|
529
|
+
: accent
|
|
530
|
+
? theme.color("--color-background-accent-solid")
|
|
531
|
+
: theme.isAndroid
|
|
517
532
|
? selected
|
|
518
533
|
? theme.material.onSecondaryContainer
|
|
519
534
|
: theme.material.onSurfaceVariant
|
|
520
535
|
: selected
|
|
521
536
|
? theme.colors.text
|
|
522
537
|
: theme.colors.labelSecondary
|
|
523
|
-
const ink = theme.isAndroid ? (selected ? theme.material.secondaryContainer : theme.material.surface) : theme.colors.background
|
|
538
|
+
const ink = accent && !off ? theme.color("--color-text-accent-solid", "#FFFFFF") : theme.isAndroid ? (selected ? theme.material.secondaryContainer : theme.material.surface) : theme.colors.background
|
|
524
539
|
return (
|
|
525
540
|
<View aria-hidden style={[styles.count, { backgroundColor: fill }]}>
|
|
526
541
|
<Text numberOfLines={1} weight="semibold" color={ink} style={styles.countText}>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactElement, type ReactNode } from "react"
|
|
1
|
+
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, useState, type ReactElement, type ReactNode } from "react"
|
|
2
2
|
import { Animated, Easing, StyleSheet, View, type LayoutChangeEvent, type NativeScrollEvent, type NativeSyntheticEvent, type StyleProp, type ViewProps, type ViewStyle } from "react-native"
|
|
3
3
|
import { Pressable } from "../../internal/Pressable"
|
|
4
4
|
import { useSafeAreaInsets } from "react-native-safe-area-context"
|
|
@@ -115,8 +115,15 @@ type TabBarContextValue = {
|
|
|
115
115
|
select: (value: string) => void
|
|
116
116
|
expand: () => void
|
|
117
117
|
keys: ReturnType<typeof useTabKeys>
|
|
118
|
+
/** iOS: the capsule draws one selection pill that slides between the tabs; each tab reports its frame for it. */
|
|
119
|
+
onTabLayout?: (value: string, frame: TabFrame) => void
|
|
120
|
+
/** The tab whose pill the capsule draws (its frame is known); until then the selected tab draws its own. */
|
|
121
|
+
slidingPill?: string
|
|
118
122
|
}
|
|
119
123
|
|
|
124
|
+
/** A tab's size and its top in the capsule (its place along the row comes from the tabs' order and widths). */
|
|
125
|
+
type TabFrame = { y: number; width: number; height: number }
|
|
126
|
+
|
|
120
127
|
const TabBarContext = createContext<TabBarContextValue | null>(null)
|
|
121
128
|
|
|
122
129
|
/** A badge shows unless it is empty: nothing, false, an empty text or a count of 0 (iOS hides `.badge(0)`). */
|
|
@@ -190,9 +197,18 @@ function IOSTab({ value, label, icon, badge, badgeColor, disabled = false, onPre
|
|
|
190
197
|
// A tab bar's labels keep their size, as UIKit's: a large text size shows the tab in the large content viewer
|
|
191
198
|
accessibilityShowsLargeContentViewer
|
|
192
199
|
accessibilityLargeContentTitle={textOf(label) || undefined}
|
|
200
|
+
onLayout={
|
|
201
|
+
// An expanded tab only: a minimized one's 40pt face is not where the pill goes when the bar expands
|
|
202
|
+
!circle && !collapsed && context.onTabLayout
|
|
203
|
+
? (event: LayoutChangeEvent) => {
|
|
204
|
+
const { y, width, height } = event.nativeEvent.layout
|
|
205
|
+
context.onTabLayout?.(value, { y, width, height })
|
|
206
|
+
}
|
|
207
|
+
: undefined
|
|
208
|
+
}
|
|
193
209
|
style={[styles.iosItem, size, circle ? null : { flexShrink: 1, minWidth: 0 }]}
|
|
194
210
|
>
|
|
195
|
-
{selected && !collapsed ? <View pointerEvents="none" style={[styles.pill, pill, { backgroundColor: theme.color("--tab-bar-item-background-selected") }]} /> : null}
|
|
211
|
+
{selected && !collapsed && (circle || context.slidingPill !== value) ? <View pointerEvents="none" style={[styles.pill, pill, { backgroundColor: theme.color("--tab-bar-item-background-selected") }]} /> : null}
|
|
196
212
|
<View style={[styles.iosIcon, { height: theme.length("--tab-bar-icon-box", 28) }]}>
|
|
197
213
|
<IconDefaultsProvider size={theme.length("--tab-bar-icon-size", 28)} color={color}>
|
|
198
214
|
{icon}
|
|
@@ -686,7 +702,62 @@ function TabBarRoot({
|
|
|
686
702
|
const visible = shown.length ? shown : capsuleTabs.slice(0, 1)
|
|
687
703
|
const reachable = [...visible, ...(circle ? [circle] : [])]
|
|
688
704
|
const keys = useTabKeys(reachable, value, select)
|
|
689
|
-
|
|
705
|
+
// iOS 26 slides the selection to the new tab: one pill in the capsule, under the tabs, moving from tab to tab
|
|
706
|
+
const [frames, setFrames] = useState<Record<string, TabFrame>>({})
|
|
707
|
+
const onTabLayout = useCallback((tab: string, next: TabFrame) => {
|
|
708
|
+
setFrames((current) => {
|
|
709
|
+
const prev = current[tab]
|
|
710
|
+
return prev && prev.y === next.y && prev.width === next.width && prev.height === next.height ? current : { ...current, [tab]: next }
|
|
711
|
+
})
|
|
712
|
+
}, [])
|
|
713
|
+
// The pill's place from the start of the capsule: its padding and the widths of the tabs before it, in their order
|
|
714
|
+
// (a reorder moves it with no resize to report; the start edge, so a right-to-left bar mirrors it as its tabs)
|
|
715
|
+
const capsuleInline = theme.length("--tab-bar-capsule-padding-inline", 10)
|
|
716
|
+
const selectedIndex = value != null && !minimized ? visible.findIndex((tab) => tab.value === value) : -1
|
|
717
|
+
const before = selectedIndex >= 0 ? visible.slice(0, selectedIndex) : []
|
|
718
|
+
const frame = selectedIndex >= 0 && theme.isIOS ? frames[visible[selectedIndex].value] : undefined
|
|
719
|
+
const pillFrame =
|
|
720
|
+
frame && before.every((tab) => frames[tab.value] != null)
|
|
721
|
+
? { ...frame, x: capsuleInline + before.reduce((sum, tab) => sum + frames[tab.value].width, 0) }
|
|
722
|
+
: undefined
|
|
723
|
+
const context: TabBarContextValue = {
|
|
724
|
+
value,
|
|
725
|
+
badgeColor,
|
|
726
|
+
minimized,
|
|
727
|
+
select,
|
|
728
|
+
expand,
|
|
729
|
+
keys,
|
|
730
|
+
onTabLayout: theme.isIOS ? onTabLayout : undefined,
|
|
731
|
+
slidingPill: pillFrame ? value : undefined,
|
|
732
|
+
}
|
|
733
|
+
const pillOutset = theme.length("--tab-bar-pill-outset", 6)
|
|
734
|
+
const [pillX] = useState(() => new Animated.Value(0))
|
|
735
|
+
const [pillWidth] = useState(() => new Animated.Value(0))
|
|
736
|
+
const pillShown = useRef(false)
|
|
737
|
+
const pillLeft = pillFrame ? pillFrame.x - pillOutset : undefined
|
|
738
|
+
const pillSpan = pillFrame ? pillFrame.width + 2 * pillOutset : undefined
|
|
739
|
+
// Before the paint, so the pill never shows a frame at its old place or at 0
|
|
740
|
+
useLayoutEffect(() => {
|
|
741
|
+
if (pillLeft == null || pillSpan == null) {
|
|
742
|
+
pillShown.current = false
|
|
743
|
+
return
|
|
744
|
+
}
|
|
745
|
+
// It appears in place (the first frame, back from a minimized bar or the circle's tab), then slides: CSS `ease`
|
|
746
|
+
// over 300ms, as SegmentedControl's thumb
|
|
747
|
+
if (!pillShown.current || theme.reducedMotion) {
|
|
748
|
+
pillX.setValue(pillLeft)
|
|
749
|
+
pillWidth.setValue(pillSpan)
|
|
750
|
+
pillShown.current = true
|
|
751
|
+
return
|
|
752
|
+
}
|
|
753
|
+
const easing = Easing.bezier(0.25, 0.1, 0.25, 1)
|
|
754
|
+
const animation = Animated.parallel([
|
|
755
|
+
Animated.timing(pillX, { toValue: pillLeft, duration: 300, easing, useNativeDriver: false }),
|
|
756
|
+
Animated.timing(pillWidth, { toValue: pillSpan, duration: 300, easing, useNativeDriver: false }),
|
|
757
|
+
])
|
|
758
|
+
animation.start()
|
|
759
|
+
return () => animation.stop()
|
|
760
|
+
}, [pillLeft, pillSpan, theme.reducedMotion, pillX, pillWidth])
|
|
690
761
|
|
|
691
762
|
if (theme.isAndroid) {
|
|
692
763
|
return (
|
|
@@ -761,6 +832,15 @@ function TabBarRoot({
|
|
|
761
832
|
elevation="raised"
|
|
762
833
|
style={[styles.capsule, minimized ? { padding: capsulePadding } : { paddingVertical: capsulePadding, paddingHorizontal: theme.length("--tab-bar-capsule-padding-inline", 10) }]}
|
|
763
834
|
>
|
|
835
|
+
{pillFrame ? (
|
|
836
|
+
<Animated.View
|
|
837
|
+
pointerEvents="none"
|
|
838
|
+
style={[
|
|
839
|
+
styles.pill,
|
|
840
|
+
{ top: pillFrame.y, height: pillFrame.height, start: pillX, width: pillWidth, backgroundColor: theme.color("--tab-bar-item-background-selected") },
|
|
841
|
+
]}
|
|
842
|
+
/>
|
|
843
|
+
) : null}
|
|
764
844
|
{visible.map((tab) => (
|
|
765
845
|
<Tab key={tab.value} {...tab} context={context} />
|
|
766
846
|
))}
|