@zag-js/slider 1.47.0 → 1.48.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.
@@ -1,150 +1,141 @@
1
- // src/slider.style.ts
2
1
  import { getValuePercent, getValueTransformer, toPx } from "@zag-js/utils";
2
+ // src/slider.style.ts
3
3
  function getBounds(value) {
4
- const firstValue = value[0];
5
- const lastThumb = value[value.length - 1];
6
- return [firstValue, lastThumb];
4
+ return [value[0], value[value.length - 1]];
7
5
  }
8
6
  function getRangeOffsets(params) {
9
- const { prop, computed } = params;
10
- const valuePercent = computed("valuePercent");
11
- const [firstPercent, lastPercent] = getBounds(valuePercent);
12
- if (valuePercent.length === 1) {
13
- if (prop("origin") === "center") {
14
- const isNegative = valuePercent[0] < 50;
15
- const start = isNegative ? `${valuePercent[0]}%` : "50%";
16
- const end = isNegative ? "50%" : `${100 - valuePercent[0]}%`;
17
- return { start, end };
18
- }
19
- if (prop("origin") === "end") {
20
- return { start: `${lastPercent}%`, end: "0%" };
21
- }
22
- return { start: "0%", end: `${100 - lastPercent}%` };
23
- }
24
- return { start: `${firstPercent}%`, end: `${100 - lastPercent}%` };
7
+ const { prop, computed } = params;
8
+ const valuePercent = computed("valuePercent");
9
+ const [firstPercent, lastPercent] = getBounds(valuePercent);
10
+ if (valuePercent.length === 1) {
11
+ if (prop("origin") === "center") {
12
+ const isNegative = valuePercent[0] < 50;
13
+ return {
14
+ start: isNegative ? `${valuePercent[0]}%` : "50%",
15
+ end: isNegative ? "50%" : `${100 - valuePercent[0]}%`
16
+ };
17
+ }
18
+ if (prop("origin") === "end") return {
19
+ start: `${lastPercent}%`,
20
+ end: "0%"
21
+ };
22
+ return {
23
+ start: "0%",
24
+ end: `${100 - lastPercent}%`
25
+ };
26
+ }
27
+ return {
28
+ start: `${firstPercent}%`,
29
+ end: `${100 - lastPercent}%`
30
+ };
25
31
  }
26
32
  function getRangeStyle(params) {
27
- const { computed } = params;
28
- const isVertical = computed("isVertical");
29
- const isRtl = computed("isRtl");
30
- if (isVertical) {
31
- return {
32
- position: "absolute",
33
- bottom: "var(--slider-range-start)",
34
- top: "var(--slider-range-end)"
35
- };
36
- }
37
- return {
38
- position: "absolute",
39
- [isRtl ? "right" : "left"]: "var(--slider-range-start)",
40
- [isRtl ? "left" : "right"]: "var(--slider-range-end)"
41
- };
33
+ const { computed } = params;
34
+ const isVertical = computed("isVertical");
35
+ const isRtl = computed("isRtl");
36
+ if (isVertical) return {
37
+ position: "absolute",
38
+ bottom: "var(--slider-range-start)",
39
+ top: "var(--slider-range-end)"
40
+ };
41
+ return {
42
+ position: "absolute",
43
+ [isRtl ? "right" : "left"]: "var(--slider-range-start)",
44
+ [isRtl ? "left" : "right"]: "var(--slider-range-end)"
45
+ };
42
46
  }
43
47
  function getVerticalThumbOffset(params, value) {
44
- const { context, prop } = params;
45
- const { height = 0 } = context.get("thumbSize") ?? {};
46
- const getValue = getValueTransformer([prop("min"), prop("max")], [-height / 2, height / 2]);
47
- return parseFloat(getValue(value).toFixed(2));
48
+ const { context, prop } = params;
49
+ const { height = 0 } = context.get("thumbSize") ?? {};
50
+ const getValue = getValueTransformer([prop("min"), prop("max")], [-height / 2, height / 2]);
51
+ return parseFloat(getValue(value).toFixed(2));
48
52
  }
49
53
  function getHorizontalThumbOffset(params, value) {
50
- const { computed, context, prop } = params;
51
- const { width = 0 } = context.get("thumbSize") ?? {};
52
- const isRtl = computed("isRtl");
53
- if (isRtl) {
54
- const getValue2 = getValueTransformer([prop("max"), prop("min")], [-width / 2, width / 2]);
55
- return -1 * parseFloat(getValue2(value).toFixed(2));
56
- }
57
- const getValue = getValueTransformer([prop("min"), prop("max")], [-width / 2, width / 2]);
58
- return parseFloat(getValue(value).toFixed(2));
54
+ const { computed, context, prop } = params;
55
+ const { width = 0 } = context.get("thumbSize") ?? {};
56
+ if (computed("isRtl")) {
57
+ const getValue = getValueTransformer([prop("max"), prop("min")], [-width / 2, width / 2]);
58
+ return -1 * parseFloat(getValue(value).toFixed(2));
59
+ }
60
+ const getValue = getValueTransformer([prop("min"), prop("max")], [-width / 2, width / 2]);
61
+ return parseFloat(getValue(value).toFixed(2));
59
62
  }
60
63
  function getOffset(params, percent, value) {
61
- const { computed, prop } = params;
62
- if (prop("thumbAlignment") === "center") return `${percent}%`;
63
- const offset = computed("isVertical") ? getVerticalThumbOffset(params, value) : getHorizontalThumbOffset(params, value);
64
- return `calc(${percent}% - ${offset}px)`;
64
+ const { computed, prop } = params;
65
+ if (prop("thumbAlignment") === "center") return `${percent}%`;
66
+ return `calc(${percent}% - ${computed("isVertical") ? getVerticalThumbOffset(params, value) : getHorizontalThumbOffset(params, value)}px)`;
65
67
  }
66
68
  function getThumbOffset(params, value) {
67
- const { prop } = params;
68
- const percent = getValuePercent(value, prop("min"), prop("max")) * 100;
69
- return getOffset(params, percent, value);
69
+ const { prop } = params;
70
+ return getOffset(params, getValuePercent(value, prop("min"), prop("max")) * 100, value);
70
71
  }
71
72
  function getVisibility(params) {
72
- const { computed, prop } = params;
73
- let visibility = "visible";
74
- if (prop("thumbAlignment") === "contain" && !computed("hasMeasuredThumbSize")) {
75
- visibility = "hidden";
76
- }
77
- return visibility;
73
+ const { computed, prop } = params;
74
+ let visibility = "visible";
75
+ if (prop("thumbAlignment") === "contain" && !computed("hasMeasuredThumbSize")) visibility = "hidden";
76
+ return visibility;
78
77
  }
79
78
  function getThumbStyle(params, index) {
80
- const { computed, context } = params;
81
- const placementProp = computed("isVertical") ? "bottom" : "insetInlineStart";
82
- const focusedIndex = context.get("focusedIndex");
83
- return {
84
- visibility: getVisibility(params),
85
- position: "absolute",
86
- transform: "var(--slider-thumb-transform)",
87
- [placementProp]: `var(--slider-thumb-offset-${index})`,
88
- zIndex: focusedIndex === index ? 1 : void 0
89
- };
79
+ const { computed, context } = params;
80
+ const placementProp = computed("isVertical") ? "bottom" : "insetInlineStart";
81
+ const focusedIndex = context.get("focusedIndex");
82
+ return {
83
+ visibility: getVisibility(params),
84
+ position: "absolute",
85
+ transform: "var(--slider-thumb-transform)",
86
+ [placementProp]: `var(--slider-thumb-offset-${index})`,
87
+ zIndex: focusedIndex === index ? 1 : void 0
88
+ };
90
89
  }
91
90
  function getControlStyle() {
92
- return {
93
- touchAction: "none",
94
- userSelect: "none",
95
- WebkitUserSelect: "none",
96
- position: "relative"
97
- };
91
+ return {
92
+ touchAction: "none",
93
+ userSelect: "none",
94
+ WebkitUserSelect: "none",
95
+ position: "relative"
96
+ };
98
97
  }
99
98
  function getRootStyle(params) {
100
- const { context, computed } = params;
101
- const isVertical = computed("isVertical");
102
- const isRtl = computed("isRtl");
103
- const range = getRangeOffsets(params);
104
- const thumbSize = context.get("thumbSize");
105
- const offsetStyles = context.get("value").reduce((styles, value, index) => {
106
- const offset = getThumbOffset(params, value);
107
- return { ...styles, [`--slider-thumb-offset-${index}`]: offset };
108
- }, {});
109
- return {
110
- ...offsetStyles,
111
- "--slider-thumb-width": toPx(thumbSize?.width),
112
- "--slider-thumb-height": toPx(thumbSize?.height),
113
- "--slider-thumb-transform": isVertical ? "translateY(50%)" : isRtl ? "translateX(50%)" : "translateX(-50%)",
114
- "--slider-range-start": range.start,
115
- "--slider-range-end": range.end
116
- };
99
+ const { context, computed } = params;
100
+ const isVertical = computed("isVertical");
101
+ const isRtl = computed("isRtl");
102
+ const range = getRangeOffsets(params);
103
+ const thumbSize = context.get("thumbSize");
104
+ return {
105
+ ...context.get("value").reduce((styles, value, index) => {
106
+ const offset = getThumbOffset(params, value);
107
+ return {
108
+ ...styles,
109
+ [`--slider-thumb-offset-${index}`]: offset
110
+ };
111
+ }, {}),
112
+ "--slider-thumb-width": toPx(thumbSize?.width),
113
+ "--slider-thumb-height": toPx(thumbSize?.height),
114
+ "--slider-thumb-transform": isVertical ? "translateY(50%)" : isRtl ? "translateX(50%)" : "translateX(-50%)",
115
+ "--slider-range-start": range.start,
116
+ "--slider-range-end": range.end
117
+ };
117
118
  }
118
119
  function getMarkerStyle(params, value) {
119
- const { computed } = params;
120
- const isHorizontal = computed("isHorizontal");
121
- const isRtl = computed("isRtl");
122
- return {
123
- visibility: getVisibility(params),
124
- position: "absolute",
125
- pointerEvents: "none",
126
- [isHorizontal ? "insetInlineStart" : "bottom"]: getThumbOffset(params, value),
127
- translate: "var(--translate-x) var(--translate-y)",
128
- "--translate-x": isHorizontal ? isRtl ? "50%" : "-50%" : "0%",
129
- "--translate-y": !isHorizontal ? "50%" : "0%"
130
- };
120
+ const { computed } = params;
121
+ const isHorizontal = computed("isHorizontal");
122
+ const isRtl = computed("isRtl");
123
+ return {
124
+ visibility: getVisibility(params),
125
+ position: "absolute",
126
+ pointerEvents: "none",
127
+ [isHorizontal ? "insetInlineStart" : "bottom"]: getThumbOffset(params, value),
128
+ translate: "var(--translate-x) var(--translate-y)",
129
+ "--translate-x": isHorizontal ? isRtl ? "50%" : "-50%" : "0%",
130
+ "--translate-y": !isHorizontal ? "50%" : "0%"
131
+ };
131
132
  }
132
133
  function getMarkerGroupStyle() {
133
- return {
134
- userSelect: "none",
135
- WebkitUserSelect: "none",
136
- pointerEvents: "none",
137
- position: "relative"
138
- };
134
+ return {
135
+ userSelect: "none",
136
+ WebkitUserSelect: "none",
137
+ pointerEvents: "none",
138
+ position: "relative"
139
+ };
139
140
  }
140
- export {
141
- getControlStyle,
142
- getMarkerGroupStyle,
143
- getMarkerStyle,
144
- getRangeOffsets,
145
- getRangeStyle,
146
- getRootStyle,
147
- getThumbOffset,
148
- getThumbStyle,
149
- getVisibility
150
- };
141
+ export { getControlStyle, getMarkerGroupStyle, getMarkerStyle, getRangeOffsets, getRangeStyle, getRootStyle, getThumbOffset, getThumbStyle, getVisibility };