@zag-js/number-input 0.53.0 → 0.54.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.mts +7 -7
- package/dist/index.d.ts +7 -7
- package/dist/index.js +871 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +842 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/number-input.connect.ts +199 -185
- package/src/number-input.types.ts +7 -7
package/dist/index.js
CHANGED
|
@@ -1,8 +1,877 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
anatomy: () => anatomy,
|
|
24
|
+
connect: () => connect,
|
|
25
|
+
machine: () => machine
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/number-input.anatomy.ts
|
|
30
|
+
var import_anatomy = require("@zag-js/anatomy");
|
|
31
|
+
var anatomy = (0, import_anatomy.createAnatomy)("numberInput").parts(
|
|
32
|
+
"root",
|
|
33
|
+
"label",
|
|
34
|
+
"input",
|
|
35
|
+
"control",
|
|
36
|
+
"incrementTrigger",
|
|
37
|
+
"decrementTrigger",
|
|
38
|
+
"scrubber"
|
|
39
|
+
);
|
|
40
|
+
var parts = anatomy.build();
|
|
41
|
+
|
|
42
|
+
// src/number-input.connect.ts
|
|
43
|
+
var import_dom_event = require("@zag-js/dom-event");
|
|
44
|
+
var import_dom_query2 = require("@zag-js/dom-query");
|
|
45
|
+
var import_number_utils2 = require("@zag-js/number-utils");
|
|
46
|
+
|
|
47
|
+
// src/number-input.dom.ts
|
|
48
|
+
var import_dom_query = require("@zag-js/dom-query");
|
|
49
|
+
var import_number_utils = require("@zag-js/number-utils");
|
|
50
|
+
var dom = (0, import_dom_query.createScope)({
|
|
51
|
+
getRootId: (ctx) => ctx.ids?.root ?? `number-input:${ctx.id}`,
|
|
52
|
+
getInputId: (ctx) => ctx.ids?.input ?? `number-input:${ctx.id}:input`,
|
|
53
|
+
getIncrementTriggerId: (ctx) => ctx.ids?.incrementTrigger ?? `number-input:${ctx.id}:inc`,
|
|
54
|
+
getDecrementTriggerId: (ctx) => ctx.ids?.decrementTrigger ?? `number-input:${ctx.id}:dec`,
|
|
55
|
+
getScrubberId: (ctx) => ctx.ids?.scrubber ?? `number-input:${ctx.id}:scrubber`,
|
|
56
|
+
getCursorId: (ctx) => `number-input:${ctx.id}:cursor`,
|
|
57
|
+
getLabelId: (ctx) => ctx.ids?.label ?? `number-input:${ctx.id}:label`,
|
|
58
|
+
getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
|
|
59
|
+
getIncrementTriggerEl: (ctx) => dom.getById(ctx, dom.getIncrementTriggerId(ctx)),
|
|
60
|
+
getDecrementTriggerEl: (ctx) => dom.getById(ctx, dom.getDecrementTriggerId(ctx)),
|
|
61
|
+
getScrubberEl: (ctx) => dom.getById(ctx, dom.getScrubberId(ctx)),
|
|
62
|
+
getCursorEl: (ctx) => dom.getDoc(ctx).getElementById(dom.getCursorId(ctx)),
|
|
63
|
+
getPressedTriggerEl: (ctx, hint = ctx.hint) => {
|
|
64
|
+
let btnEl = null;
|
|
65
|
+
if (hint === "increment") {
|
|
66
|
+
btnEl = dom.getIncrementTriggerEl(ctx);
|
|
67
|
+
}
|
|
68
|
+
if (hint === "decrement") {
|
|
69
|
+
btnEl = dom.getDecrementTriggerEl(ctx);
|
|
70
|
+
}
|
|
71
|
+
return btnEl;
|
|
72
|
+
},
|
|
73
|
+
setupVirtualCursor(ctx) {
|
|
74
|
+
if ((0, import_dom_query.isSafari)())
|
|
75
|
+
return;
|
|
76
|
+
dom.createVirtualCursor(ctx);
|
|
77
|
+
return () => {
|
|
78
|
+
dom.getCursorEl(ctx)?.remove();
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
preventTextSelection(ctx) {
|
|
82
|
+
const doc = dom.getDoc(ctx);
|
|
83
|
+
const html = doc.documentElement;
|
|
84
|
+
const body = doc.body;
|
|
85
|
+
body.style.pointerEvents = "none";
|
|
86
|
+
html.style.userSelect = "none";
|
|
87
|
+
html.style.cursor = "ew-resize";
|
|
88
|
+
return () => {
|
|
89
|
+
body.style.pointerEvents = "";
|
|
90
|
+
html.style.userSelect = "";
|
|
91
|
+
html.style.cursor = "";
|
|
92
|
+
if (!html.style.length) {
|
|
93
|
+
html.removeAttribute("style");
|
|
94
|
+
}
|
|
95
|
+
if (!body.style.length) {
|
|
96
|
+
body.removeAttribute("style");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
getMousementValue(ctx, event) {
|
|
101
|
+
const x = (0, import_number_utils.roundToDevicePixel)(event.movementX);
|
|
102
|
+
const y = (0, import_number_utils.roundToDevicePixel)(event.movementY);
|
|
103
|
+
let hint = x > 0 ? "increment" : x < 0 ? "decrement" : null;
|
|
104
|
+
if (ctx.isRtl && hint === "increment")
|
|
105
|
+
hint = "decrement";
|
|
106
|
+
if (ctx.isRtl && hint === "decrement")
|
|
107
|
+
hint = "increment";
|
|
108
|
+
const point = {
|
|
109
|
+
x: ctx.scrubberCursorPoint.x + x,
|
|
110
|
+
y: ctx.scrubberCursorPoint.y + y
|
|
111
|
+
};
|
|
112
|
+
const win = dom.getWin(ctx);
|
|
113
|
+
const width = win.innerWidth;
|
|
114
|
+
const half = (0, import_number_utils.roundToDevicePixel)(7.5);
|
|
115
|
+
point.x = (0, import_number_utils.wrap)(point.x + half, width) - half;
|
|
116
|
+
return { hint, point };
|
|
117
|
+
},
|
|
118
|
+
createVirtualCursor(ctx) {
|
|
119
|
+
const doc = dom.getDoc(ctx);
|
|
120
|
+
const el = doc.createElement("div");
|
|
121
|
+
el.className = "scrubber--cursor";
|
|
122
|
+
el.id = dom.getCursorId(ctx);
|
|
123
|
+
Object.assign(el.style, {
|
|
124
|
+
width: "15px",
|
|
125
|
+
height: "15px",
|
|
126
|
+
position: "fixed",
|
|
127
|
+
pointerEvents: "none",
|
|
128
|
+
left: "0px",
|
|
129
|
+
top: "0px",
|
|
130
|
+
zIndex: import_dom_query.MAX_Z_INDEX,
|
|
131
|
+
transform: ctx.scrubberCursorPoint ? `translate3d(${ctx.scrubberCursorPoint.x}px, ${ctx.scrubberCursorPoint.y}px, 0px)` : void 0,
|
|
132
|
+
willChange: "transform"
|
|
133
|
+
});
|
|
134
|
+
el.innerHTML = `
|
|
2
135
|
<svg width="46" height="15" style="left: -15.5px; position: absolute; top: 0; filter: drop-shadow(rgba(0, 0, 0, 0.4) 0px 1px 1.1px);">
|
|
3
136
|
<g transform="translate(2 3)">
|
|
4
137
|
<path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z" style="stroke-width: 2px; stroke: white;"></path>
|
|
5
138
|
<path fill-rule="evenodd" d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z"></path>
|
|
6
139
|
</g>
|
|
7
|
-
</svg>`;doc.body.appendChild(el)}});function connect(state,send,normalize){const focused=state.hasTag("focus");const disabled=state.context.isDisabled;const readOnly=state.context.readOnly;const empty=state.context.isValueEmpty;const invalid=state.context.isOutOfRange||!!state.context.invalid;const isIncrementDisabled=disabled||!state.context.canIncrement||readOnly;const isDecrementDisabled=disabled||!state.context.canDecrement||readOnly;const translations=state.context.translations;return{focused,invalid,empty,value:state.context.formattedValue,valueAsNumber:state.context.valueAsNumber,setValue(value){send({type:"VALUE.SET",value})},clearValue(){send("VALUE.CLEAR")},increment(){send("VALUE.INCREMENT")},decrement(){send("VALUE.DECREMENT")},setToMax(){send({type:"VALUE.SET",value:state.context.max})},setToMin(){send({type:"VALUE.SET",value:state.context.min})},focus(){dom.getInputEl(state.context)?.focus()},rootProps:normalize.element({id:dom.getRootId(state.context),...parts.root.attrs,dir:state.context.dir,"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"data-focus":(0,import_dom_query2.dataAttr)(focused),"data-invalid":(0,import_dom_query2.dataAttr)(invalid)}),labelProps:normalize.label({...parts.label.attrs,dir:state.context.dir,"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"data-focus":(0,import_dom_query2.dataAttr)(focused),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),id:dom.getLabelId(state.context),htmlFor:dom.getInputId(state.context)}),controlProps:normalize.element({...parts.control.attrs,dir:state.context.dir,role:"group","aria-disabled":disabled,"data-focus":(0,import_dom_query2.dataAttr)(focused),"data-disabled":(0,import_dom_query2.dataAttr)(disabled),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),"aria-invalid":(0,import_dom_query2.ariaAttr)(state.context.invalid)}),inputProps:normalize.input({...parts.input.attrs,dir:state.context.dir,name:state.context.name,form:state.context.form,id:dom.getInputId(state.context),role:"spinbutton",defaultValue:state.context.formattedValue,pattern:state.context.pattern,inputMode:state.context.inputMode,"aria-invalid":(0,import_dom_query2.ariaAttr)(invalid),"data-invalid":(0,import_dom_query2.dataAttr)(invalid),disabled,"data-disabled":(0,import_dom_query2.dataAttr)(disabled),readOnly:!!state.context.readOnly,autoComplete:"off",autoCorrect:"off",spellCheck:"false",type:"text","aria-roledescription":"numberfield","aria-valuemin":state.context.min,"aria-valuemax":state.context.max,"aria-valuenow":Number.isNaN(state.context.valueAsNumber)?void 0:state.context.valueAsNumber,"aria-valuetext":state.context.valueText,onFocus(){send("INPUT.FOCUS")},onBlur(){send({type:"INPUT.COMMIT",src:"blur"})},onChange(event){send({type:"INPUT.CHANGE",target:event.currentTarget,hint:"set"})},onBeforeInput(event){try{const{selectionStart,selectionEnd,value}=event.currentTarget;const nextValue=value.slice(0,selectionStart)+(event.data??"")+value.slice(selectionEnd);const isValid=state.context.parser.isValidPartialNumber(nextValue);if(!isValid){event.preventDefault()}}catch{}},onKeyDown(event){if(event.defaultPrevented)return;if(readOnly)return;if((0,import_dom_query2.isComposingEvent)(event))return;const step=(0,import_dom_event.getEventStep)(event)*state.context.step;const keyMap={ArrowUp(){send({type:"INPUT.ARROW_UP",step});event.preventDefault()},ArrowDown(){send({type:"INPUT.ARROW_DOWN",step});event.preventDefault()},Home(){send("INPUT.HOME");event.preventDefault()},End(){send("INPUT.END");event.preventDefault()},Enter(){send({type:"INPUT.COMMIT",src:"enter"})}};const exec=keyMap[event.key];exec?.(event)}}),decrementTriggerProps:normalize.button({...parts.decrementTrigger.attrs,dir:state.context.dir,id:dom.getDecrementTriggerId(state.context),disabled:isDecrementDisabled,"data-disabled":(0,import_dom_query2.dataAttr)(isDecrementDisabled),"aria-label":translations.decrementLabel,type:"button",tabIndex:-1,"aria-controls":dom.getInputId(state.context),onPointerDown(event){if(isDecrementDisabled||!(0,import_dom_event.isLeftClick)(event))return;send({type:"TRIGGER.PRESS_DOWN",hint:"decrement",pointerType:event.pointerType});if(event.pointerType==="mouse"){event.preventDefault()}if(event.pointerType==="touch"){event.currentTarget?.focus({preventScroll:true})}},onPointerUp(event){send({type:"TRIGGER.PRESS_UP",hint:"decrement",pointerType:event.pointerType})},onPointerLeave(){if(isDecrementDisabled)return;send({type:"TRIGGER.PRESS_UP",hint:"decrement"})}}),incrementTriggerProps:normalize.button({...parts.incrementTrigger.attrs,dir:state.context.dir,id:dom.getIncrementTriggerId(state.context),disabled:isIncrementDisabled,"data-disabled":(0,import_dom_query2.dataAttr)(isIncrementDisabled),"aria-label":translations.incrementLabel,type:"button",tabIndex:-1,"aria-controls":dom.getInputId(state.context),onPointerDown(event){if(isIncrementDisabled||!(0,import_dom_event.isLeftClick)(event))return;send({type:"TRIGGER.PRESS_DOWN",hint:"increment",pointerType:event.pointerType});if(event.pointerType==="mouse"){event.preventDefault()}if(event.pointerType==="touch"){event.currentTarget?.focus({preventScroll:true})}},onPointerUp(event){send({type:"TRIGGER.PRESS_UP",hint:"increment",pointerType:event.pointerType})},onPointerLeave(event){send({type:"TRIGGER.PRESS_UP",hint:"increment",pointerType:event.pointerType})}}),scrubberProps:normalize.element({...parts.scrubber.attrs,dir:state.context.dir,"data-disabled":(0,import_dom_query2.dataAttr)(disabled),id:dom.getScrubberId(state.context),role:"presentation",onMouseDown(event){if(disabled)return;const point=(0,import_dom_event.getEventPoint)(event);point.x=point.x-(0,import_number_utils2.roundToDevicePixel)(7.5);point.y=point.y-(0,import_number_utils2.roundToDevicePixel)(7.5);send({type:"SCRUBBER.PRESS_DOWN",point});event.preventDefault()},style:{cursor:disabled?void 0:"ew-resize"}})}}var import_core2=require("@zag-js/core");var import_dom_event2=require("@zag-js/dom-event");var import_dom_query3=require("@zag-js/dom-query");var import_form_utils=require("@zag-js/form-utils");var import_number_utils3=require("@zag-js/number-utils");var import_utils=require("@zag-js/utils");function recordCursor(inputEl){if(inputEl.ownerDocument.activeElement!==inputEl)return;try{const{selectionStart:start,selectionEnd:end,value}=inputEl;const beforeTxt=value.substring(0,start);const afterTxt=value.substring(end);return{start,end,value,beforeTxt,afterTxt}}catch{}}function restoreCursor(inputEl,selection){if(inputEl.ownerDocument.activeElement!==inputEl)return;if(!selection){inputEl.setSelectionRange(inputEl.value.length,inputEl.value.length);return}try{const{value}=inputEl;const{beforeTxt="",afterTxt="",start}=selection;let startPos=value.length;if(value.endsWith(afterTxt)){startPos=value.length-afterTxt.length}else if(value.startsWith(beforeTxt)){startPos=beforeTxt.length}else if(start!=null){const beforeLastChar=beforeTxt[start-1];const newIndex=value.indexOf(beforeLastChar,start-1);if(newIndex!==-1){startPos=newIndex+1}}inputEl.setSelectionRange(startPos,startPos)}catch{}}var import_number=require("@internationalized/number");var import_core=require("@zag-js/core");var createFormatter=(locale,options={})=>{return(0,import_core.ref)(new Intl.NumberFormat(locale,options))};var createParser=(locale,options={})=>{return(0,import_core.ref)(new import_number.NumberParser(locale,options))};var parseValue=(ctx,value)=>{if(!ctx.formatOptions)return parseFloat(value);return ctx.parser.parse(String(value))};var formatValue=(ctx,value)=>{if(Number.isNaN(value))return"";if(!ctx.formatOptions)return value.toString();return ctx.formatter.format(value)};var{not,and}=import_core2.guards;function machine(userContext){const ctx=(0,import_utils.compact)(userContext);return(0,import_core2.createMachine)({id:"number-input",initial:"idle",context:{dir:"ltr",locale:"en-US",focusInputOnChange:true,clampValueOnBlur:true,allowOverflow:false,inputMode:"decimal",pattern:"[0-9]*(.[0-9]+)?",value:"",step:1,min:Number.MIN_SAFE_INTEGER,max:Number.MAX_SAFE_INTEGER,invalid:false,spinOnPress:true,...ctx,hint:null,scrubberCursorPoint:null,fieldsetDisabled:false,formatter:createFormatter(ctx.locale||"en-US",ctx.formatOptions),parser:createParser(ctx.locale||"en-US",ctx.formatOptions),translations:{incrementLabel:"increment value",decrementLabel:"decrease value",...ctx.translations}},computed:{isRtl:ctx2=>ctx2.dir==="rtl",valueAsNumber:ctx2=>parseValue(ctx2,ctx2.value),formattedValue:ctx2=>formatValue(ctx2,ctx2.valueAsNumber),isAtMin:ctx2=>(0,import_number_utils3.isAtMin)(ctx2.valueAsNumber,ctx2),isAtMax:ctx2=>(0,import_number_utils3.isAtMax)(ctx2.valueAsNumber,ctx2),isOutOfRange:ctx2=>!(0,import_number_utils3.isWithinRange)(ctx2.valueAsNumber,ctx2),isValueEmpty:ctx2=>ctx2.value==="",isDisabled:ctx2=>!!ctx2.disabled||ctx2.fieldsetDisabled,canIncrement:ctx2=>ctx2.allowOverflow||!ctx2.isAtMax,canDecrement:ctx2=>ctx2.allowOverflow||!ctx2.isAtMin,valueText:ctx2=>ctx2.translations.valueText?.(ctx2.value)},watch:{formatOptions:["setFormatterAndParser","syncInputElement"],locale:["setFormatterAndParser","syncInputElement"],value:["syncInputElement"],isOutOfRange:["invokeOnInvalid"],scrubberCursorPoint:["setVirtualCursorPosition"]},activities:["trackFormControl"],on:{"VALUE.SET":{actions:["setRawValue","setHintToSet"]},"VALUE.CLEAR":{actions:["clearValue"]},"VALUE.INCREMENT":{actions:["increment"]},"VALUE.DECREMENT":{actions:["decrement"]}},states:{idle:{on:{"TRIGGER.PRESS_DOWN":[{guard:"isTouchPointer",target:"before:spin",actions:["setHint"]},{target:"before:spin",actions:["focusInput","invokeOnFocus","setHint"]}],"SCRUBBER.PRESS_DOWN":{target:"scrubbing",actions:["focusInput","invokeOnFocus","setHint","setCursorPoint"]},"INPUT.FOCUS":{target:"focused",actions:["focusInput","invokeOnFocus"]}}},focused:{tags:"focus",activities:"attachWheelListener",on:{"TRIGGER.PRESS_DOWN":[{guard:"isTouchPointer",target:"before:spin",actions:["setHint"]},{target:"before:spin",actions:["focusInput","setHint"]}],"SCRUBBER.PRESS_DOWN":{target:"scrubbing",actions:["focusInput","setHint","setCursorPoint"]},"INPUT.ARROW_UP":{actions:"increment"},"INPUT.ARROW_DOWN":{actions:"decrement"},"INPUT.HOME":{actions:"decrementToMin"},"INPUT.END":{actions:"incrementToMax"},"INPUT.CHANGE":{actions:["setValue","setHint"]},"INPUT.COMMIT":[{guard:and("clampValueOnBlur",not("isInRange")),target:"idle",actions:["setClampedValue","clearHint","invokeOnBlur"]},{target:"idle",actions:["setFormattedValue","clearHint","invokeOnBlur"]}]}},"before:spin":{tags:"focus",activities:"trackButtonDisabled",entry:(0,import_core2.choose)([{guard:"isIncrementHint",actions:"increment"},{guard:"isDecrementHint",actions:"decrement"}]),after:{CHANGE_DELAY:{target:"spinning",guard:and("isInRange","spinOnPress")}},on:{"TRIGGER.PRESS_UP":[{guard:"isTouchPointer",target:"focused",actions:"clearHint"},{target:"focused",actions:["focusInput","clearHint"]}]}},spinning:{tags:"focus",activities:"trackButtonDisabled",every:[{delay:"CHANGE_INTERVAL",guard:and(not("isAtMin"),"isIncrementHint"),actions:"increment"},{delay:"CHANGE_INTERVAL",guard:and(not("isAtMax"),"isDecrementHint"),actions:"decrement"}],on:{"TRIGGER.PRESS_UP":{target:"focused",actions:["focusInput","clearHint"]}}},scrubbing:{tags:"focus",activities:["activatePointerLock","trackMousemove","setupVirtualCursor","preventTextSelection"],on:{"SCRUBBER.POINTER_UP":{target:"focused",actions:["focusInput","clearCursorPoint"]},"SCRUBBER.POINTER_MOVE":[{guard:"isIncrementHint",actions:["increment","setCursorPoint"]},{guard:"isDecrementHint",actions:["decrement","setCursorPoint"]}]}}}},{delays:{CHANGE_INTERVAL:50,CHANGE_DELAY:300},guards:{clampValueOnBlur:ctx2=>ctx2.clampValueOnBlur,isAtMin:ctx2=>ctx2.isAtMin,spinOnPress:ctx2=>!!ctx2.spinOnPress,isAtMax:ctx2=>ctx2.isAtMax,isInRange:ctx2=>!ctx2.isOutOfRange,isDecrementHint:(ctx2,evt)=>(evt.hint??ctx2.hint)==="decrement",isIncrementHint:(ctx2,evt)=>(evt.hint??ctx2.hint)==="increment",isTouchPointer:(_ctx,evt)=>evt.pointerType==="touch"},activities:{trackFormControl(ctx2,_evt,{initialContext}){const inputEl=dom.getInputEl(ctx2);return(0,import_form_utils.trackFormControl)(inputEl,{onFieldsetDisabledChange(disabled){ctx2.fieldsetDisabled=disabled},onFormReset(){set.value(ctx2,initialContext.value)}})},setupVirtualCursor(ctx2){return dom.setupVirtualCursor(ctx2)},preventTextSelection(ctx2){return dom.preventTextSelection(ctx2)},trackButtonDisabled(ctx2,_evt,{send}){const btn=dom.getPressedTriggerEl(ctx2,ctx2.hint);return(0,import_dom_query3.observeAttributes)(btn,{attributes:["disabled"],callback(){send({type:"TRIGGER.PRESS_UP",src:"attr"})}})},attachWheelListener(ctx2,_evt,{send}){const inputEl=dom.getInputEl(ctx2);if(!inputEl||!dom.isActiveElement(ctx2,inputEl)||!ctx2.allowMouseWheel)return;function onWheel(event){event.preventDefault();const dir=Math.sign(event.deltaY)*-1;if(dir===1){send("VALUE.INCREMENT")}else if(dir===-1){send("VALUE.DECREMENT")}}return(0,import_dom_event2.addDomEvent)(inputEl,"wheel",onWheel,{passive:false})},activatePointerLock(ctx2){if((0,import_dom_query3.isSafari)())return;return(0,import_dom_event2.requestPointerLock)(dom.getDoc(ctx2))},trackMousemove(ctx2,_evt,{send}){const doc=dom.getDoc(ctx2);function onMousemove(event){if(!ctx2.scrubberCursorPoint)return;const value=dom.getMousementValue(ctx2,event);if(!value.hint)return;send({type:"SCRUBBER.POINTER_MOVE",hint:value.hint,point:value.point})}function onMouseup(){send("SCRUBBER.POINTER_UP")}return(0,import_utils.callAll)((0,import_dom_event2.addDomEvent)(doc,"mousemove",onMousemove,false),(0,import_dom_event2.addDomEvent)(doc,"mouseup",onMouseup,false))}},actions:{focusInput(ctx2){if(!ctx2.focusInputOnChange)return;const inputEl=dom.getInputEl(ctx2);if(dom.isActiveElement(ctx2,inputEl))return;(0,import_dom_query3.raf)(()=>inputEl?.focus({preventScroll:true}))},increment(ctx2,evt){const nextValue=(0,import_number_utils3.increment)(ctx2.valueAsNumber,evt.step??ctx2.step);const value=formatValue(ctx2,(0,import_number_utils3.clamp)(nextValue,ctx2));set.value(ctx2,value)},decrement(ctx2,evt){const nextValue=(0,import_number_utils3.decrement)(ctx2.valueAsNumber,evt.step??ctx2.step);const value=formatValue(ctx2,(0,import_number_utils3.clamp)(nextValue,ctx2));set.value(ctx2,value)},setClampedValue(ctx2){const nextValue=(0,import_number_utils3.clamp)(ctx2.valueAsNumber,ctx2);set.value(ctx2,formatValue(ctx2,nextValue))},setRawValue(ctx2,evt){const parsedValue=parseValue(ctx2,evt.value);const value=formatValue(ctx2,(0,import_number_utils3.clamp)(parsedValue,ctx2));set.value(ctx2,value)},setValue(ctx2,evt){const value=evt.target?.value??evt.value;set.value(ctx2,value)},clearValue(ctx2){set.value(ctx2,"")},incrementToMax(ctx2){const value=formatValue(ctx2,ctx2.max);set.value(ctx2,value)},decrementToMin(ctx2){const value=formatValue(ctx2,ctx2.min);set.value(ctx2,value)},setHint(ctx2,evt){ctx2.hint=evt.hint},clearHint(ctx2){ctx2.hint=null},setHintToSet(ctx2){ctx2.hint="set"},invokeOnFocus(ctx2){ctx2.onFocusChange?.({focused:true,value:ctx2.formattedValue,valueAsNumber:ctx2.valueAsNumber})},invokeOnBlur(ctx2){ctx2.onFocusChange?.({focused:false,value:ctx2.formattedValue,valueAsNumber:ctx2.valueAsNumber})},invokeOnInvalid(ctx2){if(!ctx2.isOutOfRange)return;const reason=ctx2.valueAsNumber>ctx2.max?"rangeOverflow":"rangeUnderflow";ctx2.onValueInvalid?.({reason,value:ctx2.formattedValue,valueAsNumber:ctx2.valueAsNumber})},syncInputElement(ctx2,evt){const value=evt.type.endsWith("CHANGE")?ctx2.value:ctx2.formattedValue;sync.input(ctx2,value)},setFormattedValue(ctx2){set.value(ctx2,ctx2.formattedValue)},setCursorPoint(ctx2,evt){ctx2.scrubberCursorPoint=evt.point},clearCursorPoint(ctx2){ctx2.scrubberCursorPoint=null},setVirtualCursorPosition(ctx2){const cursorEl=dom.getCursorEl(ctx2);if(!cursorEl||!ctx2.scrubberCursorPoint)return;const{x,y}=ctx2.scrubberCursorPoint;cursorEl.style.transform=`translate3d(${x}px, ${y}px, 0px)`},setFormatterAndParser(ctx2){if(!ctx2.locale)return;ctx2.formatter=createFormatter(ctx2.locale,ctx2.formatOptions);ctx2.parser=createParser(ctx2.locale,ctx2.formatOptions)}},compareFns:{formatOptions:(a,b)=>(0,import_utils.isEqual)(a,b),scrubberCursorPoint:(a,b)=>(0,import_utils.isEqual)(a,b)}})}var sync={input(ctx,value){const inputEl=dom.getInputEl(ctx);if(!inputEl)return;const sel=recordCursor(inputEl);(0,import_dom_query3.raf)(()=>{dom.setValue(inputEl,value);restoreCursor(inputEl,sel)})}};var invoke={onChange:ctx=>{ctx.onValueChange?.({value:ctx.value,valueAsNumber:ctx.valueAsNumber})}};var set={value:(ctx,value)=>{if((0,import_utils.isEqual)(ctx.value,value))return;ctx.value=value;invoke.onChange(ctx)}};0&&(module.exports={anatomy,connect,machine});
|
|
140
|
+
</svg>`;
|
|
141
|
+
doc.body.appendChild(el);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// src/number-input.connect.ts
|
|
146
|
+
function connect(state, send, normalize) {
|
|
147
|
+
const focused = state.hasTag("focus");
|
|
148
|
+
const disabled = state.context.isDisabled;
|
|
149
|
+
const readOnly = state.context.readOnly;
|
|
150
|
+
const empty = state.context.isValueEmpty;
|
|
151
|
+
const invalid = state.context.isOutOfRange || !!state.context.invalid;
|
|
152
|
+
const isIncrementDisabled = disabled || !state.context.canIncrement || readOnly;
|
|
153
|
+
const isDecrementDisabled = disabled || !state.context.canDecrement || readOnly;
|
|
154
|
+
const translations = state.context.translations;
|
|
155
|
+
return {
|
|
156
|
+
focused,
|
|
157
|
+
invalid,
|
|
158
|
+
empty,
|
|
159
|
+
value: state.context.formattedValue,
|
|
160
|
+
valueAsNumber: state.context.valueAsNumber,
|
|
161
|
+
setValue(value) {
|
|
162
|
+
send({ type: "VALUE.SET", value });
|
|
163
|
+
},
|
|
164
|
+
clearValue() {
|
|
165
|
+
send("VALUE.CLEAR");
|
|
166
|
+
},
|
|
167
|
+
increment() {
|
|
168
|
+
send("VALUE.INCREMENT");
|
|
169
|
+
},
|
|
170
|
+
decrement() {
|
|
171
|
+
send("VALUE.DECREMENT");
|
|
172
|
+
},
|
|
173
|
+
setToMax() {
|
|
174
|
+
send({ type: "VALUE.SET", value: state.context.max });
|
|
175
|
+
},
|
|
176
|
+
setToMin() {
|
|
177
|
+
send({ type: "VALUE.SET", value: state.context.min });
|
|
178
|
+
},
|
|
179
|
+
focus() {
|
|
180
|
+
dom.getInputEl(state.context)?.focus();
|
|
181
|
+
},
|
|
182
|
+
getRootProps() {
|
|
183
|
+
return normalize.element({
|
|
184
|
+
id: dom.getRootId(state.context),
|
|
185
|
+
...parts.root.attrs,
|
|
186
|
+
dir: state.context.dir,
|
|
187
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(disabled),
|
|
188
|
+
"data-focus": (0, import_dom_query2.dataAttr)(focused),
|
|
189
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid)
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
getLabelProps() {
|
|
193
|
+
return normalize.label({
|
|
194
|
+
...parts.label.attrs,
|
|
195
|
+
dir: state.context.dir,
|
|
196
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(disabled),
|
|
197
|
+
"data-focus": (0, import_dom_query2.dataAttr)(focused),
|
|
198
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
199
|
+
id: dom.getLabelId(state.context),
|
|
200
|
+
htmlFor: dom.getInputId(state.context)
|
|
201
|
+
});
|
|
202
|
+
},
|
|
203
|
+
getControlProps() {
|
|
204
|
+
return normalize.element({
|
|
205
|
+
...parts.control.attrs,
|
|
206
|
+
dir: state.context.dir,
|
|
207
|
+
role: "group",
|
|
208
|
+
"aria-disabled": disabled,
|
|
209
|
+
"data-focus": (0, import_dom_query2.dataAttr)(focused),
|
|
210
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(disabled),
|
|
211
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
212
|
+
"aria-invalid": (0, import_dom_query2.ariaAttr)(state.context.invalid)
|
|
213
|
+
});
|
|
214
|
+
},
|
|
215
|
+
getInputProps() {
|
|
216
|
+
return normalize.input({
|
|
217
|
+
...parts.input.attrs,
|
|
218
|
+
dir: state.context.dir,
|
|
219
|
+
name: state.context.name,
|
|
220
|
+
form: state.context.form,
|
|
221
|
+
id: dom.getInputId(state.context),
|
|
222
|
+
role: "spinbutton",
|
|
223
|
+
defaultValue: state.context.formattedValue,
|
|
224
|
+
pattern: state.context.pattern,
|
|
225
|
+
inputMode: state.context.inputMode,
|
|
226
|
+
"aria-invalid": (0, import_dom_query2.ariaAttr)(invalid),
|
|
227
|
+
"data-invalid": (0, import_dom_query2.dataAttr)(invalid),
|
|
228
|
+
disabled,
|
|
229
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(disabled),
|
|
230
|
+
readOnly: !!state.context.readOnly,
|
|
231
|
+
autoComplete: "off",
|
|
232
|
+
autoCorrect: "off",
|
|
233
|
+
spellCheck: "false",
|
|
234
|
+
type: "text",
|
|
235
|
+
"aria-roledescription": "numberfield",
|
|
236
|
+
"aria-valuemin": state.context.min,
|
|
237
|
+
"aria-valuemax": state.context.max,
|
|
238
|
+
"aria-valuenow": Number.isNaN(state.context.valueAsNumber) ? void 0 : state.context.valueAsNumber,
|
|
239
|
+
"aria-valuetext": state.context.valueText,
|
|
240
|
+
onFocus() {
|
|
241
|
+
send("INPUT.FOCUS");
|
|
242
|
+
},
|
|
243
|
+
onBlur() {
|
|
244
|
+
send({ type: "INPUT.COMMIT", src: "blur" });
|
|
245
|
+
},
|
|
246
|
+
onChange(event) {
|
|
247
|
+
send({ type: "INPUT.CHANGE", target: event.currentTarget, hint: "set" });
|
|
248
|
+
},
|
|
249
|
+
onBeforeInput(event) {
|
|
250
|
+
try {
|
|
251
|
+
const { selectionStart, selectionEnd, value } = event.currentTarget;
|
|
252
|
+
const nextValue = value.slice(0, selectionStart) + (event.data ?? "") + value.slice(selectionEnd);
|
|
253
|
+
const isValid = state.context.parser.isValidPartialNumber(nextValue);
|
|
254
|
+
if (!isValid) {
|
|
255
|
+
event.preventDefault();
|
|
256
|
+
}
|
|
257
|
+
} catch {
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
onKeyDown(event) {
|
|
261
|
+
if (event.defaultPrevented)
|
|
262
|
+
return;
|
|
263
|
+
if (readOnly)
|
|
264
|
+
return;
|
|
265
|
+
if ((0, import_dom_query2.isComposingEvent)(event))
|
|
266
|
+
return;
|
|
267
|
+
const step = (0, import_dom_event.getEventStep)(event) * state.context.step;
|
|
268
|
+
const keyMap = {
|
|
269
|
+
ArrowUp() {
|
|
270
|
+
send({ type: "INPUT.ARROW_UP", step });
|
|
271
|
+
event.preventDefault();
|
|
272
|
+
},
|
|
273
|
+
ArrowDown() {
|
|
274
|
+
send({ type: "INPUT.ARROW_DOWN", step });
|
|
275
|
+
event.preventDefault();
|
|
276
|
+
},
|
|
277
|
+
Home() {
|
|
278
|
+
send("INPUT.HOME");
|
|
279
|
+
event.preventDefault();
|
|
280
|
+
},
|
|
281
|
+
End() {
|
|
282
|
+
send("INPUT.END");
|
|
283
|
+
event.preventDefault();
|
|
284
|
+
},
|
|
285
|
+
Enter() {
|
|
286
|
+
send({ type: "INPUT.COMMIT", src: "enter" });
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
const exec = keyMap[event.key];
|
|
290
|
+
exec?.(event);
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
},
|
|
294
|
+
getDecrementTriggerProps() {
|
|
295
|
+
return normalize.button({
|
|
296
|
+
...parts.decrementTrigger.attrs,
|
|
297
|
+
dir: state.context.dir,
|
|
298
|
+
id: dom.getDecrementTriggerId(state.context),
|
|
299
|
+
disabled: isDecrementDisabled,
|
|
300
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(isDecrementDisabled),
|
|
301
|
+
"aria-label": translations.decrementLabel,
|
|
302
|
+
type: "button",
|
|
303
|
+
tabIndex: -1,
|
|
304
|
+
"aria-controls": dom.getInputId(state.context),
|
|
305
|
+
onPointerDown(event) {
|
|
306
|
+
if (isDecrementDisabled || !(0, import_dom_event.isLeftClick)(event))
|
|
307
|
+
return;
|
|
308
|
+
send({ type: "TRIGGER.PRESS_DOWN", hint: "decrement", pointerType: event.pointerType });
|
|
309
|
+
if (event.pointerType === "mouse") {
|
|
310
|
+
event.preventDefault();
|
|
311
|
+
}
|
|
312
|
+
if (event.pointerType === "touch") {
|
|
313
|
+
event.currentTarget?.focus({ preventScroll: true });
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
onPointerUp(event) {
|
|
317
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "decrement", pointerType: event.pointerType });
|
|
318
|
+
},
|
|
319
|
+
onPointerLeave() {
|
|
320
|
+
if (isDecrementDisabled)
|
|
321
|
+
return;
|
|
322
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "decrement" });
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
},
|
|
326
|
+
getIncrementTriggerProps() {
|
|
327
|
+
return normalize.button({
|
|
328
|
+
...parts.incrementTrigger.attrs,
|
|
329
|
+
dir: state.context.dir,
|
|
330
|
+
id: dom.getIncrementTriggerId(state.context),
|
|
331
|
+
disabled: isIncrementDisabled,
|
|
332
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(isIncrementDisabled),
|
|
333
|
+
"aria-label": translations.incrementLabel,
|
|
334
|
+
type: "button",
|
|
335
|
+
tabIndex: -1,
|
|
336
|
+
"aria-controls": dom.getInputId(state.context),
|
|
337
|
+
onPointerDown(event) {
|
|
338
|
+
if (isIncrementDisabled || !(0, import_dom_event.isLeftClick)(event))
|
|
339
|
+
return;
|
|
340
|
+
send({ type: "TRIGGER.PRESS_DOWN", hint: "increment", pointerType: event.pointerType });
|
|
341
|
+
if (event.pointerType === "mouse") {
|
|
342
|
+
event.preventDefault();
|
|
343
|
+
}
|
|
344
|
+
if (event.pointerType === "touch") {
|
|
345
|
+
event.currentTarget?.focus({ preventScroll: true });
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
onPointerUp(event) {
|
|
349
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType });
|
|
350
|
+
},
|
|
351
|
+
onPointerLeave(event) {
|
|
352
|
+
send({ type: "TRIGGER.PRESS_UP", hint: "increment", pointerType: event.pointerType });
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
},
|
|
356
|
+
getScrubberProps() {
|
|
357
|
+
return normalize.element({
|
|
358
|
+
...parts.scrubber.attrs,
|
|
359
|
+
dir: state.context.dir,
|
|
360
|
+
"data-disabled": (0, import_dom_query2.dataAttr)(disabled),
|
|
361
|
+
id: dom.getScrubberId(state.context),
|
|
362
|
+
role: "presentation",
|
|
363
|
+
onMouseDown(event) {
|
|
364
|
+
if (disabled)
|
|
365
|
+
return;
|
|
366
|
+
const point = (0, import_dom_event.getEventPoint)(event);
|
|
367
|
+
point.x = point.x - (0, import_number_utils2.roundToDevicePixel)(7.5);
|
|
368
|
+
point.y = point.y - (0, import_number_utils2.roundToDevicePixel)(7.5);
|
|
369
|
+
send({ type: "SCRUBBER.PRESS_DOWN", point });
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
},
|
|
372
|
+
style: {
|
|
373
|
+
cursor: disabled ? void 0 : "ew-resize"
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/number-input.machine.ts
|
|
381
|
+
var import_core2 = require("@zag-js/core");
|
|
382
|
+
var import_dom_event2 = require("@zag-js/dom-event");
|
|
383
|
+
var import_dom_query3 = require("@zag-js/dom-query");
|
|
384
|
+
var import_form_utils = require("@zag-js/form-utils");
|
|
385
|
+
var import_number_utils3 = require("@zag-js/number-utils");
|
|
386
|
+
var import_utils = require("@zag-js/utils");
|
|
387
|
+
|
|
388
|
+
// src/cursor.ts
|
|
389
|
+
function recordCursor(inputEl) {
|
|
390
|
+
if (inputEl.ownerDocument.activeElement !== inputEl)
|
|
391
|
+
return;
|
|
392
|
+
try {
|
|
393
|
+
const { selectionStart: start, selectionEnd: end, value } = inputEl;
|
|
394
|
+
const beforeTxt = value.substring(0, start);
|
|
395
|
+
const afterTxt = value.substring(end);
|
|
396
|
+
return {
|
|
397
|
+
start,
|
|
398
|
+
end,
|
|
399
|
+
value,
|
|
400
|
+
beforeTxt,
|
|
401
|
+
afterTxt
|
|
402
|
+
};
|
|
403
|
+
} catch {
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
function restoreCursor(inputEl, selection) {
|
|
407
|
+
if (inputEl.ownerDocument.activeElement !== inputEl)
|
|
408
|
+
return;
|
|
409
|
+
if (!selection) {
|
|
410
|
+
inputEl.setSelectionRange(inputEl.value.length, inputEl.value.length);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
try {
|
|
414
|
+
const { value } = inputEl;
|
|
415
|
+
const { beforeTxt = "", afterTxt = "", start } = selection;
|
|
416
|
+
let startPos = value.length;
|
|
417
|
+
if (value.endsWith(afterTxt)) {
|
|
418
|
+
startPos = value.length - afterTxt.length;
|
|
419
|
+
} else if (value.startsWith(beforeTxt)) {
|
|
420
|
+
startPos = beforeTxt.length;
|
|
421
|
+
} else if (start != null) {
|
|
422
|
+
const beforeLastChar = beforeTxt[start - 1];
|
|
423
|
+
const newIndex = value.indexOf(beforeLastChar, start - 1);
|
|
424
|
+
if (newIndex !== -1) {
|
|
425
|
+
startPos = newIndex + 1;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
inputEl.setSelectionRange(startPos, startPos);
|
|
429
|
+
} catch {
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// src/number-input.utils.ts
|
|
434
|
+
var import_number = require("@internationalized/number");
|
|
435
|
+
var import_core = require("@zag-js/core");
|
|
436
|
+
var createFormatter = (locale, options = {}) => {
|
|
437
|
+
return (0, import_core.ref)(new Intl.NumberFormat(locale, options));
|
|
438
|
+
};
|
|
439
|
+
var createParser = (locale, options = {}) => {
|
|
440
|
+
return (0, import_core.ref)(new import_number.NumberParser(locale, options));
|
|
441
|
+
};
|
|
442
|
+
var parseValue = (ctx, value) => {
|
|
443
|
+
if (!ctx.formatOptions)
|
|
444
|
+
return parseFloat(value);
|
|
445
|
+
return ctx.parser.parse(String(value));
|
|
446
|
+
};
|
|
447
|
+
var formatValue = (ctx, value) => {
|
|
448
|
+
if (Number.isNaN(value))
|
|
449
|
+
return "";
|
|
450
|
+
if (!ctx.formatOptions)
|
|
451
|
+
return value.toString();
|
|
452
|
+
return ctx.formatter.format(value);
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
// src/number-input.machine.ts
|
|
456
|
+
var { not, and } = import_core2.guards;
|
|
457
|
+
function machine(userContext) {
|
|
458
|
+
const ctx = (0, import_utils.compact)(userContext);
|
|
459
|
+
return (0, import_core2.createMachine)(
|
|
460
|
+
{
|
|
461
|
+
id: "number-input",
|
|
462
|
+
initial: "idle",
|
|
463
|
+
context: {
|
|
464
|
+
dir: "ltr",
|
|
465
|
+
locale: "en-US",
|
|
466
|
+
focusInputOnChange: true,
|
|
467
|
+
clampValueOnBlur: true,
|
|
468
|
+
allowOverflow: false,
|
|
469
|
+
inputMode: "decimal",
|
|
470
|
+
pattern: "[0-9]*(.[0-9]+)?",
|
|
471
|
+
value: "",
|
|
472
|
+
step: 1,
|
|
473
|
+
min: Number.MIN_SAFE_INTEGER,
|
|
474
|
+
max: Number.MAX_SAFE_INTEGER,
|
|
475
|
+
invalid: false,
|
|
476
|
+
spinOnPress: true,
|
|
477
|
+
...ctx,
|
|
478
|
+
hint: null,
|
|
479
|
+
scrubberCursorPoint: null,
|
|
480
|
+
fieldsetDisabled: false,
|
|
481
|
+
formatter: createFormatter(ctx.locale || "en-US", ctx.formatOptions),
|
|
482
|
+
parser: createParser(ctx.locale || "en-US", ctx.formatOptions),
|
|
483
|
+
translations: {
|
|
484
|
+
incrementLabel: "increment value",
|
|
485
|
+
decrementLabel: "decrease value",
|
|
486
|
+
...ctx.translations
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
computed: {
|
|
490
|
+
isRtl: (ctx2) => ctx2.dir === "rtl",
|
|
491
|
+
valueAsNumber: (ctx2) => parseValue(ctx2, ctx2.value),
|
|
492
|
+
formattedValue: (ctx2) => formatValue(ctx2, ctx2.valueAsNumber),
|
|
493
|
+
isAtMin: (ctx2) => (0, import_number_utils3.isAtMin)(ctx2.valueAsNumber, ctx2),
|
|
494
|
+
isAtMax: (ctx2) => (0, import_number_utils3.isAtMax)(ctx2.valueAsNumber, ctx2),
|
|
495
|
+
isOutOfRange: (ctx2) => !(0, import_number_utils3.isWithinRange)(ctx2.valueAsNumber, ctx2),
|
|
496
|
+
isValueEmpty: (ctx2) => ctx2.value === "",
|
|
497
|
+
isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled,
|
|
498
|
+
canIncrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMax,
|
|
499
|
+
canDecrement: (ctx2) => ctx2.allowOverflow || !ctx2.isAtMin,
|
|
500
|
+
valueText: (ctx2) => ctx2.translations.valueText?.(ctx2.value)
|
|
501
|
+
},
|
|
502
|
+
watch: {
|
|
503
|
+
formatOptions: ["setFormatterAndParser", "syncInputElement"],
|
|
504
|
+
locale: ["setFormatterAndParser", "syncInputElement"],
|
|
505
|
+
value: ["syncInputElement"],
|
|
506
|
+
isOutOfRange: ["invokeOnInvalid"],
|
|
507
|
+
scrubberCursorPoint: ["setVirtualCursorPosition"]
|
|
508
|
+
},
|
|
509
|
+
activities: ["trackFormControl"],
|
|
510
|
+
on: {
|
|
511
|
+
"VALUE.SET": {
|
|
512
|
+
actions: ["setRawValue", "setHintToSet"]
|
|
513
|
+
},
|
|
514
|
+
"VALUE.CLEAR": {
|
|
515
|
+
actions: ["clearValue"]
|
|
516
|
+
},
|
|
517
|
+
"VALUE.INCREMENT": {
|
|
518
|
+
actions: ["increment"]
|
|
519
|
+
},
|
|
520
|
+
"VALUE.DECREMENT": {
|
|
521
|
+
actions: ["decrement"]
|
|
522
|
+
}
|
|
523
|
+
},
|
|
524
|
+
states: {
|
|
525
|
+
idle: {
|
|
526
|
+
on: {
|
|
527
|
+
"TRIGGER.PRESS_DOWN": [
|
|
528
|
+
{ guard: "isTouchPointer", target: "before:spin", actions: ["setHint"] },
|
|
529
|
+
{
|
|
530
|
+
target: "before:spin",
|
|
531
|
+
actions: ["focusInput", "invokeOnFocus", "setHint"]
|
|
532
|
+
}
|
|
533
|
+
],
|
|
534
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
535
|
+
target: "scrubbing",
|
|
536
|
+
actions: ["focusInput", "invokeOnFocus", "setHint", "setCursorPoint"]
|
|
537
|
+
},
|
|
538
|
+
"INPUT.FOCUS": {
|
|
539
|
+
target: "focused",
|
|
540
|
+
actions: ["focusInput", "invokeOnFocus"]
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
},
|
|
544
|
+
focused: {
|
|
545
|
+
tags: "focus",
|
|
546
|
+
activities: "attachWheelListener",
|
|
547
|
+
on: {
|
|
548
|
+
"TRIGGER.PRESS_DOWN": [
|
|
549
|
+
{ guard: "isTouchPointer", target: "before:spin", actions: ["setHint"] },
|
|
550
|
+
{ target: "before:spin", actions: ["focusInput", "setHint"] }
|
|
551
|
+
],
|
|
552
|
+
"SCRUBBER.PRESS_DOWN": {
|
|
553
|
+
target: "scrubbing",
|
|
554
|
+
actions: ["focusInput", "setHint", "setCursorPoint"]
|
|
555
|
+
},
|
|
556
|
+
"INPUT.ARROW_UP": {
|
|
557
|
+
actions: "increment"
|
|
558
|
+
},
|
|
559
|
+
"INPUT.ARROW_DOWN": {
|
|
560
|
+
actions: "decrement"
|
|
561
|
+
},
|
|
562
|
+
"INPUT.HOME": {
|
|
563
|
+
actions: "decrementToMin"
|
|
564
|
+
},
|
|
565
|
+
"INPUT.END": {
|
|
566
|
+
actions: "incrementToMax"
|
|
567
|
+
},
|
|
568
|
+
"INPUT.CHANGE": {
|
|
569
|
+
actions: ["setValue", "setHint"]
|
|
570
|
+
},
|
|
571
|
+
"INPUT.COMMIT": [
|
|
572
|
+
{
|
|
573
|
+
guard: and("clampValueOnBlur", not("isInRange")),
|
|
574
|
+
target: "idle",
|
|
575
|
+
actions: ["setClampedValue", "clearHint", "invokeOnBlur"]
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
target: "idle",
|
|
579
|
+
actions: ["setFormattedValue", "clearHint", "invokeOnBlur"]
|
|
580
|
+
}
|
|
581
|
+
]
|
|
582
|
+
}
|
|
583
|
+
},
|
|
584
|
+
"before:spin": {
|
|
585
|
+
tags: "focus",
|
|
586
|
+
activities: "trackButtonDisabled",
|
|
587
|
+
entry: (0, import_core2.choose)([
|
|
588
|
+
{ guard: "isIncrementHint", actions: "increment" },
|
|
589
|
+
{ guard: "isDecrementHint", actions: "decrement" }
|
|
590
|
+
]),
|
|
591
|
+
after: {
|
|
592
|
+
CHANGE_DELAY: {
|
|
593
|
+
target: "spinning",
|
|
594
|
+
guard: and("isInRange", "spinOnPress")
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
on: {
|
|
598
|
+
"TRIGGER.PRESS_UP": [
|
|
599
|
+
{ guard: "isTouchPointer", target: "focused", actions: "clearHint" },
|
|
600
|
+
{ target: "focused", actions: ["focusInput", "clearHint"] }
|
|
601
|
+
]
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
spinning: {
|
|
605
|
+
tags: "focus",
|
|
606
|
+
activities: "trackButtonDisabled",
|
|
607
|
+
every: [
|
|
608
|
+
{
|
|
609
|
+
delay: "CHANGE_INTERVAL",
|
|
610
|
+
guard: and(not("isAtMin"), "isIncrementHint"),
|
|
611
|
+
actions: "increment"
|
|
612
|
+
},
|
|
613
|
+
{
|
|
614
|
+
delay: "CHANGE_INTERVAL",
|
|
615
|
+
guard: and(not("isAtMax"), "isDecrementHint"),
|
|
616
|
+
actions: "decrement"
|
|
617
|
+
}
|
|
618
|
+
],
|
|
619
|
+
on: {
|
|
620
|
+
"TRIGGER.PRESS_UP": {
|
|
621
|
+
target: "focused",
|
|
622
|
+
actions: ["focusInput", "clearHint"]
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
},
|
|
626
|
+
scrubbing: {
|
|
627
|
+
tags: "focus",
|
|
628
|
+
activities: ["activatePointerLock", "trackMousemove", "setupVirtualCursor", "preventTextSelection"],
|
|
629
|
+
on: {
|
|
630
|
+
"SCRUBBER.POINTER_UP": {
|
|
631
|
+
target: "focused",
|
|
632
|
+
actions: ["focusInput", "clearCursorPoint"]
|
|
633
|
+
},
|
|
634
|
+
"SCRUBBER.POINTER_MOVE": [
|
|
635
|
+
{
|
|
636
|
+
guard: "isIncrementHint",
|
|
637
|
+
actions: ["increment", "setCursorPoint"]
|
|
638
|
+
},
|
|
639
|
+
{
|
|
640
|
+
guard: "isDecrementHint",
|
|
641
|
+
actions: ["decrement", "setCursorPoint"]
|
|
642
|
+
}
|
|
643
|
+
]
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
},
|
|
648
|
+
{
|
|
649
|
+
delays: {
|
|
650
|
+
CHANGE_INTERVAL: 50,
|
|
651
|
+
CHANGE_DELAY: 300
|
|
652
|
+
},
|
|
653
|
+
guards: {
|
|
654
|
+
clampValueOnBlur: (ctx2) => ctx2.clampValueOnBlur,
|
|
655
|
+
isAtMin: (ctx2) => ctx2.isAtMin,
|
|
656
|
+
spinOnPress: (ctx2) => !!ctx2.spinOnPress,
|
|
657
|
+
isAtMax: (ctx2) => ctx2.isAtMax,
|
|
658
|
+
isInRange: (ctx2) => !ctx2.isOutOfRange,
|
|
659
|
+
isDecrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "decrement",
|
|
660
|
+
isIncrementHint: (ctx2, evt) => (evt.hint ?? ctx2.hint) === "increment",
|
|
661
|
+
isTouchPointer: (_ctx, evt) => evt.pointerType === "touch"
|
|
662
|
+
},
|
|
663
|
+
activities: {
|
|
664
|
+
trackFormControl(ctx2, _evt, { initialContext }) {
|
|
665
|
+
const inputEl = dom.getInputEl(ctx2);
|
|
666
|
+
return (0, import_form_utils.trackFormControl)(inputEl, {
|
|
667
|
+
onFieldsetDisabledChange(disabled) {
|
|
668
|
+
ctx2.fieldsetDisabled = disabled;
|
|
669
|
+
},
|
|
670
|
+
onFormReset() {
|
|
671
|
+
set.value(ctx2, initialContext.value);
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
},
|
|
675
|
+
setupVirtualCursor(ctx2) {
|
|
676
|
+
return dom.setupVirtualCursor(ctx2);
|
|
677
|
+
},
|
|
678
|
+
preventTextSelection(ctx2) {
|
|
679
|
+
return dom.preventTextSelection(ctx2);
|
|
680
|
+
},
|
|
681
|
+
trackButtonDisabled(ctx2, _evt, { send }) {
|
|
682
|
+
const btn = dom.getPressedTriggerEl(ctx2, ctx2.hint);
|
|
683
|
+
return (0, import_dom_query3.observeAttributes)(btn, {
|
|
684
|
+
attributes: ["disabled"],
|
|
685
|
+
callback() {
|
|
686
|
+
send({ type: "TRIGGER.PRESS_UP", src: "attr" });
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
},
|
|
690
|
+
attachWheelListener(ctx2, _evt, { send }) {
|
|
691
|
+
const inputEl = dom.getInputEl(ctx2);
|
|
692
|
+
if (!inputEl || !dom.isActiveElement(ctx2, inputEl) || !ctx2.allowMouseWheel)
|
|
693
|
+
return;
|
|
694
|
+
function onWheel(event) {
|
|
695
|
+
event.preventDefault();
|
|
696
|
+
const dir = Math.sign(event.deltaY) * -1;
|
|
697
|
+
if (dir === 1) {
|
|
698
|
+
send("VALUE.INCREMENT");
|
|
699
|
+
} else if (dir === -1) {
|
|
700
|
+
send("VALUE.DECREMENT");
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return (0, import_dom_event2.addDomEvent)(inputEl, "wheel", onWheel, { passive: false });
|
|
704
|
+
},
|
|
705
|
+
activatePointerLock(ctx2) {
|
|
706
|
+
if ((0, import_dom_query3.isSafari)())
|
|
707
|
+
return;
|
|
708
|
+
return (0, import_dom_event2.requestPointerLock)(dom.getDoc(ctx2));
|
|
709
|
+
},
|
|
710
|
+
trackMousemove(ctx2, _evt, { send }) {
|
|
711
|
+
const doc = dom.getDoc(ctx2);
|
|
712
|
+
function onMousemove(event) {
|
|
713
|
+
if (!ctx2.scrubberCursorPoint)
|
|
714
|
+
return;
|
|
715
|
+
const value = dom.getMousementValue(ctx2, event);
|
|
716
|
+
if (!value.hint)
|
|
717
|
+
return;
|
|
718
|
+
send({
|
|
719
|
+
type: "SCRUBBER.POINTER_MOVE",
|
|
720
|
+
hint: value.hint,
|
|
721
|
+
point: value.point
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
function onMouseup() {
|
|
725
|
+
send("SCRUBBER.POINTER_UP");
|
|
726
|
+
}
|
|
727
|
+
return (0, import_utils.callAll)(
|
|
728
|
+
(0, import_dom_event2.addDomEvent)(doc, "mousemove", onMousemove, false),
|
|
729
|
+
(0, import_dom_event2.addDomEvent)(doc, "mouseup", onMouseup, false)
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
actions: {
|
|
734
|
+
focusInput(ctx2) {
|
|
735
|
+
if (!ctx2.focusInputOnChange)
|
|
736
|
+
return;
|
|
737
|
+
const inputEl = dom.getInputEl(ctx2);
|
|
738
|
+
if (dom.isActiveElement(ctx2, inputEl))
|
|
739
|
+
return;
|
|
740
|
+
(0, import_dom_query3.raf)(() => inputEl?.focus({ preventScroll: true }));
|
|
741
|
+
},
|
|
742
|
+
increment(ctx2, evt) {
|
|
743
|
+
const nextValue = (0, import_number_utils3.increment)(ctx2.valueAsNumber, evt.step ?? ctx2.step);
|
|
744
|
+
const value = formatValue(ctx2, (0, import_number_utils3.clamp)(nextValue, ctx2));
|
|
745
|
+
set.value(ctx2, value);
|
|
746
|
+
},
|
|
747
|
+
decrement(ctx2, evt) {
|
|
748
|
+
const nextValue = (0, import_number_utils3.decrement)(ctx2.valueAsNumber, evt.step ?? ctx2.step);
|
|
749
|
+
const value = formatValue(ctx2, (0, import_number_utils3.clamp)(nextValue, ctx2));
|
|
750
|
+
set.value(ctx2, value);
|
|
751
|
+
},
|
|
752
|
+
setClampedValue(ctx2) {
|
|
753
|
+
const nextValue = (0, import_number_utils3.clamp)(ctx2.valueAsNumber, ctx2);
|
|
754
|
+
set.value(ctx2, formatValue(ctx2, nextValue));
|
|
755
|
+
},
|
|
756
|
+
setRawValue(ctx2, evt) {
|
|
757
|
+
const parsedValue = parseValue(ctx2, evt.value);
|
|
758
|
+
const value = formatValue(ctx2, (0, import_number_utils3.clamp)(parsedValue, ctx2));
|
|
759
|
+
set.value(ctx2, value);
|
|
760
|
+
},
|
|
761
|
+
setValue(ctx2, evt) {
|
|
762
|
+
const value = evt.target?.value ?? evt.value;
|
|
763
|
+
set.value(ctx2, value);
|
|
764
|
+
},
|
|
765
|
+
clearValue(ctx2) {
|
|
766
|
+
set.value(ctx2, "");
|
|
767
|
+
},
|
|
768
|
+
incrementToMax(ctx2) {
|
|
769
|
+
const value = formatValue(ctx2, ctx2.max);
|
|
770
|
+
set.value(ctx2, value);
|
|
771
|
+
},
|
|
772
|
+
decrementToMin(ctx2) {
|
|
773
|
+
const value = formatValue(ctx2, ctx2.min);
|
|
774
|
+
set.value(ctx2, value);
|
|
775
|
+
},
|
|
776
|
+
setHint(ctx2, evt) {
|
|
777
|
+
ctx2.hint = evt.hint;
|
|
778
|
+
},
|
|
779
|
+
clearHint(ctx2) {
|
|
780
|
+
ctx2.hint = null;
|
|
781
|
+
},
|
|
782
|
+
setHintToSet(ctx2) {
|
|
783
|
+
ctx2.hint = "set";
|
|
784
|
+
},
|
|
785
|
+
invokeOnFocus(ctx2) {
|
|
786
|
+
ctx2.onFocusChange?.({
|
|
787
|
+
focused: true,
|
|
788
|
+
value: ctx2.formattedValue,
|
|
789
|
+
valueAsNumber: ctx2.valueAsNumber
|
|
790
|
+
});
|
|
791
|
+
},
|
|
792
|
+
invokeOnBlur(ctx2) {
|
|
793
|
+
ctx2.onFocusChange?.({
|
|
794
|
+
focused: false,
|
|
795
|
+
value: ctx2.formattedValue,
|
|
796
|
+
valueAsNumber: ctx2.valueAsNumber
|
|
797
|
+
});
|
|
798
|
+
},
|
|
799
|
+
invokeOnInvalid(ctx2) {
|
|
800
|
+
if (!ctx2.isOutOfRange)
|
|
801
|
+
return;
|
|
802
|
+
const reason = ctx2.valueAsNumber > ctx2.max ? "rangeOverflow" : "rangeUnderflow";
|
|
803
|
+
ctx2.onValueInvalid?.({
|
|
804
|
+
reason,
|
|
805
|
+
value: ctx2.formattedValue,
|
|
806
|
+
valueAsNumber: ctx2.valueAsNumber
|
|
807
|
+
});
|
|
808
|
+
},
|
|
809
|
+
syncInputElement(ctx2, evt) {
|
|
810
|
+
const value = evt.type.endsWith("CHANGE") ? ctx2.value : ctx2.formattedValue;
|
|
811
|
+
sync.input(ctx2, value);
|
|
812
|
+
},
|
|
813
|
+
setFormattedValue(ctx2) {
|
|
814
|
+
set.value(ctx2, ctx2.formattedValue);
|
|
815
|
+
},
|
|
816
|
+
setCursorPoint(ctx2, evt) {
|
|
817
|
+
ctx2.scrubberCursorPoint = evt.point;
|
|
818
|
+
},
|
|
819
|
+
clearCursorPoint(ctx2) {
|
|
820
|
+
ctx2.scrubberCursorPoint = null;
|
|
821
|
+
},
|
|
822
|
+
setVirtualCursorPosition(ctx2) {
|
|
823
|
+
const cursorEl = dom.getCursorEl(ctx2);
|
|
824
|
+
if (!cursorEl || !ctx2.scrubberCursorPoint)
|
|
825
|
+
return;
|
|
826
|
+
const { x, y } = ctx2.scrubberCursorPoint;
|
|
827
|
+
cursorEl.style.transform = `translate3d(${x}px, ${y}px, 0px)`;
|
|
828
|
+
},
|
|
829
|
+
setFormatterAndParser(ctx2) {
|
|
830
|
+
if (!ctx2.locale)
|
|
831
|
+
return;
|
|
832
|
+
ctx2.formatter = createFormatter(ctx2.locale, ctx2.formatOptions);
|
|
833
|
+
ctx2.parser = createParser(ctx2.locale, ctx2.formatOptions);
|
|
834
|
+
}
|
|
835
|
+
},
|
|
836
|
+
compareFns: {
|
|
837
|
+
formatOptions: (a, b) => (0, import_utils.isEqual)(a, b),
|
|
838
|
+
scrubberCursorPoint: (a, b) => (0, import_utils.isEqual)(a, b)
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
var sync = {
|
|
844
|
+
input(ctx, value) {
|
|
845
|
+
const inputEl = dom.getInputEl(ctx);
|
|
846
|
+
if (!inputEl)
|
|
847
|
+
return;
|
|
848
|
+
const sel = recordCursor(inputEl);
|
|
849
|
+
(0, import_dom_query3.raf)(() => {
|
|
850
|
+
dom.setValue(inputEl, value);
|
|
851
|
+
restoreCursor(inputEl, sel);
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
var invoke = {
|
|
856
|
+
onChange: (ctx) => {
|
|
857
|
+
ctx.onValueChange?.({
|
|
858
|
+
value: ctx.value,
|
|
859
|
+
valueAsNumber: ctx.valueAsNumber
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
var set = {
|
|
864
|
+
value: (ctx, value) => {
|
|
865
|
+
if ((0, import_utils.isEqual)(ctx.value, value))
|
|
866
|
+
return;
|
|
867
|
+
ctx.value = value;
|
|
868
|
+
invoke.onChange(ctx);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
872
|
+
0 && (module.exports = {
|
|
873
|
+
anatomy,
|
|
874
|
+
connect,
|
|
875
|
+
machine
|
|
876
|
+
});
|
|
8
877
|
//# sourceMappingURL=index.js.map
|