@plexui/ui 0.56.0 → 0.57.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.
|
@@ -23,11 +23,11 @@ export const stepValue = (current, direction, { min, max, step, wrap }) => {
|
|
|
23
23
|
return next;
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
|
-
* Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own
|
|
27
|
-
* name it with `aria-label` or `aria-labelledby
|
|
26
|
+
* Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own
|
|
27
|
+
* (`showValue` puts it between the segments): name it with `aria-label` or `aria-labelledby`. Keyboard focus lands on the
|
|
28
28
|
* stepper, a spin button: Up and Down step, Page Up and Page Down step ten times, Home and End go to the range ends.
|
|
29
29
|
*/
|
|
30
|
-
export function Stepper({ id, value: valueProp, defaultValue, onChange, min = 0, max = 100, step = 1, wrap = false, autoRepeat = true, disabled = false, pill = true, decrementLabel = "Decrease", incrementLabel = "Increase", onRemove, removeLabel = "Remove", name, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, onFocus, onBlur, className, }) {
|
|
30
|
+
export function Stepper({ id, value: valueProp, defaultValue, onChange, min = 0, max = 100, step = 1, wrap = false, autoRepeat = true, disabled = false, pill = true, showValue = false, decrementLabel = "Decrease", incrementLabel = "Increase", onRemove, removeLabel = "Remove", name, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, onFocus, onBlur, className, }) {
|
|
31
31
|
const [internalValue, setInternalValue] = useState(() => defaultValue ?? min);
|
|
32
32
|
const value = valueProp ?? internalValue;
|
|
33
33
|
// Timers and key handlers read the value through this ref; an uncontrolled step advances it at once.
|
|
@@ -123,6 +123,6 @@ export function Stepper({ id, value: valueProp, defaultValue, onChange, min = 0,
|
|
|
123
123
|
// Keeps the focus where it is, as a native stepper takes none.
|
|
124
124
|
onMouseDown: (event) => event.preventDefault(), onPointerDown: (event) => startPress(kind, event), onPointerUp: stopRepeat, onPointerCancel: stopRepeat, onLostPointerCapture: stopRepeat, onClick: (event) => handleClick(kind, event), children: _jsx(Icon, { className: s.Icon }) }));
|
|
125
125
|
};
|
|
126
|
-
return (_jsxs("div", { id: id, role: "spinbutton", tabIndex: disabled ? -1 : 0, "aria-valuenow": value, "aria-valuemin": min, "aria-valuemax": max, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-keyshortcuts": removes ? "Delete" : undefined, "aria-disabled": disabled || undefined, className: clsx(s.Stepper, className), "data-disabled": disabled ? "" : undefined, "data-pill": pill ? "" : undefined, onKeyDown: handleKeyDown, onFocus: onFocus, onBlur: onBlur, children: [segment("decrement"), _jsx("span", { className: s.Separator, "aria-hidden": "true" }), segment("increment"), name !== undefined && _jsx("input", { type: "hidden", name: name, value: value, disabled: disabled })] }));
|
|
126
|
+
return (_jsxs("div", { id: id, role: "spinbutton", tabIndex: disabled ? -1 : 0, "aria-valuenow": value, "aria-valuemin": min, "aria-valuemax": max, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, "aria-keyshortcuts": removes ? "Delete" : undefined, "aria-disabled": disabled || undefined, className: clsx(s.Stepper, className), "data-disabled": disabled ? "" : undefined, "data-pill": pill ? "" : undefined, "data-show-value": showValue ? "" : undefined, onKeyDown: handleKeyDown, onFocus: onFocus, onBlur: onBlur, children: [segment("decrement"), showValue ? (_jsx("span", { className: s.Value, "aria-hidden": "true", children: value })) : (_jsx("span", { className: s.Separator, "aria-hidden": "true" })), segment("increment"), name !== undefined && _jsx("input", { type: "hidden", name: name, value: value, disabled: disabled })] }));
|
|
127
127
|
}
|
|
128
128
|
//# sourceMappingURL=Stepper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Stepper.js","sourceRoot":"","sources":["../../../../src/components/Stepper/Stepper.tsx"],"names":[],"mappings":"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E5C,KAAC,IAAI,IAAC,SAAS,EAAE,CAAC,CAAC,IAAI,GAAI,GACpB,CACV,CAAA;IACH,CAAC,CAAA;IAED,OAAO,CACL,eACE,EAAE,EAAE,EAAE,EACN,IAAI,EAAC,YAAY,EACjB,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,mBACZ,KAAK,mBACL,GAAG,mBACH,GAAG,gBACN,SAAS,qBACJ,cAAc,sBACb,eAAe,uBACd,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,mBAClC,QAAQ,IAAI,SAAS,EACpC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,SAAS,CAAC,mBACtB,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,eAC7B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAChC,SAAS,EAAE,aAAa,EACxB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,aAEb,OAAO,CAAC,WAAW,CAAC,EACrB,eAAM,SAAS,EAAE,CAAC,CAAC,SAAS,iBAAc,MAAM,GAAG,EAClD,OAAO,CAAC,WAAW,CAAC,EACpB,IAAI,KAAK,SAAS,IAAI,gBAAO,IAAI,EAAC,QAAQ,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,GAAI,IACxF,CACP,CAAA;AACH,CAAC","sourcesContent":["\"use client\"\n\nimport clsx from \"clsx\"\nimport {\n type FocusEventHandler,\n type KeyboardEvent,\n type MouseEvent,\n type PointerEvent,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from \"react\"\nimport s from \"@plexui/styles/components/Stepper/Stepper.module.css\"\nimport { useLatestValue } from \"../../hooks/useLatestValue\"\nimport { Minus, Plus, RemoveTrash } from \"../Icon\"\n\nexport type StepperProps = {\n /** The `id` of the stepper. */\n id?: string\n /** The controlled value. Use with `onChange`. */\n value?: number\n /** The value when it is initially rendered. Use when you do not need to control the value. @default min */\n defaultValue?: number\n /** Called with the next value after a step. */\n onChange?: (value: number) => void\n /** The lowest value. @default 0 */\n min?: number\n /** The highest value. @default 100 */\n max?: number\n /** The amount each step adds or removes. @default 1 */\n step?: number\n /** A step past `max` continues from `min`, and one below `min` from `max`. @default false */\n wrap?: boolean\n /** Holding a segment keeps stepping, as a native stepper does. @default true */\n autoRepeat?: boolean\n /** When `true`, prevents the user from interacting with the stepper. */\n disabled?: boolean\n /** Fully rounded ends. @default true */\n pill?: boolean\n /** Accessible name of the decrement segment. @default \"Decrease\" */\n decrementLabel?: string\n /** Accessible name of the increment segment. @default \"Increase\" */\n incrementLabel?: string\n /**\n * Called when the decrement segment is pressed at `min`. With it, the segment shows a trash can there and removes\n * the item, as a cart's stepper does at a quantity of one (without `wrap`).\n */\n onRemove?: () => void\n /** Accessible name of the decrement segment when it removes. @default \"Remove\" */\n removeLabel?: string\n /** The name of the stepper. Submits its value with the owning form. */\n name?: string\n /** Accessible name of the value the stepper changes, when no visible label names it. */\n \"aria-label\"?: string\n /** The `id` of the visible label of the value the stepper changes. */\n \"aria-labelledby\"?: string\n /** The `id` of an element that describes the stepper, such as the text that shows its value. */\n \"aria-describedby\"?: string\n /** Event handler called when the stepper gains focus. */\n onFocus?: FocusEventHandler<HTMLDivElement>\n /** Event handler called when the stepper loses focus. */\n onBlur?: FocusEventHandler<HTMLDivElement>\n /** CSS classes applied to the stepper */\n className?: string\n}\n\n/** Delay before a held segment starts repeating, and the time between repeats, in ms. */\nconst REPEAT_DELAY = 400\nconst REPEAT_INTERVAL = 100\n\nconst decimals = (n: number) => {\n const [, fraction = \"\"] = String(n).split(\".\")\n return fraction.length\n}\n\n/** The value one step away, clamped to the range or wrapped, without floating point drift. */\nexport const stepValue = (current: number, direction: 1 | -1, { min, max, step, wrap }: { min: number; max: number; step: number; wrap: boolean }) => {\n const precision = Math.max(decimals(step), decimals(min), decimals(current))\n const next = Number((current + direction * step).toFixed(precision))\n if (next > max) return wrap ? min : max\n if (next < min) return wrap ? max : min\n return next\n}\n\n/**\n * Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own:\n * name it with `aria-label` or `aria-labelledby` and show the value beside it. Keyboard focus lands on the\n * stepper, a spin button: Up and Down step, Page Up and Page Down step ten times, Home and End go to the range ends.\n */\nexport function Stepper({\n id,\n value: valueProp,\n defaultValue,\n onChange,\n min = 0,\n max = 100,\n step = 1,\n wrap = false,\n autoRepeat = true,\n disabled = false,\n pill = true,\n decrementLabel = \"Decrease\",\n incrementLabel = \"Increase\",\n onRemove,\n removeLabel = \"Remove\",\n name,\n \"aria-label\": ariaLabel,\n \"aria-labelledby\": ariaLabelledBy,\n \"aria-describedby\": ariaDescribedBy,\n onFocus,\n onBlur,\n className,\n}: StepperProps) {\n const [internalValue, setInternalValue] = useState(() => defaultValue ?? min)\n const value = valueProp ?? internalValue\n // Timers and key handlers read the value through this ref; an uncontrolled step advances it at once.\n const valueRef = useLatestValue(value)\n const onChangeRef = useLatestValue(onChange)\n const optionsRef = useLatestValue({ min, max, step, wrap, disabled, controlled: valueProp !== undefined })\n const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)\n const [pressed, setPressed] = useState<\"decrement\" | \"increment\" | null>(null)\n\n const commit = useCallback((next: number) => {\n if (next === valueRef.current) return false\n // A controlled value changes only when the owner passes it back, as in Vue.\n if (!optionsRef.current.controlled) {\n valueRef.current = next\n setInternalValue(next)\n }\n onChangeRef.current?.(next)\n return true\n }, [valueRef, optionsRef, onChangeRef])\n\n const stepBy = useCallback((direction: 1 | -1, times = 1) => {\n let next = valueRef.current\n for (let i = 0; i < times; i++) next = stepValue(next, direction, optionsRef.current)\n return commit(next)\n }, [commit, valueRef, optionsRef])\n\n const stopRepeat = useCallback(() => {\n clearTimeout(timer.current)\n timer.current = undefined\n setPressed(null)\n }, [])\n\n useEffect(() => stopRepeat, [stopRepeat])\n\n const atMin = !wrap && value <= min\n const atMax = !wrap && value >= max\n // At the minimum the decrement segment removes the item, when the owner can remove it\n const removes = atMin && onRemove !== undefined\n const onRemoveRef = useLatestValue(onRemove)\n\n const startPress = (segment: \"decrement\" | \"increment\", event: PointerEvent<HTMLButtonElement>) => {\n if (event.button !== 0 || disabled) return\n if (segment === \"decrement\" && removes) {\n event.currentTarget.setPointerCapture?.(event.pointerId)\n setPressed(segment)\n onRemoveRef.current?.()\n return\n }\n if (segment === \"decrement\" ? atMin : atMax) return\n event.currentTarget.setPointerCapture?.(event.pointerId)\n setPressed(segment)\n const direction = segment === \"decrement\" ? -1 : 1\n stepBy(direction)\n if (!autoRepeat) return\n const repeat = () => {\n if (optionsRef.current.disabled || !stepBy(direction)) return stopRepeat()\n timer.current = setTimeout(repeat, REPEAT_INTERVAL)\n }\n timer.current = setTimeout(repeat, REPEAT_DELAY)\n }\n\n // A pointer steps on press, so only a click without one (assistive technology, `element.click()`) steps here.\n const handleClick = (segment: \"decrement\" | \"increment\", event: MouseEvent<HTMLButtonElement>) => {\n if (event.detail !== 0 || disabled) return\n if (segment === \"decrement\" && removes) return onRemoveRef.current?.()\n stepBy(segment === \"decrement\" ? -1 : 1)\n }\n\n const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n if (disabled || event.altKey || event.ctrlKey || event.metaKey) return\n const keys: Record<string, () => void> = {\n ArrowUp: () => stepBy(1),\n ArrowDown: () => stepBy(-1),\n PageUp: () => stepBy(1, 10),\n PageDown: () => stepBy(-1, 10),\n Home: () => commit(min),\n End: () => commit(max),\n // At the minimum Delete or Backspace removes, as the trash can does; the arrows never do\n ...(removes && { Delete: () => onRemoveRef.current?.(), Backspace: () => onRemoveRef.current?.() }),\n }\n const action = keys[event.key]\n if (!action) return\n event.preventDefault()\n action()\n }\n\n const segment = (kind: \"decrement\" | \"increment\") => {\n const remove = kind === \"decrement\" && removes\n const atEnd = !remove && (kind === \"decrement\" ? atMin : atMax)\n const Icon = remove ? RemoveTrash : kind === \"decrement\" ? Minus : Plus\n return (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={remove ? removeLabel : kind === \"decrement\" ? decrementLabel : incrementLabel}\n aria-disabled={atEnd || undefined}\n disabled={disabled}\n className={s.Segment}\n data-segment={kind}\n data-remove={remove ? \"\" : undefined}\n data-disabled={atEnd || disabled ? \"\" : undefined}\n data-pressed={pressed === kind && !disabled ? \"\" : undefined}\n // Keeps the focus where it is, as a native stepper takes none.\n onMouseDown={(event) => event.preventDefault()}\n onPointerDown={(event) => startPress(kind, event)}\n onPointerUp={stopRepeat}\n onPointerCancel={stopRepeat}\n onLostPointerCapture={stopRepeat}\n onClick={(event) => handleClick(kind, event)}\n >\n <Icon className={s.Icon} />\n </button>\n )\n }\n\n return (\n <div\n id={id}\n role=\"spinbutton\"\n tabIndex={disabled ? -1 : 0}\n aria-valuenow={value}\n aria-valuemin={min}\n aria-valuemax={max}\n aria-label={ariaLabel}\n aria-labelledby={ariaLabelledBy}\n aria-describedby={ariaDescribedBy}\n aria-keyshortcuts={removes ? \"Delete\" : undefined}\n aria-disabled={disabled || undefined}\n className={clsx(s.Stepper, className)}\n data-disabled={disabled ? \"\" : undefined}\n data-pill={pill ? \"\" : undefined}\n onKeyDown={handleKeyDown}\n onFocus={onFocus}\n onBlur={onBlur}\n >\n {segment(\"decrement\")}\n <span className={s.Separator} aria-hidden=\"true\" />\n {segment(\"increment\")}\n {name !== undefined && <input type=\"hidden\" name={name} value={value} disabled={disabled} />}\n </div>\n )\n}\n"]}
|
|
1
|
+
{"version":3,"file":"Stepper.js","sourceRoot":"","sources":["../../../../src/components/Stepper/Stepper.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAKL,WAAW,EACX,SAAS,EACT,MAAM,EACN,QAAQ,GACT,MAAM,OAAO,CAAA;AACd,OAAO,CAAC,MAAM,sDAAsD,CAAA;AACpE,OAAO,EAAE,cAA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,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,mBACZ,KAAK,mBACL,GAAG,mBACH,GAAG,gBACN,SAAS,qBACJ,cAAc,sBACb,eAAe,uBACd,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,mBAClC,QAAQ,IAAI,SAAS,EACpC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,SAAS,CAAC,mBACtB,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,eAC7B,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,qBACf,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAC3C,SAAS,EAAE,aAAa,EACxB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,aAEb,OAAO,CAAC,WAAW,CAAC,EAEpB,SAAS,CAAC,CAAC,CAAC,CACX,eAAM,SAAS,EAAE,CAAC,CAAC,KAAK,iBAAc,MAAM,YACzC,KAAK,GACD,CACR,CAAC,CAAC,CAAC,CACF,eAAM,SAAS,EAAE,CAAC,CAAC,SAAS,iBAAc,MAAM,GAAG,CACpD,EACA,OAAO,CAAC,WAAW,CAAC,EACpB,IAAI,KAAK,SAAS,IAAI,gBAAO,IAAI,EAAC,QAAQ,EAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,GAAI,IACxF,CACP,CAAA;AACH,CAAC","sourcesContent":["\"use client\"\n\nimport clsx from \"clsx\"\nimport {\n type FocusEventHandler,\n type KeyboardEvent,\n type MouseEvent,\n type PointerEvent,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from \"react\"\nimport s from \"@plexui/styles/components/Stepper/Stepper.module.css\"\nimport { useLatestValue } from \"../../hooks/useLatestValue\"\nimport { Minus, Plus, RemoveTrash } from \"../Icon\"\n\nexport type StepperProps = {\n /** The `id` of the stepper. */\n id?: string\n /** The controlled value. Use with `onChange`. */\n value?: number\n /** The value when it is initially rendered. Use when you do not need to control the value. @default min */\n defaultValue?: number\n /** Called with the next value after a step. */\n onChange?: (value: number) => void\n /** The lowest value. @default 0 */\n min?: number\n /** The highest value. @default 100 */\n max?: number\n /** The amount each step adds or removes. @default 1 */\n step?: number\n /** A step past `max` continues from `min`, and one below `min` from `max`. @default false */\n wrap?: boolean\n /** Holding a segment keeps stepping, as a native stepper does. @default true */\n autoRepeat?: boolean\n /** When `true`, prevents the user from interacting with the stepper. */\n disabled?: boolean\n /** Fully rounded ends. @default true */\n pill?: boolean\n /**\n * Shows the value between the minus and the plus on one fill, as a cart's quantity: round segments and no\n * separator. Without it the stepper shows no value, as the native one. @default false\n */\n showValue?: boolean\n /** Accessible name of the decrement segment. @default \"Decrease\" */\n decrementLabel?: string\n /** Accessible name of the increment segment. @default \"Increase\" */\n incrementLabel?: string\n /**\n * Called when the decrement segment is pressed at `min`. With it, the segment shows a trash can there and removes\n * the item, as a cart's stepper does at a quantity of one (without `wrap`).\n */\n onRemove?: () => void\n /** Accessible name of the decrement segment when it removes. @default \"Remove\" */\n removeLabel?: string\n /** The name of the stepper. Submits its value with the owning form. */\n name?: string\n /** Accessible name of the value the stepper changes, when no visible label names it. */\n \"aria-label\"?: string\n /** The `id` of the visible label of the value the stepper changes. */\n \"aria-labelledby\"?: string\n /** The `id` of an element that describes the stepper, such as the text that shows its value. */\n \"aria-describedby\"?: string\n /** Event handler called when the stepper gains focus. */\n onFocus?: FocusEventHandler<HTMLDivElement>\n /** Event handler called when the stepper loses focus. */\n onBlur?: FocusEventHandler<HTMLDivElement>\n /** CSS classes applied to the stepper */\n className?: string\n}\n\n/** Delay before a held segment starts repeating, and the time between repeats, in ms. */\nconst REPEAT_DELAY = 400\nconst REPEAT_INTERVAL = 100\n\nconst decimals = (n: number) => {\n const [, fraction = \"\"] = String(n).split(\".\")\n return fraction.length\n}\n\n/** The value one step away, clamped to the range or wrapped, without floating point drift. */\nexport const stepValue = (current: number, direction: 1 | -1, { min, max, step, wrap }: { min: number; max: number; step: number; wrap: boolean }) => {\n const precision = Math.max(decimals(step), decimals(min), decimals(current))\n const next = Number((current + direction * step).toFixed(precision))\n if (next > max) return wrap ? min : max\n if (next < min) return wrap ? max : min\n return next\n}\n\n/**\n * Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own\n * (`showValue` puts it between the segments): name it with `aria-label` or `aria-labelledby`. Keyboard focus lands on the\n * stepper, a spin button: Up and Down step, Page Up and Page Down step ten times, Home and End go to the range ends.\n */\nexport function Stepper({\n id,\n value: valueProp,\n defaultValue,\n onChange,\n min = 0,\n max = 100,\n step = 1,\n wrap = false,\n autoRepeat = true,\n disabled = false,\n pill = true,\n showValue = false,\n decrementLabel = \"Decrease\",\n incrementLabel = \"Increase\",\n onRemove,\n removeLabel = \"Remove\",\n name,\n \"aria-label\": ariaLabel,\n \"aria-labelledby\": ariaLabelledBy,\n \"aria-describedby\": ariaDescribedBy,\n onFocus,\n onBlur,\n className,\n}: StepperProps) {\n const [internalValue, setInternalValue] = useState(() => defaultValue ?? min)\n const value = valueProp ?? internalValue\n // Timers and key handlers read the value through this ref; an uncontrolled step advances it at once.\n const valueRef = useLatestValue(value)\n const onChangeRef = useLatestValue(onChange)\n const optionsRef = useLatestValue({ min, max, step, wrap, disabled, controlled: valueProp !== undefined })\n const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)\n const [pressed, setPressed] = useState<\"decrement\" | \"increment\" | null>(null)\n\n const commit = useCallback((next: number) => {\n if (next === valueRef.current) return false\n // A controlled value changes only when the owner passes it back, as in Vue.\n if (!optionsRef.current.controlled) {\n valueRef.current = next\n setInternalValue(next)\n }\n onChangeRef.current?.(next)\n return true\n }, [valueRef, optionsRef, onChangeRef])\n\n const stepBy = useCallback((direction: 1 | -1, times = 1) => {\n let next = valueRef.current\n for (let i = 0; i < times; i++) next = stepValue(next, direction, optionsRef.current)\n return commit(next)\n }, [commit, valueRef, optionsRef])\n\n const stopRepeat = useCallback(() => {\n clearTimeout(timer.current)\n timer.current = undefined\n setPressed(null)\n }, [])\n\n useEffect(() => stopRepeat, [stopRepeat])\n\n const atMin = !wrap && value <= min\n const atMax = !wrap && value >= max\n // At the minimum the decrement segment removes the item, when the owner can remove it\n const removes = atMin && onRemove !== undefined\n const onRemoveRef = useLatestValue(onRemove)\n\n const startPress = (segment: \"decrement\" | \"increment\", event: PointerEvent<HTMLButtonElement>) => {\n if (event.button !== 0 || disabled) return\n if (segment === \"decrement\" && removes) {\n event.currentTarget.setPointerCapture?.(event.pointerId)\n setPressed(segment)\n onRemoveRef.current?.()\n return\n }\n if (segment === \"decrement\" ? atMin : atMax) return\n event.currentTarget.setPointerCapture?.(event.pointerId)\n setPressed(segment)\n const direction = segment === \"decrement\" ? -1 : 1\n stepBy(direction)\n if (!autoRepeat) return\n const repeat = () => {\n if (optionsRef.current.disabled || !stepBy(direction)) return stopRepeat()\n timer.current = setTimeout(repeat, REPEAT_INTERVAL)\n }\n timer.current = setTimeout(repeat, REPEAT_DELAY)\n }\n\n // A pointer steps on press, so only a click without one (assistive technology, `element.click()`) steps here.\n const handleClick = (segment: \"decrement\" | \"increment\", event: MouseEvent<HTMLButtonElement>) => {\n if (event.detail !== 0 || disabled) return\n if (segment === \"decrement\" && removes) return onRemoveRef.current?.()\n stepBy(segment === \"decrement\" ? -1 : 1)\n }\n\n const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {\n if (disabled || event.altKey || event.ctrlKey || event.metaKey) return\n const keys: Record<string, () => void> = {\n ArrowUp: () => stepBy(1),\n ArrowDown: () => stepBy(-1),\n PageUp: () => stepBy(1, 10),\n PageDown: () => stepBy(-1, 10),\n Home: () => commit(min),\n End: () => commit(max),\n // At the minimum Delete or Backspace removes, as the trash can does; the arrows never do\n ...(removes && { Delete: () => onRemoveRef.current?.(), Backspace: () => onRemoveRef.current?.() }),\n }\n const action = keys[event.key]\n if (!action) return\n event.preventDefault()\n action()\n }\n\n const segment = (kind: \"decrement\" | \"increment\") => {\n const remove = kind === \"decrement\" && removes\n const atEnd = !remove && (kind === \"decrement\" ? atMin : atMax)\n const Icon = remove ? RemoveTrash : kind === \"decrement\" ? Minus : Plus\n return (\n <button\n type=\"button\"\n tabIndex={-1}\n aria-label={remove ? removeLabel : kind === \"decrement\" ? decrementLabel : incrementLabel}\n aria-disabled={atEnd || undefined}\n disabled={disabled}\n className={s.Segment}\n data-segment={kind}\n data-remove={remove ? \"\" : undefined}\n data-disabled={atEnd || disabled ? \"\" : undefined}\n data-pressed={pressed === kind && !disabled ? \"\" : undefined}\n // Keeps the focus where it is, as a native stepper takes none.\n onMouseDown={(event) => event.preventDefault()}\n onPointerDown={(event) => startPress(kind, event)}\n onPointerUp={stopRepeat}\n onPointerCancel={stopRepeat}\n onLostPointerCapture={stopRepeat}\n onClick={(event) => handleClick(kind, event)}\n >\n <Icon className={s.Icon} />\n </button>\n )\n }\n\n return (\n <div\n id={id}\n role=\"spinbutton\"\n tabIndex={disabled ? -1 : 0}\n aria-valuenow={value}\n aria-valuemin={min}\n aria-valuemax={max}\n aria-label={ariaLabel}\n aria-labelledby={ariaLabelledBy}\n aria-describedby={ariaDescribedBy}\n aria-keyshortcuts={removes ? \"Delete\" : undefined}\n aria-disabled={disabled || undefined}\n className={clsx(s.Stepper, className)}\n data-disabled={disabled ? \"\" : undefined}\n data-pill={pill ? \"\" : undefined}\n data-show-value={showValue ? \"\" : undefined}\n onKeyDown={handleKeyDown}\n onFocus={onFocus}\n onBlur={onBlur}\n >\n {segment(\"decrement\")}\n {/* The spin button already reads its value, so the shown one is not read again */}\n {showValue ? (\n <span className={s.Value} aria-hidden=\"true\">\n {value}\n </span>\n ) : (\n <span className={s.Separator} aria-hidden=\"true\" />\n )}\n {segment(\"increment\")}\n {name !== undefined && <input type=\"hidden\" name={name} value={value} disabled={disabled} />}\n </div>\n )\n}\n"]}
|
|
@@ -22,6 +22,11 @@ export type StepperProps = {
|
|
|
22
22
|
disabled?: boolean;
|
|
23
23
|
/** Fully rounded ends. @default true */
|
|
24
24
|
pill?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Shows the value between the minus and the plus on one fill, as a cart's quantity: round segments and no
|
|
27
|
+
* separator. Without it the stepper shows no value, as the native one. @default false
|
|
28
|
+
*/
|
|
29
|
+
showValue?: boolean;
|
|
25
30
|
/** Accessible name of the decrement segment. @default "Decrease" */
|
|
26
31
|
decrementLabel?: string;
|
|
27
32
|
/** Accessible name of the increment segment. @default "Increase" */
|
|
@@ -56,8 +61,8 @@ export declare const stepValue: (current: number, direction: 1 | -1, { min, max,
|
|
|
56
61
|
wrap: boolean;
|
|
57
62
|
}) => number;
|
|
58
63
|
/**
|
|
59
|
-
* Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own
|
|
60
|
-
* name it with `aria-label` or `aria-labelledby
|
|
64
|
+
* Two segments that decrease and increase a number, as the iOS 26 stepper does. It shows no value of its own
|
|
65
|
+
* (`showValue` puts it between the segments): name it with `aria-label` or `aria-labelledby`. Keyboard focus lands on the
|
|
61
66
|
* stepper, a spin button: Up and Down step, Page Up and Page Down step ten times, Home and End go to the range ends.
|
|
62
67
|
*/
|
|
63
|
-
export declare function Stepper({ id, value: valueProp, defaultValue, onChange, min, max, step, wrap, autoRepeat, disabled, pill, decrementLabel, incrementLabel, onRemove, removeLabel, name, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, onFocus, onBlur, className, }: StepperProps): import("react/jsx-runtime").JSX.Element;
|
|
68
|
+
export declare function Stepper({ id, value: valueProp, defaultValue, onChange, min, max, step, wrap, autoRepeat, disabled, pill, showValue, decrementLabel, incrementLabel, onRemove, removeLabel, name, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, onFocus, onBlur, className, }: StepperProps): import("react/jsx-runtime").JSX.Element;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plexui/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.57.0",
|
|
4
4
|
"description": "Modern design system for building high-quality applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -99,7 +99,7 @@
|
|
|
99
99
|
"@maskito/core": "^5.2.2",
|
|
100
100
|
"@maskito/kit": "^5.2.2",
|
|
101
101
|
"@maskito/react": "^5.2.2",
|
|
102
|
-
"@plexui/styles": "^0.
|
|
102
|
+
"@plexui/styles": "^0.15.0",
|
|
103
103
|
"@radix-ui/react-dismissable-layer": "1.2.1",
|
|
104
104
|
"@radix-ui/react-focus-guards": "1.2.0",
|
|
105
105
|
"@radix-ui/react-focus-scope": "1.3.1",
|