zcomponents-ui 1.2.3 → 1.3.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.
Files changed (33) hide show
  1. package/README.md +20 -0
  2. package/dist/{index-Cfnntm1b.js → index-BiSP8JEr.js} +1 -7
  3. package/dist/index-BiSP8JEr.js.map +1 -0
  4. package/dist/{index-BZL-Zu45.js → index-DZGmLslf.js} +1 -7
  5. package/dist/index-DZGmLslf.js.map +1 -0
  6. package/dist/index.cjs.js +576 -13
  7. package/dist/index.cjs.js.map +1 -1
  8. package/dist/index.d.ts +121 -4
  9. package/dist/index.esm.js +578 -16
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/react-hook-form.cjs.js +1 -1
  12. package/dist/react-hook-form.d.ts +3 -3
  13. package/dist/react-hook-form.esm.js +1 -1
  14. package/dist/types/components/ZDrop/types/zDropTypes.d.ts +2 -2
  15. package/dist/types/components/ZRange/components/ZRangeIndicator/index.d.ts +3 -0
  16. package/dist/types/components/ZRange/components/ZRangeInputs/index.d.ts +3 -0
  17. package/dist/types/components/ZRange/components/ZRangeLabel/index.d.ts +3 -0
  18. package/dist/types/components/ZRange/components/ZRangeSliderValue/index.d.ts +3 -0
  19. package/dist/types/components/ZRange/components/ZRangeTrack/index.d.ts +3 -0
  20. package/dist/types/components/ZRange/helpers/calculateStepPosition.d.ts +1 -0
  21. package/dist/types/components/ZRange/helpers/checkIsInputSwapAllowed.d.ts +1 -0
  22. package/dist/types/components/ZRange/helpers/convertToUnit.d.ts +6 -0
  23. package/dist/types/components/ZRange/helpers/getCssVariablePxValue.d.ts +1 -0
  24. package/dist/types/components/ZRange/helpers/getMinMax.d.ts +4 -0
  25. package/dist/types/components/ZRange/index.d.ts +3 -0
  26. package/dist/types/components/ZRange/rangeValue.d.ts +13 -0
  27. package/dist/types/components/ZRange/types/zRangeTypes.d.ts +103 -0
  28. package/dist/types/index.d.ts +2 -0
  29. package/dist/zcomponents-ui.css +1 -1
  30. package/dist/zcomponents-ui.css.map +1 -1
  31. package/package.json +1 -1
  32. package/dist/index-BZL-Zu45.js.map +0 -1
  33. package/dist/index-Cfnntm1b.js.map +0 -1
package/dist/index.esm.js CHANGED
@@ -1,21 +1,21 @@
1
- import { c as classNames, g as getAvailableSpace, u as useOutsideClose } from './index-Cfnntm1b.js';
2
- export { a as ZDrop, Z as ZDropField } from './index-Cfnntm1b.js';
1
+ import { c as classNames, g as getAvailableSpace, u as useOutsideClose } from './index-BiSP8JEr.js';
2
+ export { a as ZDrop, Z as ZDropField } from './index-BiSP8JEr.js';
3
3
  import { jsxs, jsx } from 'react/jsx-runtime';
4
- import { useContext, useRef, useState, useCallback, useLayoutEffect, useEffect, createContext } from 'react';
4
+ import { useContext, useRef, useState, useCallback, useLayoutEffect, useEffect, createContext, useMemo } from 'react';
5
5
  import 'react-hook-form';
6
6
 
7
- var styles = {"zd-button":"ZDropButton-module_zd-button__XVZ-w","zd-button__toggle":"ZDropButton-module_zd-button__toggle__0jdWn","zd-button__toggle-title":"ZDropButton-module_zd-button__toggle-title__OaiVF","zd-button__toggle-icon":"ZDropButton-module_zd-button__toggle-icon__G4tnb","zd-button__content":"ZDropButton-module_zd-button__content__FS-6m","zd-button__search":"ZDropButton-module_zd-button__search__6Nbrx","zd-button__search-input-field":"ZDropButton-module_zd-button__search-input-field__K2avH","zd-button__search-input":"ZDropButton-module_zd-button__search-input__1e643","zd-button__search-clear-btn":"ZDropButton-module_zd-button__search-clear-btn__hJo2h","zd-button__search-clear-btn-icon":"ZDropButton-module_zd-button__search-clear-btn-icon__PWwHy","zd-button__list":"ZDropButton-module_zd-button__list__prV7p","zd-button__list-item":"ZDropButton-module_zd-button__list-item__uRK0D","zd-button__list-item--active":"ZDropButton-module_zd-button__list-item--active__oPoZA","zd-button__list-item-icon":"ZDropButton-module_zd-button__list-item-icon__xgqOm","zd-button__list-item-title":"ZDropButton-module_zd-button__list-item-title__RHTpc"};
7
+ var styles$1 = {"zd-button":"ZDropButton-module_zd-button__XVZ-w","zd-button__toggle":"ZDropButton-module_zd-button__toggle__0jdWn","zd-button__toggle-title":"ZDropButton-module_zd-button__toggle-title__OaiVF","zd-button__toggle-icon":"ZDropButton-module_zd-button__toggle-icon__G4tnb","zd-button__content":"ZDropButton-module_zd-button__content__FS-6m","zd-button__search":"ZDropButton-module_zd-button__search__6Nbrx","zd-button__search-input-field":"ZDropButton-module_zd-button__search-input-field__K2avH","zd-button__search-input":"ZDropButton-module_zd-button__search-input__1e643","zd-button__search-clear-btn":"ZDropButton-module_zd-button__search-clear-btn__hJo2h","zd-button__search-clear-btn-icon":"ZDropButton-module_zd-button__search-clear-btn-icon__PWwHy","zd-button__list":"ZDropButton-module_zd-button__list__prV7p","zd-button__list-item":"ZDropButton-module_zd-button__list-item__uRK0D","zd-button__list-item--active":"ZDropButton-module_zd-button__list-item--active__oPoZA","zd-button__list-item-icon":"ZDropButton-module_zd-button__list-item-icon__xgqOm","zd-button__list-item-title":"ZDropButton-module_zd-button__list-item-title__RHTpc"};
8
8
 
9
9
  const ButtonDropdownToggle = (props) => {
10
10
  const { title, toggleIcon, toggleClassName, children, onButtonToggleClick } = props;
11
- const toggleClasses = classNames(styles["zd-button__toggle"], toggleClassName);
11
+ const toggleClasses = classNames(styles$1["zd-button__toggle"], toggleClassName);
12
12
  const onToggleClick = (e) => {
13
13
  onButtonToggleClick();
14
14
  };
15
15
  const onToggleMouseLeave = (e) => {
16
16
  e.currentTarget.blur();
17
17
  };
18
- return (jsxs("button", { className: toggleClasses, onMouseLeave: onToggleMouseLeave, onClick: onToggleClick, children: [toggleIcon && (jsx("div", { className: styles["zd-button__toggle-icon"], children: toggleIcon })), title && (jsx("span", { className: styles["zd-button__toggle-title"], children: title })), children] }));
18
+ return (jsxs("button", { className: toggleClasses, onMouseLeave: onToggleMouseLeave, onClick: onToggleClick, children: [toggleIcon && (jsx("div", { className: styles$1["zd-button__toggle-icon"], children: toggleIcon })), title && (jsx("span", { className: styles$1["zd-button__toggle-title"], children: title })), children] }));
19
19
  };
20
20
 
21
21
  const ZDropButtonContent = (props) => {
@@ -140,19 +140,19 @@ const ZDropButtonContent = (props) => {
140
140
  if (!isOpen) {
141
141
  return null;
142
142
  }
143
- return (jsx("div", { className: styles["zd-button__content"], style: positionStyles, ref: contentRef, children: children }));
143
+ return (jsx("div", { className: styles$1["zd-button__content"], style: positionStyles, ref: contentRef, children: children }));
144
144
  };
145
145
 
146
146
  const ZDropButtonList = (props) => {
147
147
  const { children } = props;
148
- return jsx("ul", { className: styles["zd-button__list"], children: children });
148
+ return jsx("ul", { className: styles$1["zd-button__list"], children: children });
149
149
  };
150
150
 
151
151
  const ZDropButtonListItem = (props) => {
152
152
  const { index, title, Icon, urlPath, className, linkAs: LinkTag = "a", isActive = false, children, } = props;
153
153
  const { addToRefs, optionsRef, onItemSelect, searchInputRef } = useContext(ZDropButtonContext);
154
- const itemContainerClasses = (isActive) => classNames(styles["zd-button__list-item"], {
155
- [styles["zd-button__list-item--active"]]: !urlPath && isActive,
154
+ const itemContainerClasses = (isActive) => classNames(styles$1["zd-button__list-item"], {
155
+ [styles$1["zd-button__list-item--active"]]: !urlPath && isActive,
156
156
  }, className);
157
157
  const onItemClick = (e) => {
158
158
  if (!urlPath) {
@@ -216,15 +216,15 @@ const ZDropButtonListItem = (props) => {
216
216
  };
217
217
  const ItemTypeComponent = urlPath ? LinkTag : "div";
218
218
  return (jsx("li", { children: jsxs(ItemTypeComponent, { ref: (el) => addToRefs === null || addToRefs === void 0 ? void 0 : addToRefs(el, index), className: itemContainerClasses(isActive), tabIndex: 0, onClick: onItemClick, onMouseEnter: onItemMouseEnter, onMouseLeave: onItemMouseLeave, onKeyDown: onItemKeyDown, ...(urlPath &&
219
- (LinkTag === "a" ? { href: urlPath } : { to: urlPath })), children: [Icon && (jsx("div", { className: styles["zd-button__list-item-icon"], children: Icon })), jsx("span", { className: styles["zd-button__list-item-title"], children: title }), children] }) }));
219
+ (LinkTag === "a" ? { href: urlPath } : { to: urlPath })), children: [Icon && (jsx("div", { className: styles$1["zd-button__list-item-icon"], children: Icon })), jsx("span", { className: styles$1["zd-button__list-item-title"], children: title }), children] }) }));
220
220
  };
221
221
 
222
222
  const ZDropButtonSearch = (props) => {
223
223
  const { placeholder, searchIcon, clearIcon, searchClassName, shouldFocusOnOpen = false, } = props;
224
224
  const { onSearch, searchInputRef, optionsRef, isOpen } = useContext(ZDropButtonContext);
225
225
  const [isSearchActive, setIsSearchActive] = useState(false);
226
- const searchFormClasses = classNames(styles["zd-button__search"]);
227
- const searchInputFieldClasses = classNames(styles["zd-button__search-input-field"], { active: isSearchActive }, searchClassName);
226
+ const searchFormClasses = classNames(styles$1["zd-button__search"]);
227
+ const searchInputFieldClasses = classNames(styles$1["zd-button__search-input-field"], { active: isSearchActive }, searchClassName);
228
228
  const onInputChange = (e) => {
229
229
  onSearch === null || onSearch === void 0 ? void 0 : onSearch(e);
230
230
  };
@@ -268,7 +268,7 @@ const ZDropButtonSearch = (props) => {
268
268
  searchInputRef.current.focus();
269
269
  }
270
270
  }, []);
271
- return (jsx("form", { className: searchFormClasses, onMouseEnter: onSearchMouseEnter, onMouseLeave: onSearchMouseLeave, onKeyDown: onSearchKeyDown, children: jsxs("div", { className: searchInputFieldClasses, children: [searchIcon && jsx("div", { children: searchIcon }), jsx("input", { className: styles["zd-button__search-input"], onChange: onInputChange, ref: searchInputRef, placeholder: placeholder }), jsx("button", { className: styles["zd-button__search-clear-btn"], type: "button", onClick: onClearClick, children: clearIcon || (jsx("span", { className: styles["zd-button__search-clear-btn-icon"] })) })] }) }));
271
+ return (jsx("form", { className: searchFormClasses, onMouseEnter: onSearchMouseEnter, onMouseLeave: onSearchMouseLeave, onKeyDown: onSearchKeyDown, children: jsxs("div", { className: searchInputFieldClasses, children: [searchIcon && jsx("div", { children: searchIcon }), jsx("input", { className: styles$1["zd-button__search-input"], onChange: onInputChange, ref: searchInputRef, placeholder: placeholder }), jsx("button", { className: styles$1["zd-button__search-clear-btn"], type: "button", onClick: onClearClick, children: clearIcon || (jsx("span", { className: styles$1["zd-button__search-clear-btn-icon"] })) })] }) }));
272
272
  };
273
273
 
274
274
  const ZDropButtonContext = createContext({});
@@ -278,7 +278,7 @@ const ZDropButton = (props) => {
278
278
  const optionsRef = useRef([]);
279
279
  const searchInputRef = useRef(null);
280
280
  const [isOpen, setIsOpen] = useState(false);
281
- const buttonContainerClasses = classNames(styles["zd-button"], className);
281
+ const buttonContainerClasses = classNames(styles$1["zd-button"], className);
282
282
  const addToRefs = (element, index) => {
283
283
  return (optionsRef.current[index] = element);
284
284
  };
@@ -317,5 +317,567 @@ ZDropButton.List = ZDropButtonList;
317
317
  ZDropButton.Item = ZDropButtonListItem;
318
318
  ZDropButton.Search = ZDropButtonSearch;
319
319
 
320
- export { ZDropButton };
320
+ const thumbInteractArea = 24;
321
+ const checkIsInputSwapAllowed = (activeIndex, comparedIndex, clientPosition, valuePosition) => {
322
+ return (activeIndex === comparedIndex &&
323
+ clientPosition !== null &&
324
+ clientPosition !== undefined &&
325
+ clientPosition < valuePosition + thumbInteractArea &&
326
+ clientPosition > valuePosition - thumbInteractArea);
327
+ };
328
+
329
+ const getMinMax = (firstValue, secondValue) => {
330
+ const min = firstValue < secondValue ? firstValue : secondValue;
331
+ const max = firstValue > secondValue ? firstValue : secondValue;
332
+ return { min, max };
333
+ };
334
+
335
+ // import { UnitList } from "../types/zRangeTypes";
336
+ const convertToUnit = (rawValue, units = [], unitDivisors = [], isIndicatorUnitHidden = false, scaleFn = (x) => x) => {
337
+ var _a, _b;
338
+ if (!units.length)
339
+ return String(rawValue);
340
+ if (!Number.isFinite(rawValue))
341
+ return String(rawValue);
342
+ const scaled = scaleFn(rawValue);
343
+ const sign = scaled < 0 ? "-" : "";
344
+ const value = Math.abs(scaled);
345
+ const divisors = unitDivisors
346
+ .slice(0, units.length)
347
+ .map((d) => (d > 0 ? d : 1));
348
+ if (!divisors.length) {
349
+ return `${sign}${value.toFixed(units[0].fractionDigits)} ${units[0].unit}`;
350
+ }
351
+ let idx = 0;
352
+ for (let i = 0; i < units.length; i++) {
353
+ const d = (_a = divisors[i]) !== null && _a !== void 0 ? _a : 1;
354
+ if (value >= d)
355
+ idx = i;
356
+ }
357
+ const d = (_b = divisors[idx]) !== null && _b !== void 0 ? _b : 1;
358
+ const converted = value / d;
359
+ if (isIndicatorUnitHidden) {
360
+ return `${sign}${converted.toFixed(units[idx].fractionDigits)}`;
361
+ }
362
+ return `${sign}${converted.toFixed(units[idx].fractionDigits)} ${units[idx].unit}`;
363
+ };
364
+
365
+ const calculateStepPosition = (currentValue, element, min, max) => {
366
+ if (!element) {
367
+ return 0;
368
+ }
369
+ return ((currentValue - min) * element.clientWidth) / (max - min);
370
+ };
371
+
372
+ class ZRangeValue {
373
+ constructor(min, max, unitDivisors, unitList, scale) {
374
+ this.min = min;
375
+ this.max = max;
376
+ this.unitDivisors = unitDivisors;
377
+ this.unitList = unitList;
378
+ this.scale = scale;
379
+ this.min.getPosition = this.getMinPosition.bind(this);
380
+ this.max.getPosition = this.getMaxPosition.bind(this);
381
+ }
382
+ getMinPosition() {
383
+ return calculateStepPosition(this.min.value, this.min.inputRef.current, this.min.initialValue, this.max.initialValue);
384
+ }
385
+ getMaxPosition() {
386
+ return calculateStepPosition(this.max.value, this.max.inputRef.current, this.min.initialValue, this.max.initialValue);
387
+ }
388
+ getRangeMiddle() {
389
+ return (this.max.value - this.min.value) / 2;
390
+ }
391
+ getValueLabel(forValue) {
392
+ const value = getMinMax(this.min.value, this.max.value)[forValue];
393
+ const valueLabel = this.scale ? this.scale(value) : value;
394
+ return this.unitList
395
+ ? convertToUnit(valueLabel, this.unitList, this.unitDivisors).toString()
396
+ : `${valueLabel}`;
397
+ }
398
+ }
399
+
400
+ var styles = {"z-range":"ZRange-module_z-range__z6bqS","z-range__container":"ZRange-module_z-range__container__RSKdQ","z-range__label":"ZRange-module_z-range__label__vcLKv","z-range__value-indicator-icon":"ZRange-module_z-range__value-indicator-icon__A4CDC","z-range__inputs-wrapper":"ZRange-module_z-range__inputs-wrapper__K3sk3","z-range__slider":"ZRange-module_z-range__slider__fubBh","z-range__slider-track":"ZRange-module_z-range__slider-track__m8rsi","z-range__slider-track-selected-range":"ZRange-module_z-range__slider-track-selected-range__P0Qm4","z-range__slider-values-wrapper":"ZRange-module_z-range__slider-values-wrapper__jdtV1","z-range__slider-value":"ZRange-module_z-range__slider-value__rSDuP","z-range__slider-first-value":"ZRange-module_z-range__slider-first-value__LzOA9"};
401
+
402
+ const getCssVariablePxValue = (element, cssVariableName, fallbackPx) => {
403
+ const rawValue = getComputedStyle(element)
404
+ .getPropertyValue(cssVariableName)
405
+ .trim();
406
+ const parsedPx = parseFloat(rawValue);
407
+ return Number.isFinite(parsedPx) ? parsedPx : fallbackPx;
408
+ };
409
+
410
+ const ZRangeSliderValue = (props) => {
411
+ const { value, icon, sliderValueClassName } = props;
412
+ const thumbClasses = classNames(styles["z-range__slider-value"], styles["z-range__slider-first-value"], sliderValueClassName);
413
+ return (jsxs("div", { className: thumbClasses, children: [icon && jsx("span", { children: icon }), jsx("span", { children: value })] }));
414
+ };
415
+
416
+ const indicatorTopGap = 2; // px
417
+ const ZRangeIndicator = (props) => {
418
+ const { valueIndicatorRef, indicatorStyles, indicatorValue, rangeTrackRef, inputElement, scaleFunction, unitList, unitDivisors, scale, icon, iconKey, isHighlighted, isIndicatorUnitHidden = false, } = props;
419
+ const [valueIndicatorStyles, setValueIndicatorStyles] = useState({});
420
+ const indicatorClasses = classNames("z-range__value-indicator", {
421
+ "z-range__value-indicator--active": typeof indicatorValue === "number" && isHighlighted,
422
+ }, indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicator, {
423
+ ...((indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMin)
424
+ ? { [indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMin]: iconKey === "min" }
425
+ : {}),
426
+ ...((indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMax)
427
+ ? { [indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorMax]: iconKey === "max" }
428
+ : {}),
429
+ });
430
+ const indicatorIconClasses = classNames(styles["z-range__value-indicator-icon"], indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorIcon);
431
+ const indicatorValueClasses = classNames("z-range__value-indicator-value", indicatorStyles === null || indicatorStyles === void 0 ? void 0 : indicatorStyles.indicatorValue);
432
+ useLayoutEffect(() => {
433
+ var _a;
434
+ if (((_a = rangeTrackRef === null || rangeTrackRef === void 0 ? void 0 : rangeTrackRef.current) === null || _a === void 0 ? void 0 : _a.offsetHeight) && inputElement) {
435
+ setValueIndicatorStyles({
436
+ bottom: `${rangeTrackRef.current.offsetHeight +
437
+ getCssVariablePxValue(inputElement, "--thumb-size", 16) / 2 +
438
+ indicatorTopGap}px`,
439
+ });
440
+ }
441
+ }, [indicatorValue, valueIndicatorRef, rangeTrackRef]);
442
+ return (jsxs("div", { ref: valueIndicatorRef, style: valueIndicatorStyles, className: indicatorClasses, children: [icon != null && typeof indicatorValue === "number" && (jsx("div", { className: indicatorIconClasses, children: icon }, iconKey !== null && iconKey !== void 0 ? iconKey : "indicator-icon")), typeof indicatorValue === "number" && (jsx("span", { className: indicatorValueClasses, children: convertToUnit(scale ? scaleFunction(indicatorValue) : indicatorValue, unitList, unitDivisors, isIndicatorUnitHidden) }))] }));
443
+ };
444
+
445
+ const ZRangeTrack = (props) => {
446
+ const { rangeTrackRef, selectedRangeRef, selectedRangeStyle, trackStyles } = props;
447
+ const { trackContainer, trackRange, trackSelected } = trackStyles;
448
+ const trackClasses = classNames(styles["z-range__slider"], trackContainer);
449
+ const rangeClasses = classNames(styles["z-range__slider-track"], trackRange);
450
+ const selectedRangeClasses = classNames(styles["z-range__slider-track-selected-range"], trackSelected);
451
+ return (jsxs("div", { className: trackClasses, children: [jsx("div", { ref: rangeTrackRef, className: rangeClasses }), jsx("div", { ref: selectedRangeRef, className: selectedRangeClasses, style: selectedRangeStyle })] }));
452
+ };
453
+
454
+ const ZRangeInputs = (props) => {
455
+ const { rangeValue, min, max, step, inputMinClasses, inputMaxClasses, onInputChange, onMouseDown, onTouchStart, onMouseMove, onTouchMove, onMouseUp, onTouchEnd, } = props;
456
+ return (jsxs("div", { className: styles["z-range__inputs-wrapper"], children: [jsx("input", { ref: rangeValue.min.inputRef, name: "0", type: "range", min: min, max: max,
457
+ // step={step || 1}
458
+ step: "any", value: rangeValue.min.value, className: inputMinClasses, onInput: onInputChange, onMouseDown: onMouseDown, onTouchStart: onTouchStart, onMouseMove: onMouseMove, onTouchMove: onTouchMove, onMouseUp: onMouseUp, onTouchEnd: onTouchEnd }), jsx("input", { ref: rangeValue.max.inputRef, name: "1", type: "range", min: min, max: max,
459
+ // step={step || 1}
460
+ step: "any", value: rangeValue.max.value, className: inputMaxClasses, onInput: onInputChange, onMouseDown: onMouseDown, onTouchStart: onTouchStart, onMouseMove: onMouseMove, onTouchMove: onTouchMove, onMouseUp: onMouseUp, onTouchEnd: onTouchEnd })] }));
461
+ };
462
+
463
+ const ZRangeLabel = (props) => {
464
+ const { label, labelClassName } = props;
465
+ const labelClasses = classNames(styles["z-range__label"], labelClassName);
466
+ return jsx("div", { className: labelClasses, children: label });
467
+ };
468
+
469
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
470
+ const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
471
+ const ZRange = (props) => {
472
+ var _a;
473
+ const { value, name, min, max, step, scale, unitDivisors, unitList, label, onSelect, onChange, icons, stylesClasses, isIndicatorUnitHidden, } = props;
474
+ const scaleFunction = (x) =>
475
+ // eslint-disable-next-line no-new-func
476
+ scale ? new Function("x", `return ${scale}`)(x) : x;
477
+ const containerRef = useRef(null);
478
+ const rangeTrackRef = useRef(null);
479
+ const selectedRangeRef = useRef(null);
480
+ const valueIndicatorRef = useRef(null);
481
+ const thumbOffsetRef = useRef({ 0: 0, 1: 0 });
482
+ const [indicatorValue, setIndicatorValue] = useState(undefined);
483
+ const [activeInputIndex, setActiveInputIndex] = useState(0);
484
+ const [rangeValue, setRangeValue] = useState(new ZRangeValue({
485
+ value: typeof (value === null || value === void 0 ? void 0 : value.min) === "number" ? value.min : min,
486
+ initialValue: min,
487
+ inputRef: useRef(null),
488
+ }, {
489
+ value: typeof (value === null || value === void 0 ? void 0 : value.max) === "number" ? value.max : max,
490
+ initialValue: max,
491
+ inputRef: useRef(null),
492
+ }, unitDivisors, unitList, scaleFunction));
493
+ const [clientPositionX, setClientPositionX] = useState(null);
494
+ const [isMoving, setIsMoving] = useState(false);
495
+ const [isHighlighted, setIsHighlighted] = useState(false);
496
+ const [selectedRangeStyle, setSelectedRangeStyle] = useState({
497
+ width: 0,
498
+ left: 0,
499
+ right: 0,
500
+ });
501
+ const stepValue = typeof step === "number" && step > 0 ? step : 1;
502
+ const maxForInput = min + Math.ceil((max - min) / stepValue) * stepValue;
503
+ const mapValueToInputRange = (domainValue) => domainValue >= max ? maxForInput : domainValue;
504
+ const mapInputRangeToValue = (inputValue) => Math.min(inputValue, max);
505
+ const rangeValueForInput = useMemo(() => {
506
+ return new ZRangeValue({
507
+ ...rangeValue.min,
508
+ value: mapValueToInputRange(rangeValue.min.value),
509
+ initialValue: min,
510
+ inputRef: rangeValue.min.inputRef,
511
+ }, {
512
+ ...rangeValue.max,
513
+ value: mapValueToInputRange(rangeValue.max.value),
514
+ initialValue: maxForInput,
515
+ inputRef: rangeValue.max.inputRef,
516
+ }, unitDivisors, unitList, scaleFunction);
517
+ // eslint-disable-next-line react-hooks/exhaustive-deps
518
+ }, [
519
+ rangeValue.min.value,
520
+ rangeValue.max.value,
521
+ min,
522
+ max,
523
+ maxForInput,
524
+ stepValue,
525
+ unitDivisors,
526
+ unitList,
527
+ scale,
528
+ ]);
529
+ const containerClasses = classNames(styles["z-range__container"], stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.container);
530
+ const inputMinClasses = classNames("z-range__input", "z-range__input-first", {
531
+ "z-range__input--active": activeInputIndex === 0 && isHighlighted,
532
+ }, stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.input, activeInputIndex === 0 && isHighlighted && (stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive)
533
+ ? stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive
534
+ : "");
535
+ const inputMaxClasses = classNames("z-range__input", "z-range__input-second", { "z-range__input--active": activeInputIndex === 1 && isHighlighted }, stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.input, activeInputIndex === 1 && isHighlighted && (stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive)
536
+ ? stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.inputActive
537
+ : "");
538
+ useEffect(() => {
539
+ if (!value)
540
+ return;
541
+ const nextMin = clamp(value.min, min, max);
542
+ const nextMax = clamp(value.max, min, max);
543
+ const nextRangeValue = new ZRangeValue({
544
+ ...rangeValue.min,
545
+ value: nextMin,
546
+ initialValue: min,
547
+ inputRef: rangeValue.min.inputRef,
548
+ }, {
549
+ ...rangeValue.max,
550
+ value: nextMax,
551
+ initialValue: max,
552
+ inputRef: rangeValue.max.inputRef,
553
+ }, unitDivisors, unitList, scaleFunction);
554
+ setRangeValue(nextRangeValue);
555
+ setIndicatorValue(undefined);
556
+ thumbOffsetRef.current[0] = 0;
557
+ thumbOffsetRef.current[1] = 0;
558
+ requestAnimationFrame(() => {
559
+ const val = activeInputIndex === 0 ? nextMin : nextMax;
560
+ setRangeValuePosition(val);
561
+ });
562
+ // eslint-disable-next-line react-hooks/exhaustive-deps
563
+ }, [value === null || value === void 0 ? void 0 : value.min, value === null || value === void 0 ? void 0 : value.max, min, max]);
564
+ const getThumbCenterXInContainer = (input, currentValueReal, containerEl) => {
565
+ const currentValue = mapValueToInputRange(currentValueReal);
566
+ const thumbTravelRatio = clamp01((currentValue - min) / (maxForInput - min));
567
+ const inputRect = input.getBoundingClientRect();
568
+ const containerRect = containerEl.getBoundingClientRect();
569
+ const thumbSize = getCssVariablePxValue(input, "--thumb-size", 16);
570
+ const usable = inputRect.width - thumbSize;
571
+ const thumbCenterXInInput = thumbSize / 2 + usable * thumbTravelRatio;
572
+ const inputLeftInContainer = inputRect.left - containerRect.left;
573
+ return inputLeftInContainer + thumbCenterXInInput;
574
+ };
575
+ const setRangeValuePosition = (currentValueReal, inputIndex = activeInputIndex) => {
576
+ var _a;
577
+ const indicator = valueIndicatorRef.current;
578
+ const container = containerRef.current;
579
+ const input = (_a = (inputIndex === 0
580
+ ? rangeValue.min.inputRef.current
581
+ : rangeValue.max.inputRef.current)) !== null && _a !== void 0 ? _a : rangeValue.min.inputRef.current;
582
+ if (!indicator || !container || !input)
583
+ return;
584
+ const distanceLeft = getThumbCenterXInContainer(input, currentValueReal, container);
585
+ indicator.style.left = `${Math.round(distanceLeft)}px`;
586
+ indicator.style.transform = `translateX(-50%)`;
587
+ };
588
+ const updateRangeValues = (currentValueReal, inputIndex) => {
589
+ const updatedRangeValue = new ZRangeValue({
590
+ ...(inputIndex === 0
591
+ ? { ...rangeValue.min, value: currentValueReal }
592
+ : rangeValue.min),
593
+ }, {
594
+ ...(inputIndex === 0
595
+ ? rangeValue.max
596
+ : { ...rangeValue.max, value: currentValueReal }),
597
+ }, rangeValue.unitDivisors, rangeValue.unitList, rangeValue.scale);
598
+ setRangeValue(updatedRangeValue);
599
+ setIndicatorValue(currentValueReal);
600
+ if (!onChange)
601
+ return;
602
+ const { min: currentMin, max: currentMax } = getMinMax(updatedRangeValue.min.value, updatedRangeValue.max.value);
603
+ onChange({
604
+ ...updatedRangeValue,
605
+ min: { ...updatedRangeValue.min, value: currentMin },
606
+ max: { ...updatedRangeValue.max, value: currentMax },
607
+ }, name);
608
+ };
609
+ const onInputChange = (e) => {
610
+ if (!rangeTrackRef.current ||
611
+ !rangeValue.min.inputRef.current ||
612
+ !rangeValue.max.inputRef.current) {
613
+ return;
614
+ }
615
+ const currentValue = mapInputRangeToValue(Number(e.target.value));
616
+ const rangeTrackRect = rangeTrackRef.current.getBoundingClientRect();
617
+ const clientPositionOnTrack = clientPositionX != null ? clientPositionX - rangeTrackRect.left : null;
618
+ const minValueStepPosition = rangeValue.getMinPosition();
619
+ const maxValueStepPosition = rangeValue.getMaxPosition();
620
+ if (checkIsInputSwapAllowed(activeInputIndex, 0, clientPositionOnTrack, maxValueStepPosition)) {
621
+ return;
622
+ }
623
+ if (checkIsInputSwapAllowed(activeInputIndex, 1, clientPositionOnTrack, minValueStepPosition)) {
624
+ return;
625
+ }
626
+ const { min: currentMin, max: currentMax } = getMinMax(rangeValue.min.value, rangeValue.max.value);
627
+ const rangeMiddle = (currentMax - currentMin) / 2;
628
+ if (rangeValue.max.value > rangeValue.min.value) {
629
+ if (currentValue < currentMin + rangeMiddle) {
630
+ setActiveInputIndex(0);
631
+ setRangeValuePosition(currentValue);
632
+ updateRangeValues(currentValue, 0);
633
+ return;
634
+ }
635
+ if (currentValue > currentMin + rangeMiddle) {
636
+ setActiveInputIndex(1);
637
+ setRangeValuePosition(currentValue);
638
+ updateRangeValues(currentValue, 1);
639
+ return;
640
+ }
641
+ }
642
+ if (rangeValue.min.value > rangeValue.max.value) {
643
+ if (currentValue < currentMin + rangeMiddle) {
644
+ setActiveInputIndex(1);
645
+ setRangeValuePosition(currentValue);
646
+ updateRangeValues(currentValue, 1);
647
+ return;
648
+ }
649
+ if (currentValue > currentMin + rangeMiddle) {
650
+ setActiveInputIndex(0);
651
+ setRangeValuePosition(currentValue);
652
+ updateRangeValues(currentValue, 0);
653
+ return;
654
+ }
655
+ }
656
+ setRangeValuePosition(currentValue);
657
+ updateRangeValues(currentValue, activeInputIndex);
658
+ };
659
+ const onMoveStart = (e) => {
660
+ setIsMoving(true);
661
+ setIsHighlighted(true);
662
+ const clientX = "clientX" in e ? e.clientX : e.touches[0].clientX;
663
+ setClientPositionX(clientX);
664
+ const container = containerRef.current;
665
+ const input = activeInputIndex === 0
666
+ ? rangeValue.min.inputRef.current
667
+ : rangeValue.max.inputRef.current;
668
+ if (container && input) {
669
+ const containerRect = container.getBoundingClientRect();
670
+ const pointerXInContainer = clientX - containerRect.left;
671
+ const currentValReal = activeInputIndex === 0 ? rangeValue.min.value : rangeValue.max.value;
672
+ const thumbXInContainer = getThumbCenterXInContainer(input, currentValReal, container);
673
+ const thumbSize = getCssVariablePxValue(input, "--thumb-size", 16);
674
+ const dist = Math.abs(pointerXInContainer - thumbXInContainer);
675
+ thumbOffsetRef.current[activeInputIndex] =
676
+ dist <= thumbSize / 2 ? pointerXInContainer - thumbXInContainer : 0;
677
+ }
678
+ if (!rangeTrackRef.current ||
679
+ !rangeValue.min.inputRef.current ||
680
+ !rangeValue.max.inputRef.current) {
681
+ return;
682
+ }
683
+ const rangeTrackRect = rangeTrackRef.current.getBoundingClientRect();
684
+ const clientPositionOnTrack = clientX - rangeTrackRect.left;
685
+ const minValueStepPosition = rangeValue.getMinPosition();
686
+ const maxValueStepPosition = rangeValue.getMaxPosition();
687
+ if (checkIsInputSwapAllowed(activeInputIndex, 0, clientPositionOnTrack, maxValueStepPosition)) {
688
+ setActiveInputIndex(1);
689
+ thumbOffsetRef.current[1] = 0;
690
+ setRangeValuePosition(rangeValue.max.value, 1);
691
+ updateRangeValues(rangeValue.max.value, 1);
692
+ }
693
+ if (checkIsInputSwapAllowed(activeInputIndex, 1, clientPositionOnTrack, minValueStepPosition)) {
694
+ setActiveInputIndex(0);
695
+ thumbOffsetRef.current[0] = 0;
696
+ setRangeValuePosition(rangeValue.min.value);
697
+ updateRangeValues(rangeValue.min.value, 0);
698
+ }
699
+ };
700
+ const onMove = (e) => {
701
+ var _a, _b;
702
+ if (!isMoving)
703
+ return;
704
+ e.preventDefault();
705
+ e.stopPropagation();
706
+ const currentPositionX = "clientX" in e ? e.clientX : (_b = (_a = e.touches) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.clientX;
707
+ const element = activeInputIndex === 0
708
+ ? rangeValue.min.inputRef
709
+ : rangeValue.max.inputRef;
710
+ if (!(element.current && currentPositionX != null))
711
+ return;
712
+ const slider = element.current;
713
+ const sliderRect = slider.getBoundingClientRect();
714
+ const offsetX = currentPositionX -
715
+ sliderRect.left -
716
+ thumbOffsetRef.current[activeInputIndex];
717
+ const thumbSize = getCssVariablePxValue(slider, "--thumb-size", 16);
718
+ const usable = Math.max(1, sliderRect.width - thumbSize);
719
+ const posOnUsable = clamp(offsetX - thumbSize / 2, 0, usable);
720
+ const ratio = posOnUsable / usable;
721
+ const rawValue = min + ratio * (maxForInput - min);
722
+ const roundedValue = Math.round((rawValue - min) / stepValue) * stepValue + min;
723
+ const limitedInputValue = clamp(roundedValue, min, maxForInput);
724
+ const limitedRealValue = mapInputRangeToValue(limitedInputValue);
725
+ setRangeValuePosition(limitedRealValue, activeInputIndex);
726
+ updateRangeValues(limitedRealValue, activeInputIndex);
727
+ };
728
+ const onMoveStop = (e) => {
729
+ var _a, _b, _c;
730
+ setIsMoving(false);
731
+ setIsHighlighted(false);
732
+ if (!((_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.value) &&
733
+ rangeValue.min.inputRef.current &&
734
+ rangeValue.max.inputRef.current) {
735
+ const firstValue = activeInputIndex === 0
736
+ ? mapInputRangeToValue(Number((_b = rangeValue.min.inputRef.current) === null || _b === void 0 ? void 0 : _b.value))
737
+ : rangeValue.min.value;
738
+ const secondValue = activeInputIndex === 1
739
+ ? mapInputRangeToValue(Number((_c = rangeValue.max.inputRef.current) === null || _c === void 0 ? void 0 : _c.value))
740
+ : rangeValue.max.value;
741
+ const { min: lastMin, max: lastMax } = getMinMax(firstValue, secondValue);
742
+ onSelect === null || onSelect === void 0 ? void 0 : onSelect({
743
+ ...rangeValue,
744
+ min: { ...rangeValue.min, value: lastMin },
745
+ max: { ...rangeValue.max, value: lastMax },
746
+ }, name);
747
+ updateRangeValues(activeInputIndex === 0 ? lastMin : lastMax, activeInputIndex);
748
+ return;
749
+ }
750
+ const targetValueReal = mapInputRangeToValue(Number(e.target.value));
751
+ const { min: currentMin, max: currentMax } = getMinMax(e.target.name === "0"
752
+ ? targetValueReal
753
+ : rangeValue.min.value, e.target.name === "0"
754
+ ? rangeValue.max.value
755
+ : targetValueReal);
756
+ onSelect === null || onSelect === void 0 ? void 0 : onSelect({
757
+ ...rangeValue,
758
+ min: { ...rangeValue.min, value: currentMin },
759
+ max: { ...rangeValue.max, value: currentMax },
760
+ }, name);
761
+ };
762
+ const onMoveEnd = (e) => {
763
+ onMoveStop(e);
764
+ e.stopPropagation();
765
+ };
766
+ useEffect(() => {
767
+ if (!isMoving)
768
+ return;
769
+ const handleTouchEnd = () => {
770
+ setIsMoving(false);
771
+ setIsHighlighted(false);
772
+ };
773
+ document.addEventListener("touchend", handleTouchEnd);
774
+ return () => document.removeEventListener("touchend", handleTouchEnd);
775
+ }, [isMoving]);
776
+ useEffect(() => {
777
+ if (!isMoving)
778
+ return;
779
+ const handleMouseUp = () => {
780
+ setIsMoving(false);
781
+ setIsHighlighted(false);
782
+ };
783
+ document.addEventListener("mouseup", handleMouseUp);
784
+ return () => document.removeEventListener("mouseup", handleMouseUp);
785
+ }, [isMoving]);
786
+ const calculateSelectedRangeStyle = () => {
787
+ if (!rangeTrackRef.current)
788
+ return;
789
+ const innerWidth = rangeTrackRef.current.clientWidth;
790
+ const { min: valueMinReal, max: valueMaxReal } = getMinMax(rangeValue.min.value, rangeValue.max.value);
791
+ const valueMin = mapValueToInputRange(valueMinReal);
792
+ const valueMax = mapValueToInputRange(valueMaxReal);
793
+ const ratioMin = clamp01((valueMin - min) / (maxForInput - min));
794
+ const ratioMax = clamp01((valueMax - min) / (maxForInput - min));
795
+ const leftPx = innerWidth * ratioMin;
796
+ const rightPx = innerWidth * ratioMax;
797
+ setSelectedRangeStyle({
798
+ left: `${leftPx}px`,
799
+ width: `${Math.max(0, rightPx - leftPx)}px`,
800
+ });
801
+ };
802
+ useEffect(() => {
803
+ calculateSelectedRangeStyle();
804
+ // eslint-disable-next-line react-hooks/exhaustive-deps
805
+ }, [
806
+ rangeValue.min.value,
807
+ rangeValue.max.value,
808
+ min,
809
+ max,
810
+ stepValue,
811
+ maxForInput,
812
+ ]);
813
+ useEffect(() => {
814
+ setRangeValuePosition(activeInputIndex === 0 ? rangeValue.min.value : rangeValue.max.value);
815
+ // eslint-disable-next-line react-hooks/exhaustive-deps
816
+ }, []);
817
+ useLayoutEffect(() => {
818
+ const track = rangeTrackRef.current;
819
+ if (!track)
820
+ return;
821
+ let rafId = null;
822
+ const updateValue = () => {
823
+ calculateSelectedRangeStyle();
824
+ const updatedValue = typeof indicatorValue === "number"
825
+ ? indicatorValue
826
+ : activeInputIndex === 0
827
+ ? rangeValue.min.value
828
+ : rangeValue.max.value;
829
+ setRangeValuePosition(updatedValue);
830
+ };
831
+ const updateSchedule = () => {
832
+ if (rafId !== null)
833
+ return;
834
+ rafId = requestAnimationFrame(() => {
835
+ rafId = null;
836
+ updateValue();
837
+ });
838
+ };
839
+ updateSchedule();
840
+ const observer = new ResizeObserver(updateSchedule);
841
+ observer.observe(track);
842
+ window.addEventListener("resize", updateSchedule);
843
+ return () => {
844
+ observer.disconnect();
845
+ window.removeEventListener("resize", updateSchedule);
846
+ if (rafId !== null)
847
+ cancelAnimationFrame(rafId);
848
+ };
849
+ }, [
850
+ min,
851
+ max,
852
+ stepValue,
853
+ maxForInput,
854
+ indicatorValue,
855
+ activeInputIndex,
856
+ rangeValue.min.value,
857
+ rangeValue.max.value,
858
+ ]);
859
+ const lowValue = Math.min(rangeValue.min.value, rangeValue.max.value);
860
+ const indicatorIsMin = typeof indicatorValue === "number" ? indicatorValue === lowValue : false;
861
+ const indicatorIcon = (icons === null || icons === void 0 ? void 0 : icons.indicatorMin) && (icons === null || icons === void 0 ? void 0 : icons.indicatorMax)
862
+ ? indicatorIsMin
863
+ ? icons.indicatorMin
864
+ : icons.indicatorMax
865
+ : undefined;
866
+ const indicatorIconKey = indicatorIsMin ? "min" : "max";
867
+ return (jsxs("div", { className: containerClasses, children: [label && (jsx(ZRangeLabel, { label: label, labelClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.label })), jsxs("div", { ref: containerRef, className: styles["z-range"], children: [jsx(ZRangeInputs, { rangeValue: rangeValueForInput, min: min, max: maxForInput, step: stepValue, inputMinClasses: inputMinClasses, inputMaxClasses: inputMaxClasses, onInputChange: onInputChange, onMouseDown: onMoveStart, onTouchStart: onMoveStart, onMouseMove: onMove, onTouchMove: onMove, onMouseUp: onMoveEnd, onTouchEnd: onMoveEnd }), jsx(ZRangeTrack, { rangeTrackRef: rangeTrackRef, selectedRangeRef: selectedRangeRef, selectedRangeStyle: selectedRangeStyle, trackStyles: {
868
+ trackContainer: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackContainer,
869
+ trackRange: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackRange,
870
+ trackSelected: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.trackSelected,
871
+ } }), jsx(ZRangeIndicator, { valueIndicatorRef: valueIndicatorRef, indicatorStyles: {
872
+ indicator: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicator,
873
+ indicatorMin: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorMin,
874
+ indicatorMax: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorMax,
875
+ indicatorValue: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorValue,
876
+ indicatorIcon: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.indicatorIcon,
877
+ }, indicatorValue: indicatorValue, rangeTrackRef: rangeTrackRef, inputElement: activeInputIndex === 0
878
+ ? rangeValue.min.inputRef.current
879
+ : (_a = rangeValue.max.inputRef.current) !== null && _a !== void 0 ? _a : rangeValue.min.inputRef.current, scaleFunction: scaleFunction, unitList: unitList, unitDivisors: unitDivisors, scale: scale, icon: indicatorIcon, iconKey: indicatorIconKey, isHighlighted: isHighlighted, isIndicatorUnitHidden: isIndicatorUnitHidden })] }), jsxs("div", { className: styles["z-range__slider-values-wrapper"], children: [jsx(ZRangeSliderValue, { sliderValueClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.sliderValue, value: rangeValue.getValueLabel("min"), icon: icons === null || icons === void 0 ? void 0 : icons.thumbMin }), jsx(ZRangeSliderValue, { sliderValueClassName: stylesClasses === null || stylesClasses === void 0 ? void 0 : stylesClasses.sliderValue, value: rangeValue.getValueLabel("max"), icon: icons === null || icons === void 0 ? void 0 : icons.thumbMax })] })] }));
880
+ };
881
+
882
+ export { ZDropButton, ZRange };
321
883
  //# sourceMappingURL=index.esm.js.map