@zag-js/slider 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 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +851 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/slider.connect.ts +98 -84
- package/src/slider.types.ts +7 -7
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,852 @@
|
|
|
1
|
-
import{createAnatomy}from"@zag-js/anatomy";var anatomy=createAnatomy("slider").parts("root","label","thumb","valueText","track","range","control","markerGroup","marker");var parts=anatomy.build();import{getEventKey,getEventPoint,getEventStep,isLeftClick,isModifierKey}from"@zag-js/dom-event";import{ariaAttr,dataAttr}from"@zag-js/dom-query";import{getPercentValue as getPercentValue2,getValuePercent as getValuePercent2}from"@zag-js/numeric-range";import{getRelativePoint}from"@zag-js/dom-event";import{createScope,queryAll}from"@zag-js/dom-query";import{dispatchInputValueEvent}from"@zag-js/form-utils";import{getPercentValue}from"@zag-js/numeric-range";import{getValuePercent,getValueTransformer}from"@zag-js/numeric-range";function getBounds(value){const firstValue=value[0];const lastThumb=value[value.length-1];return[firstValue,lastThumb]}function getRangeOffsets(ctx){const[firstPercent,lastPercent]=getBounds(ctx.valuePercent);if(ctx.valuePercent.length===1){if(ctx.origin==="center"){const isNegative=ctx.valuePercent[0]<50;const start=isNegative?`${ctx.valuePercent[0]}%`:"50%";const end=isNegative?"50%":`${100-ctx.valuePercent[0]}%`;return{start,end}}return{start:"0%",end:`${100-lastPercent}%`}}return{start:`${firstPercent}%`,end:`${100-lastPercent}%`}}function getRangeStyle(ctx){if(ctx.isVertical){return{position:"absolute",bottom:"var(--slider-range-start)",top:"var(--slider-range-end)"}}return{position:"absolute",[ctx.isRtl?"right":"left"]:"var(--slider-range-start)",[ctx.isRtl?"left":"right"]:"var(--slider-range-end)"}}function getVerticalThumbOffset(ctx){const{height=0}=ctx.thumbSize??{};const getValue=getValueTransformer([ctx.min,ctx.max],[-height/2,height/2]);return parseFloat(getValue(ctx.value).toFixed(2))}function getHorizontalThumbOffset(ctx){const{width=0}=ctx.thumbSize??{};if(ctx.isRtl){const getValue2=getValueTransformer([ctx.max,ctx.min],[-width/2,width/2]);return-1*parseFloat(getValue2(ctx.value).toFixed(2))}const getValue=getValueTransformer([ctx.min,ctx.max],[-width/2,width/2]);return parseFloat(getValue(ctx.value).toFixed(2))}function getOffset(ctx,percent){if(ctx.thumbAlignment==="center")return`${percent}%`;const offset=ctx.isVertical?getVerticalThumbOffset(ctx):getHorizontalThumbOffset(ctx);return`calc(${percent}% - ${offset}px)`}function getThumbOffset(ctx){let percent=getValuePercent(ctx.value,ctx.min,ctx.max)*100;return getOffset(ctx,percent)}function getVisibility(ctx){let visibility="visible";if(ctx.thumbAlignment==="contain"&&!ctx.hasMeasuredThumbSize){visibility="hidden"}return visibility}function getThumbStyle(ctx,index){const placementProp=ctx.isVertical?"bottom":"insetInlineStart";return{visibility:getVisibility(ctx),position:"absolute",transform:"var(--slider-thumb-transform)",[placementProp]:`var(--slider-thumb-offset-${index})`}}function getControlStyle(){return{touchAction:"none",userSelect:"none",position:"relative"}}function getRootStyle(ctx){const range=getRangeOffsets(ctx);const offsetStyles=ctx.value.reduce((styles,value,index)=>{const offset=getThumbOffset({...ctx,value});return{...styles,[`--slider-thumb-offset-${index}`]:offset}},{});return{...offsetStyles,"--slider-thumb-transform":ctx.isVertical?"translateY(50%)":ctx.isRtl?"translateX(50%)":"translateX(-50%)","--slider-range-start":range.start,"--slider-range-end":range.end}}function getMarkerStyle(ctx,value){return{visibility:getVisibility(ctx),position:"absolute",pointerEvents:"none",[ctx.isHorizontal?"insetInlineStart":"bottom"]:getThumbOffset({...ctx,value}),translate:"var(--tx) var(--ty)","--tx":ctx.isHorizontal?ctx.isRtl?"50%":"-50%":"0%","--ty":!ctx.isHorizontal?"50%":"0%"}}function getMarkerGroupStyle(){return{userSelect:"none",pointerEvents:"none",position:"relative"}}var styleGetterFns={getRootStyle,getControlStyle,getThumbStyle,getRangeStyle,getMarkerStyle,getMarkerGroupStyle};var dom=createScope({...styleGetterFns,getRootId:ctx=>ctx.ids?.root??`slider:${ctx.id}`,getThumbId:(ctx,index)=>ctx.ids?.thumb?.(index)??`slider:${ctx.id}:thumb:${index}`,getHiddenInputId:(ctx,index)=>`slider:${ctx.id}:input:${index}`,getControlId:ctx=>ctx.ids?.control??`slider:${ctx.id}:control`,getTrackId:ctx=>ctx.ids?.track??`slider:${ctx.id}:track`,getRangeId:ctx=>ctx.ids?.range??`slider:${ctx.id}:range`,getLabelId:ctx=>ctx.ids?.label??`slider:${ctx.id}:label`,getValueTextId:ctx=>ctx.ids?.valueText??`slider:${ctx.id}:valueText`,getMarkerId:(ctx,value)=>ctx.ids?.marker?.(value)??`slider:${ctx.id}:marker:${value}`,getRootEl:ctx=>dom.getById(ctx,dom.getRootId(ctx)),getThumbEl:(ctx,index)=>dom.getById(ctx,dom.getThumbId(ctx,index)),getHiddenInputEl:(ctx,index)=>dom.getById(ctx,dom.getHiddenInputId(ctx,index)),getControlEl:ctx=>dom.getById(ctx,dom.getControlId(ctx)),getElements:ctx=>queryAll(dom.getControlEl(ctx),"[role=slider]"),getFirstEl:ctx=>dom.getElements(ctx)[0],getRangeEl:ctx=>dom.getById(ctx,dom.getRangeId(ctx)),getValueFromPoint(ctx,point){const controlEl=dom.getControlEl(ctx);if(!controlEl)return;const relativePoint=getRelativePoint(point,controlEl);const percent=relativePoint.getPercentValue({orientation:ctx.orientation,dir:ctx.dir,inverted:{y:true}});return getPercentValue(percent,ctx.min,ctx.max,ctx.step)},dispatchChangeEvent(ctx){const valueArray=Array.from(ctx.value);valueArray.forEach((value,index)=>{const inputEl=dom.getHiddenInputEl(ctx,index);if(!inputEl)return;dispatchInputValueEvent(inputEl,{value})})}});import{clampValue,getClosestValueIndex,getNextStepValue,getPreviousStepValue,getValueRanges,snapValueToStep}from"@zag-js/numeric-range";function normalizeValues(ctx,nextValues){return nextValues.map((value,index,values)=>{return constrainValue({...ctx,value:values},value,index)})}function getRangeAtIndex(ctx,index){return getValueRanges(ctx.value,ctx.min,ctx.max,ctx.minStepsBetweenThumbs)[index]}function constrainValue(ctx,value,index){const range=getRangeAtIndex(ctx,index);const snapValue=snapValueToStep(value,ctx.min,ctx.max,ctx.step);return clampValue(snapValue,range.min,range.max)}function decrement(ctx,index,step){const idx=index??ctx.focusedIndex;const range=getRangeAtIndex(ctx,idx);const nextValues=getPreviousStepValue(idx,{...range,step:step??ctx.step,values:ctx.value});nextValues[idx]=clampValue(nextValues[idx],range.min,range.max);return nextValues}function increment(ctx,index,step){const idx=index??ctx.focusedIndex;const range=getRangeAtIndex(ctx,idx);const nextValues=getNextStepValue(idx,{...range,step:step??ctx.step,values:ctx.value});nextValues[idx]=clampValue(nextValues[idx],range.min,range.max);return nextValues}function getClosestIndex(ctx,pointValue){return getClosestValueIndex(ctx.value,pointValue)}function assignArray(current,next){for(let i=0;i<next.length;i++){const value=next[i];current[i]=value}}function connect(state,send,normalize){const ariaLabel=state.context["aria-label"];const ariaLabelledBy=state.context["aria-labelledby"];const sliderValue=state.context.value;const focused=state.matches("focus");const dragging=state.matches("dragging");const disabled=state.context.isDisabled;const invalid=state.context.invalid;const interactive=state.context.isInteractive;const isHorizontal=state.context.orientation==="horizontal";const isVertical=state.context.orientation==="vertical";function getValuePercentFn(value){return getValuePercent2(value,state.context.min,state.context.max)}function getPercentValueFn(percent){return getPercentValue2(percent,state.context.min,state.context.max,state.context.step)}return{value:state.context.value,dragging,focused,setValue(value){send({type:"SET_VALUE",value})},getThumbValue(index){return sliderValue[index]},setThumbValue(index,value){send({type:"SET_VALUE",index,value})},getValuePercent:getValuePercentFn,getPercentValue:getPercentValueFn,getThumbPercent(index){return getValuePercentFn(sliderValue[index])},setThumbPercent(index,percent){const value=getPercentValueFn(percent);send({type:"SET_VALUE",index,value})},getThumbMin(index){return getRangeAtIndex(state.context,index).min},getThumbMax(index){return getRangeAtIndex(state.context,index).max},increment(index){send({type:"INCREMENT",index})},decrement(index){send({type:"DECREMENT",index})},focus(){if(!interactive)return;send({type:"FOCUS",index:0})},labelProps:normalize.label({...parts.label.attrs,dir:state.context.dir,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getLabelId(state.context),htmlFor:dom.getHiddenInputId(state.context,0),onClick(event){if(!interactive)return;event.preventDefault();dom.getFirstEl(state.context)?.focus()},style:{userSelect:"none"}}),rootProps:normalize.element({...parts.root.attrs,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getRootId(state.context),dir:state.context.dir,style:dom.getRootStyle(state.context)}),valueTextProps:normalize.element({...parts.valueText.attrs,dir:state.context.dir,"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),id:dom.getValueTextId(state.context)}),trackProps:normalize.element({...parts.track.attrs,dir:state.context.dir,id:dom.getTrackId(state.context),"data-disabled":dataAttr(disabled),"data-invalid":dataAttr(invalid),"data-orientation":state.context.orientation,"data-focus":dataAttr(focused),style:{position:"relative"}}),getThumbProps(props2){const{index=0,name}=props2;const value=sliderValue[index];const range=getRangeAtIndex(state.context,index);const valueText=state.context.getAriaValueText?.({value,index});const _ariaLabel=Array.isArray(ariaLabel)?ariaLabel[index]:ariaLabel;const _ariaLabelledBy=Array.isArray(ariaLabelledBy)?ariaLabelledBy[index]:ariaLabelledBy;return normalize.element({...parts.thumb.attrs,dir:state.context.dir,"data-index":index,"data-name":name,id:dom.getThumbId(state.context,index),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-focus":dataAttr(focused&&state.context.focusedIndex===index),draggable:false,"aria-disabled":ariaAttr(disabled),"aria-label":_ariaLabel,"aria-labelledby":_ariaLabelledBy??dom.getLabelId(state.context),"aria-orientation":state.context.orientation,"aria-valuemax":range.max,"aria-valuemin":range.min,"aria-valuenow":sliderValue[index],"aria-valuetext":valueText,role:"slider",tabIndex:disabled?void 0:0,style:dom.getThumbStyle(state.context,index),onPointerDown(event){if(!interactive)return;send({type:"THUMB_POINTER_DOWN",index});event.stopPropagation()},onBlur(){if(!interactive)return;send("BLUR")},onFocus(){if(!interactive)return;send({type:"FOCUS",index})},onKeyDown(event){if(event.defaultPrevented)return;if(!interactive)return;const step=getEventStep(event)*state.context.step;const keyMap={ArrowUp(){if(isHorizontal)return;send({type:"ARROW_INC",step,src:"ArrowUp"})},ArrowDown(){if(isHorizontal)return;send({type:"ARROW_DEC",step,src:"ArrowDown"})},ArrowLeft(){if(isVertical)return;send({type:"ARROW_DEC",step,src:"ArrowLeft"})},ArrowRight(){if(isVertical)return;send({type:"ARROW_INC",step,src:"ArrowRight"})},PageUp(){send({type:"ARROW_INC",step,src:"PageUp"})},PageDown(){send({type:"ARROW_DEC",step,src:"PageDown"})},Home(){send("HOME")},End(){send("END")}};const key=getEventKey(event,state.context);const exec=keyMap[key];if(exec){exec(event);event.preventDefault();event.stopPropagation()}}})},getHiddenInputProps(props2){const{index=0,name}=props2;return normalize.input({name:name??(state.context.name?state.context.name+(state.context.value.length>1?"[]":""):void 0),form:state.context.form,type:"text",hidden:true,defaultValue:state.context.value[index],id:dom.getHiddenInputId(state.context,index)})},rangeProps:normalize.element({id:dom.getRangeId(state.context),...parts.range.attrs,dir:state.context.dir,"data-focus":dataAttr(focused),"data-invalid":dataAttr(invalid),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,style:dom.getRangeStyle(state.context)}),controlProps:normalize.element({...parts.control.attrs,dir:state.context.dir,id:dom.getControlId(state.context),"data-disabled":dataAttr(disabled),"data-orientation":state.context.orientation,"data-invalid":dataAttr(invalid),"data-focus":dataAttr(focused),style:dom.getControlStyle(),onPointerDown(event){if(!interactive)return;if(!isLeftClick(event))return;if(isModifierKey(event))return;const point=getEventPoint(event);send({type:"POINTER_DOWN",point});event.preventDefault();event.stopPropagation()}}),markerGroupProps:normalize.element({...parts.markerGroup.attrs,role:"presentation",dir:state.context.dir,"aria-hidden":true,"data-orientation":state.context.orientation,style:dom.getMarkerGroupStyle()}),getMarkerProps(props2){const style=dom.getMarkerStyle(state.context,props2.value);let markerState;const first=state.context.value[0];const last=state.context.value[state.context.value.length-1];if(props2.value<first){markerState="under-value"}else if(props2.value>last){markerState="over-value"}else{markerState="at-value"}return normalize.element({...parts.marker.attrs,id:dom.getMarkerId(state.context,props2.value),role:"presentation",dir:state.context.dir,"data-orientation":state.context.orientation,"data-value":props2.value,"data-disabled":dataAttr(disabled),"data-state":markerState,style})}}}import{createMachine}from"@zag-js/core";import{trackPointerMove}from"@zag-js/dom-event";import{raf}from"@zag-js/dom-query";import{trackElementsSize}from"@zag-js/element-size";import{trackFormControl}from"@zag-js/form-utils";import{getValuePercent as getValuePercent3}from"@zag-js/numeric-range";import{compact,isEqual}from"@zag-js/utils";var isEqualSize=(a,b)=>{return a?.width===b?.width&&a?.height===b?.height};function machine(userContext){const ctx=compact(userContext);return createMachine({id:"slider",initial:"idle",context:{thumbSize:null,thumbAlignment:"contain",min:0,max:100,step:1,value:[0],origin:"start",orientation:"horizontal",dir:"ltr",minStepsBetweenThumbs:0,disabled:false,...ctx,focusedIndex:-1,fieldsetDisabled:false},computed:{isHorizontal:ctx2=>ctx2.orientation==="horizontal",isVertical:ctx2=>ctx2.orientation==="vertical",isRtl:ctx2=>ctx2.orientation==="horizontal"&&ctx2.dir==="rtl",isDisabled:ctx2=>!!ctx2.disabled||ctx2.fieldsetDisabled,isInteractive:ctx2=>!(ctx2.readOnly||ctx2.isDisabled),hasMeasuredThumbSize:ctx2=>ctx2.thumbSize!=null,valuePercent(ctx2){return ctx2.value.map(value=>100*getValuePercent3(value,ctx2.min,ctx2.max))}},watch:{value:["syncInputElements"]},entry:["coarseValue"],activities:["trackFormControlState","trackThumbsSize"],on:{SET_VALUE:[{guard:"hasIndex",actions:"setValueAtIndex"},{actions:"setValue"}],INCREMENT:{actions:"incrementThumbAtIndex"},DECREMENT:{actions:"decrementThumbAtIndex"}},states:{idle:{on:{POINTER_DOWN:{target:"dragging",actions:["setClosestThumbIndex","setPointerValue","focusActiveThumb"]},FOCUS:{target:"focus",actions:"setFocusedIndex"},THUMB_POINTER_DOWN:{target:"dragging",actions:["setFocusedIndex","focusActiveThumb"]}}},focus:{entry:"focusActiveThumb",on:{POINTER_DOWN:{target:"dragging",actions:["setClosestThumbIndex","setPointerValue","focusActiveThumb"]},THUMB_POINTER_DOWN:{target:"dragging",actions:["setFocusedIndex","focusActiveThumb"]},ARROW_DEC:{actions:["decrementThumbAtIndex","invokeOnChangeEnd"]},ARROW_INC:{actions:["incrementThumbAtIndex","invokeOnChangeEnd"]},HOME:{actions:["setFocusedThumbToMin","invokeOnChangeEnd"]},END:{actions:["setFocusedThumbToMax","invokeOnChangeEnd"]},BLUR:{target:"idle",actions:"clearFocusedIndex"}}},dragging:{entry:"focusActiveThumb",activities:"trackPointerMove",on:{POINTER_UP:{target:"focus",actions:"invokeOnChangeEnd"},POINTER_MOVE:{actions:"setPointerValue"}}}}},{guards:{hasIndex:(_ctx,evt)=>evt.index!=null},activities:{trackFormControlState(ctx2,_evt,{initialContext}){return trackFormControl(dom.getRootEl(ctx2),{onFieldsetDisabledChange(disabled){ctx2.fieldsetDisabled=disabled},onFormReset(){set.value(ctx2,initialContext.value)}})},trackPointerMove(ctx2,_evt,{send}){return trackPointerMove(dom.getDoc(ctx2),{onPointerMove(info){send({type:"POINTER_MOVE",point:info.point})},onPointerUp(){send("POINTER_UP")}})},trackThumbsSize(ctx2){if(ctx2.thumbAlignment!=="contain"||ctx2.thumbSize)return;return trackElementsSize({getNodes:()=>dom.getElements(ctx2),observeMutation:true,callback(size){if(!size||isEqualSize(ctx2.thumbSize,size))return;ctx2.thumbSize=size}})}},actions:{syncInputElements(ctx2){ctx2.value.forEach((value,index)=>{const inputEl=dom.getHiddenInputEl(ctx2,index);dom.setValue(inputEl,value)})},invokeOnChangeEnd(ctx2){ctx2.onValueChangeEnd?.({value:ctx2.value})},setClosestThumbIndex(ctx2,evt){const pointValue=dom.getValueFromPoint(ctx2,evt.point);if(pointValue==null)return;const focusedIndex=getClosestIndex(ctx2,pointValue);set.focusedIndex(ctx2,focusedIndex)},setFocusedIndex(ctx2,evt){set.focusedIndex(ctx2,evt.index)},clearFocusedIndex(ctx2){set.focusedIndex(ctx2,-1)},setPointerValue(ctx2,evt){const pointerValue=dom.getValueFromPoint(ctx2,evt.point);if(pointerValue==null)return;const value=constrainValue(ctx2,pointerValue,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,value)},focusActiveThumb(ctx2){raf(()=>{const thumbEl=dom.getThumbEl(ctx2,ctx2.focusedIndex);thumbEl?.focus({preventScroll:true})})},decrementThumbAtIndex(ctx2,evt){const value=decrement(ctx2,evt.index,evt.step);set.value(ctx2,value)},incrementThumbAtIndex(ctx2,evt){const value=increment(ctx2,evt.index,evt.step);set.value(ctx2,value)},setFocusedThumbToMin(ctx2){const{min}=getRangeAtIndex(ctx2,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,min)},setFocusedThumbToMax(ctx2){const{max}=getRangeAtIndex(ctx2,ctx2.focusedIndex);set.valueAtIndex(ctx2,ctx2.focusedIndex,max)},coarseValue(ctx2){const value=normalizeValues(ctx2,ctx2.value);set.value(ctx2,value)},setValueAtIndex(ctx2,evt){const value=constrainValue(ctx2,evt.value,evt.index);set.valueAtIndex(ctx2,evt.index,value)},setValue(ctx2,evt){const value=normalizeValues(ctx2,evt.value);set.value(ctx2,value)}}})}var invoke={change:ctx=>{ctx.onValueChange?.({value:Array.from(ctx.value)});dom.dispatchChangeEvent(ctx)},focusChange:ctx=>{ctx.onFocusChange?.({value:Array.from(ctx.value),focusedIndex:ctx.focusedIndex})}};var set={valueAtIndex:(ctx,index,value)=>{if(isEqual(ctx.value[index],value))return;ctx.value[index]=value;invoke.change(ctx)},value:(ctx,value)=>{if(isEqual(ctx.value,value))return;assignArray(ctx.value,value);invoke.change(ctx)},focusedIndex:(ctx,index)=>{if(isEqual(ctx.focusedIndex,index))return;ctx.focusedIndex=index;invoke.focusChange(ctx)}};import{createProps}from"@zag-js/types";import{createSplitProps}from"@zag-js/utils";var props=createProps()(["aria-label","aria-labelledby","dir","disabled","form","getAriaValueText","getRootNode","id","ids","invalid","max","min","minStepsBetweenThumbs","name","onFocusChange","onValueChange","onValueChangeEnd","orientation","origin","readOnly","step","thumbAlignment","thumbAlignment","thumbSize","value"]);var splitProps=createSplitProps(props);var thumbProps=createProps()(["index","name"]);var splitThumbProps=createSplitProps(thumbProps);export{anatomy,connect,machine,props,splitProps,splitThumbProps,thumbProps};
|
|
1
|
+
// src/slider.anatomy.ts
|
|
2
|
+
import { createAnatomy } from "@zag-js/anatomy";
|
|
3
|
+
var anatomy = createAnatomy("slider").parts(
|
|
4
|
+
"root",
|
|
5
|
+
"label",
|
|
6
|
+
"thumb",
|
|
7
|
+
"valueText",
|
|
8
|
+
"track",
|
|
9
|
+
"range",
|
|
10
|
+
"control",
|
|
11
|
+
"markerGroup",
|
|
12
|
+
"marker"
|
|
13
|
+
);
|
|
14
|
+
var parts = anatomy.build();
|
|
15
|
+
|
|
16
|
+
// src/slider.connect.ts
|
|
17
|
+
import {
|
|
18
|
+
getEventKey,
|
|
19
|
+
getEventPoint,
|
|
20
|
+
getEventStep,
|
|
21
|
+
isLeftClick,
|
|
22
|
+
isModifierKey
|
|
23
|
+
} from "@zag-js/dom-event";
|
|
24
|
+
import { ariaAttr, dataAttr } from "@zag-js/dom-query";
|
|
25
|
+
import { getPercentValue as getPercentValue2, getValuePercent as getValuePercent2 } from "@zag-js/numeric-range";
|
|
26
|
+
|
|
27
|
+
// src/slider.dom.ts
|
|
28
|
+
import { getRelativePoint } from "@zag-js/dom-event";
|
|
29
|
+
import { createScope, queryAll } from "@zag-js/dom-query";
|
|
30
|
+
import { dispatchInputValueEvent } from "@zag-js/form-utils";
|
|
31
|
+
import { getPercentValue } from "@zag-js/numeric-range";
|
|
32
|
+
|
|
33
|
+
// src/slider.style.ts
|
|
34
|
+
import { getValuePercent, getValueTransformer } from "@zag-js/numeric-range";
|
|
35
|
+
function getBounds(value) {
|
|
36
|
+
const firstValue = value[0];
|
|
37
|
+
const lastThumb = value[value.length - 1];
|
|
38
|
+
return [firstValue, lastThumb];
|
|
39
|
+
}
|
|
40
|
+
function getRangeOffsets(ctx) {
|
|
41
|
+
const [firstPercent, lastPercent] = getBounds(ctx.valuePercent);
|
|
42
|
+
if (ctx.valuePercent.length === 1) {
|
|
43
|
+
if (ctx.origin === "center") {
|
|
44
|
+
const isNegative = ctx.valuePercent[0] < 50;
|
|
45
|
+
const start = isNegative ? `${ctx.valuePercent[0]}%` : "50%";
|
|
46
|
+
const end = isNegative ? "50%" : `${100 - ctx.valuePercent[0]}%`;
|
|
47
|
+
return { start, end };
|
|
48
|
+
}
|
|
49
|
+
return { start: "0%", end: `${100 - lastPercent}%` };
|
|
50
|
+
}
|
|
51
|
+
return { start: `${firstPercent}%`, end: `${100 - lastPercent}%` };
|
|
52
|
+
}
|
|
53
|
+
function getRangeStyle(ctx) {
|
|
54
|
+
if (ctx.isVertical) {
|
|
55
|
+
return {
|
|
56
|
+
position: "absolute",
|
|
57
|
+
bottom: "var(--slider-range-start)",
|
|
58
|
+
top: "var(--slider-range-end)"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
position: "absolute",
|
|
63
|
+
[ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
|
|
64
|
+
[ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getVerticalThumbOffset(ctx) {
|
|
68
|
+
const { height = 0 } = ctx.thumbSize ?? {};
|
|
69
|
+
const getValue = getValueTransformer([ctx.min, ctx.max], [-height / 2, height / 2]);
|
|
70
|
+
return parseFloat(getValue(ctx.value).toFixed(2));
|
|
71
|
+
}
|
|
72
|
+
function getHorizontalThumbOffset(ctx) {
|
|
73
|
+
const { width = 0 } = ctx.thumbSize ?? {};
|
|
74
|
+
if (ctx.isRtl) {
|
|
75
|
+
const getValue2 = getValueTransformer([ctx.max, ctx.min], [-width / 2, width / 2]);
|
|
76
|
+
return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
|
|
77
|
+
}
|
|
78
|
+
const getValue = getValueTransformer([ctx.min, ctx.max], [-width / 2, width / 2]);
|
|
79
|
+
return parseFloat(getValue(ctx.value).toFixed(2));
|
|
80
|
+
}
|
|
81
|
+
function getOffset(ctx, percent) {
|
|
82
|
+
if (ctx.thumbAlignment === "center")
|
|
83
|
+
return `${percent}%`;
|
|
84
|
+
const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
|
|
85
|
+
return `calc(${percent}% - ${offset}px)`;
|
|
86
|
+
}
|
|
87
|
+
function getThumbOffset(ctx) {
|
|
88
|
+
let percent = getValuePercent(ctx.value, ctx.min, ctx.max) * 100;
|
|
89
|
+
return getOffset(ctx, percent);
|
|
90
|
+
}
|
|
91
|
+
function getVisibility(ctx) {
|
|
92
|
+
let visibility = "visible";
|
|
93
|
+
if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
|
|
94
|
+
visibility = "hidden";
|
|
95
|
+
}
|
|
96
|
+
return visibility;
|
|
97
|
+
}
|
|
98
|
+
function getThumbStyle(ctx, index) {
|
|
99
|
+
const placementProp = ctx.isVertical ? "bottom" : "insetInlineStart";
|
|
100
|
+
return {
|
|
101
|
+
visibility: getVisibility(ctx),
|
|
102
|
+
position: "absolute",
|
|
103
|
+
transform: "var(--slider-thumb-transform)",
|
|
104
|
+
[placementProp]: `var(--slider-thumb-offset-${index})`
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function getControlStyle() {
|
|
108
|
+
return {
|
|
109
|
+
touchAction: "none",
|
|
110
|
+
userSelect: "none",
|
|
111
|
+
position: "relative"
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function getRootStyle(ctx) {
|
|
115
|
+
const range = getRangeOffsets(ctx);
|
|
116
|
+
const offsetStyles = ctx.value.reduce((styles, value, index) => {
|
|
117
|
+
const offset = getThumbOffset({ ...ctx, value });
|
|
118
|
+
return { ...styles, [`--slider-thumb-offset-${index}`]: offset };
|
|
119
|
+
}, {});
|
|
120
|
+
return {
|
|
121
|
+
...offsetStyles,
|
|
122
|
+
"--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : ctx.isRtl ? "translateX(50%)" : "translateX(-50%)",
|
|
123
|
+
"--slider-range-start": range.start,
|
|
124
|
+
"--slider-range-end": range.end
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function getMarkerStyle(ctx, value) {
|
|
128
|
+
return {
|
|
129
|
+
// @ts-expect-error
|
|
130
|
+
visibility: getVisibility(ctx),
|
|
131
|
+
position: "absolute",
|
|
132
|
+
pointerEvents: "none",
|
|
133
|
+
// @ts-expect-error
|
|
134
|
+
[ctx.isHorizontal ? "insetInlineStart" : "bottom"]: getThumbOffset({ ...ctx, value }),
|
|
135
|
+
translate: "var(--tx) var(--ty)",
|
|
136
|
+
"--tx": ctx.isHorizontal ? ctx.isRtl ? "50%" : "-50%" : "0%",
|
|
137
|
+
"--ty": !ctx.isHorizontal ? "50%" : "0%"
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function getMarkerGroupStyle() {
|
|
141
|
+
return {
|
|
142
|
+
userSelect: "none",
|
|
143
|
+
pointerEvents: "none",
|
|
144
|
+
position: "relative"
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
var styleGetterFns = {
|
|
148
|
+
getRootStyle,
|
|
149
|
+
getControlStyle,
|
|
150
|
+
getThumbStyle,
|
|
151
|
+
getRangeStyle,
|
|
152
|
+
getMarkerStyle,
|
|
153
|
+
getMarkerGroupStyle
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/slider.dom.ts
|
|
157
|
+
var dom = createScope({
|
|
158
|
+
...styleGetterFns,
|
|
159
|
+
getRootId: (ctx) => ctx.ids?.root ?? `slider:${ctx.id}`,
|
|
160
|
+
getThumbId: (ctx, index) => ctx.ids?.thumb?.(index) ?? `slider:${ctx.id}:thumb:${index}`,
|
|
161
|
+
getHiddenInputId: (ctx, index) => `slider:${ctx.id}:input:${index}`,
|
|
162
|
+
getControlId: (ctx) => ctx.ids?.control ?? `slider:${ctx.id}:control`,
|
|
163
|
+
getTrackId: (ctx) => ctx.ids?.track ?? `slider:${ctx.id}:track`,
|
|
164
|
+
getRangeId: (ctx) => ctx.ids?.range ?? `slider:${ctx.id}:range`,
|
|
165
|
+
getLabelId: (ctx) => ctx.ids?.label ?? `slider:${ctx.id}:label`,
|
|
166
|
+
getValueTextId: (ctx) => ctx.ids?.valueText ?? `slider:${ctx.id}:valueText`,
|
|
167
|
+
getMarkerId: (ctx, value) => ctx.ids?.marker?.(value) ?? `slider:${ctx.id}:marker:${value}`,
|
|
168
|
+
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
169
|
+
getThumbEl: (ctx, index) => dom.getById(ctx, dom.getThumbId(ctx, index)),
|
|
170
|
+
getHiddenInputEl: (ctx, index) => dom.getById(ctx, dom.getHiddenInputId(ctx, index)),
|
|
171
|
+
getControlEl: (ctx) => dom.getById(ctx, dom.getControlId(ctx)),
|
|
172
|
+
getElements: (ctx) => queryAll(dom.getControlEl(ctx), "[role=slider]"),
|
|
173
|
+
getFirstEl: (ctx) => dom.getElements(ctx)[0],
|
|
174
|
+
getRangeEl: (ctx) => dom.getById(ctx, dom.getRangeId(ctx)),
|
|
175
|
+
getValueFromPoint(ctx, point) {
|
|
176
|
+
const controlEl = dom.getControlEl(ctx);
|
|
177
|
+
if (!controlEl)
|
|
178
|
+
return;
|
|
179
|
+
const relativePoint = getRelativePoint(point, controlEl);
|
|
180
|
+
const percent = relativePoint.getPercentValue({
|
|
181
|
+
orientation: ctx.orientation,
|
|
182
|
+
dir: ctx.dir,
|
|
183
|
+
inverted: { y: true }
|
|
184
|
+
});
|
|
185
|
+
return getPercentValue(percent, ctx.min, ctx.max, ctx.step);
|
|
186
|
+
},
|
|
187
|
+
dispatchChangeEvent(ctx) {
|
|
188
|
+
const valueArray = Array.from(ctx.value);
|
|
189
|
+
valueArray.forEach((value, index) => {
|
|
190
|
+
const inputEl = dom.getHiddenInputEl(ctx, index);
|
|
191
|
+
if (!inputEl)
|
|
192
|
+
return;
|
|
193
|
+
dispatchInputValueEvent(inputEl, { value });
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// src/slider.utils.ts
|
|
199
|
+
import {
|
|
200
|
+
clampValue,
|
|
201
|
+
getClosestValueIndex,
|
|
202
|
+
getNextStepValue,
|
|
203
|
+
getPreviousStepValue,
|
|
204
|
+
getValueRanges,
|
|
205
|
+
snapValueToStep
|
|
206
|
+
} from "@zag-js/numeric-range";
|
|
207
|
+
function normalizeValues(ctx, nextValues) {
|
|
208
|
+
return nextValues.map((value, index, values) => {
|
|
209
|
+
return constrainValue({ ...ctx, value: values }, value, index);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
function getRangeAtIndex(ctx, index) {
|
|
213
|
+
return getValueRanges(ctx.value, ctx.min, ctx.max, ctx.minStepsBetweenThumbs)[index];
|
|
214
|
+
}
|
|
215
|
+
function constrainValue(ctx, value, index) {
|
|
216
|
+
const range = getRangeAtIndex(ctx, index);
|
|
217
|
+
const snapValue = snapValueToStep(value, ctx.min, ctx.max, ctx.step);
|
|
218
|
+
return clampValue(snapValue, range.min, range.max);
|
|
219
|
+
}
|
|
220
|
+
function decrement(ctx, index, step) {
|
|
221
|
+
const idx = index ?? ctx.focusedIndex;
|
|
222
|
+
const range = getRangeAtIndex(ctx, idx);
|
|
223
|
+
const nextValues = getPreviousStepValue(idx, {
|
|
224
|
+
...range,
|
|
225
|
+
step: step ?? ctx.step,
|
|
226
|
+
values: ctx.value
|
|
227
|
+
});
|
|
228
|
+
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
229
|
+
return nextValues;
|
|
230
|
+
}
|
|
231
|
+
function increment(ctx, index, step) {
|
|
232
|
+
const idx = index ?? ctx.focusedIndex;
|
|
233
|
+
const range = getRangeAtIndex(ctx, idx);
|
|
234
|
+
const nextValues = getNextStepValue(idx, {
|
|
235
|
+
...range,
|
|
236
|
+
step: step ?? ctx.step,
|
|
237
|
+
values: ctx.value
|
|
238
|
+
});
|
|
239
|
+
nextValues[idx] = clampValue(nextValues[idx], range.min, range.max);
|
|
240
|
+
return nextValues;
|
|
241
|
+
}
|
|
242
|
+
function getClosestIndex(ctx, pointValue) {
|
|
243
|
+
return getClosestValueIndex(ctx.value, pointValue);
|
|
244
|
+
}
|
|
245
|
+
function assignArray(current, next) {
|
|
246
|
+
for (let i = 0; i < next.length; i++) {
|
|
247
|
+
const value = next[i];
|
|
248
|
+
current[i] = value;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/slider.connect.ts
|
|
253
|
+
function connect(state, send, normalize) {
|
|
254
|
+
const ariaLabel = state.context["aria-label"];
|
|
255
|
+
const ariaLabelledBy = state.context["aria-labelledby"];
|
|
256
|
+
const sliderValue = state.context.value;
|
|
257
|
+
const focused = state.matches("focus");
|
|
258
|
+
const dragging = state.matches("dragging");
|
|
259
|
+
const disabled = state.context.isDisabled;
|
|
260
|
+
const invalid = state.context.invalid;
|
|
261
|
+
const interactive = state.context.isInteractive;
|
|
262
|
+
const isHorizontal = state.context.orientation === "horizontal";
|
|
263
|
+
const isVertical = state.context.orientation === "vertical";
|
|
264
|
+
function getValuePercentFn(value) {
|
|
265
|
+
return getValuePercent2(value, state.context.min, state.context.max);
|
|
266
|
+
}
|
|
267
|
+
function getPercentValueFn(percent) {
|
|
268
|
+
return getPercentValue2(percent, state.context.min, state.context.max, state.context.step);
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
value: state.context.value,
|
|
272
|
+
dragging,
|
|
273
|
+
focused,
|
|
274
|
+
setValue(value) {
|
|
275
|
+
send({ type: "SET_VALUE", value });
|
|
276
|
+
},
|
|
277
|
+
getThumbValue(index) {
|
|
278
|
+
return sliderValue[index];
|
|
279
|
+
},
|
|
280
|
+
setThumbValue(index, value) {
|
|
281
|
+
send({ type: "SET_VALUE", index, value });
|
|
282
|
+
},
|
|
283
|
+
getValuePercent: getValuePercentFn,
|
|
284
|
+
getPercentValue: getPercentValueFn,
|
|
285
|
+
getThumbPercent(index) {
|
|
286
|
+
return getValuePercentFn(sliderValue[index]);
|
|
287
|
+
},
|
|
288
|
+
setThumbPercent(index, percent) {
|
|
289
|
+
const value = getPercentValueFn(percent);
|
|
290
|
+
send({ type: "SET_VALUE", index, value });
|
|
291
|
+
},
|
|
292
|
+
getThumbMin(index) {
|
|
293
|
+
return getRangeAtIndex(state.context, index).min;
|
|
294
|
+
},
|
|
295
|
+
getThumbMax(index) {
|
|
296
|
+
return getRangeAtIndex(state.context, index).max;
|
|
297
|
+
},
|
|
298
|
+
increment(index) {
|
|
299
|
+
send({ type: "INCREMENT", index });
|
|
300
|
+
},
|
|
301
|
+
decrement(index) {
|
|
302
|
+
send({ type: "DECREMENT", index });
|
|
303
|
+
},
|
|
304
|
+
focus() {
|
|
305
|
+
if (!interactive)
|
|
306
|
+
return;
|
|
307
|
+
send({ type: "FOCUS", index: 0 });
|
|
308
|
+
},
|
|
309
|
+
getLabelProps() {
|
|
310
|
+
return normalize.label({
|
|
311
|
+
...parts.label.attrs,
|
|
312
|
+
dir: state.context.dir,
|
|
313
|
+
"data-disabled": dataAttr(disabled),
|
|
314
|
+
"data-orientation": state.context.orientation,
|
|
315
|
+
"data-invalid": dataAttr(invalid),
|
|
316
|
+
"data-focus": dataAttr(focused),
|
|
317
|
+
id: dom.getLabelId(state.context),
|
|
318
|
+
htmlFor: dom.getHiddenInputId(state.context, 0),
|
|
319
|
+
onClick(event) {
|
|
320
|
+
if (!interactive)
|
|
321
|
+
return;
|
|
322
|
+
event.preventDefault();
|
|
323
|
+
dom.getFirstEl(state.context)?.focus();
|
|
324
|
+
},
|
|
325
|
+
style: {
|
|
326
|
+
userSelect: "none"
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
getRootProps() {
|
|
331
|
+
return normalize.element({
|
|
332
|
+
...parts.root.attrs,
|
|
333
|
+
"data-disabled": dataAttr(disabled),
|
|
334
|
+
"data-orientation": state.context.orientation,
|
|
335
|
+
"data-invalid": dataAttr(invalid),
|
|
336
|
+
"data-focus": dataAttr(focused),
|
|
337
|
+
id: dom.getRootId(state.context),
|
|
338
|
+
dir: state.context.dir,
|
|
339
|
+
style: dom.getRootStyle(state.context)
|
|
340
|
+
});
|
|
341
|
+
},
|
|
342
|
+
getValueTextProps() {
|
|
343
|
+
return normalize.element({
|
|
344
|
+
...parts.valueText.attrs,
|
|
345
|
+
dir: state.context.dir,
|
|
346
|
+
"data-disabled": dataAttr(disabled),
|
|
347
|
+
"data-orientation": state.context.orientation,
|
|
348
|
+
"data-invalid": dataAttr(invalid),
|
|
349
|
+
"data-focus": dataAttr(focused),
|
|
350
|
+
id: dom.getValueTextId(state.context)
|
|
351
|
+
});
|
|
352
|
+
},
|
|
353
|
+
getTrackProps() {
|
|
354
|
+
return normalize.element({
|
|
355
|
+
...parts.track.attrs,
|
|
356
|
+
dir: state.context.dir,
|
|
357
|
+
id: dom.getTrackId(state.context),
|
|
358
|
+
"data-disabled": dataAttr(disabled),
|
|
359
|
+
"data-invalid": dataAttr(invalid),
|
|
360
|
+
"data-orientation": state.context.orientation,
|
|
361
|
+
"data-focus": dataAttr(focused),
|
|
362
|
+
style: { position: "relative" }
|
|
363
|
+
});
|
|
364
|
+
},
|
|
365
|
+
getThumbProps(props2) {
|
|
366
|
+
const { index = 0, name } = props2;
|
|
367
|
+
const value = sliderValue[index];
|
|
368
|
+
const range = getRangeAtIndex(state.context, index);
|
|
369
|
+
const valueText = state.context.getAriaValueText?.({ value, index });
|
|
370
|
+
const _ariaLabel = Array.isArray(ariaLabel) ? ariaLabel[index] : ariaLabel;
|
|
371
|
+
const _ariaLabelledBy = Array.isArray(ariaLabelledBy) ? ariaLabelledBy[index] : ariaLabelledBy;
|
|
372
|
+
return normalize.element({
|
|
373
|
+
...parts.thumb.attrs,
|
|
374
|
+
dir: state.context.dir,
|
|
375
|
+
"data-index": index,
|
|
376
|
+
"data-name": name,
|
|
377
|
+
id: dom.getThumbId(state.context, index),
|
|
378
|
+
"data-disabled": dataAttr(disabled),
|
|
379
|
+
"data-orientation": state.context.orientation,
|
|
380
|
+
"data-focus": dataAttr(focused && state.context.focusedIndex === index),
|
|
381
|
+
draggable: false,
|
|
382
|
+
"aria-disabled": ariaAttr(disabled),
|
|
383
|
+
"aria-label": _ariaLabel,
|
|
384
|
+
"aria-labelledby": _ariaLabelledBy ?? dom.getLabelId(state.context),
|
|
385
|
+
"aria-orientation": state.context.orientation,
|
|
386
|
+
"aria-valuemax": range.max,
|
|
387
|
+
"aria-valuemin": range.min,
|
|
388
|
+
"aria-valuenow": sliderValue[index],
|
|
389
|
+
"aria-valuetext": valueText,
|
|
390
|
+
role: "slider",
|
|
391
|
+
tabIndex: disabled ? void 0 : 0,
|
|
392
|
+
style: dom.getThumbStyle(state.context, index),
|
|
393
|
+
onPointerDown(event) {
|
|
394
|
+
if (!interactive)
|
|
395
|
+
return;
|
|
396
|
+
send({ type: "THUMB_POINTER_DOWN", index });
|
|
397
|
+
event.stopPropagation();
|
|
398
|
+
},
|
|
399
|
+
onBlur() {
|
|
400
|
+
if (!interactive)
|
|
401
|
+
return;
|
|
402
|
+
send("BLUR");
|
|
403
|
+
},
|
|
404
|
+
onFocus() {
|
|
405
|
+
if (!interactive)
|
|
406
|
+
return;
|
|
407
|
+
send({ type: "FOCUS", index });
|
|
408
|
+
},
|
|
409
|
+
onKeyDown(event) {
|
|
410
|
+
if (event.defaultPrevented)
|
|
411
|
+
return;
|
|
412
|
+
if (!interactive)
|
|
413
|
+
return;
|
|
414
|
+
const step = getEventStep(event) * state.context.step;
|
|
415
|
+
const keyMap = {
|
|
416
|
+
ArrowUp() {
|
|
417
|
+
if (isHorizontal)
|
|
418
|
+
return;
|
|
419
|
+
send({ type: "ARROW_INC", step, src: "ArrowUp" });
|
|
420
|
+
},
|
|
421
|
+
ArrowDown() {
|
|
422
|
+
if (isHorizontal)
|
|
423
|
+
return;
|
|
424
|
+
send({ type: "ARROW_DEC", step, src: "ArrowDown" });
|
|
425
|
+
},
|
|
426
|
+
ArrowLeft() {
|
|
427
|
+
if (isVertical)
|
|
428
|
+
return;
|
|
429
|
+
send({ type: "ARROW_DEC", step, src: "ArrowLeft" });
|
|
430
|
+
},
|
|
431
|
+
ArrowRight() {
|
|
432
|
+
if (isVertical)
|
|
433
|
+
return;
|
|
434
|
+
send({ type: "ARROW_INC", step, src: "ArrowRight" });
|
|
435
|
+
},
|
|
436
|
+
PageUp() {
|
|
437
|
+
send({ type: "ARROW_INC", step, src: "PageUp" });
|
|
438
|
+
},
|
|
439
|
+
PageDown() {
|
|
440
|
+
send({ type: "ARROW_DEC", step, src: "PageDown" });
|
|
441
|
+
},
|
|
442
|
+
Home() {
|
|
443
|
+
send("HOME");
|
|
444
|
+
},
|
|
445
|
+
End() {
|
|
446
|
+
send("END");
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
const key = getEventKey(event, state.context);
|
|
450
|
+
const exec = keyMap[key];
|
|
451
|
+
if (exec) {
|
|
452
|
+
exec(event);
|
|
453
|
+
event.preventDefault();
|
|
454
|
+
event.stopPropagation();
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
},
|
|
459
|
+
getHiddenInputProps(props2) {
|
|
460
|
+
const { index = 0, name } = props2;
|
|
461
|
+
return normalize.input({
|
|
462
|
+
name: name ?? (state.context.name ? state.context.name + (state.context.value.length > 1 ? "[]" : "") : void 0),
|
|
463
|
+
form: state.context.form,
|
|
464
|
+
type: "text",
|
|
465
|
+
hidden: true,
|
|
466
|
+
defaultValue: state.context.value[index],
|
|
467
|
+
id: dom.getHiddenInputId(state.context, index)
|
|
468
|
+
});
|
|
469
|
+
},
|
|
470
|
+
getRangeProps() {
|
|
471
|
+
return normalize.element({
|
|
472
|
+
id: dom.getRangeId(state.context),
|
|
473
|
+
...parts.range.attrs,
|
|
474
|
+
dir: state.context.dir,
|
|
475
|
+
"data-focus": dataAttr(focused),
|
|
476
|
+
"data-invalid": dataAttr(invalid),
|
|
477
|
+
"data-disabled": dataAttr(disabled),
|
|
478
|
+
"data-orientation": state.context.orientation,
|
|
479
|
+
style: dom.getRangeStyle(state.context)
|
|
480
|
+
});
|
|
481
|
+
},
|
|
482
|
+
getControlProps() {
|
|
483
|
+
return normalize.element({
|
|
484
|
+
...parts.control.attrs,
|
|
485
|
+
dir: state.context.dir,
|
|
486
|
+
id: dom.getControlId(state.context),
|
|
487
|
+
"data-disabled": dataAttr(disabled),
|
|
488
|
+
"data-orientation": state.context.orientation,
|
|
489
|
+
"data-invalid": dataAttr(invalid),
|
|
490
|
+
"data-focus": dataAttr(focused),
|
|
491
|
+
style: dom.getControlStyle(),
|
|
492
|
+
onPointerDown(event) {
|
|
493
|
+
if (!interactive)
|
|
494
|
+
return;
|
|
495
|
+
if (!isLeftClick(event))
|
|
496
|
+
return;
|
|
497
|
+
if (isModifierKey(event))
|
|
498
|
+
return;
|
|
499
|
+
const point = getEventPoint(event);
|
|
500
|
+
send({ type: "POINTER_DOWN", point });
|
|
501
|
+
event.preventDefault();
|
|
502
|
+
event.stopPropagation();
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
},
|
|
506
|
+
getMarkerGroupProps() {
|
|
507
|
+
return normalize.element({
|
|
508
|
+
...parts.markerGroup.attrs,
|
|
509
|
+
role: "presentation",
|
|
510
|
+
dir: state.context.dir,
|
|
511
|
+
"aria-hidden": true,
|
|
512
|
+
"data-orientation": state.context.orientation,
|
|
513
|
+
style: dom.getMarkerGroupStyle()
|
|
514
|
+
});
|
|
515
|
+
},
|
|
516
|
+
getMarkerProps(props2) {
|
|
517
|
+
const style = dom.getMarkerStyle(state.context, props2.value);
|
|
518
|
+
let markerState;
|
|
519
|
+
const first = state.context.value[0];
|
|
520
|
+
const last = state.context.value[state.context.value.length - 1];
|
|
521
|
+
if (props2.value < first) {
|
|
522
|
+
markerState = "under-value";
|
|
523
|
+
} else if (props2.value > last) {
|
|
524
|
+
markerState = "over-value";
|
|
525
|
+
} else {
|
|
526
|
+
markerState = "at-value";
|
|
527
|
+
}
|
|
528
|
+
return normalize.element({
|
|
529
|
+
...parts.marker.attrs,
|
|
530
|
+
id: dom.getMarkerId(state.context, props2.value),
|
|
531
|
+
role: "presentation",
|
|
532
|
+
dir: state.context.dir,
|
|
533
|
+
"data-orientation": state.context.orientation,
|
|
534
|
+
"data-value": props2.value,
|
|
535
|
+
"data-disabled": dataAttr(disabled),
|
|
536
|
+
"data-state": markerState,
|
|
537
|
+
style
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// src/slider.machine.ts
|
|
544
|
+
import { createMachine } from "@zag-js/core";
|
|
545
|
+
import { trackPointerMove } from "@zag-js/dom-event";
|
|
546
|
+
import { raf } from "@zag-js/dom-query";
|
|
547
|
+
import { trackElementsSize } from "@zag-js/element-size";
|
|
548
|
+
import { trackFormControl } from "@zag-js/form-utils";
|
|
549
|
+
import { getValuePercent as getValuePercent3 } from "@zag-js/numeric-range";
|
|
550
|
+
import { compact, isEqual } from "@zag-js/utils";
|
|
551
|
+
var isEqualSize = (a, b) => {
|
|
552
|
+
return a?.width === b?.width && a?.height === b?.height;
|
|
553
|
+
};
|
|
554
|
+
function machine(userContext) {
|
|
555
|
+
const ctx = compact(userContext);
|
|
556
|
+
return createMachine(
|
|
557
|
+
{
|
|
558
|
+
id: "slider",
|
|
559
|
+
initial: "idle",
|
|
560
|
+
context: {
|
|
561
|
+
thumbSize: null,
|
|
562
|
+
thumbAlignment: "contain",
|
|
563
|
+
min: 0,
|
|
564
|
+
max: 100,
|
|
565
|
+
step: 1,
|
|
566
|
+
value: [0],
|
|
567
|
+
origin: "start",
|
|
568
|
+
orientation: "horizontal",
|
|
569
|
+
dir: "ltr",
|
|
570
|
+
minStepsBetweenThumbs: 0,
|
|
571
|
+
disabled: false,
|
|
572
|
+
...ctx,
|
|
573
|
+
focusedIndex: -1,
|
|
574
|
+
fieldsetDisabled: false
|
|
575
|
+
},
|
|
576
|
+
computed: {
|
|
577
|
+
isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
|
|
578
|
+
isVertical: (ctx2) => ctx2.orientation === "vertical",
|
|
579
|
+
isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
|
|
580
|
+
isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled,
|
|
581
|
+
isInteractive: (ctx2) => !(ctx2.readOnly || ctx2.isDisabled),
|
|
582
|
+
hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize != null,
|
|
583
|
+
valuePercent(ctx2) {
|
|
584
|
+
return ctx2.value.map((value) => 100 * getValuePercent3(value, ctx2.min, ctx2.max));
|
|
585
|
+
}
|
|
586
|
+
},
|
|
587
|
+
watch: {
|
|
588
|
+
value: ["syncInputElements"]
|
|
589
|
+
},
|
|
590
|
+
entry: ["coarseValue"],
|
|
591
|
+
activities: ["trackFormControlState", "trackThumbsSize"],
|
|
592
|
+
on: {
|
|
593
|
+
SET_VALUE: [
|
|
594
|
+
{
|
|
595
|
+
guard: "hasIndex",
|
|
596
|
+
actions: "setValueAtIndex"
|
|
597
|
+
},
|
|
598
|
+
{ actions: "setValue" }
|
|
599
|
+
],
|
|
600
|
+
INCREMENT: {
|
|
601
|
+
actions: "incrementThumbAtIndex"
|
|
602
|
+
},
|
|
603
|
+
DECREMENT: {
|
|
604
|
+
actions: "decrementThumbAtIndex"
|
|
605
|
+
}
|
|
606
|
+
},
|
|
607
|
+
states: {
|
|
608
|
+
idle: {
|
|
609
|
+
on: {
|
|
610
|
+
POINTER_DOWN: {
|
|
611
|
+
target: "dragging",
|
|
612
|
+
actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"]
|
|
613
|
+
},
|
|
614
|
+
FOCUS: {
|
|
615
|
+
target: "focus",
|
|
616
|
+
actions: "setFocusedIndex"
|
|
617
|
+
},
|
|
618
|
+
THUMB_POINTER_DOWN: {
|
|
619
|
+
target: "dragging",
|
|
620
|
+
actions: ["setFocusedIndex", "focusActiveThumb"]
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
focus: {
|
|
625
|
+
entry: "focusActiveThumb",
|
|
626
|
+
on: {
|
|
627
|
+
POINTER_DOWN: {
|
|
628
|
+
target: "dragging",
|
|
629
|
+
actions: ["setClosestThumbIndex", "setPointerValue", "focusActiveThumb"]
|
|
630
|
+
},
|
|
631
|
+
THUMB_POINTER_DOWN: {
|
|
632
|
+
target: "dragging",
|
|
633
|
+
actions: ["setFocusedIndex", "focusActiveThumb"]
|
|
634
|
+
},
|
|
635
|
+
ARROW_DEC: {
|
|
636
|
+
actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
|
|
637
|
+
},
|
|
638
|
+
ARROW_INC: {
|
|
639
|
+
actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
|
|
640
|
+
},
|
|
641
|
+
HOME: {
|
|
642
|
+
actions: ["setFocusedThumbToMin", "invokeOnChangeEnd"]
|
|
643
|
+
},
|
|
644
|
+
END: {
|
|
645
|
+
actions: ["setFocusedThumbToMax", "invokeOnChangeEnd"]
|
|
646
|
+
},
|
|
647
|
+
BLUR: {
|
|
648
|
+
target: "idle",
|
|
649
|
+
actions: "clearFocusedIndex"
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
dragging: {
|
|
654
|
+
entry: "focusActiveThumb",
|
|
655
|
+
activities: "trackPointerMove",
|
|
656
|
+
on: {
|
|
657
|
+
POINTER_UP: {
|
|
658
|
+
target: "focus",
|
|
659
|
+
actions: "invokeOnChangeEnd"
|
|
660
|
+
},
|
|
661
|
+
POINTER_MOVE: {
|
|
662
|
+
actions: "setPointerValue"
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
},
|
|
668
|
+
{
|
|
669
|
+
guards: {
|
|
670
|
+
hasIndex: (_ctx, evt) => evt.index != null
|
|
671
|
+
},
|
|
672
|
+
activities: {
|
|
673
|
+
trackFormControlState(ctx2, _evt, { initialContext }) {
|
|
674
|
+
return trackFormControl(dom.getRootEl(ctx2), {
|
|
675
|
+
onFieldsetDisabledChange(disabled) {
|
|
676
|
+
ctx2.fieldsetDisabled = disabled;
|
|
677
|
+
},
|
|
678
|
+
onFormReset() {
|
|
679
|
+
set.value(ctx2, initialContext.value);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
},
|
|
683
|
+
trackPointerMove(ctx2, _evt, { send }) {
|
|
684
|
+
return trackPointerMove(dom.getDoc(ctx2), {
|
|
685
|
+
onPointerMove(info) {
|
|
686
|
+
send({ type: "POINTER_MOVE", point: info.point });
|
|
687
|
+
},
|
|
688
|
+
onPointerUp() {
|
|
689
|
+
send("POINTER_UP");
|
|
690
|
+
}
|
|
691
|
+
});
|
|
692
|
+
},
|
|
693
|
+
trackThumbsSize(ctx2) {
|
|
694
|
+
if (ctx2.thumbAlignment !== "contain" || ctx2.thumbSize)
|
|
695
|
+
return;
|
|
696
|
+
return trackElementsSize({
|
|
697
|
+
getNodes: () => dom.getElements(ctx2),
|
|
698
|
+
observeMutation: true,
|
|
699
|
+
callback(size) {
|
|
700
|
+
if (!size || isEqualSize(ctx2.thumbSize, size))
|
|
701
|
+
return;
|
|
702
|
+
ctx2.thumbSize = size;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
},
|
|
707
|
+
actions: {
|
|
708
|
+
syncInputElements(ctx2) {
|
|
709
|
+
ctx2.value.forEach((value, index) => {
|
|
710
|
+
const inputEl = dom.getHiddenInputEl(ctx2, index);
|
|
711
|
+
dom.setValue(inputEl, value);
|
|
712
|
+
});
|
|
713
|
+
},
|
|
714
|
+
invokeOnChangeEnd(ctx2) {
|
|
715
|
+
ctx2.onValueChangeEnd?.({ value: ctx2.value });
|
|
716
|
+
},
|
|
717
|
+
setClosestThumbIndex(ctx2, evt) {
|
|
718
|
+
const pointValue = dom.getValueFromPoint(ctx2, evt.point);
|
|
719
|
+
if (pointValue == null)
|
|
720
|
+
return;
|
|
721
|
+
const focusedIndex = getClosestIndex(ctx2, pointValue);
|
|
722
|
+
set.focusedIndex(ctx2, focusedIndex);
|
|
723
|
+
},
|
|
724
|
+
setFocusedIndex(ctx2, evt) {
|
|
725
|
+
set.focusedIndex(ctx2, evt.index);
|
|
726
|
+
},
|
|
727
|
+
clearFocusedIndex(ctx2) {
|
|
728
|
+
set.focusedIndex(ctx2, -1);
|
|
729
|
+
},
|
|
730
|
+
setPointerValue(ctx2, evt) {
|
|
731
|
+
const pointerValue = dom.getValueFromPoint(ctx2, evt.point);
|
|
732
|
+
if (pointerValue == null)
|
|
733
|
+
return;
|
|
734
|
+
const value = constrainValue(ctx2, pointerValue, ctx2.focusedIndex);
|
|
735
|
+
set.valueAtIndex(ctx2, ctx2.focusedIndex, value);
|
|
736
|
+
},
|
|
737
|
+
focusActiveThumb(ctx2) {
|
|
738
|
+
raf(() => {
|
|
739
|
+
const thumbEl = dom.getThumbEl(ctx2, ctx2.focusedIndex);
|
|
740
|
+
thumbEl?.focus({ preventScroll: true });
|
|
741
|
+
});
|
|
742
|
+
},
|
|
743
|
+
decrementThumbAtIndex(ctx2, evt) {
|
|
744
|
+
const value = decrement(ctx2, evt.index, evt.step);
|
|
745
|
+
set.value(ctx2, value);
|
|
746
|
+
},
|
|
747
|
+
incrementThumbAtIndex(ctx2, evt) {
|
|
748
|
+
const value = increment(ctx2, evt.index, evt.step);
|
|
749
|
+
set.value(ctx2, value);
|
|
750
|
+
},
|
|
751
|
+
setFocusedThumbToMin(ctx2) {
|
|
752
|
+
const { min } = getRangeAtIndex(ctx2, ctx2.focusedIndex);
|
|
753
|
+
set.valueAtIndex(ctx2, ctx2.focusedIndex, min);
|
|
754
|
+
},
|
|
755
|
+
setFocusedThumbToMax(ctx2) {
|
|
756
|
+
const { max } = getRangeAtIndex(ctx2, ctx2.focusedIndex);
|
|
757
|
+
set.valueAtIndex(ctx2, ctx2.focusedIndex, max);
|
|
758
|
+
},
|
|
759
|
+
coarseValue(ctx2) {
|
|
760
|
+
const value = normalizeValues(ctx2, ctx2.value);
|
|
761
|
+
set.value(ctx2, value);
|
|
762
|
+
},
|
|
763
|
+
setValueAtIndex(ctx2, evt) {
|
|
764
|
+
const value = constrainValue(ctx2, evt.value, evt.index);
|
|
765
|
+
set.valueAtIndex(ctx2, evt.index, value);
|
|
766
|
+
},
|
|
767
|
+
setValue(ctx2, evt) {
|
|
768
|
+
const value = normalizeValues(ctx2, evt.value);
|
|
769
|
+
set.value(ctx2, value);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
);
|
|
774
|
+
}
|
|
775
|
+
var invoke = {
|
|
776
|
+
change: (ctx) => {
|
|
777
|
+
ctx.onValueChange?.({
|
|
778
|
+
value: Array.from(ctx.value)
|
|
779
|
+
});
|
|
780
|
+
dom.dispatchChangeEvent(ctx);
|
|
781
|
+
},
|
|
782
|
+
focusChange: (ctx) => {
|
|
783
|
+
ctx.onFocusChange?.({
|
|
784
|
+
value: Array.from(ctx.value),
|
|
785
|
+
focusedIndex: ctx.focusedIndex
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
var set = {
|
|
790
|
+
valueAtIndex: (ctx, index, value) => {
|
|
791
|
+
if (isEqual(ctx.value[index], value))
|
|
792
|
+
return;
|
|
793
|
+
ctx.value[index] = value;
|
|
794
|
+
invoke.change(ctx);
|
|
795
|
+
},
|
|
796
|
+
value: (ctx, value) => {
|
|
797
|
+
if (isEqual(ctx.value, value))
|
|
798
|
+
return;
|
|
799
|
+
assignArray(ctx.value, value);
|
|
800
|
+
invoke.change(ctx);
|
|
801
|
+
},
|
|
802
|
+
focusedIndex: (ctx, index) => {
|
|
803
|
+
if (isEqual(ctx.focusedIndex, index))
|
|
804
|
+
return;
|
|
805
|
+
ctx.focusedIndex = index;
|
|
806
|
+
invoke.focusChange(ctx);
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
// src/slider.props.ts
|
|
811
|
+
import { createProps } from "@zag-js/types";
|
|
812
|
+
import { createSplitProps } from "@zag-js/utils";
|
|
813
|
+
var props = createProps()([
|
|
814
|
+
"aria-label",
|
|
815
|
+
"aria-labelledby",
|
|
816
|
+
"dir",
|
|
817
|
+
"disabled",
|
|
818
|
+
"form",
|
|
819
|
+
"getAriaValueText",
|
|
820
|
+
"getRootNode",
|
|
821
|
+
"id",
|
|
822
|
+
"ids",
|
|
823
|
+
"invalid",
|
|
824
|
+
"max",
|
|
825
|
+
"min",
|
|
826
|
+
"minStepsBetweenThumbs",
|
|
827
|
+
"name",
|
|
828
|
+
"onFocusChange",
|
|
829
|
+
"onValueChange",
|
|
830
|
+
"onValueChangeEnd",
|
|
831
|
+
"orientation",
|
|
832
|
+
"origin",
|
|
833
|
+
"readOnly",
|
|
834
|
+
"step",
|
|
835
|
+
"thumbAlignment",
|
|
836
|
+
"thumbAlignment",
|
|
837
|
+
"thumbSize",
|
|
838
|
+
"value"
|
|
839
|
+
]);
|
|
840
|
+
var splitProps = createSplitProps(props);
|
|
841
|
+
var thumbProps = createProps()(["index", "name"]);
|
|
842
|
+
var splitThumbProps = createSplitProps(thumbProps);
|
|
843
|
+
export {
|
|
844
|
+
anatomy,
|
|
845
|
+
connect,
|
|
846
|
+
machine,
|
|
847
|
+
props,
|
|
848
|
+
splitProps,
|
|
849
|
+
splitThumbProps,
|
|
850
|
+
thumbProps
|
|
851
|
+
};
|
|
2
852
|
//# sourceMappingURL=index.mjs.map
|