Quantity button
A centered Add action that becomes an adjustable stepper with accelerating hold repeat and safe async rollback.
npx @swipeui/cli add quantity-buttonWhen to use it
- Adjust a product quantity inline in a cart or product row.
- Update cart quantities asynchronously with pending feedback and local rollback.
When not to use it
- Free numeric entry or negative values; use Number field.
- One action without a count; use Button.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add quantity-button- Adds
components/swipeui/ui/quantity-button.tsx,components/swipeui/ui/use-quantity-button.tsand 12 shared helpers. - Installs react-native-reanimated, uniwind, react-native-worklets, clsx, tailwind-merge, expo-haptics, class-variance-authority. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { QuantityButton } from '@/components/swipeui/ui/quantity-button';
<QuantityButton max={3} onValueChange={updateCart} />;Examples
The Checkout demo adjusts the quantity and line total for a pocket sketchbook. Use Limit to three to try a stock cap, Repeat while held to toggle hold gestures, and Cart sync to try a pending update. In Cart sync mode, Fail next update shows the rollback and retry behavior.
Tap Add to reveal minus, a count and plus. Hold either side to repeat with accelerating selection ticks. Release, cancellation, backgrounding, blur, disabling, unmounting, reaching a bound or starting asynchronous work ends that hold session. Async work requires a fresh press after settlement.
Settings
Repeat clocks and feedback lifetimes are content timing in milliseconds, separate from motion feel. Repeat/failure durations must be finite and positive; minPending may be zero. repeatMinInterval cannot exceed repeatInterval. Bounds are finite, nonnegative and at most Number.MAX_SAFE_INTEGER; step is at least 0.0000000001. Changes round to ten decimal places and clamp the final partial step to a bound. Nonfinite input values fall back to min.
| Setting | Default | Behavior |
|---|---|---|
value | absent | Controlled authoritative quantity. |
defaultValue | 0 | Initial uncontrolled value, clamped to bounds. |
onValueChange | absent | One persistence request per accepted update; may return a promise. Rollback never invokes it again. |
min | 0 | Inclusive lower bound; only zero displays Add. |
max | 99 | Inclusive upper bound; increment disables at this value. |
step | 1 | Positive increment/decrement; fractional and partial final steps are supported. |
addLabel | Add | Visible resting label. |
addIcon | absent | Button icon node or { color, size } render function, centered with the visible label. |
decrementIcon | absent | Optional Button icon node/render function for the decrement action; the checkout supplies trash at one and minus above one. |
decrementLabel | Decrease quantity | Spoken name for the real decrement button and adjustable decrement action. |
compact | false | Use a 28pt visible surface within 44pt touch frames; text size still follows size. The checkout pairs this with sm and the short Retry failure label. |
repeatOnHold | true | Enable held repeats independently of press feedback. |
repeatDelay | 400 | Delay before the first held change. |
repeatInterval | 180 | Initial interval between held changes. |
repeatMinInterval | 60 | Fastest held interval. |
repeatAcceleration | 0.8 | Multiply the interval after each tick; 1 keeps a constant pace. |
minPending | 450 | Minimum visible pending ring duration. |
failureDuration | 1800 | Failure label lifetime; the rolled-back count then returns. |
failureLabel | Try again | On-control failure text. A subsequent explicit press starts a new bounded update. |
onError | absent | Latest relevant failure callback; stale/unmounted settlements are ignored. |
limitFeedback | true | Warning notification once when an accepted update reaches max. Increment stays disabled when off. |
shakeOnError | true | One short failure shake; suppressed with Reduce Motion. |
animateWidth | true | Morph-role width spring between Add and the stepper. |
controlsMotion | true | Side-control arrivals on the morph role. |
countMotion | true | SlotText rolling digits; false updates in place. |
haptics | true | Global feedback or { press, repeat, limit, error } switches. |
haptics.press | true | Impact for accepted taps. |
haptics.repeat | true | Selection tick for accepted held changes. |
haptics.limit | true | Warning when max is reached, also gated by limitFeedback. |
haptics.error | true | Error notification when work fails. |
variant | solid | Shared Button solid/tinted/outline/ghost/plain surfaces and legacy aliases. Failure uses tinted destructive feedback. |
tone | from variant | Shared primary/neutral/destructive/success palette. |
size | md | Shared sm/md/lg sizes; side targets remain at least 44pt. |
shape | preset | Preset corners or explicit pill. |
highlight | true | Shared solid-surface top rim. |
glow | false | Shared solid-surface glow. |
pressEffect | both | Shared both/scale/wash/none feedback on Add and the real side controls. |
disabled | false | Prevent mutations and stop repeating. |
disabledStyle | muted | Shared muted/dim unavailable treatment. |
motion | app feel | Press, morph, number and ambient roles. Reduce Motion removes travel and retains readable feedback. |
accessibilityLabel | Add to cart | Resting button name; active adjustable name is Quantity. |
accessibilityHint | absent | Optional instructions; bounded side controls explain their limit. |
className | absent | Outer footprint classes. |
style | absent | Native outer layout override. |
textClassName | absent | Label/count classes. |
testID | absent | Root identifier; parts append add/minus/plus/count/spinner/failure and geometry suffixes. |
Controlled and asynchronous updates
Return the cart mutation promise from onValueChange. The requested quantity appears optimistically while ButtonSpinner occupies the count region. Another tap, hold or accessibility action cannot overlap that mutation. Success commits uncontrolled state and announces once. Failure restores the previous value locally, shows the failure label and announces once; it never sends a compensating server request.
With controlled value, commit the authoritative value in the parent after persistence succeeds. If the parent echoes the requested value optimistically, the component can still display the previous value on failure without invoking onValueChange again. The parent owns persistent state reconciliation. An unrelated owner value invalidates old UI settlement; the mutation lock remains until the old request settles, so a replacement update cannot overlap it. Neither an external value change nor unmount aborts the caller's underlying server work.
The Add icon/label group centers at its actual visible width. The count reserves a separate footprint, then centers digits, the ring or failure text inside it. Hidden reservations do not become part of the visible group.
Accessibility and motion
Add is a named button. The count is an adjustable element with its current value, bounds and increment/decrement actions. Named real Increase quantity and Decrease quantity buttons sit beside it, so holding is optional and the adjustable element does not swallow the gesture alternatives. Pending is busy, and side controls are disabled. Changes announce once when committed; failures announce the restored quantity. RTL mirrors the control order. Text can grow without a fixed-height count box.
Native runtime, VoiceOver/TalkBack, Dynamic Type, RTL, haptics, device motion/performance and recordings remain pending founder device review in milestone 7.9. Earlier reviews and release gate 15 remain open.
API reference
QuantityButtonProps
Behavior and gestures
- Add becomes minus, count and plus; returning to zero restores Add.
- Holding a side control repeats after repeatDelay and accelerates down to repeatMinInterval. Lift, cancellation, background, blur, disabling, unmount, bounds and async work stop the session.
- Each accepted update invokes onValueChange once. Promise work locks all mutations until settlement and minimum pending visibility. Failure rolls back locally without invoking persistence again.
- Increment is disabled at max; optional warning feedback happens once when max is reached.
Accessibility
- Add is a named button. The count exposes adjustable semantics, numeric bounds/current value and increment/decrement accessibility actions.
- Named real minus/plus controls remain independent siblings of the adjustable count, providing alternatives to hold. Busy and disabled state block duplicate actions.
- Committed changes and failures are announced once. Pending preview values are not separately announced. Native assistive technology evidence remains pending for 7.9.
- Side controls have at least 44pt targets. Logical order mirrors in RTL and count geometry grows for text.
Motion
- Uses the
press,morph,numberandambientmotion roles, so it follows your app's feel. Passmotionto tune this one instance. - Shared Button press feedback; morph-role width spring and side arrivals. SlotText rolls the count and ButtonSpinner occupies the same centered slot.
- Reduce Motion removes width/side/shake travel and count rolling, retaining fading readable pending/error feedback. Repeat clocks are content timing, independent of motion.
Performance
- JavaScript refs own serial mutations and repeat state; worklets capture only primitives/shared values, never CSS easing objects.
- No immediate JavaScript shared-value readback. Timers/listeners clean up on unmount; use-async-action prevents stale settlement.
- No new dependency, telemetry, network service or proprietary source.
Notes for AI
- Return a promise for asynchronous cart persistence. Rollback is local UI state, never a second onValueChange call.
- Controlled owners should commit the authoritative quantity when persistence succeeds. Pending shows the requested quantity; an optimistic owner echo is rolled back locally on failure. Unrelated owner changes invalidate old visual settlement but keep the mutation lock until underlying work settles.
- Use repeatOnHold, repeatDelay, repeatInterval, repeatMinInterval and repeatAcceleration independently of motion.
- Use independent limitFeedback, shakeOnError, animateWidth, controlsMotion, countMotion and per-feature haptics switches.
- Bounds are finite and nonnegative, step is at least 1e-10 and results round to ten decimals; max is at most MAX_SAFE_INTEGER. Caller owns server cancellation and reconciliation.
- Native accessibility, haptics, motion and recordings require actual device evidence; pending founder review / 7.9.
- Checkout specimens start at one with caller-supplied decrementIcon/decrementLabel showing a trash action for the last item. The Checkout demo turns repeat off while Cart sync is enabled.
- compact=true renders a 28pt visual surface inside 44pt touch frames. Pair size=sm with a short failureLabel for a compact checkout row.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.