@delacour/react-native-ui 0.1.0-alpha.20261009010834 → 0.1.0-alpha.20261009011200
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/package.json +4 -3
- package/src/components/slide-button/AGENTS.md +122 -0
- package/src/components/slide-button/index.ts +43 -0
- package/src/components/slide-button/slide-button-label.tsx +70 -0
- package/src/components/slide-button/slide-button-thumb.tsx +93 -0
- package/src/components/slide-button/slide-button.context.tsx +76 -0
- package/src/components/slide-button/slide-button.tsx +457 -0
- package/src/components/slide-button/slide-button.variants.test.ts +288 -0
- package/src/components/slide-button/slide-button.variants.ts +324 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.20261009011200",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
"./screen": "./src/components/screen/index.ts",
|
|
70
70
|
"./separator": "./src/components/separator/index.ts",
|
|
71
71
|
"./skeleton": "./src/components/skeleton/index.ts",
|
|
72
|
+
"./slide-button": "./src/components/slide-button/index.ts",
|
|
72
73
|
"./slider": "./src/components/slider/index.ts",
|
|
73
74
|
"./spinner": "./src/components/spinner/index.ts",
|
|
74
75
|
"./stack-card": "./src/components/stack-card/index.ts",
|
|
@@ -112,8 +113,8 @@
|
|
|
112
113
|
},
|
|
113
114
|
"peerDependencies": {
|
|
114
115
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
115
|
-
"@delacour/react-native-bottom-sheet": "0.1.0-alpha.
|
|
116
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
116
|
+
"@delacour/react-native-bottom-sheet": "0.1.0-alpha.20261009011200",
|
|
117
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20261009011200",
|
|
117
118
|
"@legendapp/list": ">=3.3",
|
|
118
119
|
"expo-linear-gradient": ">=15",
|
|
119
120
|
"expo-router": ">=57",
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# SlideButton
|
|
2
|
+
|
|
3
|
+
A control confirmed by dragging a handle across a rail — "Slide to ship", "Slide
|
|
4
|
+
to transfer $240". For an action that deserves deliberation where a dialog would
|
|
5
|
+
be too much ceremony. Compound root plus `SlideButton.Label` and
|
|
6
|
+
`SlideButton.Thumb`.
|
|
7
|
+
|
|
8
|
+
`import { SlideButton } from "@delacour/react-native-ui/slide-button";`
|
|
9
|
+
|
|
10
|
+
## Files
|
|
11
|
+
|
|
12
|
+
| File | What it holds |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `index.ts` | → `@delacour/react-native-ui/slide-button` |
|
|
15
|
+
| `slide-button.tsx` | Root, the gesture, the internal trail, and the `Object.assign` compound surface |
|
|
16
|
+
| `slide-button-label.tsx` | `SlideButton.Label` |
|
|
17
|
+
| `slide-button-thumb.tsx` | `SlideButton.Thumb`, and the internal chevron-to-tick glyph |
|
|
18
|
+
| `slide-button.context.tsx` | `SlideButtonContext`, `useSlideButton` |
|
|
19
|
+
| `slide-button.variants.ts` | The slotted `tv()`, the motion constants and the pure resolvers |
|
|
20
|
+
| `slide-button.variants.test.ts` | |
|
|
21
|
+
|
|
22
|
+
There is no `slide-button.types.ts`: every prop type has exactly one consumer.
|
|
23
|
+
|
|
24
|
+
## Design
|
|
25
|
+
|
|
26
|
+
- **Variants**: `secondary` (default), `destructive`, `success`. **Sizes**:
|
|
27
|
+
`sm`, `md`, `lg`. Plus `isFullWidth` and `isDisabled`.
|
|
28
|
+
- **There is no `primary` variant.** The handle is neutral, so a primary rail
|
|
29
|
+
with a neutral handle draws the same picture as `secondary` in any theme whose
|
|
30
|
+
primary sits close to its foreground — the default palette included. Two names
|
|
31
|
+
for one look is a choice a caller cannot see the difference in.
|
|
32
|
+
- **The rail is a button's box.** `h-button-*` and `rounded-button-*` at each
|
|
33
|
+
size, so a slide button stacks level with a `Button` or a progress button in
|
|
34
|
+
the same column. A test pins the classes; it names a TODO for the category
|
|
35
|
+
lead to compare against `progressButtonVariants` directly once that lands.
|
|
36
|
+
- **The handle is neutral in every variant** — `bg-background` with a
|
|
37
|
+
`border-border` hairline, inset 4pt, about 1.6× as wide as it is tall. Only the
|
|
38
|
+
rail, the trail and the label carry the variant. A coloured handle on a
|
|
39
|
+
coloured trail would disappear into its own wake.
|
|
40
|
+
- **The handle's width is measured, not tabulated.** `resolveSlideHandleWidth`
|
|
41
|
+
reads the rail's laid-out height, so a consumer who retunes
|
|
42
|
+
`--spacing-button-*` keeps the handle in proportion.
|
|
43
|
+
- **The trail ends under the handle's middle, not at its rear edge.** A stadium's
|
|
44
|
+
rear is a curve; a trail stopping at the straight edge leaves a sliver of rail
|
|
45
|
+
above and below it. Ending under the handle reads as one shape — the handle
|
|
46
|
+
dragging its colour along — while still sharing an edge with it as seen.
|
|
47
|
+
- **The trail is invisible at rest.** It fades in over the first 8pt of travel
|
|
48
|
+
(`resolveSlideTrailOpacity`). Ending under the handle's middle, it would
|
|
49
|
+
otherwise peek out behind the resting handle's rear curve — a coloured sliver
|
|
50
|
+
that makes an untouched control look half-started.
|
|
51
|
+
- **The label changes colour where the trail covers it.** A second copy in the
|
|
52
|
+
trail's foreground (`labelOnTrail`) is clipped to the trail's width
|
|
53
|
+
(`resolveSlideTrailWidth`, shared with the trail). A state variant's trail is
|
|
54
|
+
its full colour, and the soft label colour on it is the same hue on the same
|
|
55
|
+
hue: the words vanished exactly where the trail reached them.
|
|
56
|
+
- **The label keeps a handle's width clear on both sides**
|
|
57
|
+
(`resolveSlideLabelGutter`). Symmetric, so it stays centred; a long label
|
|
58
|
+
truncates before it runs under the resting handle.
|
|
59
|
+
- **The label is centred in the whole rail and never fades or moves.** The handle
|
|
60
|
+
passes over it. A label that faded would leave the control saying nothing for
|
|
61
|
+
the second half of the gesture, the half where the hand most wants to know what
|
|
62
|
+
it is confirming.
|
|
63
|
+
- **The handle tracks the finger 1:1; only the release is sprung.** Any easing on
|
|
64
|
+
the way out would make the handle lag the thumb, which reads as resistance.
|
|
65
|
+
- **A release confirms on look-ahead *and* reach.** `resolveSlideRelease`
|
|
66
|
+
projects the release `velocity × 0.08s` ahead; it confirms only when that
|
|
67
|
+
projection reaches the threshold **and** the hand itself got within 0.25 of it.
|
|
68
|
+
The second condition is what stops a hard flick from halfway: the habit-driven
|
|
69
|
+
swipe this control exists to refuse.
|
|
70
|
+
- **`threshold={1}` has no shortcut at all.** Only a release within half a point
|
|
71
|
+
of the far end confirms. That is the setting for something irreversible.
|
|
72
|
+
- **`onComplete` fires the moment the release confirms**, alongside the handle's
|
|
73
|
+
spring to the end — not when the spring settles. A spring can be interrupted
|
|
74
|
+
(an unmount, a re-layout), and a confirmation that hung on its callback could
|
|
75
|
+
be silently lost.
|
|
76
|
+
- **A requested completion holds the handle at the end.** Controlled, the release
|
|
77
|
+
reports `onCompletedChange(true)` and the handle waits at the end until
|
|
78
|
+
`isCompleted` answers. Springing back while the caller's work is in flight
|
|
79
|
+
would read as a refusal. `isCompleted` going false springs it home; true keeps
|
|
80
|
+
it there with the tick.
|
|
81
|
+
- **A rejected promise takes the handle home.** `onComplete` may return a
|
|
82
|
+
promise. If it rejects, the button reports `onCompletedChange(false)` and the
|
|
83
|
+
handle springs back, so a failed request never looks confirmed and a
|
|
84
|
+
controlled caller has a way out of the pending state that does not need a
|
|
85
|
+
`true` it never meant.
|
|
86
|
+
- **Disabled refuses the drag outright.** The handle does not move at all. A
|
|
87
|
+
handle that followed and sprang back would read as a failed slide rather than
|
|
88
|
+
a refused one.
|
|
89
|
+
- **Disabled still claims the drag.** The pan stays enabled and every callback
|
|
90
|
+
returns early. A disabled recognizer lets the touch fall through to whatever
|
|
91
|
+
is behind it — inside a stack with a full-screen back swipe, dragging a
|
|
92
|
+
disabled slide navigated back. Found on the simulator.
|
|
93
|
+
- **One `Gesture.Pan()` on the rail, `activeOffsetX ±8`, `failOffsetY ±12`.** A
|
|
94
|
+
vertical scroll that starts on the rail fails the pan first, so the control
|
|
95
|
+
survives inside a vertical `ScrollView`. A cancelled pan returns home.
|
|
96
|
+
- **RTL flips with `I18nManager.isRTL`.** Every positioned part uses logical
|
|
97
|
+
insets (`start-*`), the translation and the drag are multiplied by `-1`, and
|
|
98
|
+
the chevron becomes `IconChevronLeft`.
|
|
99
|
+
- **Haptics are off by default.** `haptic` names the tick played the first time
|
|
100
|
+
a drag crosses the threshold (re-armed if it drops back under); a `success`
|
|
101
|
+
knock follows on commit. Both are worklet calls, in the frame that earned them.
|
|
102
|
+
- **Reduced motion keeps the travel.** Springs become a 180ms `withTiming`. The
|
|
103
|
+
handle still moves to the end: a confirmation control that showed nothing on
|
|
104
|
+
confirming would be broken.
|
|
105
|
+
- **The chevron crosses into a tick** — opacity and a quarter turn, 180ms, off
|
|
106
|
+
one shared value. Children on `SlideButton.Thumb` replace both marks.
|
|
107
|
+
- **Screen readers confirm with one action.** The rail is a `button` whose label
|
|
108
|
+
is the `SlideButton.Label` text (or an explicit `accessibilityLabel`), with an
|
|
109
|
+
`activate` action labelled by `accessibilityActionLabel` — default "Confirm";
|
|
110
|
+
pass an imperative that names the outcome, "Ship order". Activating moves the
|
|
111
|
+
handle to the end and fires `onComplete`. `accessibilityState` carries
|
|
112
|
+
`disabled` and `checked: isCompleted`.
|
|
113
|
+
- **The thumb is drawn last however it was written**, and composed in when the
|
|
114
|
+
children hold none — `Switch`'s rule. Part detection checks `displayName` as
|
|
115
|
+
well as identity, for the reason `Switch` documents.
|
|
116
|
+
|
|
117
|
+
## Out of scope
|
|
118
|
+
|
|
119
|
+
- A `primary` variant — see above.
|
|
120
|
+
- A vertical slide.
|
|
121
|
+
- A label that changes on completion. The caller swaps the label's text from
|
|
122
|
+
`isCompleted` if they want one.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export { SlideButton, type SlideButtonProps } from "./slide-button";
|
|
2
|
+
export {
|
|
3
|
+
type SlideButtonContextValue,
|
|
4
|
+
SlideButtonProvider,
|
|
5
|
+
useSlideButton,
|
|
6
|
+
useSlideButtonContext,
|
|
7
|
+
} from "./slide-button.context";
|
|
8
|
+
export {
|
|
9
|
+
isSlideArmed,
|
|
10
|
+
resolveSlideHandleWidth,
|
|
11
|
+
resolveSlideLabelGutter,
|
|
12
|
+
resolveSlideRelease,
|
|
13
|
+
resolveSlideThreshold,
|
|
14
|
+
resolveSlideTrailOpacity,
|
|
15
|
+
resolveSlideTrailWidth,
|
|
16
|
+
resolveSlideTravel,
|
|
17
|
+
SLIDE_BUTTON_ACTIVE_OFFSET_X,
|
|
18
|
+
SLIDE_BUTTON_ARM_SLACK,
|
|
19
|
+
SLIDE_BUTTON_DEFAULT_AUTO_RESET_MS,
|
|
20
|
+
SLIDE_BUTTON_DEFAULT_SIZE,
|
|
21
|
+
SLIDE_BUTTON_DEFAULT_THRESHOLD,
|
|
22
|
+
SLIDE_BUTTON_DEFAULT_VARIANT,
|
|
23
|
+
SLIDE_BUTTON_FAIL_OFFSET_Y,
|
|
24
|
+
SLIDE_BUTTON_FAR_END_SLOP,
|
|
25
|
+
SLIDE_BUTTON_GLYPH_MS,
|
|
26
|
+
SLIDE_BUTTON_GLYPH_TOKEN,
|
|
27
|
+
SLIDE_BUTTON_HANDLE_RATIO,
|
|
28
|
+
SLIDE_BUTTON_INSET,
|
|
29
|
+
SLIDE_BUTTON_LOOKAHEAD,
|
|
30
|
+
SLIDE_BUTTON_MIN_THRESHOLD,
|
|
31
|
+
SLIDE_BUTTON_REDUCED_MOTION_MS,
|
|
32
|
+
SLIDE_BUTTON_SIZES,
|
|
33
|
+
SLIDE_BUTTON_SPRING,
|
|
34
|
+
SLIDE_BUTTON_TRAIL_FADE,
|
|
35
|
+
SLIDE_BUTTON_VARIANTS,
|
|
36
|
+
type SlideButtonSize,
|
|
37
|
+
type SlideButtonVariant,
|
|
38
|
+
type SlideButtonVariantProps,
|
|
39
|
+
type SlideRelease,
|
|
40
|
+
slideButtonVariants,
|
|
41
|
+
} from "./slide-button.variants";
|
|
42
|
+
export type { SlideButtonLabelProps } from "./slide-button-label";
|
|
43
|
+
export type { SlideButtonThumbProps } from "./slide-button-thumb";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from "react";
|
|
2
|
+
import Animated, { useAnimatedStyle } from "react-native-reanimated";
|
|
3
|
+
import { Text, type TextProps } from "../text";
|
|
4
|
+
import { useSlideButtonPart } from "./slide-button.context";
|
|
5
|
+
import {
|
|
6
|
+
resolveSlideLabelGutter,
|
|
7
|
+
resolveSlideTrailOpacity,
|
|
8
|
+
resolveSlideTrailWidth,
|
|
9
|
+
SLIDE_BUTTON_INSET,
|
|
10
|
+
slideButtonVariants,
|
|
11
|
+
} from "./slide-button.variants";
|
|
12
|
+
|
|
13
|
+
export type SlideButtonLabelProps = Omit<TextProps, "children" | "className"> & {
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
className?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What the slide does, centred in the whole rail.
|
|
20
|
+
*
|
|
21
|
+
* **It does not fade or move.** The handle passes over it. A label that faded as
|
|
22
|
+
* the handle advanced would leave the control saying nothing for the second half
|
|
23
|
+
* of the gesture — the half where the hand most wants to know what it is about to
|
|
24
|
+
* confirm.
|
|
25
|
+
*
|
|
26
|
+
* **It changes colour where the trail covers it.** A second copy, in the trail's
|
|
27
|
+
* own foreground, is clipped to the trail's width. A state variant's trail is
|
|
28
|
+
* its full colour, and its soft label colour on that fill is the same hue on the
|
|
29
|
+
* same hue — the words would vanish exactly where the trail reached them.
|
|
30
|
+
*
|
|
31
|
+
* **It keeps a handle's width clear on both sides.** Symmetric, so it stays
|
|
32
|
+
* centred in the rail; wide enough that a long label truncates before it runs
|
|
33
|
+
* under the resting handle.
|
|
34
|
+
*
|
|
35
|
+
* Its text is also the rail's accessibility label, read off by the root, so the
|
|
36
|
+
* screen reader announces the same words the eye reads. The rail is the
|
|
37
|
+
* accessible element, so neither copy is read on its own.
|
|
38
|
+
*/
|
|
39
|
+
export function SlideButtonLabel({ className, ...props }: SlideButtonLabelProps): ReactElement {
|
|
40
|
+
const { variant, size, handleWidth, offset, travel } = useSlideButtonPart("SlideButton.Label");
|
|
41
|
+
const slots = slideButtonVariants({ size, variant });
|
|
42
|
+
|
|
43
|
+
const frameStyle = useAnimatedStyle(() => ({
|
|
44
|
+
paddingHorizontal: resolveSlideLabelGutter({ handleWidth: handleWidth.value, inset: SLIDE_BUTTON_INSET }),
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
const clipStyle = useAnimatedStyle(() => ({
|
|
48
|
+
opacity: handleWidth.value > 0 ? resolveSlideTrailOpacity(offset.value) : 0,
|
|
49
|
+
width: resolveSlideTrailWidth({ handleWidth: handleWidth.value, inset: SLIDE_BUTTON_INSET, offset: offset.value }),
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
const onTrailStyle = useAnimatedStyle(() => ({
|
|
53
|
+
paddingHorizontal: resolveSlideLabelGutter({ handleWidth: handleWidth.value, inset: SLIDE_BUTTON_INSET }),
|
|
54
|
+
width: travel.value + handleWidth.value + SLIDE_BUTTON_INSET * 2,
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<Animated.View className={slots.labelFrame()} pointerEvents="none" style={frameStyle}>
|
|
60
|
+
<Text className={slots.label({ className })} numberOfLines={1} {...props} />
|
|
61
|
+
</Animated.View>
|
|
62
|
+
<Animated.View className={slots.labelClip()} pointerEvents="none" style={clipStyle}>
|
|
63
|
+
<Animated.View className={slots.labelOnTrailFrame()} style={onTrailStyle}>
|
|
64
|
+
<Text className={slots.labelOnTrail({ className })} numberOfLines={1} {...props} />
|
|
65
|
+
</Animated.View>
|
|
66
|
+
</Animated.View>
|
|
67
|
+
</>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
SlideButtonLabel.displayName = "DelacourUI.SlideButton.Label";
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode, useMemo } from "react";
|
|
2
|
+
import type { ViewProps } from "react-native";
|
|
3
|
+
import Animated, { useAnimatedStyle } from "react-native-reanimated";
|
|
4
|
+
import { useThemeColor } from "../../hooks/use-theme-color";
|
|
5
|
+
import { IconCheckmark1Small, IconChevronLeft, IconChevronRight } from "../../icons/central";
|
|
6
|
+
import { Icon, IconDefaultsProvider } from "../icon";
|
|
7
|
+
import { useSlideButtonPart } from "./slide-button.context";
|
|
8
|
+
import { SLIDE_BUTTON_GLYPH_TOKEN, slideButtonVariants } from "./slide-button.variants";
|
|
9
|
+
|
|
10
|
+
export type SlideButtonThumbProps = Omit<ViewProps, "children" | "style"> & {
|
|
11
|
+
/** Replaces the chevron. An `Icon` inherits the handle's glyph size and colour. */
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
className?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The handle the finger drags.
|
|
18
|
+
*
|
|
19
|
+
* Composed in automatically when the children hold none, and drawn last however
|
|
20
|
+
* the children were ordered, so it always passes *over* the label.
|
|
21
|
+
*
|
|
22
|
+
* **It holds no gesture of its own.** One `Gesture.Pan()` on the rail drives it,
|
|
23
|
+
* so a drag that lands on the rail beside the handle still moves it — the same
|
|
24
|
+
* trade `Switch.Thumb` and `Slider.Thumb` make.
|
|
25
|
+
*
|
|
26
|
+
* **One animated style, one node**: width, visibility and position together,
|
|
27
|
+
* because two `useAnimatedStyle`s on one view fight for the same props.
|
|
28
|
+
*
|
|
29
|
+
* Invisible until the rail has been measured — a handle drawn before then has no
|
|
30
|
+
* width and sits at a garbage offset for a frame.
|
|
31
|
+
*
|
|
32
|
+
* With no children it draws a chevron pointing the way it travels (mirrored
|
|
33
|
+
* under RTL), which crosses into a tick when the slide confirms. Children replace
|
|
34
|
+
* both: a custom glyph is the caller's to change.
|
|
35
|
+
*/
|
|
36
|
+
export function SlideButtonThumb({ className, children, ...props }: SlideButtonThumbProps): ReactElement {
|
|
37
|
+
const { size, variant, isRTL, offset, handleWidth } = useSlideButtonPart("SlideButton.Thumb");
|
|
38
|
+
const direction = isRTL ? -1 : 1;
|
|
39
|
+
|
|
40
|
+
const thumbStyle = useAnimatedStyle(() => ({
|
|
41
|
+
opacity: handleWidth.value > 0 ? 1 : 0,
|
|
42
|
+
transform: [{ translateX: offset.value * direction }],
|
|
43
|
+
width: handleWidth.value,
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
const slots = slideButtonVariants({ size, variant });
|
|
47
|
+
const glyphColor = useThemeColor(SLIDE_BUTTON_GLYPH_TOKEN);
|
|
48
|
+
const glyphClassName = slots.thumbGlyph();
|
|
49
|
+
const iconDefaults = useMemo(
|
|
50
|
+
() => ({ className: glyphClassName, color: glyphColor ?? "" }),
|
|
51
|
+
[glyphClassName, glyphColor]
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<Animated.View className={slots.thumb({ className })} style={thumbStyle} {...props}>
|
|
56
|
+
<IconDefaultsProvider value={iconDefaults}>{children ?? <SlideButtonGlyph />}</IconDefaultsProvider>
|
|
57
|
+
</Animated.View>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
SlideButtonThumb.displayName = "DelacourUI.SlideButton.Thumb";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The default glyph: a chevron toward the travel that crosses into a tick.
|
|
64
|
+
*
|
|
65
|
+
* Both marks are always mounted, stacked, and swap by opacity and a quarter turn
|
|
66
|
+
* off one shared value — so the crossing runs on the UI thread and never waits on
|
|
67
|
+
* a re-render to start.
|
|
68
|
+
*/
|
|
69
|
+
function SlideButtonGlyph(): ReactElement {
|
|
70
|
+
const { isRTL, glyph } = useSlideButtonPart("SlideButton.Thumb");
|
|
71
|
+
const direction = isRTL ? -1 : 1;
|
|
72
|
+
|
|
73
|
+
const chevronStyle = useAnimatedStyle(() => ({
|
|
74
|
+
opacity: 1 - glyph.value,
|
|
75
|
+
transform: [{ rotate: `${glyph.value * 90 * direction}deg` }],
|
|
76
|
+
}));
|
|
77
|
+
const tickStyle = useAnimatedStyle(() => ({
|
|
78
|
+
opacity: glyph.value,
|
|
79
|
+
transform: [{ rotate: `${(glyph.value - 1) * 90 * direction}deg` }],
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<>
|
|
84
|
+
<Animated.View className="absolute" style={chevronStyle}>
|
|
85
|
+
<Icon icon={isRTL ? IconChevronLeft : IconChevronRight} />
|
|
86
|
+
</Animated.View>
|
|
87
|
+
<Animated.View className="absolute" style={tickStyle}>
|
|
88
|
+
<Icon icon={IconCheckmark1Small} />
|
|
89
|
+
</Animated.View>
|
|
90
|
+
</>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
SlideButtonGlyph.displayName = "DelacourUI.SlideButton.Thumb.Glyph";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createContext, type ReactElement, type ReactNode, use } from "react";
|
|
2
|
+
import type { SharedValue } from "react-native-reanimated";
|
|
3
|
+
import type { SlideButtonSize, SlideButtonVariant } from "./slide-button.variants";
|
|
4
|
+
|
|
5
|
+
export type SlideButtonContextValue = {
|
|
6
|
+
variant: SlideButtonVariant;
|
|
7
|
+
size: SlideButtonSize;
|
|
8
|
+
/** Whether the slide has been confirmed, on the **JS thread** — what accessibility reads. */
|
|
9
|
+
isCompleted: boolean;
|
|
10
|
+
isDisabled: boolean;
|
|
11
|
+
/** Whether the layout runs right to left, so the handle travels leftwards. */
|
|
12
|
+
isRTL: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* How far the handle has travelled from its resting end, in points, on the
|
|
15
|
+
* **UI thread**. Always positive — the direction is applied where it is drawn.
|
|
16
|
+
*/
|
|
17
|
+
offset: SharedValue<number>;
|
|
18
|
+
/** How far the handle can travel, in points. `0` until the rail has been measured. */
|
|
19
|
+
travel: SharedValue<number>;
|
|
20
|
+
/** The handle's width, from the rail's measured height. `0` until then. */
|
|
21
|
+
handleWidth: SharedValue<number>;
|
|
22
|
+
/** The chevron-to-tick crossing: `0` shows the chevron, `1` the tick. */
|
|
23
|
+
glyph: SharedValue<number>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const SlideButtonContext = createContext<SlideButtonContextValue | null>(null);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Supplies one slide button's state and shared values to its own parts.
|
|
30
|
+
*
|
|
31
|
+
* Lives in its own module, importing nothing but React and types, so a part can
|
|
32
|
+
* read it without importing `./slide-button` — that import would close a cycle.
|
|
33
|
+
*/
|
|
34
|
+
export function SlideButtonProvider({
|
|
35
|
+
value,
|
|
36
|
+
children,
|
|
37
|
+
}: {
|
|
38
|
+
value: SlideButtonContextValue;
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
}): ReactElement {
|
|
41
|
+
return <SlideButtonContext value={value}>{children}</SlideButtonContext>;
|
|
42
|
+
}
|
|
43
|
+
SlideButtonProvider.displayName = "DelacourUI.SlideButton.Provider";
|
|
44
|
+
|
|
45
|
+
/** The enclosing slide button's state, or null outside a `<SlideButton>`. */
|
|
46
|
+
export function useSlideButtonContext(): SlideButtonContextValue | null {
|
|
47
|
+
return use(SlideButtonContext);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Reads the enclosing slide button's state.
|
|
52
|
+
*
|
|
53
|
+
* Lets a custom handle or label match the control — read `offset` against
|
|
54
|
+
* `travel` to draw anything that follows the drag. Throws outside a
|
|
55
|
+
* `<SlideButton>`; use {@link useSlideButtonContext} where it is optional.
|
|
56
|
+
*/
|
|
57
|
+
export function useSlideButton(): SlideButtonContextValue {
|
|
58
|
+
const context = useSlideButtonContext();
|
|
59
|
+
if (!context) {
|
|
60
|
+
throw new Error("useSlideButton must be called inside a <SlideButton>.");
|
|
61
|
+
}
|
|
62
|
+
return context;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The enclosing slide button's state, for a compound part that cannot work without one.
|
|
67
|
+
*
|
|
68
|
+
* Internal: deliberately not re-exported from `index.ts`.
|
|
69
|
+
*/
|
|
70
|
+
export function useSlideButtonPart(component: string): SlideButtonContextValue {
|
|
71
|
+
const context = useSlideButtonContext();
|
|
72
|
+
if (!context) {
|
|
73
|
+
throw new Error(`${component} must be rendered inside a <SlideButton>.`);
|
|
74
|
+
}
|
|
75
|
+
return context;
|
|
76
|
+
}
|