@medway-ui/native 1.7.2 → 1.8.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.
Files changed (57) hide show
  1. package/README.md +4 -1
  2. package/lib/commonjs/components/Badge.js +159 -36
  3. package/lib/commonjs/components/Badge.js.map +1 -1
  4. package/lib/commonjs/components/Choice.js +35 -37
  5. package/lib/commonjs/components/Choice.js.map +1 -1
  6. package/lib/commonjs/components/SegmentControl.js +197 -0
  7. package/lib/commonjs/components/SegmentControl.js.map +1 -0
  8. package/lib/commonjs/components/Switch.js +176 -66
  9. package/lib/commonjs/components/Switch.js.map +1 -1
  10. package/lib/commonjs/editor/editorHtml.generated.js +1 -1
  11. package/lib/commonjs/editor/editorHtml.generated.js.map +1 -1
  12. package/lib/commonjs/hooks/useThemeColor.js +42 -42
  13. package/lib/commonjs/hooks/useThemeColor.js.map +1 -1
  14. package/lib/commonjs/index.js +31 -0
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/lib/raised-surface.js +24 -0
  17. package/lib/commonjs/lib/raised-surface.js.map +1 -0
  18. package/lib/module/components/Badge.js +159 -36
  19. package/lib/module/components/Badge.js.map +1 -1
  20. package/lib/module/components/Choice.js +35 -37
  21. package/lib/module/components/Choice.js.map +1 -1
  22. package/lib/module/components/SegmentControl.js +189 -0
  23. package/lib/module/components/SegmentControl.js.map +1 -0
  24. package/lib/module/components/Switch.js +181 -69
  25. package/lib/module/components/Switch.js.map +1 -1
  26. package/lib/module/editor/editorHtml.generated.js +1 -1
  27. package/lib/module/editor/editorHtml.generated.js.map +1 -1
  28. package/lib/module/hooks/useThemeColor.js +42 -42
  29. package/lib/module/hooks/useThemeColor.js.map +1 -1
  30. package/lib/module/index.js +2 -0
  31. package/lib/module/index.js.map +1 -1
  32. package/lib/module/lib/raised-surface.js +22 -0
  33. package/lib/module/lib/raised-surface.js.map +1 -0
  34. package/lib/typescript/components/Badge.d.ts +14 -4
  35. package/lib/typescript/components/Badge.d.ts.map +1 -1
  36. package/lib/typescript/components/Chip.d.ts +2 -2
  37. package/lib/typescript/components/Choice.d.ts.map +1 -1
  38. package/lib/typescript/components/SegmentControl.d.ts +37 -0
  39. package/lib/typescript/components/SegmentControl.d.ts.map +1 -0
  40. package/lib/typescript/components/Switch.d.ts +5 -5
  41. package/lib/typescript/components/Switch.d.ts.map +1 -1
  42. package/lib/typescript/components/Tag.d.ts +2 -2
  43. package/lib/typescript/editor/editorHtml.generated.d.ts.map +1 -1
  44. package/lib/typescript/index.d.ts +3 -1
  45. package/lib/typescript/index.d.ts.map +1 -1
  46. package/lib/typescript/lib/raised-surface.d.ts +3 -0
  47. package/lib/typescript/lib/raised-surface.d.ts.map +1 -0
  48. package/package.json +1 -1
  49. package/setup/tokens.native.css +49 -22
  50. package/src/components/Badge.tsx +207 -61
  51. package/src/components/Choice.tsx +66 -45
  52. package/src/components/SegmentControl.tsx +265 -0
  53. package/src/components/Switch.tsx +303 -115
  54. package/src/editor/editorHtml.generated.ts +1 -1
  55. package/src/hooks/useThemeColor.ts +42 -42
  56. package/src/index.ts +21 -1
  57. package/src/lib/raised-surface.tsx +21 -0
@@ -1,57 +1,109 @@
1
- import { useState, useEffect, type ReactNode } from "react";
2
- import { View, type ViewStyle } from "react-native";
1
+ import {
2
+ cloneElement,
3
+ isValidElement,
4
+ memo,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ type ReactElement,
10
+ type ReactNode,
11
+ } from "react";
12
+ import { StyleSheet, type ViewStyle } from "react-native";
13
+ import { cssInterop } from "nativewind";
3
14
  import * as SwitchPrimitive from "@rn-primitives/switch";
4
15
  import Animated, {
5
- useSharedValue,
16
+ Easing,
17
+ interpolateColor,
6
18
  useAnimatedStyle,
19
+ useSharedValue,
7
20
  withTiming,
8
- Easing,
21
+ type SharedValue,
9
22
  } from "react-native-reanimated";
10
23
  import { cva, type VariantProps } from "class-variance-authority";
11
24
  import { cn } from "@/lib/utils";
12
25
  import { getMinTouchHitSlop } from "@/lib/touch-target";
13
- import { useThemeColor } from "@/hooks/useThemeColor";
26
+ import { useThemeColor, withAlpha } from "@/hooks/useThemeColor";
14
27
 
15
- interface SwitchProps
16
- extends Omit<
17
- SwitchPrimitive.RootProps,
18
- "checked" | "onCheckedChange" | "children"
19
- >,
20
- VariantProps<typeof switchVariants> {
21
- checked?: boolean;
22
- defaultChecked?: boolean;
23
- onCheckedChange?: (checked: boolean) => void;
24
- checkedIcon?: ReactNode;
25
- uncheckedIcon?: ReactNode;
26
- }
28
+ // `@rn-primitives/switch` renders a plain Pressable — cssInterop makes `className` work.
29
+ const StyledSwitchRoot = cssInterop(SwitchPrimitive.Root, {
30
+ className: "style",
31
+ });
27
32
 
28
- const THUMB_SIZES: Record<"sm" | "md" | "lg", number> = {
33
+ type SwitchSize = "sm" | "md" | "lg";
34
+
35
+ type SwitchIconProps = {
36
+ color?: string;
37
+ width?: number;
38
+ height?: number;
39
+ };
40
+
41
+ const BORDER_WIDTH = 2;
42
+ /** Gap between thumb and inner edge of the track border (web `translate-x-0.5`). */
43
+ const THUMB_INSET = 2;
44
+ const CENTER: ViewStyle = { alignItems: "center", justifyContent: "center" };
45
+
46
+ const THUMB_SIZES: Record<SwitchSize, number> = {
29
47
  sm: 8,
30
48
  md: 12,
31
49
  lg: 16,
32
50
  };
33
51
 
34
- const THUMB_TRANSLATE: Record<"sm" | "md" | "lg", number> = {
35
- sm: 11,
36
- md: 15,
37
- lg: 19,
52
+ const ICON_SIZES: Record<SwitchSize, number> = {
53
+ sm: 8,
54
+ md: 12,
55
+ lg: 14,
38
56
  };
39
57
 
40
- const SWITCH_DIMENSIONS: Record<"sm" | "md" | "lg", { width: number; height: number }> = {
41
- sm: { width: 24, height: 16 },
42
- md: { width: 32, height: 20 },
43
- lg: { width: 40, height: 24 },
44
- };
58
+ /** Same scale as web `h-4 w-6` / `h-5 w-8` / `h-6 w-10` at 16px rem. */
59
+ const SWITCH_DIMENSIONS: Record<SwitchSize, { width: number; height: number }> =
60
+ {
61
+ sm: { width: 24, height: 16 },
62
+ md: { width: 32, height: 20 },
63
+ lg: { width: 40, height: 24 },
64
+ };
65
+
66
+ /**
67
+ * Thumb is a sibling of the bordered track (not a child of it), so offsets are
68
+ * from the root's outer box. That avoids Yoga content-box quirks where absolute
69
+ * children of a bordered view shift the knob down / past the right edge.
70
+ *
71
+ * off = border + inset
72
+ * on = width - border - inset - thumb
73
+ * on+disabled keeps a 1px tighter right inset, matching web.
74
+ */
75
+ function getThumbTranslateX(
76
+ trackWidth: number,
77
+ thumbSize: number,
78
+ checked: boolean,
79
+ disabled: boolean
80
+ ): number {
81
+ const off = BORDER_WIDTH + THUMB_INSET;
82
+ if (!checked) return off;
83
+ const on = trackWidth - BORDER_WIDTH - THUMB_INSET - thumbSize;
84
+ return disabled ? on - 1 : on;
85
+ }
86
+
87
+ /**
88
+ * Even curve. A strong ease-out spends the last frames crawling, which reads
89
+ * as the thumb catching. Track and thumb colors ride the same curve.
90
+ */
91
+ const THUMB_MOTION = {
92
+ duration: 250,
93
+ easing: Easing.inOut(Easing.cubic),
94
+ } as const;
45
95
 
46
- const switchVariants = cva("rounded-full border-2 items-center shrink-0", {
96
+ // Px, not `h-6 w-10`: NativeWind resolves rem as 14px on native, which shrinks
97
+ // the track below SWITCH_DIMENSIONS and pushes the thumb down and out the right.
98
+ const switchVariants = cva("shrink-0", {
47
99
  variants: {
48
100
  variant: {
49
101
  regular: "",
50
102
  },
51
103
  size: {
52
- sm: "h-4 w-6",
53
- md: "h-5 w-8",
54
- lg: "h-6 w-10",
104
+ sm: "h-[16px] w-[24px]",
105
+ md: "h-[20px] w-[32px]",
106
+ lg: "h-[24px] w-[40px]",
55
107
  },
56
108
  },
57
109
  defaultVariants: {
@@ -60,119 +112,255 @@ const switchVariants = cva("rounded-full border-2 items-center shrink-0", {
60
112
  },
61
113
  });
62
114
 
63
- function Switch({
115
+ interface SwitchProps
116
+ extends Omit<
117
+ SwitchPrimitive.RootProps,
118
+ "checked" | "onCheckedChange" | "children"
119
+ >,
120
+ VariantProps<typeof switchVariants> {
121
+ checked?: boolean;
122
+ defaultChecked?: boolean;
123
+ onCheckedChange?: (checked: boolean) => void;
124
+ checkedIcon?: ReactNode;
125
+ uncheckedIcon?: ReactNode;
126
+ }
127
+
128
+ function renderThumbIcon(icon: ReactNode, color: string, size: number) {
129
+ if (!isValidElement<SwitchIconProps>(icon)) return icon;
130
+
131
+ return cloneElement(icon as ReactElement<SwitchIconProps>, {
132
+ color: icon.props.color ?? color,
133
+ width: icon.props.width ?? size,
134
+ height: icon.props.height ?? size,
135
+ });
136
+ }
137
+
138
+ type SwitchThumbIconsProps = {
139
+ progress: SharedValue<number>;
140
+ checkedIcon: ReactNode;
141
+ uncheckedIcon: ReactNode;
142
+ checkedIconColor: string;
143
+ uncheckedIconColor: string;
144
+ iconSize: number;
145
+ };
146
+
147
+ function SwitchThumbIcons({
148
+ progress,
149
+ checkedIcon,
150
+ uncheckedIcon,
151
+ checkedIconColor,
152
+ uncheckedIconColor,
153
+ iconSize,
154
+ }: SwitchThumbIconsProps) {
155
+ const checkedIconStyle = useAnimatedStyle(() => ({
156
+ opacity: progress.value,
157
+ }));
158
+ const uncheckedIconStyle = useAnimatedStyle(() => ({
159
+ opacity: 1 - progress.value,
160
+ }));
161
+
162
+ return (
163
+ <>
164
+ <Animated.View
165
+ style={[StyleSheet.absoluteFill, CENTER, uncheckedIconStyle]}
166
+ >
167
+ {renderThumbIcon(uncheckedIcon, uncheckedIconColor, iconSize)}
168
+ </Animated.View>
169
+ <Animated.View
170
+ style={[StyleSheet.absoluteFill, CENTER, checkedIconStyle]}
171
+ >
172
+ {renderThumbIcon(checkedIcon, checkedIconColor, iconSize)}
173
+ </Animated.View>
174
+ </>
175
+ );
176
+ }
177
+
178
+ const Switch = memo(function Switch({
64
179
  className,
65
180
  size = "md",
66
- variant,
181
+ variant = "regular",
67
182
  checkedIcon,
68
183
  uncheckedIcon,
69
184
  checked,
70
185
  defaultChecked,
71
186
  onCheckedChange,
72
- disabled,
187
+ disabled = false,
73
188
  style,
74
189
  ...props
75
190
  }: SwitchProps) {
76
191
  const resolvedSize = size ?? "md";
77
192
  const dimensions = SWITCH_DIMENSIONS[resolvedSize];
78
- const [internalChecked, setInternalChecked] = useState(defaultChecked ?? false);
193
+ const thumbSize = THUMB_SIZES[resolvedSize];
194
+ const iconSize = ICON_SIZES[resolvedSize];
195
+ const [internalChecked, setInternalChecked] = useState(
196
+ defaultChecked ?? false
197
+ );
79
198
  const isControlled = checked !== undefined;
80
- const isChecked = isControlled ? (checked ?? false) : internalChecked;
81
-
82
- const trackOn = useThemeColor("primary-background-fill");
83
- const trackOff = useThemeColor("surface-card");
84
- const trackDisabled = useThemeColor("gray-background-soft-muted");
85
- const trackBorder = useThemeColor("gray-stroke-muted");
86
- const thumbOn = useThemeColor("primary-foreground-onfill");
87
- const thumbOff = useThemeColor("gray-foreground-muted");
88
- const thumbDisabled = useThemeColor("gray-background-fill-muted");
89
-
90
- const translateX = useSharedValue(isChecked ? THUMB_TRANSLATE[resolvedSize] : 2);
91
-
92
- useEffect(() => {
93
- translateX.value = withTiming(isChecked ? THUMB_TRANSLATE[resolvedSize] : 2, {
94
- duration: 180,
95
- easing: Easing.out(Easing.ease),
96
- });
97
- }, [isChecked, resolvedSize, translateX]);
98
-
99
- const thumbStyle = useAnimatedStyle(() => ({
100
- transform: [{ translateX: translateX.value }],
101
- }));
102
-
199
+ const isChecked = isControlled ? checked : internalChecked;
103
200
  const hasIcons = Boolean(checkedIcon || uncheckedIcon);
104
- const thumbSize = THUMB_SIZES[resolvedSize];
201
+
202
+ const checkedIconColor = useThemeColor(
203
+ disabled ? "gray-300" : "primary-foreground-onfill"
204
+ );
205
+ const uncheckedIconColor = useThemeColor(
206
+ disabled ? "gray-300" : "gray-foreground"
207
+ );
208
+ const trackOffColor = useThemeColor(
209
+ disabled ? "gray-background-soft-muted" : "surface-card"
210
+ );
211
+ const trackOnColor = useThemeColor(
212
+ disabled ? "gray-background-soft-muted" : "primary-background-fill"
213
+ );
214
+ const borderOffColor = useThemeColor("gray-stroke-muted");
215
+ const borderOnColor = disabled ? borderOffColor : withAlpha(borderOffColor, 0);
216
+ const thumbDisabledColor = useThemeColor("gray-background-fill-muted");
217
+ const thumbOnToken = useThemeColor("primary-foreground-onfill");
218
+ const thumbOffToken = useThemeColor("gray-foreground-muted");
219
+ const thumbOffColor = hasIcons
220
+ ? "rgba(0, 0, 0, 0)"
221
+ : disabled
222
+ ? thumbDisabledColor
223
+ : thumbOffToken;
224
+ const thumbOnColor = hasIcons
225
+ ? "rgba(0, 0, 0, 0)"
226
+ : disabled
227
+ ? thumbDisabledColor
228
+ : thumbOnToken;
229
+
230
+ const offX = getThumbTranslateX(dimensions.width, thumbSize, false, disabled);
231
+ const onX = getThumbTranslateX(dimensions.width, thumbSize, true, disabled);
232
+ // Root has no border — center against the outer height.
233
+ const thumbTop = (dimensions.height - thumbSize) / 2;
234
+
235
+ // Single UI-thread driver for position, colors and icons. In controlled mode
236
+ // the visual only moves when `checked` changes (same as the web Radix switch).
237
+ const progress = useSharedValue(isChecked ? 1 : 0);
238
+ const targetRef = useRef(isChecked);
239
+
240
+ const animateTo = (next: boolean) => {
241
+ if (targetRef.current === next) return;
242
+ targetRef.current = next;
243
+ progress.value = withTiming(next ? 1 : 0, THUMB_MOTION);
244
+ };
245
+
246
+ useLayoutEffect(() => {
247
+ animateTo(isChecked);
248
+ }, [isChecked]);
105
249
 
106
250
  const handleCheckedChange = (nextChecked: boolean) => {
107
- if (!isControlled) setInternalChecked(nextChecked);
251
+ if (!isControlled) {
252
+ animateTo(nextChecked);
253
+ setInternalChecked(nextChecked);
254
+ }
108
255
  onCheckedChange?.(nextChecked);
109
256
  };
110
257
 
111
- const trackBackgroundColor = disabled
112
- ? trackDisabled
113
- : isChecked
114
- ? trackOn
115
- : trackOff;
116
-
117
- const trackBorderColor = disabled || !isChecked ? trackBorder : "transparent";
118
-
119
- const thumbBackgroundColor = disabled
120
- ? thumbDisabled
121
- : hasIcons
122
- ? "transparent"
123
- : isChecked
124
- ? thumbOn
125
- : thumbOff;
126
-
127
- const rootStyle: ViewStyle = {
128
- width: dimensions.width,
129
- height: dimensions.height,
130
- minWidth: dimensions.width,
131
- minHeight: dimensions.height,
132
- borderRadius: dimensions.height / 2,
133
- borderWidth: 2,
134
- alignItems: "flex-start",
135
- justifyContent: "center",
136
- backgroundColor: trackBackgroundColor,
137
- borderColor: trackBorderColor,
138
- };
258
+ const trackMotionStyle = useAnimatedStyle(() => ({
259
+ backgroundColor: interpolateColor(
260
+ progress.value,
261
+ [0, 1],
262
+ [trackOffColor, trackOnColor]
263
+ ),
264
+ borderColor: interpolateColor(
265
+ progress.value,
266
+ [0, 1],
267
+ [borderOffColor, borderOnColor]
268
+ ),
269
+ }));
270
+
271
+ const thumbMotionStyle = useAnimatedStyle(() => ({
272
+ transform: [{ translateX: offX + (onX - offX) * progress.value }],
273
+ }), [offX, onX]);
274
+
275
+ const thumbColorStyle = useAnimatedStyle(() => ({
276
+ backgroundColor: interpolateColor(
277
+ progress.value,
278
+ [0, 1],
279
+ [thumbOffColor, thumbOnColor]
280
+ ),
281
+ }));
282
+
283
+ const baseRootStyle = useMemo<ViewStyle>(
284
+ () => ({
285
+ width: dimensions.width,
286
+ height: dimensions.height,
287
+ minWidth: dimensions.width,
288
+ minHeight: dimensions.height,
289
+ borderRadius: dimensions.height / 2,
290
+ borderWidth: 0,
291
+ padding: 0,
292
+ overflow: "visible",
293
+ }),
294
+ [dimensions.height, dimensions.width]
295
+ );
296
+
297
+ const trackStyle = useMemo<ViewStyle>(
298
+ () => ({
299
+ ...StyleSheet.absoluteFill,
300
+ borderRadius: dimensions.height / 2,
301
+ borderWidth: BORDER_WIDTH,
302
+ }),
303
+ [dimensions.height]
304
+ );
305
+
306
+ const thumbStyle = useMemo<ViewStyle>(
307
+ () => ({
308
+ position: "absolute",
309
+ top: thumbTop,
310
+ left: 0,
311
+ width: thumbSize,
312
+ height: thumbSize,
313
+ borderRadius: thumbSize / 2,
314
+ alignItems: "center",
315
+ justifyContent: "center",
316
+ }),
317
+ [thumbSize, thumbTop]
318
+ );
319
+
320
+ const hitSlop = useMemo(
321
+ () => getMinTouchHitSlop(dimensions.width, dimensions.height),
322
+ [dimensions.height, dimensions.width]
323
+ );
139
324
 
140
325
  return (
141
- <SwitchPrimitive.Root
326
+ <StyledSwitchRoot
327
+ hitSlop={hitSlop}
328
+ {...props}
142
329
  checked={isChecked}
143
330
  onCheckedChange={handleCheckedChange}
144
331
  disabled={disabled}
145
- hitSlop={getMinTouchHitSlop(dimensions.width, dimensions.height)}
332
+ className={cn(
333
+ switchVariants({ size: resolvedSize, variant }),
334
+ className
335
+ )}
146
336
  style={(state) => [
147
- rootStyle,
337
+ baseRootStyle,
148
338
  typeof style === "function" ? style(state) : style,
149
339
  ]}
150
- className={cn(switchVariants({ size, variant }), className)}
151
- {...props}
152
340
  >
153
- <SwitchPrimitive.Thumb asChild>
154
- <Animated.View
155
- style={[
156
- thumbStyle,
157
- {
158
- width: thumbSize,
159
- height: thumbSize,
160
- borderRadius: thumbSize / 2,
161
- backgroundColor: thumbBackgroundColor,
162
- },
163
- ]}
164
- >
165
- {hasIcons ? (
166
- <View className="flex-1 items-center justify-center">
167
- {isChecked && checkedIcon}
168
- {!isChecked && uncheckedIcon}
169
- </View>
170
- ) : null}
171
- </Animated.View>
172
- </SwitchPrimitive.Thumb>
173
- </SwitchPrimitive.Root>
341
+ <Animated.View
342
+ pointerEvents="none"
343
+ style={[trackStyle, trackMotionStyle]}
344
+ />
345
+ <Animated.View
346
+ pointerEvents="none"
347
+ collapsable={false}
348
+ style={[thumbStyle, thumbMotionStyle, thumbColorStyle]}
349
+ >
350
+ {hasIcons ? (
351
+ <SwitchThumbIcons
352
+ progress={progress}
353
+ checkedIcon={checkedIcon}
354
+ uncheckedIcon={uncheckedIcon}
355
+ checkedIconColor={checkedIconColor}
356
+ uncheckedIconColor={uncheckedIconColor}
357
+ iconSize={iconSize}
358
+ />
359
+ ) : null}
360
+ </Animated.View>
361
+ </StyledSwitchRoot>
174
362
  );
175
- }
363
+ });
176
364
 
177
365
  export { Switch, switchVariants };
178
366
  export type { SwitchProps };