@zag-js/slider 2.0.0-next.2 → 2.0.0-next.4

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