@sytechui/slider 2.4.29

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/slider.js ADDED
@@ -0,0 +1,510 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/slider.tsx
22
+ var slider_exports = {};
23
+ __export(slider_exports, {
24
+ default: () => slider_default
25
+ });
26
+ module.exports = __toCommonJS(slider_exports);
27
+ var import_react_utils4 = require("@sytechui/react-utils");
28
+ var import_system3 = require("@sytechui/system");
29
+
30
+ // src/slider-thumb.tsx
31
+ var import_system = require("@sytechui/system");
32
+ var import_tooltip = require("@sytechui/tooltip");
33
+ var import_react_utils2 = require("@sytechui/react-utils");
34
+ var import_visually_hidden = require("@react-aria/visually-hidden");
35
+
36
+ // src/use-slider-thumb.ts
37
+ var import_slider = require("@react-aria/slider");
38
+ var import_react_utils = require("@sytechui/react-utils");
39
+ var import_react = require("react");
40
+ var import_interactions = require("@react-aria/interactions");
41
+ var import_focus = require("@react-aria/focus");
42
+ var import_shared_utils = require("@sytechui/shared-utils");
43
+ var import_i18n = require("@react-aria/i18n");
44
+ function useSliderThumb(props) {
45
+ const {
46
+ ref,
47
+ as,
48
+ state,
49
+ index,
50
+ name,
51
+ trackRef,
52
+ className,
53
+ tooltipProps,
54
+ isVertical,
55
+ showTooltip,
56
+ getTooltipValue,
57
+ formatOptions,
58
+ renderThumb,
59
+ ...otherProps
60
+ } = props;
61
+ const Component = as || "div";
62
+ const domRef = (0, import_react_utils.useDOMRef)(ref);
63
+ const inputRef = (0, import_react.useRef)(null);
64
+ const numberFormatter = (0, import_i18n.useNumberFormatter)(formatOptions);
65
+ const { thumbProps, inputProps, isDragging, isFocused } = (0, import_slider.useSliderThumb)(
66
+ {
67
+ index,
68
+ trackRef,
69
+ inputRef,
70
+ name,
71
+ ...otherProps
72
+ },
73
+ state
74
+ );
75
+ const { hoverProps, isHovered } = (0, import_interactions.useHover)({
76
+ isDisabled: state.isDisabled
77
+ });
78
+ const { focusProps, isFocusVisible } = (0, import_focus.useFocusRing)();
79
+ const { pressProps, isPressed } = (0, import_interactions.usePress)({
80
+ isDisabled: state.isDisabled
81
+ });
82
+ const getThumbProps = (props2 = {}) => {
83
+ return {
84
+ ref: domRef,
85
+ "data-slot": "thumb",
86
+ "data-hover": (0, import_shared_utils.dataAttr)(isHovered),
87
+ "data-pressed": (0, import_shared_utils.dataAttr)(isPressed),
88
+ "data-dragging": (0, import_shared_utils.dataAttr)(isDragging),
89
+ "data-focused": (0, import_shared_utils.dataAttr)(isFocused),
90
+ "data-focus-visible": (0, import_shared_utils.dataAttr)(isFocusVisible),
91
+ "aria-label": props2["aria-label"] || `Slider thumb ${index !== void 0 ? `${index + 1}` : ""}`,
92
+ ...(0, import_shared_utils.mergeProps)(thumbProps, pressProps, hoverProps, otherProps),
93
+ className,
94
+ ...props2
95
+ };
96
+ };
97
+ const getTooltipProps = () => {
98
+ const stateValue = (tooltipProps == null ? void 0 : tooltipProps.content) ? tooltipProps.content : getTooltipValue ? state.values.length === 1 ? getTooltipValue(state.values[index != null ? index : 0]) : getTooltipValue(state.values, index != null ? index : 0) : state.values[index != null ? index : 0];
99
+ const value = numberFormatter && typeof stateValue === "number" ? numberFormatter.format(stateValue) : stateValue;
100
+ return {
101
+ ...tooltipProps,
102
+ placement: (tooltipProps == null ? void 0 : tooltipProps.placement) ? tooltipProps == null ? void 0 : tooltipProps.placement : isVertical ? "right" : "top",
103
+ content: (tooltipProps == null ? void 0 : tooltipProps.content) ? tooltipProps == null ? void 0 : tooltipProps.content : value,
104
+ updatePositionDeps: [isDragging, isHovered, isFocused, isFocusVisible, value],
105
+ isOpen: (tooltipProps == null ? void 0 : tooltipProps.isOpen) !== void 0 ? tooltipProps == null ? void 0 : tooltipProps.isOpen : isHovered || isDragging || isFocused || isFocusVisible,
106
+ role: "tooltip",
107
+ "aria-label": `Current value: ${value}`
108
+ };
109
+ };
110
+ const getInputProps = (props2 = {}) => {
111
+ return {
112
+ ref: inputRef,
113
+ ...(0, import_shared_utils.mergeProps)(inputProps, focusProps),
114
+ ...props2
115
+ };
116
+ };
117
+ return {
118
+ Component,
119
+ index,
120
+ showTooltip,
121
+ renderThumb,
122
+ getThumbProps,
123
+ getTooltipProps,
124
+ getInputProps
125
+ };
126
+ }
127
+
128
+ // src/slider-thumb.tsx
129
+ var import_jsx_runtime = require("react/jsx-runtime");
130
+ var SliderThumb = (0, import_system.forwardRef)((props, ref) => {
131
+ const {
132
+ Component,
133
+ index,
134
+ renderThumb,
135
+ showTooltip,
136
+ getTooltipProps,
137
+ getThumbProps,
138
+ getInputProps
139
+ } = useSliderThumb({
140
+ ...props,
141
+ ref
142
+ });
143
+ const thumbProps = {
144
+ ...getThumbProps(),
145
+ index,
146
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_visually_hidden.VisuallyHidden, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { ...getInputProps() }) })
147
+ };
148
+ const content = (0, import_react_utils2.renderFn)({
149
+ Component,
150
+ props: thumbProps,
151
+ renderCustom: renderThumb
152
+ });
153
+ return showTooltip ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_tooltip.Tooltip, { ...getTooltipProps(), children: content }) : content;
154
+ });
155
+ SliderThumb.displayName = "HeroUI.SliderThumb";
156
+ var slider_thumb_default = SliderThumb;
157
+
158
+ // src/use-slider.ts
159
+ var import_system2 = require("@sytechui/system");
160
+ var import_theme = require("@sytechui/theme");
161
+ var import_react_utils3 = require("@sytechui/react-utils");
162
+ var import_slider2 = require("@react-stately/slider");
163
+ var import_react2 = require("react");
164
+ var import_i18n2 = require("@react-aria/i18n");
165
+ var import_slider3 = require("@react-aria/slider");
166
+ var import_shared_utils2 = require("@sytechui/shared-utils");
167
+ var import_interactions2 = require("@react-aria/interactions");
168
+ function useSlider(originalProps) {
169
+ var _a, _b, _c, _d;
170
+ const globalContext = (0, import_system2.useProviderContext)();
171
+ const [props, variantProps] = (0, import_system2.mapPropsVariants)(originalProps, import_theme.slider.variantKeys);
172
+ const {
173
+ ref,
174
+ as,
175
+ name,
176
+ label,
177
+ formatOptions,
178
+ value: valueProp,
179
+ maxValue = 100,
180
+ minValue = 0,
181
+ step = 1,
182
+ showSteps = false,
183
+ showTooltip = false,
184
+ orientation = "horizontal",
185
+ marks = [],
186
+ startContent,
187
+ endContent,
188
+ fillOffset,
189
+ className,
190
+ classNames,
191
+ renderThumb,
192
+ renderLabel,
193
+ renderValue,
194
+ onChange,
195
+ onChangeEnd,
196
+ getValue,
197
+ getTooltipValue,
198
+ tooltipValueFormatOptions = formatOptions,
199
+ tooltipProps: userTooltipProps = {},
200
+ ...otherProps
201
+ } = props;
202
+ const isFixedValue = minValue === maxValue;
203
+ if (isFixedValue) {
204
+ (0, import_shared_utils2.warn)("Min and max values should not be the same. This may cause unexpected behavior.");
205
+ }
206
+ const Component = as || "div";
207
+ const shouldFilterDOMProps = typeof Component === "string";
208
+ const disableAnimation = (_b = (_a = originalProps == null ? void 0 : originalProps.disableAnimation) != null ? _a : globalContext == null ? void 0 : globalContext.disableAnimation) != null ? _b : false;
209
+ const domRef = (0, import_react_utils3.useDOMRef)(ref);
210
+ const trackRef = (0, import_react2.useRef)(null);
211
+ const numberFormatter = (0, import_i18n2.useNumberFormatter)(formatOptions);
212
+ const { direction } = (0, import_i18n2.useLocale)();
213
+ const clampValue = (0, import_react2.useCallback)(
214
+ (valueToClamp) => {
215
+ return Math.min(Math.max(valueToClamp, minValue), maxValue);
216
+ },
217
+ [minValue, maxValue]
218
+ );
219
+ const validatedValue = (0, import_react2.useMemo)(() => {
220
+ if (isFixedValue) return minValue;
221
+ if (valueProp === void 0) return void 0;
222
+ if (Array.isArray(valueProp)) {
223
+ return valueProp.map(clampValue);
224
+ }
225
+ return clampValue(valueProp);
226
+ }, [valueProp, clampValue, isFixedValue, minValue]);
227
+ const state = (0, import_slider2.useSliderState)({
228
+ ...otherProps,
229
+ value: validatedValue,
230
+ isDisabled: (_c = originalProps == null ? void 0 : originalProps.isDisabled) != null ? _c : false,
231
+ orientation,
232
+ step,
233
+ minValue,
234
+ maxValue,
235
+ numberFormatter,
236
+ onChange,
237
+ onChangeEnd
238
+ });
239
+ const tooltipProps = {
240
+ offset: 5,
241
+ delay: 0,
242
+ size: "sm",
243
+ showArrow: true,
244
+ color: (originalProps == null ? void 0 : originalProps.color) ? originalProps == null ? void 0 : originalProps.color : (_d = import_theme.slider.defaultVariants) == null ? void 0 : _d.color,
245
+ isDisabled: originalProps.isDisabled,
246
+ ...userTooltipProps
247
+ };
248
+ const { groupProps, trackProps, labelProps, outputProps } = (0, import_slider3.useSlider)(
249
+ originalProps,
250
+ state,
251
+ trackRef
252
+ );
253
+ const { isHovered, hoverProps } = (0, import_interactions2.useHover)({ isDisabled: originalProps.isDisabled });
254
+ const baseStyles = (0, import_theme.cn)(classNames == null ? void 0 : classNames.base, className);
255
+ const isVertical = orientation === "vertical";
256
+ const hasMarks = (marks == null ? void 0 : marks.length) > 0;
257
+ const hasSingleThumb = fillOffset === void 0 ? state.values.length === 1 : false;
258
+ const slots = (0, import_react2.useMemo)(
259
+ () => (0, import_theme.slider)({
260
+ ...variantProps,
261
+ hasMarks,
262
+ disableAnimation,
263
+ hasSingleThumb,
264
+ isVertical
265
+ }),
266
+ [(0, import_shared_utils2.objectToDeps)(variantProps), isVertical, disableAnimation, hasSingleThumb, hasMarks]
267
+ );
268
+ const [startOffset, endOffset] = [
269
+ state.values.length > 1 ? state.getThumbPercent(0) : fillOffset !== void 0 ? state.getValuePercent(fillOffset) : 0,
270
+ state.getThumbPercent(state.values.length - 1)
271
+ ].sort();
272
+ const value = state.values.length === 1 ? numberFormatter.format(state.values[0]) : numberFormatter.formatRange(state.values[0], state.values[state.values.length - 1]);
273
+ const steps = showSteps ? Math.floor((maxValue - minValue) / step) + 1 : 0;
274
+ const getBaseProps = (props2 = {}) => {
275
+ return {
276
+ ref: domRef,
277
+ "data-orientation": state.orientation,
278
+ "data-slot": "base",
279
+ "data-hover": isHovered,
280
+ className: slots.base({ class: baseStyles }),
281
+ ...(0, import_shared_utils2.mergeProps)(
282
+ groupProps,
283
+ hoverProps,
284
+ (0, import_react_utils3.filterDOMProps)(otherProps, {
285
+ enabled: shouldFilterDOMProps
286
+ }),
287
+ (0, import_react_utils3.filterDOMProps)(props2)
288
+ )
289
+ };
290
+ };
291
+ const getLabelWrapperProps = (props2 = {}) => {
292
+ return {
293
+ className: slots.labelWrapper({ class: classNames == null ? void 0 : classNames.labelWrapper }),
294
+ "data-slot": "labelWrapper",
295
+ ...props2
296
+ };
297
+ };
298
+ const getLabelProps = (props2 = {}) => {
299
+ return {
300
+ "data-slot": "label",
301
+ className: slots.label({ class: classNames == null ? void 0 : classNames.label }),
302
+ children: label,
303
+ ...labelProps,
304
+ ...props2
305
+ };
306
+ };
307
+ const getValueProps = (props2 = {}) => {
308
+ return {
309
+ "data-slot": "value",
310
+ className: slots.value({ class: classNames == null ? void 0 : classNames.value }),
311
+ children: getValue && typeof getValue === "function" ? getValue(state.values) : value,
312
+ ...outputProps,
313
+ ...props2
314
+ };
315
+ };
316
+ const getTrackProps = (props2 = {}) => {
317
+ const fillWidth = (endOffset - startOffset) * 100;
318
+ return {
319
+ ref: trackRef,
320
+ "data-slot": "track",
321
+ "data-thumb-hidden": !!(originalProps == null ? void 0 : originalProps.hideThumb),
322
+ "data-vertical": isVertical,
323
+ ...hasSingleThumb ? {
324
+ "data-fill-start": fillWidth > 0,
325
+ "data-fill-end": fillWidth == 100
326
+ } : {
327
+ "data-fill-start": startOffset == 0,
328
+ "data-fill-end": startOffset * 100 + fillWidth == 100
329
+ },
330
+ className: slots.track({ class: classNames == null ? void 0 : classNames.track }),
331
+ ...trackProps,
332
+ ...props2
333
+ };
334
+ };
335
+ const getTrackWrapperProps = (props2 = {}) => {
336
+ return {
337
+ "data-slot": "track-wrapper",
338
+ className: slots.trackWrapper({ class: classNames == null ? void 0 : classNames.trackWrapper }),
339
+ ...props2
340
+ };
341
+ };
342
+ const getFillerProps = (props2 = {}) => {
343
+ return {
344
+ "data-slot": "filler",
345
+ className: slots.filler({ class: classNames == null ? void 0 : classNames.filler }),
346
+ ...props2,
347
+ style: {
348
+ ...props2.style,
349
+ [isVertical ? "bottom" : direction === "rtl" ? "right" : "left"]: `${startOffset * 100}%`,
350
+ ...isVertical ? {
351
+ height: `${(endOffset - startOffset) * 100}%`
352
+ } : {
353
+ width: `${(endOffset - startOffset) * 100}%`
354
+ }
355
+ }
356
+ };
357
+ };
358
+ const getThumbProps = (index) => {
359
+ return {
360
+ name,
361
+ index,
362
+ state,
363
+ trackRef,
364
+ orientation,
365
+ isVertical,
366
+ tooltipProps,
367
+ getTooltipValue,
368
+ showTooltip,
369
+ renderThumb,
370
+ formatOptions: tooltipValueFormatOptions,
371
+ className: slots.thumb({ class: classNames == null ? void 0 : classNames.thumb })
372
+ };
373
+ };
374
+ const getStepProps = (index) => {
375
+ const percent = state.getValuePercent(index * step + minValue);
376
+ return {
377
+ className: slots.step({ class: classNames == null ? void 0 : classNames.step }),
378
+ "data-slot": "step",
379
+ "data-in-range": percent <= endOffset && percent >= startOffset,
380
+ style: {
381
+ [isVertical ? "bottom" : direction === "rtl" ? "right" : "left"]: `${percent * 100}%`
382
+ }
383
+ };
384
+ };
385
+ const getMarkProps = (mark) => {
386
+ const percent = state.getValuePercent(mark.value);
387
+ return {
388
+ className: slots.mark({ class: classNames == null ? void 0 : classNames.mark }),
389
+ "data-slot": "mark",
390
+ "data-in-range": percent <= endOffset && percent >= startOffset,
391
+ style: {
392
+ [isVertical ? "bottom" : direction === "rtl" ? "right" : "left"]: `${percent * 100}%`
393
+ },
394
+ // avoid `onDownTrack` is being called since when you click the mark,
395
+ // `onDownTrack` will calculate the percent based on the position you click
396
+ // the calculated value will be set instead of the actual value defined in `marks`
397
+ onMouseDown: (e) => e.stopPropagation(),
398
+ onPointerDown: (e) => e.stopPropagation(),
399
+ onClick: (e) => {
400
+ e.stopPropagation();
401
+ if (isFixedValue) return;
402
+ if (state.values.length === 1) {
403
+ state.setThumbPercent(0, percent);
404
+ } else {
405
+ const leftThumbVal = state.values[0];
406
+ const rightThumbVal = state.values[1];
407
+ if (mark.value < leftThumbVal) {
408
+ state.setThumbPercent(0, percent);
409
+ } else if (mark.value > rightThumbVal) {
410
+ state.setThumbPercent(1, percent);
411
+ } else if (Math.abs(mark.value - leftThumbVal) < Math.abs(mark.value - rightThumbVal)) {
412
+ state.setThumbPercent(0, percent);
413
+ } else {
414
+ state.setThumbPercent(1, percent);
415
+ }
416
+ }
417
+ }
418
+ };
419
+ };
420
+ const getStartContentProps = (props2 = {}) => ({
421
+ "data-slot": "startContent",
422
+ className: slots.startContent({ class: classNames == null ? void 0 : classNames.startContent }),
423
+ ...props2
424
+ });
425
+ const getEndContentProps = (props2 = {}) => ({
426
+ "data-slot": "endContent",
427
+ className: slots.endContent({ class: classNames == null ? void 0 : classNames.endContent }),
428
+ ...props2
429
+ });
430
+ return {
431
+ Component,
432
+ state,
433
+ value,
434
+ domRef,
435
+ label,
436
+ steps,
437
+ marks,
438
+ startContent,
439
+ endContent,
440
+ getStepProps,
441
+ getBaseProps,
442
+ getValue,
443
+ renderLabel,
444
+ renderValue,
445
+ getTrackWrapperProps,
446
+ getLabelWrapperProps,
447
+ getLabelProps,
448
+ getValueProps,
449
+ getTrackProps,
450
+ getFillerProps,
451
+ getThumbProps,
452
+ getMarkProps,
453
+ getStartContentProps,
454
+ getEndContentProps
455
+ };
456
+ }
457
+
458
+ // src/slider.tsx
459
+ var import_jsx_runtime2 = require("react/jsx-runtime");
460
+ var Slider = (0, import_system3.forwardRef)((props, ref) => {
461
+ const {
462
+ Component,
463
+ state,
464
+ label,
465
+ steps,
466
+ marks,
467
+ startContent,
468
+ endContent,
469
+ getStepProps,
470
+ getBaseProps,
471
+ renderValue,
472
+ renderLabel,
473
+ getTrackWrapperProps,
474
+ getLabelWrapperProps,
475
+ getLabelProps,
476
+ getValueProps,
477
+ getTrackProps,
478
+ getFillerProps,
479
+ getThumbProps,
480
+ getMarkProps,
481
+ getStartContentProps,
482
+ getEndContentProps
483
+ } = useSlider({ ...props, ref });
484
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(Component, { ...getBaseProps(), children: [
485
+ label && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { ...getLabelWrapperProps(), children: [
486
+ (0, import_react_utils4.renderFn)({
487
+ Component: "label",
488
+ props: getLabelProps(),
489
+ renderCustom: renderLabel
490
+ }),
491
+ (0, import_react_utils4.renderFn)({
492
+ Component: "output",
493
+ props: getValueProps(),
494
+ renderCustom: renderValue
495
+ })
496
+ ] }),
497
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { ...getTrackWrapperProps(), children: [
498
+ startContent && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getStartContentProps(), children: startContent }),
499
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { ...getTrackProps(), children: [
500
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getFillerProps() }),
501
+ Number.isFinite(steps) && Array.from({ length: steps }, (_, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getStepProps(index) }, index)),
502
+ state.values.map((_, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(slider_thumb_default, { ...getThumbProps(index) }, index)),
503
+ (marks == null ? void 0 : marks.length) > 0 && marks.map((mark, index) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getMarkProps(mark), children: mark.label }, index))
504
+ ] }),
505
+ endContent && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...getEndContentProps(), children: endContent })
506
+ ] })
507
+ ] });
508
+ });
509
+ Slider.displayName = "HeroUI.Slider";
510
+ var slider_default = Slider;
@@ -0,0 +1,10 @@
1
+ "use client";
2
+ import {
3
+ slider_default
4
+ } from "./chunk-CJCHSOKZ.mjs";
5
+ import "./chunk-IQZHN75L.mjs";
6
+ import "./chunk-M2PJK6HJ.mjs";
7
+ import "./chunk-ORNKXCKK.mjs";
8
+ export {
9
+ slider_default as default
10
+ };