"use client"; import { Minus, Plus } from "lucide-react"; import { AnimatePresence, type HTMLMotionProps, motion, useReducedMotion, } from "motion/react"; import { createContext, type MouseEvent, type ReactNode, type Ref, useCallback, useContext, useLayoutEffect, useMemo, useRef, useState, } from "react"; import { Liquid, LiquidItem, type LiquidTransition, } from "@/components/motion/liquid"; import { EASE_OUT, SPRING_PRESS, } from "@/lib/ease"; import { formatNumber, useLocale, useMessages } from "@/lib/i18n"; import { mergeRefs } from "@/lib/merge-refs"; import { cn } from "@/lib/utils"; /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const adaptiveStepperMessages = { label: "Quantity", summary: "{label}. Current value: {value}", decrease: "Decrease value", increase: "Increase value", }; export type AdaptiveStepperMessages = typeof adaptiveStepperMessages; // The deliberately elastic separation curve from the liquid email reference. const STEPPER_LIQUID_TRANSITION = { duration: 600, ease: [0.22, 1.3, 0.71, 1], } as const satisfies LiquidTransition; type StepDirection = -1 | 0 | 1; type AdaptiveStepperContextValue = { value: number; valueText: string; direction: StepDirection; atMin: boolean; atMax: boolean; disabled: boolean; reduce: boolean; t: (key: keyof AdaptiveStepperMessages & string) => string; decrement: (restoreFocus: boolean) => void; increment: (restoreFocus: boolean) => void; decrementRef: React.MutableRefObject; incrementRef: React.MutableRefObject; }; const AdaptiveStepperContext = createContext( null, ); function useAdaptiveStepperContext(component: string) { const context = useContext(AdaptiveStepperContext); if (!context) { throw new Error(`${component} must be used within `); } return context; } function clamp(value: number, min: number, max: number) { return Math.min(max, Math.max(min, value)); } function cleanNumber(value: number) { return Number(value.toFixed(10)); } function nextStep( value: number, direction: -1 | 1, min: number, max: number, step: number, ) { if (direction === 1) { const nextIndex = Math.floor((value - min) / step + 1e-10) + 1; return cleanNumber(Math.min(max, min + nextIndex * step)); } const previousIndex = Math.ceil((value - min) / step - 1e-10) - 1; return cleanNumber(Math.max(min, min + previousIndex * step)); } export interface AdaptiveStepperProps { children: ReactNode; /** The current value, for controlled use. */ value?: number; /** The initial value when uncontrolled. Default: 0 */ defaultValue?: number; /** Called with the new value when a button steps it. */ onValueChange?: (value: number) => void; /** Lowest allowed value. Default: 0 */ min?: number; /** Highest allowed value. Default: 10 */ max?: number; /** Amount each press adds or removes. Default: 1 */ step?: number; /** Disables both buttons. Default: false */ disabled?: boolean; /** Form field name. Renders a hidden input carrying the value. */ name?: string; /** Formats the value read to screen readers. Defaults to a locale number. */ formatValueText?: (value: number) => string; /** Classes merged onto the root fieldset. */ className?: string; /** Accessible name for the stepper. Default: "Quantity" */ "aria-label"?: string; /** Override any built-in string. `{label}` and `{value}` fill `summary`. */ labels?: Partial; } export function AdaptiveStepper({ children, value: controlledValue, defaultValue = 0, onValueChange, min = 0, max = 10, step = 1, disabled = false, name, formatValueText, className, "aria-label": ariaLabelProp, labels, }: AdaptiveStepperProps) { const t = useMessages("adaptiveStepper", adaptiveStepperMessages, labels); const { locale } = useLocale(); const ariaLabel = ariaLabelProp ?? t("label"); const reduce = useReducedMotion() ?? false; const decrementRef = useRef(null); const incrementRef = useRef(null); const lower = Number.isFinite(min) ? min : 0; const suppliedMax = Number.isFinite(max) ? max : lower; const upper = suppliedMax > lower ? suppliedMax : lower; const stride = Number.isFinite(step) && step > 0 ? step : 1; const [internalValue, setInternalValue] = useState(() => clamp(Number.isFinite(defaultValue) ? defaultValue : lower, lower, upper), ); const controlled = controlledValue !== undefined; const suppliedValue = controlled ? controlledValue : internalValue; const currentValue = clamp( Number.isFinite(suppliedValue) ? suppliedValue : lower, lower, upper, ); const previousValueRef = useRef(currentValue); const currentValueRef = useRef(currentValue); const direction: StepDirection = currentValue === previousValueRef.current ? 0 : currentValue > previousValueRef.current ? 1 : -1; useLayoutEffect(() => { previousValueRef.current = currentValue; currentValueRef.current = currentValue; }, [currentValue]); const commit = useCallback( (nextValue: number, restoreFocus: boolean) => { const next = clamp(cleanNumber(nextValue), lower, upper); if (next === currentValue) return; if (!controlled) setInternalValue(next); onValueChange?.(next); if (!restoreFocus) return; requestAnimationFrame(() => { if (next === upper && currentValueRef.current === upper) { decrementRef.current?.focus(); } else if (next === lower && currentValueRef.current === lower) { incrementRef.current?.focus(); } }); }, [controlled, currentValue, lower, onValueChange, upper], ); const decrement = useCallback( (restoreFocus: boolean) => { if (disabled || currentValue <= lower) return; commit(nextStep(currentValue, -1, lower, upper, stride), restoreFocus); }, [commit, currentValue, disabled, lower, stride, upper], ); const increment = useCallback( (restoreFocus: boolean) => { if (disabled || currentValue >= upper) return; commit(nextStep(currentValue, 1, lower, upper, stride), restoreFocus); }, [commit, currentValue, disabled, lower, stride, upper], ); const valueText = formatValueText?.(currentValue) ?? formatNumber(currentValue, locale); const context = useMemo( () => ({ value: currentValue, valueText, direction, atMin: currentValue <= lower, atMax: currentValue >= upper, disabled, reduce, t, decrement, increment, decrementRef, incrementRef, }), [ currentValue, decrement, direction, disabled, increment, lower, reduce, t, upper, valueText, ], ); return (
{t("summary", { label: ariaLabel, value: valueText })} {children} {name ? : null}
); } export interface AdaptiveStepperActionProps extends Omit, "children" | "onClick"> { children?: ReactNode; /** Called on click before the step. Call `preventDefault()` to skip the step. */ onClick?: (event: MouseEvent) => void; /** Ref to the underlying button element. */ ref?: Ref; } function StepperAction({ direction, children, className, onClick, ref, style, tabIndex, "aria-label": ariaLabel, ...props }: AdaptiveStepperActionProps & { direction: -1 | 1 }) { const context = useAdaptiveStepperContext("AdaptiveStepper action"); const hidden = direction === -1 ? context.atMin : context.atMax; const actionRef = direction === -1 ? context.decrementRef : context.incrementRef; const label = ariaLabel ?? context.t(direction === -1 ? "decrease" : "increase"); const action = direction === -1 ? context.decrement : context.increment; const x = direction === -1 ? hidden ? 32 : 0 : hidden ? 136 : 168; return ( ); } export function AdaptiveStepperDecrement(props: AdaptiveStepperActionProps) { return ; } export interface AdaptiveStepperValueProps extends Omit, "children"> { children?: ReactNode | ((value: number) => ReactNode); } export function AdaptiveStepperValue({ children, className, style, ...props }: AdaptiveStepperValueProps) { const context = useAdaptiveStepperContext("AdaptiveStepperValue"); const geometry = context.atMin && context.atMax ? { x: 0, width: 216 } : context.atMin ? { x: 0, width: 152 } : context.atMax ? { x: 64, width: 152 } : { x: 64, width: 88 }; const displayValue = typeof children === "function" ? children(context.value) : children; const renderedValue = displayValue ?? context.value; const canRoll = typeof renderedValue === "number" || typeof renderedValue === "string"; const distance = context.reduce || !canRoll ? 0 : context.direction * 32; const enterFrom = `translateY(${distance}%)`; const exitTo = `translateY(${-distance}%)`; return ( {context.valueText} ); } export function AdaptiveStepperIncrement(props: AdaptiveStepperActionProps) { return ; }