@radix-ui/react-slider 0.1.5-rc.5 → 0.1.5-rc.50

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.
@@ -1,2 +1,572 @@
1
- import{createCollection as e}from"@radix-ui/react-collection";import{Primitive as t}from"@radix-ui/react-primitive";import{useSize as n}from"@radix-ui/react-use-size";import{usePrevious as r}from"@radix-ui/react-use-previous";import{useDirection as o}from"@radix-ui/react-direction";import{useControllableState as i}from"@radix-ui/react-use-controllable-state";import{createContextScope as a}from"@radix-ui/react-context";import{useComposedRefs as l}from"@radix-ui/react-compose-refs";import{composeEventHandlers as d}from"@radix-ui/primitive";import{clamp as s}from"@radix-ui/number";import*as u from"react";import c from"@babel/runtime/helpers/esm/extends";const m=["PageUp","PageDown"],f=["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"],p={ltr:["ArrowDown","Home","ArrowLeft","PageDown"],rtl:["ArrowDown","Home","ArrowRight","PageDown"]},[S,h,v]=e("Slider"),[w,g]=a("Slider",[v]);export{g as createSliderScope};const[x,b]=w("Slider");export const Slider=/*#__PURE__*/u.forwardRef(((e,t)=>{const{name:n,min:r=0,max:o=100,step:a=1,orientation:d="horizontal",disabled:p=!1,minStepsBetweenThumbs:h=0,defaultValue:v=[r],value:w,onValueChange:g=(()=>{}),...b}=e,[E,y]=u.useState(null),_=l(t,(e=>y(e))),M=u.useRef(new Set),K=u.useRef(0),T="horizontal"===d,k=!E||Boolean(E.closest("form")),C=T?R:D,[I=[],z]=i({prop:w,defaultProp:v,onChange:e=>{var t;null===(t=[...M.current][K.current])||void 0===t||t.focus(),g(e)}});function A(e,t){const n=function(e){return(String(e).split(".")[1]||"").length}(a),i=function(e,t){const n=Math.pow(10,t);return Math.round(e*n)/n}(Math.round((e-r)/a)*a+r,n),l=s(i,[r,o]);z(((e=[])=>{const n=function(e=[],t,n){const r=[...e];return r[n]=t,r.sort(((e,t)=>e-t))}(e,l,t);return function(e,t){if(t>0){const n=function(e){return e.slice(0,-1).map(((t,n)=>e[n+1]-t))}(e);return Math.min(...n)>=t}return!0}(n,h*a)?(K.current=n.indexOf(l),String(n)===String(e)?e:n):e}))}/*#__PURE__*/return u.createElement(x,{scope:e.__scopeSlider,disabled:p,min:r,max:o,valueIndexToChangeRef:K,thumbs:M.current,values:I,orientation:d},/*#__PURE__*/u.createElement(S.Provider,{scope:e.__scopeSlider},/*#__PURE__*/u.createElement(S.Slot,{scope:e.__scopeSlider},/*#__PURE__*/u.createElement(C,c({"aria-disabled":p,"data-disabled":p?"":void 0},b,{ref:_,min:r,max:o,onSlideStart:p?void 0:function(e){const t=function(e,t){if(1===e.length)return 0;const n=e.map((e=>Math.abs(e-t))),r=Math.min(...n);return n.indexOf(r)}(I,e);A(e,t)},onSlideMove:p?void 0:function(e){A(e,K.current)},onHomeKeyDown:()=>!p&&A(r,0),onEndKeyDown:()=>!p&&A(o,I.length-1),onStepKeyDown:({event:e,direction:t})=>{if(!p){const n=m.includes(e.key)||e.shiftKey&&f.includes(e.key)?10:1,r=K.current;A(I[r]+a*n*t,r)}}})))),k&&I.map(((e,t)=>/*#__PURE__*/u.createElement(P,{key:t,name:n?n+(I.length>1?"[]":""):void 0,value:e}))))}));/*#__PURE__*/const[E,y]=w("Slider",{startEdge:"left",endEdge:"right",size:"width",direction:1}),R=/*#__PURE__*/u.forwardRef(((e,t)=>{const{min:n,max:r,dir:i,onSlideStart:a,onSlideMove:d,onStepKeyDown:s,...m}=e,[f,S]=u.useState(null),h=l(t,(e=>S(e))),v=u.useRef(),w=o(i),g="ltr"===w;function x(e){const t=v.current||f.getBoundingClientRect(),o=T([0,t.width],g?[n,r]:[r,n]);return v.current=t,o(e-t.left)}/*#__PURE__*/return u.createElement(E,{scope:e.__scopeSlider,startEdge:g?"left":"right",endEdge:g?"right":"left",direction:g?1:-1,size:"width"},/*#__PURE__*/u.createElement(_,c({dir:w,"data-orientation":"horizontal"},m,{ref:h,style:{...m.style,"--radix-slider-thumb-transform":"translateX(-50%)"},onSlideStart:e=>{const t=x(e.clientX);null==a||a(t)},onSlideMove:e=>{const t=x(e.clientX);null==d||d(t)},onSlideEnd:()=>v.current=void 0,onStepKeyDown:e=>{const t=p[w].includes(e.key);null==s||s({event:e,direction:t?-1:1})}})))})),D=/*#__PURE__*/u.forwardRef(((e,t)=>{const{min:n,max:r,onSlideStart:o,onSlideMove:i,onStepKeyDown:a,...d}=e,s=u.useRef(null),m=l(t,s),f=u.useRef();function S(e){const t=f.current||s.current.getBoundingClientRect(),o=T([0,t.height],[r,n]);return f.current=t,o(e-t.top)}/*#__PURE__*/return u.createElement(E,{scope:e.__scopeSlider,startEdge:"bottom",endEdge:"top",size:"height",direction:1},/*#__PURE__*/u.createElement(_,c({"data-orientation":"vertical"},d,{ref:m,style:{...d.style,"--radix-slider-thumb-transform":"translateY(50%)"},onSlideStart:e=>{const t=S(e.clientY);null==o||o(t)},onSlideMove:e=>{const t=S(e.clientY);null==i||i(t)},onSlideEnd:()=>f.current=void 0,onStepKeyDown:e=>{const t=p.ltr.includes(e.key);null==a||a({event:e,direction:t?-1:1})}})))})),_=/*#__PURE__*/u.forwardRef(((e,n)=>{const{__scopeSlider:r,onSlideStart:o,onSlideMove:i,onSlideEnd:a,onHomeKeyDown:l,onEndKeyDown:s,onStepKeyDown:p,...S}=e,h=b("Slider",r);/*#__PURE__*/return u.createElement(t.span,c({},S,{ref:n,onKeyDown:d(e.onKeyDown,(e=>{"Home"===e.key?(l(e),e.preventDefault()):"End"===e.key?(s(e),e.preventDefault()):m.concat(f).includes(e.key)&&(p(e),e.preventDefault())})),onPointerDown:d(e.onPointerDown,(e=>{const t=e.target;t.setPointerCapture(e.pointerId),e.preventDefault(),h.thumbs.has(t)?t.focus():o(e)})),onPointerMove:d(e.onPointerMove,(e=>{e.target.hasPointerCapture(e.pointerId)&&i(e)})),onPointerUp:d(e.onPointerUp,(e=>{const t=e.target;t.hasPointerCapture(e.pointerId)&&(t.releasePointerCapture(e.pointerId),a(e))}))}))}));export const SliderTrack=/*#__PURE__*/u.forwardRef(((e,n)=>{const{__scopeSlider:r,...o}=e,i=b("SliderTrack",r);/*#__PURE__*/return u.createElement(t.span,c({"data-disabled":i.disabled?"":void 0,"data-orientation":i.orientation},o,{ref:n}))}));/*#__PURE__*/export const SliderRange=/*#__PURE__*/u.forwardRef(((e,n)=>{const{__scopeSlider:r,...o}=e,i=b("SliderRange",r),a=y("SliderRange",r),d=u.useRef(null),s=l(n,d),m=i.values.length,f=i.values.map((e=>K(e,i.min,i.max))),p=m>1?Math.min(...f):0,S=100-Math.max(...f);/*#__PURE__*/return u.createElement(t.span,c({"data-orientation":i.orientation,"data-disabled":i.disabled?"":void 0},o,{ref:s,style:{...e.style,[a.startEdge]:p+"%",[a.endEdge]:S+"%"}}))}));/*#__PURE__*/export const SliderThumb=/*#__PURE__*/u.forwardRef(((e,t)=>{const n=h(e.__scopeSlider),[r,o]=u.useState(null),i=l(t,(e=>o(e))),a=u.useMemo((()=>r?n().findIndex((e=>e.ref.current===r)):-1),[n,r]);/*#__PURE__*/return u.createElement(M,c({},e,{ref:i,index:a}))}));const M=/*#__PURE__*/u.forwardRef(((e,r)=>{const{__scopeSlider:o,index:i,...a}=e,s=b("SliderThumb",o),m=y("SliderThumb",o),[f,p]=u.useState(null),h=l(r,(e=>p(e))),v=n(f),w=s.values[i],g=void 0===w?0:K(w,s.min,s.max),x=function(e,t){return t>2?`Value ${e+1} of ${t}`:2===t?["Minimum","Maximum"][e]:void 0}(i,s.values.length),E=null==v?void 0:v[m.size],R=E?function(e,t,n){const r=e/2,o=T([0,50],[0,r]);return(r-o(t)*n)*n}(E,g,m.direction):0;return u.useEffect((()=>{if(f)return s.thumbs.add(f),()=>{s.thumbs.delete(f)}}),[f,s.thumbs]),/*#__PURE__*/u.createElement("span",{style:{transform:"var(--radix-slider-thumb-transform)",position:"absolute",[m.startEdge]:`calc(${g}% + ${R}px)`}},/*#__PURE__*/u.createElement(S.ItemSlot,{scope:e.__scopeSlider},/*#__PURE__*/u.createElement(t.span,c({role:"slider","aria-label":e["aria-label"]||x,"aria-valuemin":s.min,"aria-valuenow":w,"aria-valuemax":s.max,"aria-orientation":s.orientation,"data-orientation":s.orientation,"data-disabled":s.disabled?"":void 0,tabIndex:s.disabled?void 0:0},a,{ref:h,style:void 0===w?{display:"none"}:e.style,onFocus:d(e.onFocus,(()=>{s.valueIndexToChangeRef.current=i}))}))))})),P=e=>{const{value:t,...n}=e,o=u.useRef(null),i=r(t);return u.useEffect((()=>{const e=o.current,n=window.HTMLInputElement.prototype,r=Object.getOwnPropertyDescriptor(n,"value").set;if(i!==t&&r){const n=new Event("input",{bubbles:!0});r.call(e,t),e.dispatchEvent(n)}}),[i,t]),/*#__PURE__*/u.createElement("input",c({style:{display:"none"}},n,{ref:o,defaultValue:t}))};/*#__PURE__*/function K(e,t,n){return 100/(n-t)*(e-t)}function T(e,t){return n=>{if(e[0]===e[1]||t[0]===t[1])return t[0];const r=(t[1]-t[0])/(e[1]-e[0]);return t[0]+r*(n-e[0])}}export const Root=Slider;export const Track=SliderTrack;export const Range=SliderRange;export const Thumb=SliderThumb;
1
+ import $g1Vy2$babelruntimehelpersesmextends from "@babel/runtime/helpers/esm/extends";
2
+ import {forwardRef as $g1Vy2$forwardRef, useState as $g1Vy2$useState, useRef as $g1Vy2$useRef, createElement as $g1Vy2$createElement, useMemo as $g1Vy2$useMemo, useEffect as $g1Vy2$useEffect} from "react";
3
+ import {clamp as $g1Vy2$clamp} from "@radix-ui/number";
4
+ import {composeEventHandlers as $g1Vy2$composeEventHandlers} from "@radix-ui/primitive";
5
+ import {useComposedRefs as $g1Vy2$useComposedRefs} from "@radix-ui/react-compose-refs";
6
+ import {createContextScope as $g1Vy2$createContextScope} from "@radix-ui/react-context";
7
+ import {useControllableState as $g1Vy2$useControllableState} from "@radix-ui/react-use-controllable-state";
8
+ import {useDirection as $g1Vy2$useDirection} from "@radix-ui/react-direction";
9
+ import {usePrevious as $g1Vy2$usePrevious} from "@radix-ui/react-use-previous";
10
+ import {useSize as $g1Vy2$useSize} from "@radix-ui/react-use-size";
11
+ import {Primitive as $g1Vy2$Primitive} from "@radix-ui/react-primitive";
12
+ import {createCollection as $g1Vy2$createCollection} from "@radix-ui/react-collection";
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+ const $faa2e61a3361514f$var$PAGE_KEYS = [
27
+ 'PageUp',
28
+ 'PageDown'
29
+ ];
30
+ const $faa2e61a3361514f$var$ARROW_KEYS = [
31
+ 'ArrowUp',
32
+ 'ArrowDown',
33
+ 'ArrowLeft',
34
+ 'ArrowRight'
35
+ ];
36
+ const $faa2e61a3361514f$var$BACK_KEYS = {
37
+ ltr: [
38
+ 'ArrowDown',
39
+ 'Home',
40
+ 'ArrowLeft',
41
+ 'PageDown'
42
+ ],
43
+ rtl: [
44
+ 'ArrowDown',
45
+ 'Home',
46
+ 'ArrowRight',
47
+ 'PageDown'
48
+ ]
49
+ };
50
+ /* -------------------------------------------------------------------------------------------------
51
+ * Slider
52
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$SLIDER_NAME = 'Slider';
53
+ const [$faa2e61a3361514f$var$Collection, $faa2e61a3361514f$var$useCollection, $faa2e61a3361514f$var$createCollectionScope] = $g1Vy2$createCollection($faa2e61a3361514f$var$SLIDER_NAME);
54
+ const [$faa2e61a3361514f$var$createSliderContext, $faa2e61a3361514f$export$ef72632d7b901f97] = $g1Vy2$createContextScope($faa2e61a3361514f$var$SLIDER_NAME, [
55
+ $faa2e61a3361514f$var$createCollectionScope
56
+ ]);
57
+ const [$faa2e61a3361514f$var$SliderProvider, $faa2e61a3361514f$var$useSliderContext] = $faa2e61a3361514f$var$createSliderContext($faa2e61a3361514f$var$SLIDER_NAME);
58
+ const $faa2e61a3361514f$export$472062a354075cee = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
59
+ const { name: name , min: min = 0 , max: max = 100 , step: step = 1 , orientation: orientation = 'horizontal' , disabled: disabled = false , minStepsBetweenThumbs: minStepsBetweenThumbs = 0 , defaultValue: defaultValue = [
60
+ min
61
+ ] , value: value1 , onValueChange: onValueChange = ()=>{} , ...sliderProps } = props;
62
+ const [slider, setSlider] = $g1Vy2$useState(null);
63
+ const composedRefs = $g1Vy2$useComposedRefs(forwardedRef, (node)=>setSlider(node)
64
+ );
65
+ const thumbRefs = $g1Vy2$useRef(new Set());
66
+ const valueIndexToChangeRef = $g1Vy2$useRef(0);
67
+ const isHorizontal = orientation === 'horizontal'; // We set this to true by default so that events bubble to forms without JS (SSR)
68
+ const isFormControl = slider ? Boolean(slider.closest('form')) : true;
69
+ const SliderOrientation = isHorizontal ? $faa2e61a3361514f$var$SliderHorizontal : $faa2e61a3361514f$var$SliderVertical;
70
+ const [values = [], setValues] = $g1Vy2$useControllableState({
71
+ prop: value1,
72
+ defaultProp: defaultValue,
73
+ onChange: (value)=>{
74
+ var _thumbs$valueIndexToC;
75
+ const thumbs = [
76
+ ...thumbRefs.current
77
+ ];
78
+ (_thumbs$valueIndexToC = thumbs[valueIndexToChangeRef.current]) === null || _thumbs$valueIndexToC === void 0 || _thumbs$valueIndexToC.focus();
79
+ onValueChange(value);
80
+ }
81
+ });
82
+ function handleSlideStart(value) {
83
+ const closestIndex = $faa2e61a3361514f$var$getClosestValueIndex(values, value);
84
+ updateValues(value, closestIndex);
85
+ }
86
+ function handleSlideMove(value) {
87
+ updateValues(value, valueIndexToChangeRef.current);
88
+ }
89
+ function updateValues(value, atIndex) {
90
+ const decimalCount = $faa2e61a3361514f$var$getDecimalCount(step);
91
+ const snapToStep = $faa2e61a3361514f$var$roundValue(Math.round((value - min) / step) * step + min, decimalCount);
92
+ const nextValue = $g1Vy2$clamp(snapToStep, [
93
+ min,
94
+ max
95
+ ]);
96
+ setValues((prevValues = [])=>{
97
+ const nextValues = $faa2e61a3361514f$var$getNextSortedValues(prevValues, nextValue, atIndex);
98
+ if ($faa2e61a3361514f$var$hasMinStepsBetweenValues(nextValues, minStepsBetweenThumbs * step)) {
99
+ valueIndexToChangeRef.current = nextValues.indexOf(nextValue);
100
+ return String(nextValues) === String(prevValues) ? prevValues : nextValues;
101
+ } else return prevValues;
102
+ });
103
+ }
104
+ return /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderProvider, {
105
+ scope: props.__scopeSlider,
106
+ disabled: disabled,
107
+ min: min,
108
+ max: max,
109
+ valueIndexToChangeRef: valueIndexToChangeRef,
110
+ thumbs: thumbRefs.current,
111
+ values: values,
112
+ orientation: orientation
113
+ }, /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$Collection.Provider, {
114
+ scope: props.__scopeSlider
115
+ }, /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$Collection.Slot, {
116
+ scope: props.__scopeSlider
117
+ }, /*#__PURE__*/ $g1Vy2$createElement(SliderOrientation, $g1Vy2$babelruntimehelpersesmextends({
118
+ "aria-disabled": disabled,
119
+ "data-disabled": disabled ? '' : undefined
120
+ }, sliderProps, {
121
+ ref: composedRefs,
122
+ min: min,
123
+ max: max,
124
+ onSlideStart: disabled ? undefined : handleSlideStart,
125
+ onSlideMove: disabled ? undefined : handleSlideMove,
126
+ onHomeKeyDown: ()=>!disabled && updateValues(min, 0)
127
+ ,
128
+ onEndKeyDown: ()=>!disabled && updateValues(max, values.length - 1)
129
+ ,
130
+ onStepKeyDown: ({ event: event , direction: stepDirection })=>{
131
+ if (!disabled) {
132
+ const isPageKey = $faa2e61a3361514f$var$PAGE_KEYS.includes(event.key);
133
+ const isSkipKey = isPageKey || event.shiftKey && $faa2e61a3361514f$var$ARROW_KEYS.includes(event.key);
134
+ const multiplier = isSkipKey ? 10 : 1;
135
+ const atIndex = valueIndexToChangeRef.current;
136
+ const value = values[atIndex];
137
+ const stepInDirection = step * multiplier * stepDirection;
138
+ updateValues(value + stepInDirection, atIndex);
139
+ }
140
+ }
141
+ })))), isFormControl && values.map((value, index)=>/*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$BubbleInput, {
142
+ key: index,
143
+ name: name ? name + (values.length > 1 ? '[]' : '') : undefined,
144
+ value: value
145
+ })
146
+ ));
147
+ });
148
+ /*#__PURE__*/ Object.assign($faa2e61a3361514f$export$472062a354075cee, {
149
+ displayName: $faa2e61a3361514f$var$SLIDER_NAME
150
+ });
151
+ /* -------------------------------------------------------------------------------------------------
152
+ * SliderHorizontal
153
+ * -----------------------------------------------------------------------------------------------*/ const [$faa2e61a3361514f$var$SliderOrientationProvider, $faa2e61a3361514f$var$useSliderOrientationContext] = $faa2e61a3361514f$var$createSliderContext($faa2e61a3361514f$var$SLIDER_NAME, {
154
+ startEdge: 'left',
155
+ endEdge: 'right',
156
+ size: 'width',
157
+ direction: 1
158
+ });
159
+ const $faa2e61a3361514f$var$SliderHorizontal = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
160
+ const { min: min , max: max , dir: dir , onSlideStart: onSlideStart , onSlideMove: onSlideMove , onStepKeyDown: onStepKeyDown , ...sliderProps } = props;
161
+ const [slider, setSlider] = $g1Vy2$useState(null);
162
+ const composedRefs = $g1Vy2$useComposedRefs(forwardedRef, (node)=>setSlider(node)
163
+ );
164
+ const rectRef = $g1Vy2$useRef();
165
+ const direction = $g1Vy2$useDirection(dir);
166
+ const isDirectionLTR = direction === 'ltr';
167
+ function getValueFromPointer(pointerPosition) {
168
+ const rect = rectRef.current || slider.getBoundingClientRect();
169
+ const input = [
170
+ 0,
171
+ rect.width
172
+ ];
173
+ const output = isDirectionLTR ? [
174
+ min,
175
+ max
176
+ ] : [
177
+ max,
178
+ min
179
+ ];
180
+ const value = $faa2e61a3361514f$var$linearScale(input, output);
181
+ rectRef.current = rect;
182
+ return value(pointerPosition - rect.left);
183
+ }
184
+ return /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderOrientationProvider, {
185
+ scope: props.__scopeSlider,
186
+ startEdge: isDirectionLTR ? 'left' : 'right',
187
+ endEdge: isDirectionLTR ? 'right' : 'left',
188
+ direction: isDirectionLTR ? 1 : -1,
189
+ size: "width"
190
+ }, /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderImpl, $g1Vy2$babelruntimehelpersesmextends({
191
+ dir: direction,
192
+ "data-orientation": "horizontal"
193
+ }, sliderProps, {
194
+ ref: composedRefs,
195
+ style: {
196
+ ...sliderProps.style,
197
+ ['--radix-slider-thumb-transform']: 'translateX(-50%)'
198
+ },
199
+ onSlideStart: (event)=>{
200
+ const value = getValueFromPointer(event.clientX);
201
+ onSlideStart === null || onSlideStart === void 0 || onSlideStart(value);
202
+ },
203
+ onSlideMove: (event)=>{
204
+ const value = getValueFromPointer(event.clientX);
205
+ onSlideMove === null || onSlideMove === void 0 || onSlideMove(value);
206
+ },
207
+ onSlideEnd: ()=>rectRef.current = undefined
208
+ ,
209
+ onStepKeyDown: (event)=>{
210
+ const isBackKey = $faa2e61a3361514f$var$BACK_KEYS[direction].includes(event.key);
211
+ onStepKeyDown === null || onStepKeyDown === void 0 || onStepKeyDown({
212
+ event: event,
213
+ direction: isBackKey ? -1 : 1
214
+ });
215
+ }
216
+ })));
217
+ });
218
+ /* -------------------------------------------------------------------------------------------------
219
+ * SliderVertical
220
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$SliderVertical = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
221
+ const { min: min , max: max , onSlideStart: onSlideStart , onSlideMove: onSlideMove , onStepKeyDown: onStepKeyDown , ...sliderProps } = props;
222
+ const sliderRef = $g1Vy2$useRef(null);
223
+ const ref = $g1Vy2$useComposedRefs(forwardedRef, sliderRef);
224
+ const rectRef = $g1Vy2$useRef();
225
+ function getValueFromPointer(pointerPosition) {
226
+ const rect = rectRef.current || sliderRef.current.getBoundingClientRect();
227
+ const input = [
228
+ 0,
229
+ rect.height
230
+ ];
231
+ const output = [
232
+ max,
233
+ min
234
+ ];
235
+ const value = $faa2e61a3361514f$var$linearScale(input, output);
236
+ rectRef.current = rect;
237
+ return value(pointerPosition - rect.top);
238
+ }
239
+ return /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderOrientationProvider, {
240
+ scope: props.__scopeSlider,
241
+ startEdge: "bottom",
242
+ endEdge: "top",
243
+ size: "height",
244
+ direction: 1
245
+ }, /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderImpl, $g1Vy2$babelruntimehelpersesmextends({
246
+ "data-orientation": "vertical"
247
+ }, sliderProps, {
248
+ ref: ref,
249
+ style: {
250
+ ...sliderProps.style,
251
+ ['--radix-slider-thumb-transform']: 'translateY(50%)'
252
+ },
253
+ onSlideStart: (event)=>{
254
+ const value = getValueFromPointer(event.clientY);
255
+ onSlideStart === null || onSlideStart === void 0 || onSlideStart(value);
256
+ },
257
+ onSlideMove: (event)=>{
258
+ const value = getValueFromPointer(event.clientY);
259
+ onSlideMove === null || onSlideMove === void 0 || onSlideMove(value);
260
+ },
261
+ onSlideEnd: ()=>rectRef.current = undefined
262
+ ,
263
+ onStepKeyDown: (event)=>{
264
+ const isBackKey = $faa2e61a3361514f$var$BACK_KEYS.ltr.includes(event.key);
265
+ onStepKeyDown === null || onStepKeyDown === void 0 || onStepKeyDown({
266
+ event: event,
267
+ direction: isBackKey ? -1 : 1
268
+ });
269
+ }
270
+ })));
271
+ });
272
+ /* -------------------------------------------------------------------------------------------------
273
+ * SliderImpl
274
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$SliderImpl = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
275
+ const { __scopeSlider: __scopeSlider , onSlideStart: onSlideStart , onSlideMove: onSlideMove , onSlideEnd: onSlideEnd , onHomeKeyDown: onHomeKeyDown , onEndKeyDown: onEndKeyDown , onStepKeyDown: onStepKeyDown , ...sliderProps } = props;
276
+ const context = $faa2e61a3361514f$var$useSliderContext($faa2e61a3361514f$var$SLIDER_NAME, __scopeSlider);
277
+ return /*#__PURE__*/ $g1Vy2$createElement($g1Vy2$Primitive.span, $g1Vy2$babelruntimehelpersesmextends({}, sliderProps, {
278
+ ref: forwardedRef,
279
+ onKeyDown: $g1Vy2$composeEventHandlers(props.onKeyDown, (event)=>{
280
+ if (event.key === 'Home') {
281
+ onHomeKeyDown(event); // Prevent scrolling to page start
282
+ event.preventDefault();
283
+ } else if (event.key === 'End') {
284
+ onEndKeyDown(event); // Prevent scrolling to page end
285
+ event.preventDefault();
286
+ } else if ($faa2e61a3361514f$var$PAGE_KEYS.concat($faa2e61a3361514f$var$ARROW_KEYS).includes(event.key)) {
287
+ onStepKeyDown(event); // Prevent scrolling for directional key presses
288
+ event.preventDefault();
289
+ }
290
+ }),
291
+ onPointerDown: $g1Vy2$composeEventHandlers(props.onPointerDown, (event)=>{
292
+ const target = event.target;
293
+ target.setPointerCapture(event.pointerId); // Prevent browser focus behaviour because we focus a thumb manually when values change.
294
+ event.preventDefault(); // Touch devices have a delay before focusing so won't focus if touch immediately moves
295
+ // away from target (sliding). We want thumb to focus regardless.
296
+ if (context.thumbs.has(target)) target.focus();
297
+ else onSlideStart(event);
298
+ }),
299
+ onPointerMove: $g1Vy2$composeEventHandlers(props.onPointerMove, (event)=>{
300
+ const target = event.target;
301
+ if (target.hasPointerCapture(event.pointerId)) onSlideMove(event);
302
+ }),
303
+ onPointerUp: $g1Vy2$composeEventHandlers(props.onPointerUp, (event)=>{
304
+ const target = event.target;
305
+ if (target.hasPointerCapture(event.pointerId)) {
306
+ target.releasePointerCapture(event.pointerId);
307
+ onSlideEnd(event);
308
+ }
309
+ })
310
+ }));
311
+ });
312
+ /* -------------------------------------------------------------------------------------------------
313
+ * SliderTrack
314
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$TRACK_NAME = 'SliderTrack';
315
+ const $faa2e61a3361514f$export$105594979f116971 = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
316
+ const { __scopeSlider: __scopeSlider , ...trackProps } = props;
317
+ const context = $faa2e61a3361514f$var$useSliderContext($faa2e61a3361514f$var$TRACK_NAME, __scopeSlider);
318
+ return /*#__PURE__*/ $g1Vy2$createElement($g1Vy2$Primitive.span, $g1Vy2$babelruntimehelpersesmextends({
319
+ "data-disabled": context.disabled ? '' : undefined,
320
+ "data-orientation": context.orientation
321
+ }, trackProps, {
322
+ ref: forwardedRef
323
+ }));
324
+ });
325
+ /*#__PURE__*/ Object.assign($faa2e61a3361514f$export$105594979f116971, {
326
+ displayName: $faa2e61a3361514f$var$TRACK_NAME
327
+ });
328
+ /* -------------------------------------------------------------------------------------------------
329
+ * SliderRange
330
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$RANGE_NAME = 'SliderRange';
331
+ const $faa2e61a3361514f$export$a5cf38a7a000fe77 = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
332
+ const { __scopeSlider: __scopeSlider , ...rangeProps } = props;
333
+ const context = $faa2e61a3361514f$var$useSliderContext($faa2e61a3361514f$var$RANGE_NAME, __scopeSlider);
334
+ const orientation = $faa2e61a3361514f$var$useSliderOrientationContext($faa2e61a3361514f$var$RANGE_NAME, __scopeSlider);
335
+ const ref = $g1Vy2$useRef(null);
336
+ const composedRefs = $g1Vy2$useComposedRefs(forwardedRef, ref);
337
+ const valuesCount = context.values.length;
338
+ const percentages = context.values.map((value)=>$faa2e61a3361514f$var$convertValueToPercentage(value, context.min, context.max)
339
+ );
340
+ const offsetStart = valuesCount > 1 ? Math.min(...percentages) : 0;
341
+ const offsetEnd = 100 - Math.max(...percentages);
342
+ return /*#__PURE__*/ $g1Vy2$createElement($g1Vy2$Primitive.span, $g1Vy2$babelruntimehelpersesmextends({
343
+ "data-orientation": context.orientation,
344
+ "data-disabled": context.disabled ? '' : undefined
345
+ }, rangeProps, {
346
+ ref: composedRefs,
347
+ style: {
348
+ ...props.style,
349
+ [orientation.startEdge]: offsetStart + '%',
350
+ [orientation.endEdge]: offsetEnd + '%'
351
+ }
352
+ }));
353
+ });
354
+ /*#__PURE__*/ Object.assign($faa2e61a3361514f$export$a5cf38a7a000fe77, {
355
+ displayName: $faa2e61a3361514f$var$RANGE_NAME
356
+ });
357
+ /* -------------------------------------------------------------------------------------------------
358
+ * SliderThumb
359
+ * -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$THUMB_NAME = 'SliderThumb';
360
+ const $faa2e61a3361514f$export$2c1b491743890dec = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
361
+ const getItems = $faa2e61a3361514f$var$useCollection(props.__scopeSlider);
362
+ const [thumb, setThumb] = $g1Vy2$useState(null);
363
+ const composedRefs = $g1Vy2$useComposedRefs(forwardedRef, (node)=>setThumb(node)
364
+ );
365
+ const index = $g1Vy2$useMemo(()=>thumb ? getItems().findIndex((item)=>item.ref.current === thumb
366
+ ) : -1
367
+ , [
368
+ getItems,
369
+ thumb
370
+ ]);
371
+ return /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$SliderThumbImpl, $g1Vy2$babelruntimehelpersesmextends({}, props, {
372
+ ref: composedRefs,
373
+ index: index
374
+ }));
375
+ });
376
+ const $faa2e61a3361514f$var$SliderThumbImpl = /*#__PURE__*/ $g1Vy2$forwardRef((props, forwardedRef)=>{
377
+ const { __scopeSlider: __scopeSlider , index: index , ...thumbProps } = props;
378
+ const context = $faa2e61a3361514f$var$useSliderContext($faa2e61a3361514f$var$THUMB_NAME, __scopeSlider);
379
+ const orientation = $faa2e61a3361514f$var$useSliderOrientationContext($faa2e61a3361514f$var$THUMB_NAME, __scopeSlider);
380
+ const [thumb, setThumb] = $g1Vy2$useState(null);
381
+ const composedRefs = $g1Vy2$useComposedRefs(forwardedRef, (node)=>setThumb(node)
382
+ );
383
+ const size = $g1Vy2$useSize(thumb); // We cast because index could be `-1` which would return undefined
384
+ const value = context.values[index];
385
+ const percent = value === undefined ? 0 : $faa2e61a3361514f$var$convertValueToPercentage(value, context.min, context.max);
386
+ const label = $faa2e61a3361514f$var$getLabel(index, context.values.length);
387
+ const orientationSize = size === null || size === void 0 ? void 0 : size[orientation.size];
388
+ const thumbInBoundsOffset = orientationSize ? $faa2e61a3361514f$var$getThumbInBoundsOffset(orientationSize, percent, orientation.direction) : 0;
389
+ $g1Vy2$useEffect(()=>{
390
+ if (thumb) {
391
+ context.thumbs.add(thumb);
392
+ return ()=>{
393
+ context.thumbs.delete(thumb);
394
+ };
395
+ }
396
+ }, [
397
+ thumb,
398
+ context.thumbs
399
+ ]);
400
+ return /*#__PURE__*/ $g1Vy2$createElement("span", {
401
+ style: {
402
+ transform: 'var(--radix-slider-thumb-transform)',
403
+ position: 'absolute',
404
+ [orientation.startEdge]: `calc(${percent}% + ${thumbInBoundsOffset}px)`
405
+ }
406
+ }, /*#__PURE__*/ $g1Vy2$createElement($faa2e61a3361514f$var$Collection.ItemSlot, {
407
+ scope: props.__scopeSlider
408
+ }, /*#__PURE__*/ $g1Vy2$createElement($g1Vy2$Primitive.span, $g1Vy2$babelruntimehelpersesmextends({
409
+ role: "slider",
410
+ "aria-label": props['aria-label'] || label,
411
+ "aria-valuemin": context.min,
412
+ "aria-valuenow": value,
413
+ "aria-valuemax": context.max,
414
+ "aria-orientation": context.orientation,
415
+ "data-orientation": context.orientation,
416
+ "data-disabled": context.disabled ? '' : undefined,
417
+ tabIndex: context.disabled ? undefined : 0
418
+ }, thumbProps, {
419
+ ref: composedRefs,
420
+ style: value === undefined ? {
421
+ display: 'none'
422
+ } : props.style,
423
+ onFocus: $g1Vy2$composeEventHandlers(props.onFocus, ()=>{
424
+ context.valueIndexToChangeRef.current = index;
425
+ })
426
+ }))));
427
+ });
428
+ /*#__PURE__*/ Object.assign($faa2e61a3361514f$export$2c1b491743890dec, {
429
+ displayName: $faa2e61a3361514f$var$THUMB_NAME
430
+ });
431
+ /* -----------------------------------------------------------------------------------------------*/ const $faa2e61a3361514f$var$BubbleInput = (props)=>{
432
+ const { value: value , ...inputProps } = props;
433
+ const ref = $g1Vy2$useRef(null);
434
+ const prevValue = $g1Vy2$usePrevious(value); // Bubble value change to parents (e.g form change event)
435
+ $g1Vy2$useEffect(()=>{
436
+ const input = ref.current;
437
+ const inputProto = window.HTMLInputElement.prototype;
438
+ const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'value');
439
+ const setValue = descriptor.set;
440
+ if (prevValue !== value && setValue) {
441
+ const event = new Event('input', {
442
+ bubbles: true
443
+ });
444
+ setValue.call(input, value);
445
+ input.dispatchEvent(event);
446
+ }
447
+ }, [
448
+ prevValue,
449
+ value
450
+ ]);
451
+ /**
452
+ * We purposefully do not use `type="hidden"` here otherwise forms that
453
+ * wrap it will not be able to access its value via the FormData API.
454
+ *
455
+ * We purposefully do not add the `value` attribute here to allow the value
456
+ * to be set programatically and bubble to any parent form `onChange` event.
457
+ * Adding the `value` will cause React to consider the programatic
458
+ * dispatch a duplicate and it will get swallowed.
459
+ */ return /*#__PURE__*/ $g1Vy2$createElement("input", $g1Vy2$babelruntimehelpersesmextends({
460
+ style: {
461
+ display: 'none'
462
+ }
463
+ }, inputProps, {
464
+ ref: ref,
465
+ defaultValue: value
466
+ }));
467
+ };
468
+ function $faa2e61a3361514f$var$getNextSortedValues(prevValues = [], nextValue, atIndex) {
469
+ const nextValues = [
470
+ ...prevValues
471
+ ];
472
+ nextValues[atIndex] = nextValue;
473
+ return nextValues.sort((a, b)=>a - b
474
+ );
475
+ }
476
+ function $faa2e61a3361514f$var$convertValueToPercentage(value, min, max) {
477
+ const maxSteps = max - min;
478
+ const percentPerStep = 100 / maxSteps;
479
+ return percentPerStep * (value - min);
480
+ }
481
+ /**
482
+ * Returns a label for each thumb when there are two or more thumbs
483
+ */ function $faa2e61a3361514f$var$getLabel(index, totalValues) {
484
+ if (totalValues > 2) return `Value ${index + 1} of ${totalValues}`;
485
+ else if (totalValues === 2) return [
486
+ 'Minimum',
487
+ 'Maximum'
488
+ ][index];
489
+ else return undefined;
490
+ }
491
+ /**
492
+ * Given a `values` array and a `nextValue`, determine which value in
493
+ * the array is closest to `nextValue` and return its index.
494
+ *
495
+ * @example
496
+ * // returns 1
497
+ * getClosestValueIndex([10, 30], 25);
498
+ */ function $faa2e61a3361514f$var$getClosestValueIndex(values, nextValue) {
499
+ if (values.length === 1) return 0;
500
+ const distances = values.map((value)=>Math.abs(value - nextValue)
501
+ );
502
+ const closestDistance = Math.min(...distances);
503
+ return distances.indexOf(closestDistance);
504
+ }
505
+ /**
506
+ * Offsets the thumb centre point while sliding to ensure it remains
507
+ * within the bounds of the slider when reaching the edges
508
+ */ function $faa2e61a3361514f$var$getThumbInBoundsOffset(width, left, direction) {
509
+ const halfWidth = width / 2;
510
+ const halfPercent = 50;
511
+ const offset = $faa2e61a3361514f$var$linearScale([
512
+ 0,
513
+ halfPercent
514
+ ], [
515
+ 0,
516
+ halfWidth
517
+ ]);
518
+ return (halfWidth - offset(left) * direction) * direction;
519
+ }
520
+ /**
521
+ * Gets an array of steps between each value.
522
+ *
523
+ * @example
524
+ * // returns [1, 9]
525
+ * getStepsBetweenValues([10, 11, 20]);
526
+ */ function $faa2e61a3361514f$var$getStepsBetweenValues(values) {
527
+ return values.slice(0, -1).map((value, index)=>values[index + 1] - value
528
+ );
529
+ }
530
+ /**
531
+ * Verifies the minimum steps between all values is greater than or equal
532
+ * to the expected minimum steps.
533
+ *
534
+ * @example
535
+ * // returns false
536
+ * hasMinStepsBetweenValues([1,2,3], 2);
537
+ *
538
+ * @example
539
+ * // returns true
540
+ * hasMinStepsBetweenValues([1,2,3], 1);
541
+ */ function $faa2e61a3361514f$var$hasMinStepsBetweenValues(values, minStepsBetweenValues) {
542
+ if (minStepsBetweenValues > 0) {
543
+ const stepsBetweenValues = $faa2e61a3361514f$var$getStepsBetweenValues(values);
544
+ const actualMinStepsBetweenValues = Math.min(...stepsBetweenValues);
545
+ return actualMinStepsBetweenValues >= minStepsBetweenValues;
546
+ }
547
+ return true;
548
+ } // https://github.com/tmcw-up-for-adoption/simple-linear-scale/blob/master/index.js
549
+ function $faa2e61a3361514f$var$linearScale(input, output) {
550
+ return (value)=>{
551
+ if (input[0] === input[1] || output[0] === output[1]) return output[0];
552
+ const ratio = (output[1] - output[0]) / (input[1] - input[0]);
553
+ return output[0] + ratio * (value - input[0]);
554
+ };
555
+ }
556
+ function $faa2e61a3361514f$var$getDecimalCount(value) {
557
+ return (String(value).split('.')[1] || '').length;
558
+ }
559
+ function $faa2e61a3361514f$var$roundValue(value, decimalCount) {
560
+ const rounder = Math.pow(10, decimalCount);
561
+ return Math.round(value * rounder) / rounder;
562
+ }
563
+ const $faa2e61a3361514f$export$be92b6f5f03c0fe9 = $faa2e61a3361514f$export$472062a354075cee;
564
+ const $faa2e61a3361514f$export$13921ac0cc260818 = $faa2e61a3361514f$export$105594979f116971;
565
+ const $faa2e61a3361514f$export$9a58ef0d7ad3278c = $faa2e61a3361514f$export$a5cf38a7a000fe77;
566
+ const $faa2e61a3361514f$export$6521433ed15a34db = $faa2e61a3361514f$export$2c1b491743890dec;
567
+
568
+
569
+
570
+
571
+ export {$faa2e61a3361514f$export$ef72632d7b901f97 as createSliderScope, $faa2e61a3361514f$export$472062a354075cee as Slider, $faa2e61a3361514f$export$105594979f116971 as SliderTrack, $faa2e61a3361514f$export$a5cf38a7a000fe77 as SliderRange, $faa2e61a3361514f$export$2c1b491743890dec as SliderThumb, $faa2e61a3361514f$export$be92b6f5f03c0fe9 as Root, $faa2e61a3361514f$export$13921ac0cc260818 as Track, $faa2e61a3361514f$export$9a58ef0d7ad3278c as Range, $faa2e61a3361514f$export$6521433ed15a34db as Thumb};
2
572
  //# sourceMappingURL=index.module.js.map