@tallyui/primitives 3.7.0 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1146,6 +1146,8 @@ interface RootContext$4 {
1146
1146
  step: number;
1147
1147
  disabled: boolean;
1148
1148
  onValueChange: (value: number) => void;
1149
+ /** Position of the value between min and max, 0–100 (0 when max <= min). */
1150
+ percent: number;
1149
1151
  }
1150
1152
 
1151
1153
  declare function useRootContext$4(): RootContext$4;
package/dist/index.js CHANGED
@@ -3285,7 +3285,21 @@ import { jsx as jsx24 } from "react/jsx-runtime";
3285
3285
  var DEFAULT_MIN = 0;
3286
3286
  var DEFAULT_MAX2 = 100;
3287
3287
  var DEFAULT_STEP = 1;
3288
+ var PAGE_STEPS = 10;
3289
+ function decimalsOf(n) {
3290
+ return (String(n).split(".")[1] ?? "").length;
3291
+ }
3292
+ function snapValue(raw, min, max, step) {
3293
+ if (max <= min) return min;
3294
+ const clamped = Math.min(max, Math.max(min, raw));
3295
+ if (!(step > 0)) return clamped;
3296
+ const snapped = Math.min(max, min + Math.round((clamped - min) / step) * step);
3297
+ if (max - clamped < Math.abs(clamped - snapped)) return max;
3298
+ return Number(snapped.toFixed(Math.max(decimalsOf(step), decimalsOf(min))));
3299
+ }
3288
3300
  var RootContext17 = React26.createContext(null);
3301
+ var InternalContext4 = React26.createContext(() => {
3302
+ });
3289
3303
  function useRootContext17() {
3290
3304
  const context = React26.useContext(RootContext17);
3291
3305
  if (!context) {
@@ -3317,8 +3331,73 @@ var Root31 = React26.forwardRef(function Root32({
3317
3331
  },
3318
3332
  [setValue]
3319
3333
  );
3334
+ const percent = max > min ? (Math.min(max, Math.max(min, value)) - min) / (max - min) * 100 : 0;
3335
+ const valueRef = React26.useRef(value);
3336
+ valueRef.current = value;
3337
+ const rootRef = React26.useRef(null);
3338
+ const composedRef = React26.useMemo(() => composeRefs(ref, rootRef), [ref]);
3339
+ const rectRef = React26.useRef(null);
3340
+ const pointerXRef = React26.useRef(0);
3341
+ const commitValue = (raw) => {
3342
+ if (disabled) return;
3343
+ const next = snapValue(raw, min, max, step);
3344
+ if (next === valueRef.current) return;
3345
+ valueRef.current = next;
3346
+ onValueChange(next);
3347
+ };
3348
+ const updateFromPointer = () => {
3349
+ const rect = rectRef.current;
3350
+ if (!rect || rect.width <= 0) return;
3351
+ commitValue(min + (pointerXRef.current - rect.left) / rect.width * (max - min));
3352
+ };
3353
+ const responderProps = {
3354
+ onStartShouldSetResponderCapture: () => !disabled,
3355
+ onResponderTerminationRequest: () => false,
3356
+ onResponderGrant: (event) => {
3357
+ pointerXRef.current = event.nativeEvent.pageX;
3358
+ rectRef.current = null;
3359
+ const node = rootRef.current;
3360
+ if (typeof node?.getBoundingClientRect === "function") {
3361
+ const rect = node.getBoundingClientRect();
3362
+ rectRef.current = { left: rect.left + window.scrollX, width: rect.width };
3363
+ updateFromPointer();
3364
+ } else {
3365
+ node?.measure((_x, _y, width, _height, pageX) => {
3366
+ rectRef.current = { left: pageX, width };
3367
+ updateFromPointer();
3368
+ });
3369
+ }
3370
+ },
3371
+ onResponderMove: (event) => {
3372
+ pointerXRef.current = event.nativeEvent.pageX;
3373
+ updateFromPointer();
3374
+ },
3375
+ onResponderRelease: () => {
3376
+ rectRef.current = null;
3377
+ },
3378
+ onResponderTerminate: () => {
3379
+ rectRef.current = null;
3380
+ }
3381
+ };
3382
+ const onKeyDown = (event) => {
3383
+ if (event.defaultPrevented) return;
3384
+ const current = valueRef.current;
3385
+ const next = {
3386
+ ArrowRight: current + step,
3387
+ ArrowUp: current + step,
3388
+ ArrowLeft: current - step,
3389
+ ArrowDown: current - step,
3390
+ PageUp: current + step * PAGE_STEPS,
3391
+ PageDown: current - step * PAGE_STEPS,
3392
+ Home: min,
3393
+ End: max
3394
+ }[event.key];
3395
+ if (next === void 0) return;
3396
+ event.preventDefault();
3397
+ commitValue(next);
3398
+ };
3320
3399
  const Component = asChild ? Slot : View19;
3321
- return /* @__PURE__ */ jsx24(RootContext17.Provider, { value: { value, min, max, step, disabled, onValueChange }, children: /* @__PURE__ */ jsx24(Component, { ref, role: "group", ...props }) });
3400
+ return /* @__PURE__ */ jsx24(RootContext17.Provider, { value: { value, min, max, step, disabled, onValueChange, percent }, children: /* @__PURE__ */ jsx24(InternalContext4.Provider, { value: onKeyDown, children: /* @__PURE__ */ jsx24(Component, { ref: composedRef, role: "group", ...responderProps, ...props }) }) });
3322
3401
  });
3323
3402
  Root31.displayName = "SliderRoot";
3324
3403
  var Track = React26.forwardRef(function Track2({ asChild, ...props }, ref) {
@@ -3327,15 +3406,20 @@ var Track = React26.forwardRef(function Track2({ asChild, ...props }, ref) {
3327
3406
  return /* @__PURE__ */ jsx24(Component, { ref, ...props });
3328
3407
  });
3329
3408
  Track.displayName = "SliderTrack";
3330
- var Range = React26.forwardRef(function Range2({ asChild, ...props }, ref) {
3331
- useRootContext17();
3409
+ var Range = React26.forwardRef(function Range2({ asChild, style, ...props }, ref) {
3410
+ const { percent } = useRootContext17();
3332
3411
  const Component = asChild ? Slot : View19;
3333
- return /* @__PURE__ */ jsx24(Component, { ref, ...props });
3412
+ return /* @__PURE__ */ jsx24(Component, { ref, style: [{ width: `${percent}%` }, style], ...props });
3334
3413
  });
3335
3414
  Range.displayName = "SliderRange";
3336
3415
  var Thumb3 = React26.forwardRef(function Thumb4({ asChild, ...props }, ref) {
3337
- const { value, min, max, disabled } = useRootContext17();
3416
+ const { value, min, max, disabled, percent } = useRootContext17();
3417
+ const onKeyDown = React26.useContext(InternalContext4);
3338
3418
  const Component = asChild ? Slot : Pressable16;
3419
+ const merged = mergeProps(
3420
+ { style: { position: "absolute", left: `${percent}%` }, onKeyDown },
3421
+ props
3422
+ );
3339
3423
  return /* @__PURE__ */ jsx24(
3340
3424
  Component,
3341
3425
  {
@@ -3346,7 +3430,7 @@ var Thumb3 = React26.forwardRef(function Thumb4({ asChild, ...props }, ref) {
3346
3430
  "aria-valuenow": value,
3347
3431
  disabled: disabled || void 0,
3348
3432
  "aria-disabled": disabled || void 0,
3349
- ...props
3433
+ ...merged
3350
3434
  }
3351
3435
  );
3352
3436
  });
@@ -3500,9 +3584,9 @@ import {
3500
3584
  View as View21
3501
3585
  } from "react-native";
3502
3586
  import { jsx as jsx26 } from "react/jsx-runtime";
3503
- var InternalContext4 = React28.createContext(null);
3587
+ var InternalContext5 = React28.createContext(null);
3504
3588
  function useInternalContext4() {
3505
- const context = React28.useContext(InternalContext4);
3589
+ const context = React28.useContext(InternalContext5);
3506
3590
  if (!context) {
3507
3591
  throw new Error(
3508
3592
  "Tooltip compound components cannot be rendered outside the Tooltip.Root component"
@@ -3529,7 +3613,7 @@ function Root35({ open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, c
3529
3613
  });
3530
3614
  const [triggerPosition, setTriggerPosition] = React28.useState(null);
3531
3615
  const [contentLayout, setContentLayout] = React28.useState(null);
3532
- return /* @__PURE__ */ jsx26(InternalContext4.Provider, { value: { nativeID }, children: /* @__PURE__ */ jsx26(
3616
+ return /* @__PURE__ */ jsx26(InternalContext5.Provider, { value: { nativeID }, children: /* @__PURE__ */ jsx26(
3533
3617
  RootContext19.Provider,
3534
3618
  {
3535
3619
  value: {
@@ -3593,7 +3677,7 @@ function Portal9({ forceMount, hostName, children }) {
3593
3677
  if (!forceMount && !rootValue.open) {
3594
3678
  return null;
3595
3679
  }
3596
- return /* @__PURE__ */ jsx26(Portal, { hostName, name: `${internalValue.nativeID}_portal`, children: /* @__PURE__ */ jsx26(InternalContext4.Provider, { value: internalValue, children: /* @__PURE__ */ jsx26(RootContext19.Provider, { value: rootValue, children }) }) });
3680
+ return /* @__PURE__ */ jsx26(Portal, { hostName, name: `${internalValue.nativeID}_portal`, children: /* @__PURE__ */ jsx26(InternalContext5.Provider, { value: internalValue, children: /* @__PURE__ */ jsx26(RootContext19.Provider, { value: rootValue, children }) }) });
3597
3681
  }
3598
3682
  Portal9.displayName = "TooltipPortal";
3599
3683
  var Overlay13 = React28.forwardRef(function Overlay14({ asChild, forceMount, onPress: onPressProp, closeOnPress = true, ...props }, ref) {
@@ -3686,9 +3770,9 @@ import {
3686
3770
  View as View22
3687
3771
  } from "react-native";
3688
3772
  import { jsx as jsx27 } from "react/jsx-runtime";
3689
- var InternalContext5 = React29.createContext(null);
3773
+ var InternalContext6 = React29.createContext(null);
3690
3774
  function useInternalContext5() {
3691
- const context = React29.useContext(InternalContext5);
3775
+ const context = React29.useContext(InternalContext6);
3692
3776
  if (!context) {
3693
3777
  throw new Error(
3694
3778
  "HoverCard compound components cannot be rendered outside the HoverCard.Root component"
@@ -3715,7 +3799,7 @@ function Root36({ open: openProp, defaultOpen, onOpenChange: onOpenChangeProp, c
3715
3799
  });
3716
3800
  const [triggerPosition, setTriggerPosition] = React29.useState(null);
3717
3801
  const [contentLayout, setContentLayout] = React29.useState(null);
3718
- return /* @__PURE__ */ jsx27(InternalContext5.Provider, { value: { nativeID }, children: /* @__PURE__ */ jsx27(
3802
+ return /* @__PURE__ */ jsx27(InternalContext6.Provider, { value: { nativeID }, children: /* @__PURE__ */ jsx27(
3719
3803
  RootContext20.Provider,
3720
3804
  {
3721
3805
  value: {
@@ -3779,7 +3863,7 @@ function Portal10({ forceMount, hostName, children }) {
3779
3863
  if (!forceMount && !rootValue.open) {
3780
3864
  return null;
3781
3865
  }
3782
- return /* @__PURE__ */ jsx27(Portal, { hostName, name: `${internalValue.nativeID}_portal`, children: /* @__PURE__ */ jsx27(InternalContext5.Provider, { value: internalValue, children: /* @__PURE__ */ jsx27(RootContext20.Provider, { value: rootValue, children }) }) });
3866
+ return /* @__PURE__ */ jsx27(Portal, { hostName, name: `${internalValue.nativeID}_portal`, children: /* @__PURE__ */ jsx27(InternalContext6.Provider, { value: internalValue, children: /* @__PURE__ */ jsx27(RootContext20.Provider, { value: rootValue, children }) }) });
3783
3867
  }
3784
3868
  Portal10.displayName = "HoverCardPortal";
3785
3869
  var Overlay15 = React29.forwardRef(function Overlay16({ asChild, forceMount, onPress: onPressProp, closeOnPress = true, ...props }, ref) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tallyui/primitives",
3
- "version": "3.7.0",
3
+ "version": "3.8.0",
4
4
  "type": "module",
5
5
  "description": "Headless, accessible React Native primitives for Tally UI",
6
6
  "main": "./dist/index.js",
@@ -1,7 +1,7 @@
1
1
  import * as React from 'react';
2
- import { Pressable, View } from 'react-native';
2
+ import { Pressable, View, type GestureResponderEvent } from 'react-native';
3
3
  import { useControllableState } from '../hooks';
4
- import { Slot } from '../slot';
4
+ import { Slot, composeRefs, mergeProps } from '../slot';
5
5
  import type { RootContext as RootContextType, RootProps, TrackProps, RangeProps, ThumbProps } from './types';
6
6
 
7
7
  // ---------------------------------------------------------------------------
@@ -11,12 +11,37 @@ import type { RootContext as RootContextType, RootProps, TrackProps, RangeProps,
11
11
  const DEFAULT_MIN = 0;
12
12
  const DEFAULT_MAX = 100;
13
13
  const DEFAULT_STEP = 1;
14
+ /** PageUp/PageDown move this many steps. */
15
+ const PAGE_STEPS = 10;
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Value helpers
19
+ // ---------------------------------------------------------------------------
20
+
21
+ function decimalsOf(n: number): number {
22
+ return (String(n).split('.')[1] ?? '').length;
23
+ }
24
+
25
+ /**
26
+ * Clamps to min..max and snaps to the nearest `min + k*step`, rounding off
27
+ * float noise. `max` stays reachable when it is not on the step grid.
28
+ */
29
+ function snapValue(raw: number, min: number, max: number, step: number): number {
30
+ if (max <= min) return min;
31
+ const clamped = Math.min(max, Math.max(min, raw));
32
+ if (!(step > 0)) return clamped;
33
+ const snapped = Math.min(max, min + Math.round((clamped - min) / step) * step);
34
+ if (max - clamped < Math.abs(clamped - snapped)) return max;
35
+ return Number(snapped.toFixed(Math.max(decimalsOf(step), decimalsOf(min))));
36
+ }
14
37
 
15
38
  // ---------------------------------------------------------------------------
16
39
  // Context
17
40
  // ---------------------------------------------------------------------------
18
41
 
19
42
  const RootContext = React.createContext<RootContextType | null>(null);
43
+ /** The Thumb's key handler; internal, so the public context stays unchanged. */
44
+ const InternalContext = React.createContext<(event: React.KeyboardEvent) => void>(() => {});
20
45
 
21
46
  function useRootContext(): RootContextType {
22
47
  const context = React.useContext(RootContext);
@@ -59,11 +84,89 @@ const Root = React.forwardRef<View, RootProps>(function Root(
59
84
  [setValue]
60
85
  );
61
86
 
87
+ const percent = max > min ? ((Math.min(max, Math.max(min, value)) - min) / (max - min)) * 100 : 0;
88
+
89
+ // Latest values for the gesture handlers, which outlive a single render.
90
+ const valueRef = React.useRef(value);
91
+ valueRef.current = value;
92
+ const rootRef = React.useRef<View>(null);
93
+ const composedRef = React.useMemo(() => composeRefs(ref, rootRef), [ref]);
94
+ // Root's page-relative left edge and width, measured when a gesture starts.
95
+ const rectRef = React.useRef<{ left: number; width: number } | null>(null);
96
+ const pointerXRef = React.useRef(0);
97
+
98
+ const commitValue = (raw: number) => {
99
+ if (disabled) return;
100
+ const next = snapValue(raw, min, max, step);
101
+ if (next === valueRef.current) return;
102
+ valueRef.current = next;
103
+ onValueChange(next);
104
+ };
105
+
106
+ const updateFromPointer = () => {
107
+ const rect = rectRef.current;
108
+ if (!rect || rect.width <= 0) return;
109
+ commitValue(min + ((pointerXRef.current - rect.left) / rect.width) * (max - min));
110
+ };
111
+
112
+ // The responder system drives the drag on native and on web, where
113
+ // react-native-web tracks the pointer on `document` once a gesture starts.
114
+ // The capture phase lets a press on the Thumb (a Pressable) start the drag.
115
+ const responderProps = {
116
+ onStartShouldSetResponderCapture: () => !disabled,
117
+ onResponderTerminationRequest: () => false,
118
+ onResponderGrant: (event: GestureResponderEvent) => {
119
+ pointerXRef.current = event.nativeEvent.pageX;
120
+ rectRef.current = null;
121
+ const node = rootRef.current as (View & { getBoundingClientRect?: () => DOMRect }) | null;
122
+ if (typeof node?.getBoundingClientRect === 'function') {
123
+ const rect = node.getBoundingClientRect();
124
+ rectRef.current = { left: rect.left + window.scrollX, width: rect.width };
125
+ updateFromPointer();
126
+ } else {
127
+ node?.measure((_x, _y, width, _height, pageX) => {
128
+ rectRef.current = { left: pageX, width };
129
+ updateFromPointer();
130
+ });
131
+ }
132
+ },
133
+ onResponderMove: (event: GestureResponderEvent) => {
134
+ pointerXRef.current = event.nativeEvent.pageX;
135
+ updateFromPointer();
136
+ },
137
+ onResponderRelease: () => {
138
+ rectRef.current = null;
139
+ },
140
+ onResponderTerminate: () => {
141
+ rectRef.current = null;
142
+ },
143
+ };
144
+
145
+ const onKeyDown = (event: React.KeyboardEvent) => {
146
+ if (event.defaultPrevented) return;
147
+ const current = valueRef.current;
148
+ const next: number | undefined = ({
149
+ ArrowRight: current + step,
150
+ ArrowUp: current + step,
151
+ ArrowLeft: current - step,
152
+ ArrowDown: current - step,
153
+ PageUp: current + step * PAGE_STEPS,
154
+ PageDown: current - step * PAGE_STEPS,
155
+ Home: min,
156
+ End: max,
157
+ } as Record<string, number>)[event.key];
158
+ if (next === undefined) return;
159
+ event.preventDefault();
160
+ commitValue(next);
161
+ };
162
+
62
163
  const Component = asChild ? Slot : View;
63
164
 
64
165
  return (
65
- <RootContext.Provider value={{ value, min, max, step, disabled, onValueChange }}>
66
- <Component ref={ref} role="group" {...props} />
166
+ <RootContext.Provider value={{ value, min, max, step, disabled, onValueChange, percent }}>
167
+ <InternalContext.Provider value={onKeyDown}>
168
+ <Component ref={composedRef} role="group" {...responderProps} {...props} />
169
+ </InternalContext.Provider>
67
170
  </RootContext.Provider>
68
171
  );
69
172
  });
@@ -92,14 +195,14 @@ Track.displayName = 'SliderTrack';
92
195
  // ---------------------------------------------------------------------------
93
196
 
94
197
  const Range = React.forwardRef<View, RangeProps>(function Range(
95
- { asChild, ...props },
198
+ { asChild, style, ...props },
96
199
  ref
97
200
  ) {
98
- useRootContext();
201
+ const { percent } = useRootContext();
99
202
 
100
203
  const Component = asChild ? Slot : View;
101
204
 
102
- return <Component ref={ref} {...props} />;
205
+ return <Component ref={ref} style={[{ width: `${percent}%` }, style]} {...props} />;
103
206
  });
104
207
 
105
208
  Range.displayName = 'SliderRange';
@@ -112,10 +215,17 @@ const Thumb = React.forwardRef<View, ThumbProps>(function Thumb(
112
215
  { asChild, ...props },
113
216
  ref
114
217
  ) {
115
- const { value, min, max, disabled } = useRootContext();
218
+ const { value, min, max, disabled, percent } = useRootContext();
219
+ const onKeyDown = React.useContext(InternalContext);
116
220
 
117
221
  const Component = asChild ? Slot : Pressable;
118
222
 
223
+ // Caller's style wins over the default position; caller's onKeyDown runs first.
224
+ const merged = mergeProps(
225
+ { style: { position: 'absolute', left: `${percent}%` }, onKeyDown },
226
+ props
227
+ );
228
+
119
229
  return (
120
230
  <Component
121
231
  ref={ref}
@@ -125,7 +235,7 @@ const Thumb = React.forwardRef<View, ThumbProps>(function Thumb(
125
235
  aria-valuenow={value}
126
236
  disabled={disabled || undefined}
127
237
  aria-disabled={disabled || undefined}
128
- {...props}
238
+ {...merged}
129
239
  />
130
240
  );
131
241
  });
@@ -30,6 +30,8 @@ interface RootContext {
30
30
  step: number;
31
31
  disabled: boolean;
32
32
  onValueChange: (value: number) => void;
33
+ /** Position of the value between min and max, 0–100 (0 when max <= min). */
34
+ percent: number;
33
35
  }
34
36
 
35
37
  export type { RootProps, TrackProps, RangeProps, ThumbProps, RootContext };