@plexui/react-native 0.1.1 → 0.2.1
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/README.md +34 -0
- package/dist/es/components/ActionSheet/ActionSheet.js +67 -19
- package/dist/es/components/Alert/Alert.js +163 -0
- package/dist/es/components/Avatar/Avatar.js +20 -8
- package/dist/es/components/BottomActions/BottomActions.js +34 -0
- package/dist/es/components/BottomSheet/BottomSheet.js +85 -17
- package/dist/es/components/Button/Button.js +108 -28
- package/dist/es/components/Card/Card.js +16 -8
- package/dist/es/components/Checkbox/Checkbox.js +67 -99
- package/dist/es/components/Chip/Chip.js +39 -19
- package/dist/es/components/Dialog/Dialog.js +38 -24
- package/dist/es/components/Field/Field.js +91 -0
- package/dist/es/components/FloatingLabelInput/FloatingLabelInput.js +59 -0
- package/dist/es/components/Icon/createIcon.js +13 -2
- package/dist/es/components/Indicator/CircularProgress.js +80 -0
- package/dist/es/components/Indicator/LoadingDots.js +46 -0
- package/dist/es/components/Indicator/LoadingIndicator.js +101 -0
- package/dist/es/components/Indicator/index.js +3 -0
- package/dist/es/components/Input/Input.js +69 -60
- package/dist/es/components/InputGroup/InputGroup.js +130 -0
- package/dist/es/components/List/List.js +175 -59
- package/dist/es/components/Menu/Menu.js +69 -40
- package/dist/es/components/NavigationButton/NavigationButton.js +20 -4
- package/dist/es/components/OTPInput/OTPInput.js +9 -3
- package/dist/es/components/PageControl/PageControl.js +13 -7
- package/dist/es/components/Popover/Popover.js +128 -0
- package/dist/es/components/Progress/Progress.js +142 -0
- package/dist/es/components/ProgressSteps/ProgressSteps.js +207 -0
- package/dist/es/components/RollingNumber/RollingNumber.js +125 -0
- package/dist/es/components/Select/Select.js +97 -34
- package/dist/es/components/Separator/Separator.js +31 -0
- package/dist/es/components/ShimmerText/ShimmerText.js +98 -0
- package/dist/es/components/Stepper/Stepper.js +219 -0
- package/dist/es/components/Switch/Switch.js +26 -12
- package/dist/es/components/TagInput/TagInput.js +121 -0
- package/dist/es/components/Text/Text.js +7 -4
- package/dist/es/components/TextLink/TextLink.js +42 -0
- package/dist/es/components/Textarea/Textarea.js +12 -0
- package/dist/es/components/Tip/Tip.js +193 -0
- package/dist/es/components/Toolbar/Toolbar.js +6 -2
- package/dist/es/components/Tooltip/Tooltip.js +272 -0
- package/dist/es/index.js +20 -2
- package/dist/es/internal/FloatingPanel.js +111 -0
- package/dist/es/internal/fieldStack.js +73 -0
- package/dist/es/internal/floating.js +97 -0
- package/dist/es/internal/ripple.js +119 -0
- package/dist/es/internal/row.js +2 -1
- package/dist/es/internal/transition.js +113 -0
- package/dist/es/theme/ThemeProvider.js +41 -9
- package/dist/es/theme/material.js +37 -7
- package/dist/types/components/ActionSheet/ActionSheet.d.ts +15 -7
- package/dist/types/components/Alert/Alert.d.ts +51 -0
- package/dist/types/components/Avatar/Avatar.d.ts +4 -2
- package/dist/types/components/BottomActions/BottomActions.d.ts +18 -0
- package/dist/types/components/BottomSheet/BottomSheet.d.ts +4 -2
- package/dist/types/components/Button/Button.d.ts +20 -5
- package/dist/types/components/Card/Card.d.ts +14 -1
- package/dist/types/components/Checkbox/Checkbox.d.ts +4 -3
- package/dist/types/components/Chip/Chip.d.ts +23 -4
- package/dist/types/components/Dialog/Dialog.d.ts +9 -5
- package/dist/types/components/Field/Field.d.ts +64 -0
- package/dist/types/components/FloatingLabelInput/FloatingLabelInput.d.ts +42 -0
- package/dist/types/components/Indicator/CircularProgress.d.ts +34 -0
- package/dist/types/components/Indicator/LoadingDots.d.ts +17 -0
- package/dist/types/components/Indicator/LoadingIndicator.d.ts +46 -0
- package/dist/types/components/Indicator/index.d.ts +3 -0
- package/dist/types/components/Input/Input.d.ts +13 -3
- package/dist/types/components/InputGroup/InputGroup.d.ts +71 -0
- package/dist/types/components/List/List.d.ts +57 -14
- package/dist/types/components/Menu/Menu.d.ts +4 -0
- package/dist/types/components/NavigationButton/NavigationButton.d.ts +11 -3
- package/dist/types/components/OTPInput/OTPInput.d.ts +10 -1
- package/dist/types/components/PageControl/PageControl.d.ts +11 -1
- package/dist/types/components/Popover/Popover.d.ts +86 -0
- package/dist/types/components/Progress/Progress.d.ts +56 -0
- package/dist/types/components/ProgressSteps/ProgressSteps.d.ts +61 -0
- package/dist/types/components/RollingNumber/RollingNumber.d.ts +36 -0
- package/dist/types/components/Select/Select.d.ts +36 -8
- package/dist/types/components/Separator/Separator.d.ts +19 -0
- package/dist/types/components/ShimmerText/ShimmerText.d.ts +28 -0
- package/dist/types/components/Stepper/Stepper.d.ts +61 -0
- package/dist/types/components/Switch/Switch.d.ts +5 -5
- package/dist/types/components/TagInput/TagInput.d.ts +44 -0
- package/dist/types/components/TextLink/TextLink.d.ts +30 -0
- package/dist/types/components/Textarea/Textarea.d.ts +19 -0
- package/dist/types/components/Tip/Tip.d.ts +71 -0
- package/dist/types/components/Toolbar/Toolbar.d.ts +9 -1
- package/dist/types/components/Tooltip/Tooltip.d.ts +84 -0
- package/dist/types/index.d.ts +20 -2
- package/dist/types/internal/FloatingPanel.d.ts +70 -0
- package/dist/types/internal/fieldStack.d.ts +48 -0
- package/dist/types/internal/floating.d.ts +71 -0
- package/dist/types/internal/ripple.d.ts +17 -0
- package/dist/types/internal/row.d.ts +15 -3
- package/dist/types/internal/transition.d.ts +37 -0
- package/dist/types/theme/ThemeProvider.d.ts +28 -4
- package/dist/types/theme/material.d.ts +20 -6
- package/dist/types/theme/types.d.ts +8 -0
- package/package.json +2 -2
- package/src/components/ActionSheet/ActionSheet.tsx +123 -38
- package/src/components/Alert/Alert.tsx +279 -0
- package/src/components/Avatar/Avatar.tsx +21 -9
- package/src/components/BottomActions/BottomActions.tsx +54 -0
- package/src/components/BottomSheet/BottomSheet.tsx +111 -25
- package/src/components/Button/Button.tsx +170 -35
- package/src/components/Card/Card.tsx +33 -9
- package/src/components/Checkbox/Checkbox.tsx +89 -124
- package/src/components/Chip/Chip.tsx +83 -20
- package/src/components/Dialog/Dialog.tsx +68 -37
- package/src/components/Field/Field.tsx +227 -0
- package/src/components/FloatingLabelInput/FloatingLabelInput.tsx +144 -0
- package/src/components/Icon/createIcon.tsx +16 -6
- package/src/components/Indicator/CircularProgress.tsx +141 -0
- package/src/components/Indicator/LoadingDots.tsx +79 -0
- package/src/components/Indicator/LoadingIndicator.tsx +166 -0
- package/src/components/Indicator/index.ts +3 -0
- package/src/components/Input/Input.tsx +126 -97
- package/src/components/InputGroup/InputGroup.tsx +261 -0
- package/src/components/List/List.tsx +345 -88
- package/src/components/Menu/Menu.tsx +83 -41
- package/src/components/NavigationButton/NavigationButton.tsx +35 -5
- package/src/components/OTPInput/OTPInput.tsx +21 -2
- package/src/components/PageControl/PageControl.tsx +36 -8
- package/src/components/Popover/Popover.tsx +261 -0
- package/src/components/Progress/Progress.tsx +255 -0
- package/src/components/ProgressSteps/ProgressSteps.tsx +415 -0
- package/src/components/RollingNumber/RollingNumber.tsx +189 -0
- package/src/components/Select/Select.tsx +175 -71
- package/src/components/Separator/Separator.tsx +51 -0
- package/src/components/ShimmerText/ShimmerText.tsx +144 -0
- package/src/components/Stepper/Stepper.tsx +386 -0
- package/src/components/Switch/Switch.tsx +26 -12
- package/src/components/TagInput/TagInput.tsx +227 -0
- package/src/components/Text/Text.tsx +8 -4
- package/src/components/TextLink/TextLink.tsx +77 -0
- package/src/components/Textarea/Textarea.tsx +23 -0
- package/src/components/Tip/Tip.tsx +410 -0
- package/src/components/Toolbar/Toolbar.tsx +27 -2
- package/src/components/Tooltip/Tooltip.tsx +478 -0
- package/src/index.ts +36 -2
- package/src/internal/FloatingPanel.tsx +199 -0
- package/src/internal/fieldStack.tsx +179 -0
- package/src/internal/floating.ts +131 -0
- package/src/internal/ripple.tsx +132 -0
- package/src/internal/row.ts +16 -3
- package/src/internal/transition.ts +124 -0
- package/src/theme/ThemeProvider.tsx +65 -15
- package/src/theme/material.ts +39 -7
- package/src/theme/types.ts +8 -0
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { useContext, useEffect, useState } from "react";
|
|
2
|
+
import { useContext, useEffect, useLayoutEffect, useState } from "react";
|
|
3
3
|
import { Platform, Pressable, StyleSheet, View } from "react-native";
|
|
4
|
+
import Svg, { Path } from "react-native-svg";
|
|
4
5
|
import ChevronUpDown from "../../icons/plex/ChevronUpDown";
|
|
5
6
|
import { spaceToggles } from "../../internal/a11y";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { useRipple } from "../../internal/ripple";
|
|
8
|
+
import { RowEndContext, RowFrameContext, RowHoldContext, rowHold } from "../../internal/row";
|
|
9
|
+
import { androidSelector, withAlpha } from "../../theme/material";
|
|
8
10
|
import { MaterialArrowDropDown } from "../Icon/material";
|
|
9
11
|
import { useTheme } from "../../theme/ThemeProvider";
|
|
10
|
-
import {
|
|
12
|
+
import { ChoiceDialog } from "../Dialog/Dialog";
|
|
11
13
|
import { FieldOutline, fieldLook } from "../Input/Input";
|
|
12
14
|
import { Menu, menuKeyDown, MenuSurface, useMenuAnchor } from "../Menu/Menu";
|
|
13
15
|
import { selectionColor } from "../Switch/Switch";
|
|
14
16
|
import { Text } from "../Text/Text";
|
|
15
17
|
/**
|
|
16
|
-
* A choice of one option: the field of an Input with the chosen label and a chevron. iOS 26: UIKit's pop-up button
|
|
18
|
+
* A choice of one option (or several, `multiple`): the field of an Input with the chosen label and a chevron. iOS 26: UIKit's pop-up button
|
|
17
19
|
* field with `chevron.up.chevron.down`, opening the glass menu of the options with a checkmark at the chosen one; at
|
|
18
20
|
* the end of an iOS List row it is the row's pop-up, as the web's in a phone row: the value in the row's type (the
|
|
19
21
|
* secondary label, the placeholder in the tertiary), the menu picker's chevrons after it, and the menu opening over it
|
|
20
22
|
* at its end. Android: the field with Material's dropdown arrow (at a List row's end, the row's value in the accent,
|
|
21
23
|
* as an Android value row), opening Android's single-choice dialog: the setting's name over 50dp rows with a radio,
|
|
22
|
-
* and Cancel. Choosing a row closes it.
|
|
24
|
+
* and Cancel. Choosing a row closes it. With `picker="menu"` Android opens the popup of the options instead: under the
|
|
25
|
+
* field, or from a List row the row's menu (the screen dimmed around the lit row), its chosen row checked at the end.
|
|
26
|
+
* A `multiple` select opens the menu, where each press checks or unchecks an option and the menu stays open; on
|
|
27
|
+
* Android `picker="dialog"` opens the dialog of checkbox rows instead, whose OK applies them.
|
|
23
28
|
*/
|
|
24
|
-
export function Select(
|
|
29
|
+
export function Select(props) {
|
|
30
|
+
const { options, multiple = false, placeholder = "Select…", disabled = false, invalid = false, variant = "outline", size = "md", pill = false, block = true, "aria-label": ariaLabel, picker = multiple ? "menu" : "dialog", title, cancelLabel = "Cancel", okLabel = "OK", allSelectedLabel = "All selected", testID, style, } = props;
|
|
25
31
|
const theme = useTheme();
|
|
26
32
|
const [open, setOpen] = useState(false);
|
|
27
33
|
// A field disabled while its menu is open closes the menu: nothing can be chosen from it any more
|
|
@@ -29,15 +35,43 @@ export function Select({ options, value, onChange, placeholder = "Select…", di
|
|
|
29
35
|
setOpen(false);
|
|
30
36
|
const shown = open && !disabled;
|
|
31
37
|
const android = theme.isAndroid;
|
|
32
|
-
// Android's dialog stands in the middle of the screen: only iOS's menu follows the field
|
|
33
|
-
const
|
|
38
|
+
// Android's dialog stands in the middle of the screen: only a menu (iOS's, Android's `picker="menu"`) follows the field
|
|
39
|
+
const dialog = android && picker !== "menu";
|
|
40
|
+
const { anchor, row: menuRow, place, setTrigger, setRowFrame } = useMenuAnchor(shown && !dialog);
|
|
41
|
+
const values = props.multiple ? props.value : props.value != null ? [props.value] : [];
|
|
42
|
+
const chosenOptions = options.filter((option) => values.includes(option.value));
|
|
43
|
+
// A multiple select's dialog keeps its checked rows here from its opening until OK applies them
|
|
44
|
+
const [checked, setChecked] = useState([]);
|
|
45
|
+
const [wasShown, setWasShown] = useState(shown);
|
|
46
|
+
if (wasShown !== shown) {
|
|
47
|
+
setWasShown(shown);
|
|
48
|
+
if (shown)
|
|
49
|
+
setChecked(values);
|
|
50
|
+
}
|
|
51
|
+
const apply = (next) => {
|
|
52
|
+
if (props.multiple)
|
|
53
|
+
props.onChange(options.filter((option) => next.includes(option.value)));
|
|
54
|
+
};
|
|
34
55
|
const choose = (option) => {
|
|
35
56
|
if (disabled || option.disabled)
|
|
36
57
|
return;
|
|
37
|
-
|
|
58
|
+
// A multiple select's option checks or unchecks itself
|
|
59
|
+
if (props.multiple)
|
|
60
|
+
apply(values.includes(option.value) ? values.filter((item) => item !== option.value) : [...values, option.value]);
|
|
61
|
+
else
|
|
62
|
+
props.onChange(option);
|
|
38
63
|
};
|
|
39
|
-
|
|
40
|
-
const
|
|
64
|
+
// The field's text, the web's: the chosen label; for several, the one label, "3 selected", or every one chosen
|
|
65
|
+
const shownLabel = !multiple
|
|
66
|
+
? chosenOptions[0]?.label
|
|
67
|
+
: chosenOptions.length && chosenOptions.length >= options.length
|
|
68
|
+
? allSelectedLabel
|
|
69
|
+
: chosenOptions.length === 0
|
|
70
|
+
? undefined
|
|
71
|
+
: chosenOptions.length === 1
|
|
72
|
+
? chosenOptions[0].label
|
|
73
|
+
: `${chosenOptions.length} selected`;
|
|
74
|
+
const look = fieldLook(theme, { variant, size, pill }, { focused: open, filled: shownLabel != null, disabled, invalid });
|
|
41
75
|
const row = useContext(RowEndContext);
|
|
42
76
|
const hold = useContext(RowHoldContext);
|
|
43
77
|
const rowEnd = row != null;
|
|
@@ -45,55 +79,77 @@ export function Select({ options, value, onChange, placeholder = "Select…", di
|
|
|
45
79
|
// At the end of an Android row the select is the row's value, as in an Android value row, and a press anywhere
|
|
46
80
|
// on the row opens its choice
|
|
47
81
|
const androidRow = rowEnd && android;
|
|
82
|
+
// Alone in a row whose label is hidden (a label-hidden `Field row`): the value from the row's text start and the
|
|
83
|
+
// chevrons at its end, on the row's line
|
|
84
|
+
const stacked = row?.stacked === true && block;
|
|
48
85
|
const handRow = androidRow && !disabled ? row?.press : undefined;
|
|
49
86
|
useEffect(() => {
|
|
50
87
|
if (!handRow)
|
|
51
88
|
return;
|
|
52
|
-
handRow(() => setOpen(true));
|
|
89
|
+
handRow(dialog ? () => setOpen(true) : () => place(() => setOpen(true)));
|
|
53
90
|
return () => handRow(null);
|
|
54
|
-
}, [handRow]);
|
|
91
|
+
}, [handRow, dialog, place]);
|
|
92
|
+
// Android's menu from a List row is the row's: it dims the screen around the row and stands under it
|
|
93
|
+
const frame = useContext(RowFrameContext);
|
|
94
|
+
const rowMenu = !dialog && androidRow ? frame : null;
|
|
95
|
+
useLayoutEffect(() => {
|
|
96
|
+
setRowFrame(rowMenu);
|
|
97
|
+
return () => setRowFrame(null);
|
|
98
|
+
}, [rowMenu, setRowFrame]);
|
|
55
99
|
const tertiary = theme.colors.labelTertiary;
|
|
56
100
|
const m = theme.material;
|
|
57
101
|
return (
|
|
58
102
|
// A field that moves while its menu is open takes the menu with it
|
|
59
|
-
_jsxs(View, { ref: setTrigger, collapsable: false, onLayout: shown && !
|
|
103
|
+
_jsxs(View, { ref: setTrigger, collapsable: false, onLayout: shown && !dialog ? () => place() : undefined, style: [block ? styles.block : styles.hug, style], children: [_jsxs(Pressable, { accessibilityRole: "button", accessibilityLabel: ariaLabel ? `${ariaLabel}, ${shownLabel ?? placeholder}` : undefined, accessibilityState: { disabled, expanded: open }, "aria-haspopup": dialog ? "dialog" : "menu", "aria-expanded": open, disabled: disabled, ...rowHold(hold), onPress: () => (dialog ? setOpen(true) : place(() => setOpen(true))),
|
|
60
104
|
// A row's value takes the presses of the row's height (where the row does not hand it its own)
|
|
61
105
|
hitSlop: androidRow && !row?.press ? ROW_SLOP : undefined, testID: testID,
|
|
62
106
|
// The arrow sits in Compose's trailing icon slot, 12 from the edge
|
|
63
|
-
style: [styles.box, inRow ? styles.row : androidRow ? null : [look.box, { gap: look.gap }], android && !androidRow ? { paddingEnd: 12 } : null], children: [android && !androidRow ? _jsx(FieldOutline, { frame: look.frame }) : null, androidRow ? (
|
|
107
|
+
style: [styles.box, inRow ? styles.row : androidRow ? null : [look.box, { gap: look.gap }], android && !androidRow ? { paddingEnd: 12 } : null, stacked ? styles.stacked : null], children: [android && !androidRow ? _jsx(FieldOutline, { frame: look.frame }) : null, androidRow ? (
|
|
64
108
|
// A value row's value: 16sp in the accent, the placeholder in the hint's gray, faded to half when disabled
|
|
65
|
-
_jsx(Text, { numberOfLines: 1, color: withAlpha(
|
|
109
|
+
_jsx(Text, { numberOfLines: 1, color: withAlpha(shownLabel != null ? m.primary : withAlpha(m.onSurface, 0.35), disabled ? 0.5 : 1), style: [stacked ? styles.grow : styles.shrink, styles.androidValue, stacked ? styles.startText : null], children: shownLabel ?? placeholder })) : inRow ? (_jsx(Text, { numberOfLines: 1, color: shownLabel != null && !disabled ? theme.colors.labelSecondary : tertiary, style: [stacked ? styles.grow : styles.shrink, { fontSize: theme.length("--field-row-label-font-size", 17), lineHeight: theme.length("--field-row-label-line-height", 22) }], children: shownLabel ?? placeholder })) : (_jsx(Text, { numberOfLines: 1, color: shownLabel != null ? look.text.color : look.placeholder, style: [styles.grow, { fontSize: look.text.fontSize, lineHeight: look.text.lineHeight }], children: shownLabel ?? placeholder })), inRow ? (
|
|
66
110
|
// The menu picker's chevrons, 8.5 by 22 with the 17 symbol overflowing it, 2 inside the row's end
|
|
67
111
|
_jsx(View, { style: styles.rowChevrons, children: _jsx(ChevronUpDown, { size: 17, color: disabled ? tertiary : theme.color("--mobile-accessory-secondary", theme.colors.labelSecondary) }) })) : androidRow ? null : android ? (
|
|
68
112
|
// Turned about the box's center while the picker is open, as Compose rotates its trailing icon 180°
|
|
69
|
-
_jsx(View, { style: open ? styles.turned : null, children: _jsx(MaterialArrowDropDown, { size: 24, color: look.trailingIcon }) })) : (_jsx(ChevronUpDown, { size: 17, color: theme.color("--mobile-label-secondary") }))] }),
|
|
70
|
-
// The web reads the rows as a menu of radio items (the roles of the iOS menu)
|
|
113
|
+
_jsx(View, { style: open ? styles.turned : null, children: _jsx(MaterialArrowDropDown, { size: 24, color: look.trailingIcon }) })) : (_jsx(ChevronUpDown, { size: 17, color: theme.color("--mobile-label-secondary") }))] }), dialog ? (_jsx(ChoiceDialog, { open: shown, onClose: () => setOpen(false), title: title ?? ariaLabel, cancelLabel: cancelLabel, okLabel: multiple ? okLabel : undefined, onOk: multiple ? () => apply(checked) : undefined,
|
|
114
|
+
// The web reads the rows as a menu of radio items (the roles of the iOS menu), of checkbox items for several;
|
|
115
|
+
// TalkBack as radio buttons or checkboxes
|
|
71
116
|
listProps: Platform.OS === "web"
|
|
72
117
|
? { role: "menu", "aria-label": title ?? ariaLabel, onKeyDown: (event) => menuKeyDown(event) }
|
|
73
|
-
: { accessibilityRole: "radiogroup", accessibilityLabel: title ?? ariaLabel }, testID: testID ? `${testID}-menu` : undefined, children: options.map((option) => (_jsx(ChoiceRow, { option: option, chosen: option.value
|
|
118
|
+
: { accessibilityRole: multiple ? undefined : "radiogroup", accessibilityLabel: title ?? ariaLabel }, testID: testID ? `${testID}-menu` : undefined, children: options.map((option) => (_jsx(ChoiceRow, { option: option, check: multiple, chosen: multiple ? checked.includes(option.value) : values.includes(option.value), disabled: disabled || !!option.disabled, onChoose: () => {
|
|
74
119
|
if (disabled || option.disabled || !shown)
|
|
75
120
|
return;
|
|
121
|
+
// A checkbox row checks or unchecks itself until OK; a radio row chooses and closes
|
|
122
|
+
if (multiple) {
|
|
123
|
+
setChecked((current) => (current.includes(option.value) ? current.filter((item) => item !== option.value) : [...current, option.value]));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
76
126
|
choose(option);
|
|
77
127
|
setOpen(false);
|
|
78
|
-
}, testID: testID ? `${testID}-option-${option.value}` : undefined }, option.value))) })) : (_jsx(MenuSurface, { open: shown, anchor: anchor, onClose: () => setOpen(false), check: true, cover: inRow, align: inRow ? "end" : "start",
|
|
128
|
+
}, testID: testID ? `${testID}-option-${option.value}` : undefined }, option.value))) })) : (_jsx(MenuSurface, { open: shown, anchor: anchor, onClose: () => setOpen(false), check: true, multiple: multiple, cover: inRow, align: inRow ? "end" : "start",
|
|
129
|
+
// Android's: a row's menu fits its items; a field's list is the field's width (Compose's exposed dropdown)
|
|
130
|
+
row: android ? menuRow : undefined, width: android ? (androidRow ? undefined : anchor?.width) : Math.max(theme.length("--menu-default-width", 250), anchor?.width ?? 0), "aria-label": ariaLabel, testID: testID ? `${testID}-menu` : undefined, children: options.map((option) => (_jsx(Menu.Item, { checked: values.includes(option.value), disabled: disabled || option.disabled, description: option.description, onSelect: () => choose(option), testID: testID ? `${testID}-option-${option.value}` : undefined, children: option.label }, option.value))) }))] }));
|
|
79
131
|
}
|
|
80
132
|
/**
|
|
81
|
-
* A row of Android's
|
|
82
|
-
*
|
|
83
|
-
*
|
|
133
|
+
* A row of Android's choice dialog (4 in from the dialog's sides): 50dp, a 20dp radio in a 22dp frame 22 in and 14
|
|
134
|
+
* down, the label 16sp at 55 (a description 14sp gray under it); its press the list selector's ripple from the touch
|
|
135
|
+
* point (the platform's on a device, drawn on the web). The radio: a 2dp ring in the light gray (the web's #afafaf,
|
|
136
|
+
* #5d5d5d at night), the selection color with a 10dp dot when chosen. A multiple choice's row has an 18dp square with
|
|
137
|
+
* 4dp corners in the same frame (24 in, as the web's; the reference's checkbox cell has it 1 further in and its label
|
|
138
|
+
* 1 nearer): empty, a 1.5 outline in the mid gray (#767676); checked, the selection color with a white check 2 wide.
|
|
84
139
|
*/
|
|
85
|
-
function ChoiceRow({ option, chosen, disabled, onChoose, testID }) {
|
|
140
|
+
function ChoiceRow({ option, check, chosen, disabled, onChoose, testID }) {
|
|
86
141
|
const theme = useTheme();
|
|
87
142
|
const m = theme.material;
|
|
88
143
|
const on = selectionColor(theme);
|
|
89
|
-
const
|
|
144
|
+
const dark = theme.scheme === "dark";
|
|
145
|
+
const off = check ? theme.color(dark ? "--gray-550" : "--gray-450") : theme.color(dark ? "--gray-500" : "--gray-300");
|
|
146
|
+
const nativeRipple = Platform.OS === "android";
|
|
147
|
+
const ripple = useRipple(androidSelector(theme.scheme), !nativeRipple, theme.reducedMotion);
|
|
90
148
|
const semantics = Platform.OS === "web"
|
|
91
|
-
? { role: "menuitemradio", "aria-checked": chosen, accessibilityState: { checked: chosen, disabled }, ...spaceToggles(onChoose) }
|
|
92
|
-
: { accessibilityRole: "radio", accessibilityState: { checked: chosen, disabled } };
|
|
93
|
-
return (_jsxs(Pressable, { ...semantics, disabled: disabled, onPress: onChoose,
|
|
149
|
+
? { role: (check ? "menuitemcheckbox" : "menuitemradio"), "aria-checked": chosen, accessibilityState: { checked: chosen, disabled }, ...spaceToggles(onChoose) }
|
|
150
|
+
: { accessibilityRole: check ? "checkbox" : "radio", accessibilityState: { checked: chosen, disabled } };
|
|
151
|
+
return (_jsxs(Pressable, { ...semantics, disabled: disabled, onPress: onChoose, onPressIn: nativeRipple ? undefined : ripple.start, onPressOut: nativeRipple ? undefined : ripple.end, android_ripple: nativeRipple ? { color: androidSelector(theme.scheme), foreground: true } : undefined, testID: testID, style: [styles.choice, { opacity: disabled ? 0.5 : 1 }], children: [ripple.layer, _jsx(View, { style: styles.choiceRadio, children: check ? (_jsx(View, { style: [styles.square, chosen ? { backgroundColor: on } : { borderWidth: 1.5, borderColor: off }], children: chosen ? (_jsx(Svg, { width: 18, height: 18, viewBox: "0 0 18 18", children: _jsx(Path, { d: "M4 10l3 3 7-7", fill: "none", stroke: theme.color(`--color-text-${theme.selectionColor}-solid`, "#FFFFFF"), strokeWidth: 2, strokeLinecap: "round", strokeLinejoin: "round" }) })) : null })) : (_jsx(View, { style: [styles.ring, { borderColor: chosen ? on : off }], children: chosen ? _jsx(View, { style: [styles.dot, { backgroundColor: on }] }) : null })) }), _jsxs(View, { style: [styles.choiceText, option.description != null ? styles.choiceDescribed : null], children: [_jsx(Text, { color: m.onSurface, numberOfLines: 1, style: styles.choiceLabel, children: option.label }), option.description != null ? (_jsx(Text, { color: theme.colors.labelSecondary, style: styles.choiceDescription, children: option.description })) : null] })] }));
|
|
94
152
|
}
|
|
95
|
-
/** Android's list selector: black at 6% over a light card, white at 5% at night. */
|
|
96
|
-
const listPress = (scheme) => (scheme === "dark" ? "rgba(255, 255, 255, 0.05)" : "rgba(0, 0, 0, 0.06)");
|
|
97
153
|
/** A row's value reaches the 50dp row: 15 over and under its 20 line. */
|
|
98
154
|
const ROW_SLOP = { top: 15, bottom: 15, left: 8, right: 8 };
|
|
99
155
|
const styles = StyleSheet.create({
|
|
@@ -105,6 +161,8 @@ const styles = StyleSheet.create({
|
|
|
105
161
|
shrink: { flexShrink: 1, minWidth: 0 },
|
|
106
162
|
// A row's pop-up: no field, 34 tall, the value 8 before its chevrons
|
|
107
163
|
row: { minHeight: 34, gap: 8 },
|
|
164
|
+
stacked: { minHeight: 0 },
|
|
165
|
+
startText: { textAlign: "left" },
|
|
108
166
|
rowChevrons: { width: 8.5, height: 22, marginStart: -2, marginEnd: 2, alignItems: "center", justifyContent: "center", overflow: "visible" },
|
|
109
167
|
androidValue: { fontSize: 16, lineHeight: 20, textAlign: "right" },
|
|
110
168
|
// A radio row in the dialog: 4 of padding and the row's 18 before the radio's frame, 21 and 4 after the text
|
|
@@ -112,8 +170,13 @@ const styles = StyleSheet.create({
|
|
|
112
170
|
choiceRadio: { width: 22, height: 22, marginTop: 14, alignItems: "center", justifyContent: "center" },
|
|
113
171
|
ring: { width: 20, height: 20, borderRadius: 10, borderWidth: 2, alignItems: "center", justifyContent: "center" },
|
|
114
172
|
dot: { width: 10, height: 10, borderRadius: 5 },
|
|
115
|
-
|
|
116
|
-
|
|
173
|
+
square: { width: 18, height: 18, borderRadius: 4, overflow: "hidden" },
|
|
174
|
+
// The label at 55: 11 past the radio's frame, 15 under the row's top (its baseline 31 into the row, the reference's
|
|
175
|
+
// 30.5), as the web's; one line keeps the row 50
|
|
176
|
+
choiceText: { flex: 1, minWidth: 0, marginStart: 11, paddingTop: 15, paddingBottom: 15, gap: 4 },
|
|
177
|
+
// A description: its first baseline 52.3 into the row, lines 16.4 apart (14sp Roboto's), 15 under its last line and
|
|
178
|
+
// the font's 2 of padding (a two-line description makes the row 89, as the web's)
|
|
179
|
+
choiceDescribed: { paddingBottom: 17 },
|
|
117
180
|
choiceLabel: { fontSize: 16, lineHeight: 20 },
|
|
118
|
-
choiceDescription: { fontSize: 14, lineHeight:
|
|
181
|
+
choiceDescription: { fontSize: 14, lineHeight: 16.4 },
|
|
119
182
|
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Platform, StyleSheet, View } from "react-native";
|
|
3
|
+
import { divider } from "../../theme/material";
|
|
4
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
5
|
+
/**
|
|
6
|
+
* A thin line that divides content, as @plexui/ui's Separator: `--separator-color` (`--color-border`) at
|
|
7
|
+
* `--separator-thickness` (1) on iOS, as the web draws it on a phone. On Android the line between the rows of a card
|
|
8
|
+
* in the Plex Android look: one device pixel of the divider gray (a light gray over a light card, the darkest gray at
|
|
9
|
+
* night). A vertical one stretches to the height of its row, at least a line of text.
|
|
10
|
+
*/
|
|
11
|
+
export function Separator({ orientation = "horizontal", decorative = true, testID, style }) {
|
|
12
|
+
const theme = useTheme();
|
|
13
|
+
const color = theme.isAndroid ? divider(theme) : theme.color("--separator-color", theme.colors.border);
|
|
14
|
+
// One device pixel on Android (a hairline); the token's 1 elsewhere, the web's phone line
|
|
15
|
+
const thickness = theme.isAndroid ? StyleSheet.hairlineWidth : theme.length("--separator-thickness", 1);
|
|
16
|
+
const vertical = orientation === "vertical";
|
|
17
|
+
const a11y = decorative
|
|
18
|
+
? { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", "aria-hidden": true }
|
|
19
|
+
: { role: "separator", ...(Platform.OS === "web" ? { "aria-orientation": orientation } : null) };
|
|
20
|
+
return (_jsx(View, { ...a11y, testID: testID, style: [
|
|
21
|
+
styles.line,
|
|
22
|
+
{ backgroundColor: color },
|
|
23
|
+
vertical ? { width: thickness, alignSelf: "stretch", minHeight: MIN_VERTICAL } : { height: thickness, alignSelf: "stretch" },
|
|
24
|
+
style,
|
|
25
|
+
] }));
|
|
26
|
+
}
|
|
27
|
+
/** A vertical line is at least a line of the body text tall (the web's `min-height: 1em`). */
|
|
28
|
+
const MIN_VERTICAL = 16;
|
|
29
|
+
const styles = StyleSheet.create({
|
|
30
|
+
line: { flexShrink: 0 },
|
|
31
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { Animated, Easing, Platform, StyleSheet, View } from "react-native";
|
|
4
|
+
import { cssEase, nativeDriver } from "../../internal/transition";
|
|
5
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
6
|
+
import { Text } from "../Text/Text";
|
|
7
|
+
/** The band is drawn in slices, each a copy of the text at its share of the highlight, as the web's gradient is. */
|
|
8
|
+
const SLICES = 16;
|
|
9
|
+
/**
|
|
10
|
+
* The sweep. iOS, as the web's: a band half the text's width, the text color to the highlight and back, goes from
|
|
11
|
+
* just before the text to past it in 70% of 3s (ease) and rests for the rest; the first after 0.6s. Android, the Plex
|
|
12
|
+
* Android look's flicker: a 160dp band crosses the text and twice its width in 1200ms, linear, and rests 240ms.
|
|
13
|
+
*/
|
|
14
|
+
function sweepOf(theme, width) {
|
|
15
|
+
if (theme.isAndroid) {
|
|
16
|
+
const band = 160;
|
|
17
|
+
return { band, from: -band, to: width + band, period: 1440, pass: 1200, delay: 0, easing: Easing.linear };
|
|
18
|
+
}
|
|
19
|
+
const band = width / 2;
|
|
20
|
+
return { band, from: -band, to: 1.25 * width, period: 3000, pass: 2100, delay: 600, easing: cssEase };
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Text with a shimmer for loading and in-progress states, as @plexui/ui's ShimmerText: a light band sweeps across it,
|
|
24
|
+
* over and over. It takes the props of `Text` for its type and color. With reduced motion it is plain text.
|
|
25
|
+
*/
|
|
26
|
+
export function ShimmerText(props) {
|
|
27
|
+
return _jsx(ShimmerableText, { ...props, shimmer: true });
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Text whose shimmer turns on and off, as @plexui/ui's ShimmerableText: with `shimmer` on it sweeps as ShimmerText;
|
|
31
|
+
* turned off, it finishes the current sweep and then stops, with no abrupt cut.
|
|
32
|
+
*/
|
|
33
|
+
export function ShimmerableText({ as = "div", shimmer = false, children, style, containerStyle, ...text }) {
|
|
34
|
+
const theme = useTheme();
|
|
35
|
+
const [width, setWidth] = useState(0);
|
|
36
|
+
const [height, setHeight] = useState(0);
|
|
37
|
+
const [running, setRunning] = useState(shimmer && !theme.reducedMotion);
|
|
38
|
+
const wanted = useRef(shimmer);
|
|
39
|
+
useLayoutEffect(() => {
|
|
40
|
+
wanted.current = shimmer;
|
|
41
|
+
});
|
|
42
|
+
const [clock] = useState(() => new Animated.Value(0));
|
|
43
|
+
const sweep = sweepOf(theme, width);
|
|
44
|
+
// On: run at once. Off: the running sweep ends its turn, then stops (with reduced motion at once)
|
|
45
|
+
if (shimmer && !running && !theme.reducedMotion)
|
|
46
|
+
setRunning(true);
|
|
47
|
+
if (running && theme.reducedMotion)
|
|
48
|
+
setRunning(false);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!running || width <= 0)
|
|
51
|
+
return;
|
|
52
|
+
let animation = null;
|
|
53
|
+
let cancelled = false;
|
|
54
|
+
const turn = () => {
|
|
55
|
+
clock.setValue(0);
|
|
56
|
+
animation = Animated.timing(clock, { toValue: sweep.period, duration: sweep.period, easing: Easing.linear, useNativeDriver: nativeDriver, isInteraction: false });
|
|
57
|
+
animation.start(({ finished }) => {
|
|
58
|
+
if (!finished || cancelled)
|
|
59
|
+
return;
|
|
60
|
+
if (wanted.current)
|
|
61
|
+
turn();
|
|
62
|
+
else
|
|
63
|
+
setRunning(false);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
clock.setValue(0);
|
|
67
|
+
const timer = setTimeout(turn, sweep.delay);
|
|
68
|
+
return () => {
|
|
69
|
+
cancelled = true;
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
animation?.stop();
|
|
72
|
+
};
|
|
73
|
+
}, [running, width, sweep.period, sweep.delay, clock]);
|
|
74
|
+
const onLayout = (event) => {
|
|
75
|
+
const { width: w, height: h } = event.nativeEvent.layout;
|
|
76
|
+
setWidth((old) => (old === w ? old : w));
|
|
77
|
+
setHeight((old) => (old === h ? old : h));
|
|
78
|
+
};
|
|
79
|
+
const level = /^h([1-6])$/.exec(as)?.[1];
|
|
80
|
+
const heading = level ? { accessibilityRole: "header", ...(Platform.OS === "web" ? { "aria-level": Number(level) } : null) } : null;
|
|
81
|
+
const highlight = theme.value("light-dark(var(--gray-200), var(--gray-900))");
|
|
82
|
+
const highlightColor = typeof highlight === "string" ? highlight : theme.color(theme.scheme === "dark" ? "--gray-900" : "--gray-200");
|
|
83
|
+
const slice = sweep.band / SLICES;
|
|
84
|
+
const x = clock.interpolate({ inputRange: [0, sweep.pass, sweep.period], outputRange: [sweep.from, sweep.to, sweep.to], easing: sweep.easing });
|
|
85
|
+
// react-native-web reports the layout in whole pixels: a copy that narrow would wrap a line 193.1 wide, so it takes one more
|
|
86
|
+
const copyWidth = Platform.OS === "web" ? width + 1 : width;
|
|
87
|
+
return (_jsxs(View, { onLayout: onLayout, style: containerStyle, children: [_jsx(Text, { ...heading, ...text, style: style, children: children }), running && width > 0 ? (_jsx(Animated.View, { pointerEvents: "none", accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", "aria-hidden": true, style: [styles.band, { width: sweep.band, height, transform: [{ translateX: x }] }], children: Array.from({ length: SLICES }, (_, index) => {
|
|
88
|
+
// The gradient's share of the highlight at the slice's middle: none at the edges, all of it at the center
|
|
89
|
+
const share = 1 - Math.abs((2 * (index + 0.5)) / SLICES - 1);
|
|
90
|
+
return (_jsx(View, { style: [styles.slice, { left: index * slice, width: slice + 0.5, height, opacity: share }], children: _jsx(Animated.View, { style: [styles.copy, { left: -index * slice, width: copyWidth, transform: [{ translateX: Animated.multiply(x, -1) }] }], children: _jsx(Text, { ...text, numberOfLines: text.numberOfLines, style: [style, { color: highlightColor }], children: children }) }) }, index));
|
|
91
|
+
}) })) : null] }));
|
|
92
|
+
}
|
|
93
|
+
const styles = StyleSheet.create({
|
|
94
|
+
// The band and its slices are physical (left), as the web's background position: it sweeps left to right in either direction
|
|
95
|
+
band: { position: "absolute", top: 0, left: 0 },
|
|
96
|
+
slice: { position: "absolute", top: 0, overflow: "hidden" },
|
|
97
|
+
copy: { position: "absolute", top: 0 },
|
|
98
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { Animated, Platform, Pressable, StyleSheet, View } from "react-native";
|
|
4
|
+
import Minus from "../../icons/plex/Minus";
|
|
5
|
+
import Plus from "../../icons/plex/Plus";
|
|
6
|
+
import RemoveTrash from "../../icons/plex/RemoveTrash";
|
|
7
|
+
import { cssEase, useColorTransition } from "../../internal/transition";
|
|
8
|
+
import { mix, withAlpha } from "../../theme/material";
|
|
9
|
+
import { useTheme } from "../../theme/ThemeProvider";
|
|
10
|
+
import { Text } from "../Text/Text";
|
|
11
|
+
/** Delay before a held segment starts repeating, and the time between repeats, in ms. */
|
|
12
|
+
const REPEAT_DELAY = 400;
|
|
13
|
+
const REPEAT_INTERVAL = 100;
|
|
14
|
+
const decimals = (n) => {
|
|
15
|
+
const [, fraction = ""] = String(n).split(".");
|
|
16
|
+
return fraction.length;
|
|
17
|
+
};
|
|
18
|
+
/** The value one step away, clamped to the range or wrapped, without floating point drift. */
|
|
19
|
+
export const stepValue = (current, direction, { min, max, step, wrap }) => {
|
|
20
|
+
const precision = Math.max(decimals(step), decimals(min), decimals(current));
|
|
21
|
+
const next = Number((current + direction * step).toFixed(precision));
|
|
22
|
+
if (next > max)
|
|
23
|
+
return wrap ? min : max;
|
|
24
|
+
if (next < min)
|
|
25
|
+
return wrap ? max : min;
|
|
26
|
+
return next;
|
|
27
|
+
};
|
|
28
|
+
/** The first number of a `scale` token ("1.1667 1"): how much wider the minus is drawn than the plus. */
|
|
29
|
+
const scaleX = (value) => {
|
|
30
|
+
const first = typeof value === "number" ? value : parseFloat(String(value ?? ""));
|
|
31
|
+
return Number.isFinite(first) && first > 0 ? first : 1;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The look of the stepper. iOS: the phone layer's tokens, the native 93 x 32 capsule of two 46.5pt segments, each
|
|
35
|
+
* with its own fill (the ink at 6%), a 1 x 24 separator in the tertiary label, 22pt symbols; a pressed segment
|
|
36
|
+
* darkens (15%) at once and fades back in 150ms. Android has no stepper of its own: the same capsule in Material's
|
|
37
|
+
* roles, `surfaceContainerHighest` with the pressed state layer (`onSurface` at 10%), `onSurface` symbols at 38% when
|
|
38
|
+
* they cannot act, and a 48dp target.
|
|
39
|
+
*/
|
|
40
|
+
function lookOf(theme, showValue) {
|
|
41
|
+
const m = theme.material;
|
|
42
|
+
const android = theme.isAndroid;
|
|
43
|
+
const height = showValue ? theme.length("--stepper-value-height", 36) : theme.length("--stepper-height", 32);
|
|
44
|
+
const fill = android ? m.surfaceContainerHighest : theme.color(showValue ? "--stepper-value-background" : "--stepper-segment-background");
|
|
45
|
+
return {
|
|
46
|
+
height,
|
|
47
|
+
radius: showValue ? theme.length("--stepper-value-radius", 18) : theme.length("--stepper-radius", 16),
|
|
48
|
+
segmentWidth: showValue ? height : theme.length("--stepper-segment-width", 46.5),
|
|
49
|
+
icon: showValue ? theme.length("--stepper-value-icon-size", 20) : theme.length("--stepper-icon-size", 22),
|
|
50
|
+
minusScale: scaleX(theme.token("--stepper-decrement-icon-scale")),
|
|
51
|
+
target: android ? 48 : theme.length("--stepper-target-size", 44),
|
|
52
|
+
// With the value one fill lies under the whole capsule; without it each segment has its own
|
|
53
|
+
background: showValue ? fill : "transparent",
|
|
54
|
+
segment: showValue ? "transparent" : fill,
|
|
55
|
+
pressed: android
|
|
56
|
+
? showValue
|
|
57
|
+
? withAlpha(m.onSurface, 0.1)
|
|
58
|
+
: mix(m.onSurface, fill, 0.1)
|
|
59
|
+
: theme.color(showValue ? "--stepper-value-segment-background-active" : "--stepper-segment-background-active"),
|
|
60
|
+
// The kit's symbols are in the label ink, the color the phone layer gives --color-text (the tokens' own is the desktop gray)
|
|
61
|
+
iconColor: android ? m.onSurface : theme.colors.text,
|
|
62
|
+
iconDisabled: android ? withAlpha(m.onSurface, 0.38) : theme.color("--stepper-icon-disabled-color", theme.colors.labelTertiary),
|
|
63
|
+
separator: android ? m.outlineVariant : theme.color("--stepper-separator-color", theme.colors.labelTertiary),
|
|
64
|
+
separatorInset: theme.length("--stepper-separator-inset", 4),
|
|
65
|
+
value: {
|
|
66
|
+
color: android ? m.onSurface : theme.colors.text,
|
|
67
|
+
disabled: android ? withAlpha(m.onSurface, 0.38) : theme.color("--stepper-value-disabled-color", theme.colors.labelTertiary),
|
|
68
|
+
minWidth: theme.length("--stepper-value-min-width", 24),
|
|
69
|
+
fontSize: theme.length("--stepper-value-font-size", 15),
|
|
70
|
+
lineHeight: theme.length("--stepper-value-line-height", 20),
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Two segments that decrease and increase a number, as the iOS 26 stepper (UIStepper) does, and @plexui/ui's
|
|
76
|
+
* Stepper. It shows no value of its own (`showValue` puts it between the segments, as a cart's quantity): name it
|
|
77
|
+
* with `aria-label`. A press steps at once; held, it repeats after 400ms every 100ms. For a screen reader it is one
|
|
78
|
+
* adjustable element: swipe up or down to step (Android: the increment and decrement actions). On the web it is a
|
|
79
|
+
* spin button: Up and Down step, Page Up and Page Down step ten times, Home and End go to the range ends.
|
|
80
|
+
*/
|
|
81
|
+
export function Stepper({ value: valueProp, defaultValue, onChange, min = 0, max = 100, step = 1, wrap = false, autoRepeat = true, disabled = false, pill = true, showValue = false, decrementLabel = "Decrease", incrementLabel = "Increase", onRemove, removeLabel = "Remove", "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, accessibilityLabel, testID, style, }) {
|
|
82
|
+
const theme = useTheme();
|
|
83
|
+
const look = lookOf(theme, showValue);
|
|
84
|
+
const [internalValue, setInternalValue] = useState(() => defaultValue ?? min);
|
|
85
|
+
const value = valueProp ?? internalValue;
|
|
86
|
+
// Timers and handlers read the latest props through refs; an uncontrolled step advances the value at once
|
|
87
|
+
const valueRef = useRef(value);
|
|
88
|
+
const latest = useRef({ onChange, onRemove, min, max, step, wrap, disabled, controlled: valueProp !== undefined });
|
|
89
|
+
useLayoutEffect(() => {
|
|
90
|
+
valueRef.current = value;
|
|
91
|
+
latest.current = { onChange, onRemove, min, max, step, wrap, disabled, controlled: valueProp !== undefined };
|
|
92
|
+
});
|
|
93
|
+
const timer = useRef(undefined);
|
|
94
|
+
const [pressed, setPressed] = useState(null);
|
|
95
|
+
const commit = useCallback((next) => {
|
|
96
|
+
if (next === valueRef.current)
|
|
97
|
+
return false;
|
|
98
|
+
// A controlled value changes only when the owner passes it back
|
|
99
|
+
if (!latest.current.controlled) {
|
|
100
|
+
valueRef.current = next;
|
|
101
|
+
setInternalValue(next);
|
|
102
|
+
}
|
|
103
|
+
latest.current.onChange?.(next);
|
|
104
|
+
return true;
|
|
105
|
+
}, []);
|
|
106
|
+
const stepBy = useCallback((direction, times = 1) => {
|
|
107
|
+
let next = valueRef.current;
|
|
108
|
+
for (let i = 0; i < times; i++)
|
|
109
|
+
next = stepValue(next, direction, latest.current);
|
|
110
|
+
return commit(next);
|
|
111
|
+
}, [commit]);
|
|
112
|
+
const stopRepeat = useCallback(() => {
|
|
113
|
+
clearTimeout(timer.current);
|
|
114
|
+
timer.current = undefined;
|
|
115
|
+
setPressed(null);
|
|
116
|
+
}, []);
|
|
117
|
+
useEffect(() => stopRepeat, [stopRepeat]);
|
|
118
|
+
const atMin = !wrap && value <= min;
|
|
119
|
+
const atMax = !wrap && value >= max;
|
|
120
|
+
// At the minimum the decrement segment removes the item, when the owner can remove it
|
|
121
|
+
const removes = atMin && onRemove !== undefined;
|
|
122
|
+
const startPress = (segment) => {
|
|
123
|
+
if (disabled)
|
|
124
|
+
return;
|
|
125
|
+
if (segment === "decrement" && removes) {
|
|
126
|
+
setPressed(segment);
|
|
127
|
+
latest.current.onRemove?.();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (segment === "decrement" ? atMin : atMax)
|
|
131
|
+
return;
|
|
132
|
+
setPressed(segment);
|
|
133
|
+
const direction = segment === "decrement" ? -1 : 1;
|
|
134
|
+
stepBy(direction);
|
|
135
|
+
if (!autoRepeat)
|
|
136
|
+
return;
|
|
137
|
+
const repeat = () => {
|
|
138
|
+
if (latest.current.disabled || !stepBy(direction))
|
|
139
|
+
return stopRepeat();
|
|
140
|
+
timer.current = setTimeout(repeat, REPEAT_INTERVAL);
|
|
141
|
+
};
|
|
142
|
+
timer.current = setTimeout(repeat, REPEAT_DELAY);
|
|
143
|
+
};
|
|
144
|
+
const keys = Platform.OS === "web"
|
|
145
|
+
? {
|
|
146
|
+
tabIndex: (disabled ? -1 : 0),
|
|
147
|
+
onKeyDown: (raw) => {
|
|
148
|
+
const event = raw;
|
|
149
|
+
if (disabled || event.altKey || event.ctrlKey || event.metaKey)
|
|
150
|
+
return;
|
|
151
|
+
const actions = {
|
|
152
|
+
ArrowUp: () => stepBy(1),
|
|
153
|
+
ArrowDown: () => stepBy(-1),
|
|
154
|
+
PageUp: () => stepBy(1, 10),
|
|
155
|
+
PageDown: () => stepBy(-1, 10),
|
|
156
|
+
Home: () => commit(min),
|
|
157
|
+
End: () => commit(max),
|
|
158
|
+
// At the minimum Delete or Backspace removes, as the trash can does; the arrows never do
|
|
159
|
+
...(removes && { Delete: () => latest.current.onRemove?.(), Backspace: () => latest.current.onRemove?.() }),
|
|
160
|
+
};
|
|
161
|
+
const action = actions[event.key];
|
|
162
|
+
if (!action)
|
|
163
|
+
return;
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
action();
|
|
166
|
+
},
|
|
167
|
+
role: "spinbutton",
|
|
168
|
+
"aria-keyshortcuts": removes ? "Delete" : undefined,
|
|
169
|
+
}
|
|
170
|
+
: {};
|
|
171
|
+
const onAccessibilityAction = (event) => {
|
|
172
|
+
if (disabled)
|
|
173
|
+
return;
|
|
174
|
+
const name = event.nativeEvent.actionName;
|
|
175
|
+
if (name === "increment")
|
|
176
|
+
stepBy(1);
|
|
177
|
+
else if (name === "decrement")
|
|
178
|
+
stepBy(-1);
|
|
179
|
+
else if (name === "remove")
|
|
180
|
+
latest.current.onRemove?.();
|
|
181
|
+
};
|
|
182
|
+
const segment = (kind) => {
|
|
183
|
+
const remove = kind === "decrement" && removes;
|
|
184
|
+
const atEnd = !remove && (kind === "decrement" ? atMin : atMax);
|
|
185
|
+
return (_jsx(StepperSegment, { kind: kind, remove: remove, inactive: atEnd || disabled, disabled: disabled, pressed: pressed === kind && !disabled, label: remove ? removeLabel : kind === "decrement" ? decrementLabel : incrementLabel, look: look, showValue: showValue, pill: pill, reducedMotion: theme.reducedMotion, onPressIn: () => startPress(kind), onPressOut: stopRepeat }, kind));
|
|
186
|
+
};
|
|
187
|
+
const radius = pill ? look.height / 2 : look.radius;
|
|
188
|
+
return (_jsxs(View, { ...keys, accessible: true, accessibilityRole: "adjustable", accessibilityLabel: accessibilityLabel ?? ariaLabel, "aria-labelledby": ariaLabelledBy, accessibilityValue: { min, max, now: value }, accessibilityState: { disabled }, accessibilityActions: [{ name: "increment" }, { name: "decrement" }, ...(removes ? [{ name: "remove", label: removeLabel }] : [])], onAccessibilityAction: onAccessibilityAction, "aria-valuemin": min, "aria-valuemax": max, "aria-valuenow": value, "aria-disabled": disabled || undefined, testID: testID, style: [styles.root, { height: look.height, borderRadius: radius, backgroundColor: look.background }, style], children: [segment("decrement"), showValue ? null : (_jsx(View, { pointerEvents: "none", style: [
|
|
189
|
+
styles.separator,
|
|
190
|
+
{
|
|
191
|
+
top: look.separatorInset,
|
|
192
|
+
height: look.height - 2 * look.separatorInset,
|
|
193
|
+
start: look.segmentWidth - 0.5,
|
|
194
|
+
backgroundColor: look.separator,
|
|
195
|
+
},
|
|
196
|
+
] })), showValue ? (
|
|
197
|
+
// The adjustable element already reads its value, so the shown one is not read again
|
|
198
|
+
_jsx(View, { pointerEvents: "none", importantForAccessibility: "no-hide-descendants", accessibilityElementsHidden: true, style: [styles.value, { minWidth: look.value.minWidth }], children: _jsx(Text, { weight: "medium", tabularNums: true, numberOfLines: 1, color: disabled ? look.value.disabled : look.value.color, style: { fontSize: look.value.fontSize, lineHeight: look.value.lineHeight }, children: value }) })) : null, segment("increment")] }));
|
|
199
|
+
}
|
|
200
|
+
/** A segment: its fill (the pressed one at once, back in 150ms), its symbol and a target of 44pt (48dp) in height. */
|
|
201
|
+
function StepperSegment({ kind, remove, inactive, disabled, pressed, label, look, showValue, pill, reducedMotion, onPressIn, onPressOut }) {
|
|
202
|
+
const fill = useColorTransition(pressed ? look.pressed : look.segment, { duration: pressed ? 0 : 150, easing: cssEase, reducedMotion });
|
|
203
|
+
const Icon = remove ? RemoveTrash : kind === "decrement" ? Minus : Plus;
|
|
204
|
+
const radius = pill ? look.height / 2 : look.radius;
|
|
205
|
+
const corners = showValue
|
|
206
|
+
? { borderRadius: radius }
|
|
207
|
+
: kind === "decrement"
|
|
208
|
+
? { borderTopStartRadius: radius, borderBottomStartRadius: radius }
|
|
209
|
+
: { borderTopEndRadius: radius, borderBottomEndRadius: radius };
|
|
210
|
+
const slop = Math.max(0, (look.target - look.height) / 2);
|
|
211
|
+
return (_jsx(Pressable, { accessible: false, focusable: false, disabled: disabled, "aria-label": label, hitSlop: { top: slop, bottom: slop }, onPressIn: onPressIn, onPressOut: onPressOut, style: styles.segmentTarget, children: _jsx(Animated.View, { style: [styles.segment, corners, { width: look.segmentWidth, height: look.height, backgroundColor: fill }], children: _jsx(Icon, { size: look.icon, color: inactive ? look.iconDisabled : look.iconColor, style: kind === "decrement" && !remove && look.minusScale !== 1 ? { transform: [{ scaleX: look.minusScale }] } : undefined }) }) }));
|
|
212
|
+
}
|
|
213
|
+
const styles = StyleSheet.create({
|
|
214
|
+
root: { flexDirection: "row", alignItems: "stretch", alignSelf: "flex-start", flexShrink: 0 },
|
|
215
|
+
separator: { position: "absolute", width: 1, borderRadius: 1 },
|
|
216
|
+
segmentTarget: { flexShrink: 0 },
|
|
217
|
+
segment: { alignItems: "center", justifyContent: "center" },
|
|
218
|
+
value: { alignItems: "center", justifyContent: "center", flexShrink: 0 },
|
|
219
|
+
});
|