@hzblj/kara-components 0.18.0 → 0.20.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/CHANGELOG.md +26 -0
- package/lib/module/atoms/segmented-control/segmented-control.android.js +74 -15
- package/lib/module/atoms/segmented-control/segmented-control.android.js.map +1 -1
- package/lib/module/atoms/segmented-control/segmented-control.ios.js +28 -10
- package/lib/module/atoms/segmented-control/segmented-control.ios.js.map +1 -1
- package/lib/module/atoms/segmented-control/segments.js +16 -0
- package/lib/module/atoms/segmented-control/segments.js.map +1 -0
- package/lib/typescript/atoms/segmented-control/segmented-control.ios.d.ts +4 -1
- package/lib/typescript/atoms/segmented-control/segmented-control.types.d.ts +44 -3
- package/lib/typescript/atoms/segmented-control/segments.d.ts +6 -0
- package/package.json +4 -4
- package/src/atoms/segmented-control/segmented-control.android.tsx +75 -12
- package/src/atoms/segmented-control/segmented-control.ios.tsx +32 -6
- package/src/atoms/segmented-control/segmented-control.types.ts +46 -3
- package/src/atoms/segmented-control/segments.ts +16 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# @hzblj/kara-components
|
|
2
2
|
|
|
3
|
+
## 0.20.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#60](https://github.com/hzblj/kara-ui/pull/60) [`a93a723`](https://github.com/hzblj/kara-ui/commit/a93a72369a166f22d7aed19b99d5c889b61739aa) Thanks [@hzblj](https://github.com/hzblj)! - `SegmentedControl` takes its track and its labels through the `sx` names: `bg` colours the track, `p` is the room between the track's edge and the selected pill, and `variant` sets the labels in a typography variant, as `Text` does. On iOS a `bg` track is kara-native's own view, a capsule in exactly that colour under a clear `UISegmentedControl`, because UIKit renders a control's own background translucent; the pill stays the system's (Liquid Glass on iOS 26) and the track grows by `p`. Android fills the unselected segments with `bg`; Material draws no pill to inset. kara-native's `SegmentedControl` binding gains `trackPadding`, `fontWeight` (the system face's weight, so a regular variant is no longer drawn medium) and `letterSpacing`. On iOS 26 the titles under a held or dragged pill keep their colours instead of turning the system black.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`a93a723`](https://github.com/hzblj/kara-ui/commit/a93a72369a166f22d7aed19b99d5c889b61739aa)]:
|
|
12
|
+
- @hzblj/kara-native@0.20.0
|
|
13
|
+
- @hzblj/kara-sx@0.20.0
|
|
14
|
+
- @hzblj/kara-theme@0.20.0
|
|
15
|
+
|
|
16
|
+
## 0.19.0
|
|
17
|
+
|
|
18
|
+
### Minor Changes
|
|
19
|
+
|
|
20
|
+
- [#58](https://github.com/hzblj/kara-ui/pull/58) [`024f36b`](https://github.com/hzblj/kara-ui/commit/024f36b58b3ceca20f04889b876eb7905ed7b540) Thanks [@hzblj](https://github.com/hzblj)! - `SegmentedControl` segments can show more than a label: `{label, icon}` leads with one of the app's icons in the label's colour, and `{label, image}` shows an image in its own colours instead, such as a carrier's logo from a URL, raster or SVG. `iconSize` sets their height (`s20` by default) and `spacing` the space above and below the content, for a taller control. The images load natively, as Footshop's platform-ui loads them: kara-native now depends on SDWebImage and SDWebImageSVGCoder on iOS and on androidsvg on Android, and its new `NativeImage` draws such an image in a Compose host. The label stays what screen readers say, and is shown if the image cannot be loaded.
|
|
21
|
+
|
|
22
|
+
### Patch Changes
|
|
23
|
+
|
|
24
|
+
- Updated dependencies [[`024f36b`](https://github.com/hzblj/kara-ui/commit/024f36b58b3ceca20f04889b876eb7905ed7b540)]:
|
|
25
|
+
- @hzblj/kara-native@0.19.0
|
|
26
|
+
- @hzblj/kara-sx@0.19.0
|
|
27
|
+
- @hzblj/kara-theme@0.19.0
|
|
28
|
+
|
|
3
29
|
## 0.18.0
|
|
4
30
|
|
|
5
31
|
### Minor Changes
|
|
@@ -1,35 +1,92 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { Host, SegmentedButton, SingleChoiceSegmentedButtonRow, Text } from '@expo/ui/jetpack-compose';
|
|
4
|
-
import { fillMaxWidth, testID as testIdModifier, weight } from '@expo/ui/jetpack-compose/modifiers';
|
|
4
|
+
import { fillMaxWidth, height, testID as testIdModifier, weight } from '@expo/ui/jetpack-compose/modifiers';
|
|
5
|
+
import { iconResourceName } from '@hzblj/expo-native-icons';
|
|
5
6
|
import { haptics } from '@hzblj/expo-rich-haptics';
|
|
6
|
-
import {
|
|
7
|
+
import { DrawableIcon, NativeImage } from '@hzblj/kara-native/jetpack-compose';
|
|
8
|
+
import { spacing as spacingTokens, useAppTheme } from '@hzblj/kara-theme';
|
|
7
9
|
import { useCallback, useMemo } from 'react';
|
|
8
10
|
import { useInsideHost } from "../../host-context.js";
|
|
9
|
-
import {
|
|
11
|
+
import { HStack } from "../h-stack/index.js";
|
|
12
|
+
import { imageUri, segmentOf } from "./segments.js";
|
|
13
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
10
14
|
const MATCH_HEIGHT_ONLY = {
|
|
11
15
|
horizontal: false,
|
|
12
16
|
vertical: true
|
|
13
17
|
};
|
|
14
18
|
|
|
15
|
-
/**
|
|
16
|
-
const
|
|
19
|
+
/** Material's own segment height, which `spacing` only ever adds to. */
|
|
20
|
+
const MATERIAL_HEIGHT = 40;
|
|
21
|
+
/**
|
|
22
|
+
* What a segment shows: its label, one of the app's icons before it, or an image loaded by kara-native. The icon and a
|
|
23
|
+
* label the image falls back to take the segment's content colour, so they follow the selection and `disabled`.
|
|
24
|
+
*/
|
|
25
|
+
const SegmentLabel = ({
|
|
26
|
+
iconSize,
|
|
27
|
+
segment,
|
|
28
|
+
variant
|
|
29
|
+
}) => {
|
|
30
|
+
const theme = useAppTheme();
|
|
31
|
+
const face = theme.typography[variant];
|
|
32
|
+
if (segment.image !== undefined) {
|
|
33
|
+
return /*#__PURE__*/_jsx(NativeImage, {
|
|
34
|
+
fontFamily: face.fontFamily,
|
|
35
|
+
fontSize: face.fontSize,
|
|
36
|
+
fontWeight: String(face.fontWeight),
|
|
37
|
+
imageHeight: iconSize,
|
|
38
|
+
label: segment.label,
|
|
39
|
+
source: imageUri(segment.image)
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const label = /*#__PURE__*/_jsx(Text, {
|
|
43
|
+
style: face,
|
|
44
|
+
children: segment.label
|
|
45
|
+
});
|
|
46
|
+
if (segment.icon === undefined) {
|
|
47
|
+
return label;
|
|
48
|
+
}
|
|
49
|
+
return /*#__PURE__*/_jsxs(HStack, {
|
|
50
|
+
align: "center",
|
|
51
|
+
spacing: "s6",
|
|
52
|
+
children: [/*#__PURE__*/_jsx(DrawableIcon, {
|
|
53
|
+
iconSize: iconSize,
|
|
54
|
+
resourceName: iconResourceName(segment.icon)
|
|
55
|
+
}), label]
|
|
56
|
+
});
|
|
57
|
+
};
|
|
17
58
|
|
|
18
59
|
/**
|
|
19
60
|
* Material's single-choice segmented buttons, the selected one filled with the accent like a checked `Switch`. Its own
|
|
20
61
|
* host, themed like an `Island`, so it drops into React Native layout the way the iOS control does. Inside a `Screen`
|
|
21
62
|
* or `Island` it renders into that host instead (see `useInsideHost`). Material plays nothing on a new segment, so this
|
|
22
|
-
* plays `selection`.
|
|
63
|
+
* plays `selection`. `bg` fills the unselected segments; `p` has nothing to inset, as Material draws no pill.
|
|
23
64
|
*/
|
|
24
65
|
export const SegmentedControl = ({
|
|
66
|
+
bg,
|
|
25
67
|
disabled = false,
|
|
68
|
+
iconSize = 's20',
|
|
26
69
|
onChange,
|
|
27
70
|
selectedIndex,
|
|
71
|
+
spacing,
|
|
28
72
|
testID,
|
|
29
|
-
values
|
|
73
|
+
values,
|
|
74
|
+
variant = 'subheadlineMedium'
|
|
30
75
|
}) => {
|
|
31
76
|
const theme = useAppTheme();
|
|
32
77
|
const insideHost = useInsideHost();
|
|
78
|
+
const segments = useMemo(() => values.map(segmentOf), [values]);
|
|
79
|
+
const iconPoints = spacingTokens[iconSize];
|
|
80
|
+
|
|
81
|
+
// Every segment the same width, like UIKit's: Material's would size each to its label. With `spacing`, as tall as
|
|
82
|
+
// the content and that space, and never shorter than Material's own segment.
|
|
83
|
+
const segmentModifiers = useMemo(() => {
|
|
84
|
+
if (spacing === undefined) {
|
|
85
|
+
return [weight(1)];
|
|
86
|
+
}
|
|
87
|
+
const content = Math.max(theme.typography[variant].lineHeight, iconPoints);
|
|
88
|
+
return [weight(1), height(Math.max(MATERIAL_HEIGHT, content + 2 * spacingTokens[spacing]))];
|
|
89
|
+
}, [iconPoints, spacing, theme, variant]);
|
|
33
90
|
|
|
34
91
|
// Every segment keeps the same outline, the selected one too: an accent border would run its fill out over the line
|
|
35
92
|
// the others draw inside, and make it look taller than its neighbours.
|
|
@@ -43,8 +100,9 @@ export const SegmentedControl = ({
|
|
|
43
100
|
disabledInactiveBorderColor: theme.color('border.tertiary'),
|
|
44
101
|
disabledInactiveContentColor: theme.color('btn.content.disabled'),
|
|
45
102
|
inactiveBorderColor: theme.color('border.secondary.base'),
|
|
103
|
+
inactiveContainerColor: bg === undefined ? undefined : theme.color(bg),
|
|
46
104
|
inactiveContentColor: theme.color('content.primary')
|
|
47
|
-
}), [theme]);
|
|
105
|
+
}), [bg, theme]);
|
|
48
106
|
const rowModifiers = useMemo(() => testID ? [fillMaxWidth(), testIdModifier(testID)] : [fillMaxWidth()], [testID]);
|
|
49
107
|
|
|
50
108
|
// Material reports a tap on the segment that is already selected too; iOS does not, and neither does this.
|
|
@@ -53,20 +111,21 @@ export const SegmentedControl = ({
|
|
|
53
111
|
return;
|
|
54
112
|
}
|
|
55
113
|
haptics.selection();
|
|
56
|
-
onChange?.(index,
|
|
57
|
-
}, [onChange,
|
|
114
|
+
onChange?.(index, segments[index]?.label ?? '');
|
|
115
|
+
}, [onChange, segments, selectedIndex]);
|
|
58
116
|
const row = /*#__PURE__*/_jsx(SingleChoiceSegmentedButtonRow, {
|
|
59
117
|
modifiers: rowModifiers,
|
|
60
|
-
children:
|
|
118
|
+
children: segments.map((segment, index) => /*#__PURE__*/_jsx(SegmentedButton, {
|
|
61
119
|
colors: colors,
|
|
62
120
|
enabled: !disabled,
|
|
63
|
-
modifiers:
|
|
121
|
+
modifiers: segmentModifiers,
|
|
64
122
|
onClick: () => select(index),
|
|
65
123
|
selected: index === selectedIndex,
|
|
66
124
|
children: /*#__PURE__*/_jsx(SegmentedButton.Label, {
|
|
67
|
-
children: /*#__PURE__*/_jsx(
|
|
68
|
-
|
|
69
|
-
|
|
125
|
+
children: /*#__PURE__*/_jsx(SegmentLabel, {
|
|
126
|
+
iconSize: iconPoints,
|
|
127
|
+
segment: segment,
|
|
128
|
+
variant: variant
|
|
70
129
|
})
|
|
71
130
|
})
|
|
72
131
|
}, index))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Host","SegmentedButton","SingleChoiceSegmentedButtonRow","Text","fillMaxWidth","testID","testIdModifier","weight","haptics","useAppTheme","useCallback","useMemo","useInsideHost","jsx","_jsx","MATCH_HEIGHT_ONLY","horizontal","vertical","
|
|
1
|
+
{"version":3,"names":["Host","SegmentedButton","SingleChoiceSegmentedButtonRow","Text","fillMaxWidth","height","testID","testIdModifier","weight","iconResourceName","haptics","DrawableIcon","NativeImage","spacing","spacingTokens","useAppTheme","useCallback","useMemo","useInsideHost","HStack","imageUri","segmentOf","jsx","_jsx","jsxs","_jsxs","MATCH_HEIGHT_ONLY","horizontal","vertical","MATERIAL_HEIGHT","SegmentLabel","iconSize","segment","variant","theme","face","typography","image","undefined","fontFamily","fontSize","fontWeight","String","imageHeight","label","source","style","children","icon","align","resourceName","SegmentedControl","bg","disabled","onChange","selectedIndex","values","insideHost","segments","map","iconPoints","segmentModifiers","content","Math","max","lineHeight","colors","activeBorderColor","color","activeContainerColor","activeContentColor","disabledActiveBorderColor","disabledActiveContainerColor","disabledActiveContentColor","disabledInactiveBorderColor","disabledInactiveContentColor","inactiveBorderColor","inactiveContainerColor","inactiveContentColor","rowModifiers","select","index","selection","row","modifiers","enabled","onClick","selected","Label","colorScheme","dark","matchContents","seedColor","materialSeed"],"sourceRoot":"../../../../src","sources":["atoms/segmented-control/segmented-control.android.tsx"],"mappings":";;AAAA,SAAQA,IAAI,EAAEC,eAAe,EAAEC,8BAA8B,EAAEC,IAAI,QAAO,0BAA0B;AACpG,SAAQC,YAAY,EAAEC,MAAM,EAAEC,MAAM,IAAIC,cAAc,EAAEC,MAAM,QAAO,oCAAoC;AACzG,SAAQC,gBAAgB,QAAO,0BAA0B;AACzD,SAAQC,OAAO,QAAO,0BAA0B;AAChD,SAAQC,YAAY,EAAEC,WAAW,QAAO,oCAAoC;AAC5E,SAAQC,OAAO,IAAIC,aAAa,EAAoBC,WAAW,QAAO,mBAAmB;AACzF,SAAQC,WAAW,EAAEC,OAAO,QAAO,OAAO;AAE1C,SAAQC,aAAa,QAAO,uBAAoB;AAChD,SAAQC,MAAM,QAAO,qBAAY;AAEjC,SAAQC,QAAQ,EAAEC,SAAS,QAAO,eAAY;AAAA,SAAAC,GAAA,IAAAC,IAAA,EAAAC,IAAA,IAAAC,KAAA;AAE9C,MAAMC,iBAAiB,GAAG;EAACC,UAAU,EAAE,KAAK;EAAEC,QAAQ,EAAE;AAAI,CAAC;;AAE7D;AACA,MAAMC,eAAe,GAAG,EAAE;AAQ1B;AACA;AACA;AACA;AACA,MAAMC,YAAY,GAAGA,CAAC;EAACC,QAAQ;EAAEC,OAAO;EAAEC;AAA0B,CAAC,KAAK;EACxE,MAAMC,KAAK,GAAGnB,WAAW,CAAC,CAAC;EAC3B,MAAMoB,IAAI,GAAGD,KAAK,CAACE,UAAU,CAACH,OAAO,CAAC;EAEtC,IAAID,OAAO,CAACK,KAAK,KAAKC,SAAS,EAAE;IAC/B,oBACEf,IAAA,CAACX,WAAW;MACV2B,UAAU,EAAEJ,IAAI,CAACI,UAAW;MAC5BC,QAAQ,EAAEL,IAAI,CAACK,QAAS;MACxBC,UAAU,EAAEC,MAAM,CAACP,IAAI,CAACM,UAAU,CAAE;MACpCE,WAAW,EAAEZ,QA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E5D,IAAA,CAACvB,IAAI;IAACyF,WAAW,EAAEvD,KAAK,CAACwD,IAAI,GAAG,MAAM,GAAG,OAAQ;IAACC,aAAa,EAAEjE,iBAAkB;IAACkE,SAAS,EAAE1D,KAAK,CAAC2D,YAAa;IAAA9C,QAAA,EAC/GoC;EAAG,CACA,CAAC;AAEX,CAAC","ignoreList":[]}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
import { RNHostView } from '@expo/ui/swift-ui';
|
|
4
|
+
import { iconResourceName } from '@hzblj/expo-native-icons';
|
|
4
5
|
import { SegmentedControl as NativeSegmentedControl } from '@hzblj/kara-native/swift-ui';
|
|
5
|
-
import { useAppTheme } from '@hzblj/kara-theme';
|
|
6
|
+
import { spacing as spacingTokens, useAppTheme } from '@hzblj/kara-theme';
|
|
6
7
|
import { useCallback, useMemo } from 'react';
|
|
7
8
|
import { useInsideHost } from "../../host-context.js";
|
|
9
|
+
import { imageUri, segmentOf } from "./segments.js";
|
|
8
10
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
9
11
|
const MATCH_HEIGHT_ONLY = {
|
|
10
12
|
horizontal: false,
|
|
@@ -15,39 +17,55 @@ const MATCH_HEIGHT_ONLY = {
|
|
|
15
17
|
* kara's own `UISegmentedControl`, with the pill in the accent and white text on it — the system control tints only the
|
|
16
18
|
* pill, and SwiftUI's segmented picker not even that. A plain `UIView` in React Native layout: it needs no host, takes
|
|
17
19
|
* the width it is given and UIKit's height. Inside a `Screen` or `Island` it is carried into that host by `RNHostView`
|
|
18
|
-
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback.
|
|
20
|
+
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback. A segment shows a
|
|
21
|
+
* title or an image, never both, so an icon segment is drawn natively as one image of the icon and the label, in the
|
|
22
|
+
* label's colour; an image segment is the image alone, loaded and drawn by kara-native at `iconSize`. With `bg` the
|
|
23
|
+
* track is kara-native's own view under a clear control, inset by `p`, so the pill stays the system's.
|
|
19
24
|
*/
|
|
20
25
|
export const SegmentedControl = ({
|
|
26
|
+
bg,
|
|
21
27
|
disabled = false,
|
|
28
|
+
iconSize = 's20',
|
|
22
29
|
onChange,
|
|
30
|
+
p,
|
|
23
31
|
selectedIndex,
|
|
32
|
+
spacing,
|
|
24
33
|
testID,
|
|
25
|
-
values
|
|
34
|
+
values,
|
|
35
|
+
variant = 'footnoteMedium'
|
|
26
36
|
}) => {
|
|
27
37
|
const theme = useAppTheme();
|
|
28
38
|
const insideHost = useInsideHost();
|
|
29
|
-
const face = theme.typography
|
|
39
|
+
const face = theme.typography[variant];
|
|
40
|
+
const segments = useMemo(() => values.map(segmentOf), [values]);
|
|
30
41
|
|
|
31
42
|
// The native control speaks in values; a segment's is its index, so two labels may read the same.
|
|
32
|
-
const options = useMemo(() =>
|
|
33
|
-
|
|
43
|
+
const options = useMemo(() => segments.map((segment, index) => ({
|
|
44
|
+
icon: segment.icon ? iconResourceName(segment.icon) : undefined,
|
|
45
|
+
image: imageUri(segment.image),
|
|
46
|
+
label: segment.label,
|
|
34
47
|
value: String(index)
|
|
35
|
-
})), [
|
|
48
|
+
})), [segments]);
|
|
36
49
|
const onValueChange = useCallback(value => {
|
|
37
50
|
const index = Number(value);
|
|
38
|
-
onChange?.(index,
|
|
39
|
-
}, [onChange,
|
|
51
|
+
onChange?.(index, segments[index]?.label ?? '');
|
|
52
|
+
}, [onChange, segments]);
|
|
40
53
|
const control = /*#__PURE__*/_jsx(NativeSegmentedControl, {
|
|
41
54
|
activePillColor: theme.color('fill.accent.primary'),
|
|
42
55
|
activeTextColor: theme.color('content.on.accent'),
|
|
43
56
|
disabled: disabled,
|
|
44
57
|
fontFamily: face.fontFamily,
|
|
45
58
|
fontSize: face.fontSize,
|
|
59
|
+
fontWeight: face.fontWeight,
|
|
60
|
+
iconSize: spacingTokens[iconSize],
|
|
46
61
|
inactiveTextColor: theme.color('content.primary'),
|
|
47
62
|
onValueChange: onValueChange,
|
|
48
63
|
options: options,
|
|
49
64
|
selectedValue: String(selectedIndex),
|
|
50
|
-
testID: testID
|
|
65
|
+
testID: testID,
|
|
66
|
+
trackColor: bg === undefined ? undefined : theme.color(bg),
|
|
67
|
+
trackPadding: p === undefined ? undefined : spacingTokens[p],
|
|
68
|
+
verticalPadding: spacing === undefined ? undefined : spacingTokens[spacing]
|
|
51
69
|
});
|
|
52
70
|
return insideHost ? /*#__PURE__*/_jsx(RNHostView, {
|
|
53
71
|
matchContents: MATCH_HEIGHT_ONLY,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["RNHostView","SegmentedControl","NativeSegmentedControl","useAppTheme","useCallback","useMemo","useInsideHost","jsx","_jsx","MATCH_HEIGHT_ONLY","horizontal","vertical","disabled","onChange","selectedIndex","testID","values","theme","insideHost","face","typography","
|
|
1
|
+
{"version":3,"names":["RNHostView","iconResourceName","SegmentedControl","NativeSegmentedControl","spacing","spacingTokens","useAppTheme","useCallback","useMemo","useInsideHost","imageUri","segmentOf","jsx","_jsx","MATCH_HEIGHT_ONLY","horizontal","vertical","bg","disabled","iconSize","onChange","p","selectedIndex","testID","values","variant","theme","insideHost","face","typography","segments","map","options","segment","index","icon","undefined","image","label","value","String","onValueChange","Number","control","activePillColor","color","activeTextColor","fontFamily","fontSize","fontWeight","inactiveTextColor","selectedValue","trackColor","trackPadding","verticalPadding","matchContents","children"],"sourceRoot":"../../../../src","sources":["atoms/segmented-control/segmented-control.ios.tsx"],"mappings":";;AAAA,SAAQA,UAAU,QAAO,mBAAmB;AAC5C,SAAQC,gBAAgB,QAAO,0BAA0B;AACzD,SAAQC,gBAAgB,IAAIC,sBAAsB,QAAO,6BAA6B;AACtF,SAAQC,OAAO,IAAIC,aAAa,EAAEC,WAAW,QAAO,mBAAmB;AACvE,SAAQC,WAAW,EAAEC,OAAO,QAAO,OAAO;AAE1C,SAAQC,aAAa,QAAO,uBAAoB;AAEhD,SAAQC,QAAQ,EAAEC,SAAS,Q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AAK,CAAC,EAAEI,KAAK,IAAI,EAAE,CAAC;EACjD,CAAC,EACD,CAAClB,QAAQ,EAAEU,QAAQ,CACrB,CAAC;EAED,MAAMa,OAAO,gBACX9B,IAAA,CAACV,sBAAsB;IACrByC,eAAe,EAAElB,KAAK,CAACmB,KAAK,CAAC,qBAAqB,CAAE;IACpDC,eAAe,EAAEpB,KAAK,CAACmB,KAAK,CAAC,mBAAmB,CAAE;IAClD3B,QAAQ,EAAEA,QAAS;IACnB6B,UAAU,EAAEnB,IAAI,CAACmB,UAAW;IAC5BC,QAAQ,EAAEpB,IAAI,CAACoB,QAAS;IACxBC,UAAU,EAAErB,IAAI,CAACqB,UAAW;IAC5B9B,QAAQ,EAAEd,aAAa,CAACc,QAAQ,CAAE;IAClC+B,iBAAiB,EAAExB,KAAK,CAACmB,KAAK,CAAC,iBAAiB,CAAE;IAClDJ,aAAa,EAAEA,aAAc;IAC7BT,OAAO,EAAEA,OAAQ;IACjBmB,aAAa,EAAEX,MAAM,CAAClB,aAAa,CAAE;IACrCC,MAAM,EAAEA,MAAO;IACf6B,UAAU,EAAEnC,EAAE,KAAKmB,SAAS,GAAGA,SAAS,GAAGV,KAAK,CAACmB,KAAK,CAAC5B,EAAE,CAAE;IAC3DoC,YAAY,EAAEhC,CAAC,KAAKe,SAAS,GAAGA,SAAS,GAAG/B,aAAa,CAACgB,CAAC,CAAE;IAC7DiC,eAAe,EAAElD,OAAO,KAAKgC,SAAS,GAAGA,SAAS,GAAG/B,aAAa,CAACD,OAAO;EAAE,CAC7E,CACF;EAED,OAAOuB,UAAU,gBAAGd,IAAA,CAACb,UAAU;IAACuD,aAAa,EAAEzC,iBAAkB;IAAA0C,QAAA,EAAEb;EAAO,CAAa,CAAC,GAAGA,OAAO;AACpG,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Image } from 'react-native';
|
|
4
|
+
/** A plain label is a segment with nothing else. */
|
|
5
|
+
export const segmentOf = value => typeof value === 'string' ? {
|
|
6
|
+
label: value
|
|
7
|
+
} : value;
|
|
8
|
+
|
|
9
|
+
/** What the native loaders take: a `require()`d file's packager or file URL, or the URI itself. */
|
|
10
|
+
export const imageUri = source => {
|
|
11
|
+
if (source === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return Image.resolveAssetSource(Array.isArray(source) ? source[0] : source)?.uri;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=segments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["Image","segmentOf","value","label","imageUri","source","undefined","resolveAssetSource","Array","isArray","uri"],"sourceRoot":"../../../../src","sources":["atoms/segmented-control/segments.ts"],"mappings":";;AAAA,SAAQA,KAAK,QAAiC,cAAc;AAI5D;AACA,OAAO,MAAMC,SAAS,GAAIC,KAAuC,IAC/D,OAAOA,KAAK,KAAK,QAAQ,GAAG;EAACC,KAAK,EAAED;AAAK,CAAC,GAAGA,KAAK;;AAEpD;AACA,OAAO,MAAME,QAAQ,GAAIC,MAAuC,IAAyB;EACvF,IAAIA,MAAM,KAAKC,SAAS,EAAE;IACxB,OAAOA,SAAS;EAClB;EAEA,OAAON,KAAK,CAACO,kBAAkB,CAACC,KAAK,CAACC,OAAO,CAACJ,MAAM,CAAC,GAAGA,MAAM,CAAC,CAAC,CAAC,GAAGA,MAAM,CAAC,EAAEK,GAAG;AAClF,CAAC","ignoreList":[]}
|
|
@@ -3,6 +3,9 @@ import type { SegmentedControlComponent } from './segmented-control.types';
|
|
|
3
3
|
* kara's own `UISegmentedControl`, with the pill in the accent and white text on it — the system control tints only the
|
|
4
4
|
* pill, and SwiftUI's segmented picker not even that. A plain `UIView` in React Native layout: it needs no host, takes
|
|
5
5
|
* the width it is given and UIKit's height. Inside a `Screen` or `Island` it is carried into that host by `RNHostView`
|
|
6
|
-
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback.
|
|
6
|
+
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback. A segment shows a
|
|
7
|
+
* title or an image, never both, so an icon segment is drawn natively as one image of the icon and the label, in the
|
|
8
|
+
* label's colour; an image segment is the image alone, loaded and drawn by kara-native at `iconSize`. With `bg` the
|
|
9
|
+
* track is kara-native's own view under a clear control, inset by `p`, so the pill stays the system's.
|
|
7
10
|
*/
|
|
8
11
|
export declare const SegmentedControl: SegmentedControlComponent;
|
|
@@ -1,23 +1,64 @@
|
|
|
1
|
+
import type { ColorToken, SpaceSize, TextVariant } from '@hzblj/kara-theme';
|
|
1
2
|
import type { ReactNode } from 'react';
|
|
3
|
+
import type { ImageSourcePropType } from 'react-native';
|
|
4
|
+
import type { IconName } from '../icon';
|
|
5
|
+
/** A segment that shows more than its label. */
|
|
6
|
+
export type SegmentedControlSegment = {
|
|
7
|
+
/** One of the app's icons, before the label and in the label's colour, the selected segment's or the others'. */
|
|
8
|
+
icon?: IconName;
|
|
9
|
+
/**
|
|
10
|
+
* An image in its own colours instead of the label, such as a carrier's logo: a `require()`d file, or `{uri}` for a
|
|
11
|
+
* URL (raster or SVG). The label stays what screen readers say, and is shown instead if the image cannot be loaded.
|
|
12
|
+
*/
|
|
13
|
+
image?: ImageSourcePropType;
|
|
14
|
+
/** What the segment says, and what `onChange` reports. */
|
|
15
|
+
label: string;
|
|
16
|
+
};
|
|
2
17
|
/**
|
|
3
18
|
* A row of mutually exclusive segments, the selected one in the accent: kara's own `UISegmentedControl` on iOS,
|
|
4
19
|
* Material's single-choice segmented buttons on Android. It needs no `Screen` or `Island`: iOS is a plain view, Android
|
|
5
20
|
* brings its own host. Inside one it joins that host, so it can share a stack with other native views. It takes the
|
|
6
|
-
* width it is given; the height is the platform's
|
|
21
|
+
* width it is given; the height is the platform's, or taller with `spacing`. A segment can lead with one of the app's
|
|
22
|
+
* icons or be an image, a logo, both loaded natively (SDWebImage on iOS, androidsvg on Android, as Footshop does).
|
|
23
|
+
* The track takes the `sx` names a component sized by props uses: `bg` for its colour, `p` for the room around the
|
|
24
|
+
* pill, and `variant` sets the labels like `Text`.
|
|
7
25
|
*/
|
|
8
26
|
export type SegmentedControlProps = {
|
|
27
|
+
/**
|
|
28
|
+
* The track's colour, a palette token. iOS draws it itself as a capsule under the control, in exactly this colour
|
|
29
|
+
* (UIKit renders a control's own background translucent); Android fills the unselected segments with it. The
|
|
30
|
+
* platform's own track when unset.
|
|
31
|
+
*/
|
|
32
|
+
bg?: ColorToken;
|
|
9
33
|
/** Stops the user changing the segment, and dims the control. */
|
|
10
34
|
disabled?: boolean;
|
|
35
|
+
/** The height of the segments' icons and images, a spacing token. `s20` by default. */
|
|
36
|
+
iconSize?: SpaceSize;
|
|
11
37
|
/**
|
|
12
38
|
* Called with the index and label of the segment the user picked, never for a tap on the one already selected.
|
|
13
39
|
* Android plays `selection`; iOS's control plays its own feedback.
|
|
14
40
|
*/
|
|
15
41
|
onChange?: (index: number, value: string) => void;
|
|
42
|
+
/**
|
|
43
|
+
* Room between the track's edge and the selected pill, a spacing token, with `bg`: the track grows by it and the pill
|
|
44
|
+
* keeps its height. iOS only, UIKit's own `s2` when unset; Material's segments have no pill to inset.
|
|
45
|
+
*/
|
|
46
|
+
p?: SpaceSize;
|
|
16
47
|
/** The selected segment. Controlled: pass back what `onChange` reports. */
|
|
17
48
|
selectedIndex: number;
|
|
49
|
+
/**
|
|
50
|
+
* Space above and below the segments' content, a spacing token: a taller control, for bigger icons or an easier
|
|
51
|
+
* target. Unset keeps the platform's own height, and the control is never shorter than that.
|
|
52
|
+
*/
|
|
53
|
+
spacing?: SpaceSize;
|
|
18
54
|
/** Test identifier, on the whole control. Reach a segment by its label. */
|
|
19
55
|
testID?: string;
|
|
20
|
-
/** The segments
|
|
21
|
-
values: string[];
|
|
56
|
+
/** The segments, in order: a label, or a segment with an icon or an image. */
|
|
57
|
+
values: (string | SegmentedControlSegment)[];
|
|
58
|
+
/**
|
|
59
|
+
* The labels' typography variant, the app's face if it has one. `footnoteMedium` on iOS and `subheadlineMedium` on
|
|
60
|
+
* Android by default, each platform's own segmented size. Not scaled with Dynamic Type on iOS.
|
|
61
|
+
*/
|
|
62
|
+
variant?: TextVariant;
|
|
22
63
|
};
|
|
23
64
|
export type SegmentedControlComponent = (props: SegmentedControlProps) => ReactNode;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ImageSourcePropType } from 'react-native';
|
|
2
|
+
import type { SegmentedControlSegment } from './segmented-control.types';
|
|
3
|
+
/** A plain label is a segment with nothing else. */
|
|
4
|
+
export declare const segmentOf: (value: string | SegmentedControlSegment) => SegmentedControlSegment;
|
|
5
|
+
/** What the native loaders take: a `require()`d file's packager or file URL, or the URI itself. */
|
|
6
|
+
export declare const imageUri: (source: ImageSourcePropType | undefined) => string | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hzblj/kara-components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "The atoms, molecules, organisms and templates of @hzblj/kara-ui — real SwiftUI and Jetpack Compose views through @expo/ui.",
|
|
5
5
|
"author": "Jan Blazej",
|
|
6
6
|
"license": "MIT",
|
|
@@ -61,9 +61,9 @@
|
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"@hzblj/expo-native-icons": "^0.1.0",
|
|
63
63
|
"@hzblj/expo-rich-haptics": "^0.1.0",
|
|
64
|
-
"@hzblj/kara-native": "^0.
|
|
65
|
-
"@hzblj/kara-sx": "^0.
|
|
66
|
-
"@hzblj/kara-theme": "^0.
|
|
64
|
+
"@hzblj/kara-native": "^0.20.0",
|
|
65
|
+
"@hzblj/kara-sx": "^0.20.0",
|
|
66
|
+
"@hzblj/kara-theme": "^0.20.0",
|
|
67
67
|
"sf-symbols-typescript": "^2.2.0"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
@@ -1,32 +1,94 @@
|
|
|
1
1
|
import {Host, SegmentedButton, SingleChoiceSegmentedButtonRow, Text} from '@expo/ui/jetpack-compose'
|
|
2
|
-
import {fillMaxWidth, testID as testIdModifier, weight} from '@expo/ui/jetpack-compose/modifiers'
|
|
2
|
+
import {fillMaxWidth, height, testID as testIdModifier, weight} from '@expo/ui/jetpack-compose/modifiers'
|
|
3
|
+
import {iconResourceName} from '@hzblj/expo-native-icons'
|
|
3
4
|
import {haptics} from '@hzblj/expo-rich-haptics'
|
|
4
|
-
import {
|
|
5
|
+
import {DrawableIcon, NativeImage} from '@hzblj/kara-native/jetpack-compose'
|
|
6
|
+
import {spacing as spacingTokens, type TextVariant, useAppTheme} from '@hzblj/kara-theme'
|
|
5
7
|
import {useCallback, useMemo} from 'react'
|
|
6
8
|
|
|
7
9
|
import {useInsideHost} from '../../host-context'
|
|
8
|
-
import
|
|
10
|
+
import {HStack} from '../h-stack'
|
|
11
|
+
import type {SegmentedControlComponent, SegmentedControlProps, SegmentedControlSegment} from './segmented-control.types'
|
|
12
|
+
import {imageUri, segmentOf} from './segments'
|
|
9
13
|
|
|
10
14
|
const MATCH_HEIGHT_ONLY = {horizontal: false, vertical: true}
|
|
11
15
|
|
|
12
|
-
/**
|
|
13
|
-
const
|
|
16
|
+
/** Material's own segment height, which `spacing` only ever adds to. */
|
|
17
|
+
const MATERIAL_HEIGHT = 40
|
|
18
|
+
|
|
19
|
+
type SegmentLabelProps = {
|
|
20
|
+
iconSize: number
|
|
21
|
+
segment: SegmentedControlSegment
|
|
22
|
+
variant: TextVariant
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a segment shows: its label, one of the app's icons before it, or an image loaded by kara-native. The icon and a
|
|
27
|
+
* label the image falls back to take the segment's content colour, so they follow the selection and `disabled`.
|
|
28
|
+
*/
|
|
29
|
+
const SegmentLabel = ({iconSize, segment, variant}: SegmentLabelProps) => {
|
|
30
|
+
const theme = useAppTheme()
|
|
31
|
+
const face = theme.typography[variant]
|
|
32
|
+
|
|
33
|
+
if (segment.image !== undefined) {
|
|
34
|
+
return (
|
|
35
|
+
<NativeImage
|
|
36
|
+
fontFamily={face.fontFamily}
|
|
37
|
+
fontSize={face.fontSize}
|
|
38
|
+
fontWeight={String(face.fontWeight)}
|
|
39
|
+
imageHeight={iconSize}
|
|
40
|
+
label={segment.label}
|
|
41
|
+
source={imageUri(segment.image)}
|
|
42
|
+
/>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const label = <Text style={face}>{segment.label}</Text>
|
|
47
|
+
|
|
48
|
+
if (segment.icon === undefined) {
|
|
49
|
+
return label
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<HStack align="center" spacing="s6">
|
|
54
|
+
<DrawableIcon iconSize={iconSize} resourceName={iconResourceName(segment.icon)} />
|
|
55
|
+
{label}
|
|
56
|
+
</HStack>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
14
59
|
|
|
15
60
|
/**
|
|
16
61
|
* Material's single-choice segmented buttons, the selected one filled with the accent like a checked `Switch`. Its own
|
|
17
62
|
* host, themed like an `Island`, so it drops into React Native layout the way the iOS control does. Inside a `Screen`
|
|
18
63
|
* or `Island` it renders into that host instead (see `useInsideHost`). Material plays nothing on a new segment, so this
|
|
19
|
-
* plays `selection`.
|
|
64
|
+
* plays `selection`. `bg` fills the unselected segments; `p` has nothing to inset, as Material draws no pill.
|
|
20
65
|
*/
|
|
21
66
|
export const SegmentedControl: SegmentedControlComponent = ({
|
|
67
|
+
bg,
|
|
22
68
|
disabled = false,
|
|
69
|
+
iconSize = 's20',
|
|
23
70
|
onChange,
|
|
24
71
|
selectedIndex,
|
|
72
|
+
spacing,
|
|
25
73
|
testID,
|
|
26
74
|
values,
|
|
75
|
+
variant = 'subheadlineMedium',
|
|
27
76
|
}: SegmentedControlProps) => {
|
|
28
77
|
const theme = useAppTheme()
|
|
29
78
|
const insideHost = useInsideHost()
|
|
79
|
+
const segments = useMemo(() => values.map(segmentOf), [values])
|
|
80
|
+
const iconPoints = spacingTokens[iconSize]
|
|
81
|
+
|
|
82
|
+
// Every segment the same width, like UIKit's: Material's would size each to its label. With `spacing`, as tall as
|
|
83
|
+
// the content and that space, and never shorter than Material's own segment.
|
|
84
|
+
const segmentModifiers = useMemo(() => {
|
|
85
|
+
if (spacing === undefined) {
|
|
86
|
+
return [weight(1)]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const content = Math.max(theme.typography[variant].lineHeight, iconPoints)
|
|
90
|
+
return [weight(1), height(Math.max(MATERIAL_HEIGHT, content + 2 * spacingTokens[spacing]))]
|
|
91
|
+
}, [iconPoints, spacing, theme, variant])
|
|
30
92
|
|
|
31
93
|
// Every segment keeps the same outline, the selected one too: an accent border would run its fill out over the line
|
|
32
94
|
// the others draw inside, and make it look taller than its neighbours.
|
|
@@ -41,9 +103,10 @@ export const SegmentedControl: SegmentedControlComponent = ({
|
|
|
41
103
|
disabledInactiveBorderColor: theme.color('border.tertiary'),
|
|
42
104
|
disabledInactiveContentColor: theme.color('btn.content.disabled'),
|
|
43
105
|
inactiveBorderColor: theme.color('border.secondary.base'),
|
|
106
|
+
inactiveContainerColor: bg === undefined ? undefined : theme.color(bg),
|
|
44
107
|
inactiveContentColor: theme.color('content.primary'),
|
|
45
108
|
}),
|
|
46
|
-
[theme]
|
|
109
|
+
[bg, theme]
|
|
47
110
|
)
|
|
48
111
|
|
|
49
112
|
const rowModifiers = useMemo(() => (testID ? [fillMaxWidth(), testIdModifier(testID)] : [fillMaxWidth()]), [testID])
|
|
@@ -56,24 +119,24 @@ export const SegmentedControl: SegmentedControlComponent = ({
|
|
|
56
119
|
}
|
|
57
120
|
|
|
58
121
|
haptics.selection()
|
|
59
|
-
onChange?.(index,
|
|
122
|
+
onChange?.(index, segments[index]?.label ?? '')
|
|
60
123
|
},
|
|
61
|
-
[onChange,
|
|
124
|
+
[onChange, segments, selectedIndex]
|
|
62
125
|
)
|
|
63
126
|
|
|
64
127
|
const row = (
|
|
65
128
|
<SingleChoiceSegmentedButtonRow modifiers={rowModifiers}>
|
|
66
|
-
{
|
|
129
|
+
{segments.map((segment, index) => (
|
|
67
130
|
<SegmentedButton
|
|
68
131
|
colors={colors}
|
|
69
132
|
enabled={!disabled}
|
|
70
133
|
key={index}
|
|
71
|
-
modifiers={
|
|
134
|
+
modifiers={segmentModifiers}
|
|
72
135
|
onClick={() => select(index)}
|
|
73
136
|
selected={index === selectedIndex}
|
|
74
137
|
>
|
|
75
138
|
<SegmentedButton.Label>
|
|
76
|
-
<
|
|
139
|
+
<SegmentLabel iconSize={iconPoints} segment={segment} variant={variant} />
|
|
77
140
|
</SegmentedButton.Label>
|
|
78
141
|
</SegmentedButton>
|
|
79
142
|
))}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {RNHostView} from '@expo/ui/swift-ui'
|
|
2
|
+
import {iconResourceName} from '@hzblj/expo-native-icons'
|
|
2
3
|
import {SegmentedControl as NativeSegmentedControl} from '@hzblj/kara-native/swift-ui'
|
|
3
|
-
import {useAppTheme} from '@hzblj/kara-theme'
|
|
4
|
+
import {spacing as spacingTokens, useAppTheme} from '@hzblj/kara-theme'
|
|
4
5
|
import {useCallback, useMemo} from 'react'
|
|
5
6
|
|
|
6
7
|
import {useInsideHost} from '../../host-context'
|
|
7
8
|
import type {SegmentedControlComponent, SegmentedControlProps} from './segmented-control.types'
|
|
9
|
+
import {imageUri, segmentOf} from './segments'
|
|
8
10
|
|
|
9
11
|
const MATCH_HEIGHT_ONLY = {horizontal: false, vertical: true}
|
|
10
12
|
|
|
@@ -12,28 +14,47 @@ const MATCH_HEIGHT_ONLY = {horizontal: false, vertical: true}
|
|
|
12
14
|
* kara's own `UISegmentedControl`, with the pill in the accent and white text on it — the system control tints only the
|
|
13
15
|
* pill, and SwiftUI's segmented picker not even that. A plain `UIView` in React Native layout: it needs no host, takes
|
|
14
16
|
* the width it is given and UIKit's height. Inside a `Screen` or `Island` it is carried into that host by `RNHostView`
|
|
15
|
-
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback.
|
|
17
|
+
* (see `useInsideHost`), the host's width and its own height. UIKit plays the control's own feedback. A segment shows a
|
|
18
|
+
* title or an image, never both, so an icon segment is drawn natively as one image of the icon and the label, in the
|
|
19
|
+
* label's colour; an image segment is the image alone, loaded and drawn by kara-native at `iconSize`. With `bg` the
|
|
20
|
+
* track is kara-native's own view under a clear control, inset by `p`, so the pill stays the system's.
|
|
16
21
|
*/
|
|
17
22
|
export const SegmentedControl: SegmentedControlComponent = ({
|
|
23
|
+
bg,
|
|
18
24
|
disabled = false,
|
|
25
|
+
iconSize = 's20',
|
|
19
26
|
onChange,
|
|
27
|
+
p,
|
|
20
28
|
selectedIndex,
|
|
29
|
+
spacing,
|
|
21
30
|
testID,
|
|
22
31
|
values,
|
|
32
|
+
variant = 'footnoteMedium',
|
|
23
33
|
}: SegmentedControlProps) => {
|
|
24
34
|
const theme = useAppTheme()
|
|
25
35
|
const insideHost = useInsideHost()
|
|
26
|
-
const face = theme.typography
|
|
36
|
+
const face = theme.typography[variant]
|
|
37
|
+
|
|
38
|
+
const segments = useMemo(() => values.map(segmentOf), [values])
|
|
27
39
|
|
|
28
40
|
// The native control speaks in values; a segment's is its index, so two labels may read the same.
|
|
29
|
-
const options = useMemo(
|
|
41
|
+
const options = useMemo(
|
|
42
|
+
() =>
|
|
43
|
+
segments.map((segment, index) => ({
|
|
44
|
+
icon: segment.icon ? iconResourceName(segment.icon) : undefined,
|
|
45
|
+
image: imageUri(segment.image),
|
|
46
|
+
label: segment.label,
|
|
47
|
+
value: String(index),
|
|
48
|
+
})),
|
|
49
|
+
[segments]
|
|
50
|
+
)
|
|
30
51
|
|
|
31
52
|
const onValueChange = useCallback(
|
|
32
53
|
(value: string) => {
|
|
33
54
|
const index = Number(value)
|
|
34
|
-
onChange?.(index,
|
|
55
|
+
onChange?.(index, segments[index]?.label ?? '')
|
|
35
56
|
},
|
|
36
|
-
[onChange,
|
|
57
|
+
[onChange, segments]
|
|
37
58
|
)
|
|
38
59
|
|
|
39
60
|
const control = (
|
|
@@ -43,11 +64,16 @@ export const SegmentedControl: SegmentedControlComponent = ({
|
|
|
43
64
|
disabled={disabled}
|
|
44
65
|
fontFamily={face.fontFamily}
|
|
45
66
|
fontSize={face.fontSize}
|
|
67
|
+
fontWeight={face.fontWeight}
|
|
68
|
+
iconSize={spacingTokens[iconSize]}
|
|
46
69
|
inactiveTextColor={theme.color('content.primary')}
|
|
47
70
|
onValueChange={onValueChange}
|
|
48
71
|
options={options}
|
|
49
72
|
selectedValue={String(selectedIndex)}
|
|
50
73
|
testID={testID}
|
|
74
|
+
trackColor={bg === undefined ? undefined : theme.color(bg)}
|
|
75
|
+
trackPadding={p === undefined ? undefined : spacingTokens[p]}
|
|
76
|
+
verticalPadding={spacing === undefined ? undefined : spacingTokens[spacing]}
|
|
51
77
|
/>
|
|
52
78
|
)
|
|
53
79
|
|
|
@@ -1,25 +1,68 @@
|
|
|
1
|
+
import type {ColorToken, SpaceSize, TextVariant} from '@hzblj/kara-theme'
|
|
1
2
|
import type {ReactNode} from 'react'
|
|
3
|
+
import type {ImageSourcePropType} from 'react-native'
|
|
4
|
+
|
|
5
|
+
import type {IconName} from '../icon'
|
|
6
|
+
|
|
7
|
+
/** A segment that shows more than its label. */
|
|
8
|
+
export type SegmentedControlSegment = {
|
|
9
|
+
/** One of the app's icons, before the label and in the label's colour, the selected segment's or the others'. */
|
|
10
|
+
icon?: IconName
|
|
11
|
+
/**
|
|
12
|
+
* An image in its own colours instead of the label, such as a carrier's logo: a `require()`d file, or `{uri}` for a
|
|
13
|
+
* URL (raster or SVG). The label stays what screen readers say, and is shown instead if the image cannot be loaded.
|
|
14
|
+
*/
|
|
15
|
+
image?: ImageSourcePropType
|
|
16
|
+
/** What the segment says, and what `onChange` reports. */
|
|
17
|
+
label: string
|
|
18
|
+
}
|
|
2
19
|
|
|
3
20
|
/**
|
|
4
21
|
* A row of mutually exclusive segments, the selected one in the accent: kara's own `UISegmentedControl` on iOS,
|
|
5
22
|
* Material's single-choice segmented buttons on Android. It needs no `Screen` or `Island`: iOS is a plain view, Android
|
|
6
23
|
* brings its own host. Inside one it joins that host, so it can share a stack with other native views. It takes the
|
|
7
|
-
* width it is given; the height is the platform's
|
|
24
|
+
* width it is given; the height is the platform's, or taller with `spacing`. A segment can lead with one of the app's
|
|
25
|
+
* icons or be an image, a logo, both loaded natively (SDWebImage on iOS, androidsvg on Android, as Footshop does).
|
|
26
|
+
* The track takes the `sx` names a component sized by props uses: `bg` for its colour, `p` for the room around the
|
|
27
|
+
* pill, and `variant` sets the labels like `Text`.
|
|
8
28
|
*/
|
|
9
29
|
export type SegmentedControlProps = {
|
|
30
|
+
/**
|
|
31
|
+
* The track's colour, a palette token. iOS draws it itself as a capsule under the control, in exactly this colour
|
|
32
|
+
* (UIKit renders a control's own background translucent); Android fills the unselected segments with it. The
|
|
33
|
+
* platform's own track when unset.
|
|
34
|
+
*/
|
|
35
|
+
bg?: ColorToken
|
|
10
36
|
/** Stops the user changing the segment, and dims the control. */
|
|
11
37
|
disabled?: boolean
|
|
38
|
+
/** The height of the segments' icons and images, a spacing token. `s20` by default. */
|
|
39
|
+
iconSize?: SpaceSize
|
|
12
40
|
/**
|
|
13
41
|
* Called with the index and label of the segment the user picked, never for a tap on the one already selected.
|
|
14
42
|
* Android plays `selection`; iOS's control plays its own feedback.
|
|
15
43
|
*/
|
|
16
44
|
onChange?: (index: number, value: string) => void
|
|
45
|
+
/**
|
|
46
|
+
* Room between the track's edge and the selected pill, a spacing token, with `bg`: the track grows by it and the pill
|
|
47
|
+
* keeps its height. iOS only, UIKit's own `s2` when unset; Material's segments have no pill to inset.
|
|
48
|
+
*/
|
|
49
|
+
p?: SpaceSize
|
|
17
50
|
/** The selected segment. Controlled: pass back what `onChange` reports. */
|
|
18
51
|
selectedIndex: number
|
|
52
|
+
/**
|
|
53
|
+
* Space above and below the segments' content, a spacing token: a taller control, for bigger icons or an easier
|
|
54
|
+
* target. Unset keeps the platform's own height, and the control is never shorter than that.
|
|
55
|
+
*/
|
|
56
|
+
spacing?: SpaceSize
|
|
19
57
|
/** Test identifier, on the whole control. Reach a segment by its label. */
|
|
20
58
|
testID?: string
|
|
21
|
-
/** The segments
|
|
22
|
-
values: string[]
|
|
59
|
+
/** The segments, in order: a label, or a segment with an icon or an image. */
|
|
60
|
+
values: (string | SegmentedControlSegment)[]
|
|
61
|
+
/**
|
|
62
|
+
* The labels' typography variant, the app's face if it has one. `footnoteMedium` on iOS and `subheadlineMedium` on
|
|
63
|
+
* Android by default, each platform's own segmented size. Not scaled with Dynamic Type on iOS.
|
|
64
|
+
*/
|
|
65
|
+
variant?: TextVariant
|
|
23
66
|
}
|
|
24
67
|
|
|
25
68
|
export type SegmentedControlComponent = (props: SegmentedControlProps) => ReactNode
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {Image, type ImageSourcePropType} from 'react-native'
|
|
2
|
+
|
|
3
|
+
import type {SegmentedControlSegment} from './segmented-control.types'
|
|
4
|
+
|
|
5
|
+
/** A plain label is a segment with nothing else. */
|
|
6
|
+
export const segmentOf = (value: string | SegmentedControlSegment): SegmentedControlSegment =>
|
|
7
|
+
typeof value === 'string' ? {label: value} : value
|
|
8
|
+
|
|
9
|
+
/** What the native loaders take: a `require()`d file's packager or file URL, or the URI itself. */
|
|
10
|
+
export const imageUri = (source: ImageSourcePropType | undefined): string | undefined => {
|
|
11
|
+
if (source === undefined) {
|
|
12
|
+
return undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return Image.resolveAssetSource(Array.isArray(source) ? source[0] : source)?.uri
|
|
16
|
+
}
|