@zag-js/slider 0.2.6 → 0.2.8

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.
@@ -0,0 +1,10 @@
1
+ import {
2
+ machine
3
+ } from "./chunk-A2ZK6G4F.mjs";
4
+ import "./chunk-SGCWELVB.mjs";
5
+ import "./chunk-J5IGGBVE.mjs";
6
+ import "./chunk-YREEXXZP.mjs";
7
+ import "./chunk-DRAPR6JV.mjs";
8
+ export {
9
+ machine
10
+ };
@@ -0,0 +1,26 @@
1
+ import { Style } from '@zag-js/types';
2
+ import { SharedContext, MachineContext } from './slider.types.js';
3
+ import '@zag-js/core';
4
+
5
+ declare function getThumbOffset(ctx: SharedContext): string;
6
+ declare function getThumbStyle(ctx: SharedContext): Style;
7
+ declare function getRangeStyle(ctx: Pick<SharedContext, "isVertical" | "isRtl">): Style;
8
+ declare function getControlStyle(): Style;
9
+ declare function getRootStyle(ctx: MachineContext): Style;
10
+ declare function getMarkerStyle(ctx: Pick<SharedContext, "isHorizontal" | "isRtl">, percent: number): Style;
11
+ declare function getLabelStyle(): Style;
12
+ declare function getTrackStyle(): Style;
13
+ declare function getMarkerGroupStyle(): Style;
14
+ declare const styles: {
15
+ getThumbOffset: typeof getThumbOffset;
16
+ getControlStyle: typeof getControlStyle;
17
+ getThumbStyle: typeof getThumbStyle;
18
+ getRangeStyle: typeof getRangeStyle;
19
+ getRootStyle: typeof getRootStyle;
20
+ getMarkerStyle: typeof getMarkerStyle;
21
+ getLabelStyle: typeof getLabelStyle;
22
+ getTrackStyle: typeof getTrackStyle;
23
+ getMarkerGroupStyle: typeof getMarkerGroupStyle;
24
+ };
25
+
26
+ export { styles };
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/slider.style.ts
21
+ var slider_style_exports = {};
22
+ __export(slider_style_exports, {
23
+ styles: () => styles
24
+ });
25
+ module.exports = __toCommonJS(slider_style_exports);
26
+ var import_numeric_range = require("@zag-js/numeric-range");
27
+ function getVerticalThumbOffset(ctx) {
28
+ var _a;
29
+ const { height = 0 } = (_a = ctx.thumbSize) != null ? _a : {};
30
+ const getValue = (0, import_numeric_range.getValueTransformer)([ctx.min, ctx.max], [-height / 2, height / 2]);
31
+ return parseFloat(getValue(ctx.value).toFixed(2));
32
+ }
33
+ function getHorizontalThumbOffset(ctx) {
34
+ var _a;
35
+ const { width = 0 } = (_a = ctx.thumbSize) != null ? _a : {};
36
+ if (ctx.isRtl) {
37
+ const getValue2 = (0, import_numeric_range.getValueTransformer)([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
38
+ return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
39
+ }
40
+ const getValue = (0, import_numeric_range.getValueTransformer)([ctx.min, ctx.max], [-width / 2, width / 2]);
41
+ return parseFloat(getValue(ctx.value).toFixed(2));
42
+ }
43
+ function getThumbOffset(ctx) {
44
+ const percent = (0, import_numeric_range.getValuePercent)(ctx.value, ctx.min, ctx.max) * 100;
45
+ if (ctx.thumbAlignment === "center") {
46
+ return `${percent}%`;
47
+ }
48
+ const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
49
+ return `calc(${percent}% - ${offset}px)`;
50
+ }
51
+ function getVisibility(ctx) {
52
+ let visibility = "visible";
53
+ if (ctx.thumbAlignment === "contain" && !ctx.hasMeasuredThumbSize) {
54
+ visibility = "hidden";
55
+ }
56
+ return visibility;
57
+ }
58
+ function getThumbStyle(ctx) {
59
+ const placementProp = ctx.isVertical ? "bottom" : ctx.isRtl ? "right" : "left";
60
+ return {
61
+ visibility: getVisibility(ctx),
62
+ position: "absolute",
63
+ transform: "var(--slider-thumb-transform)",
64
+ [placementProp]: "var(--slider-thumb-offset)"
65
+ };
66
+ }
67
+ function getRangeOffsets(ctx) {
68
+ let start = "0%";
69
+ let end = `${100 - ctx.valuePercent}%`;
70
+ if (ctx.origin === "center") {
71
+ const isNegative = ctx.valuePercent < 50;
72
+ start = isNegative ? `${ctx.valuePercent}%` : "50%";
73
+ end = isNegative ? "50%" : end;
74
+ }
75
+ return { start, end };
76
+ }
77
+ function getRangeStyle(ctx) {
78
+ if (ctx.isVertical) {
79
+ return {
80
+ position: "absolute",
81
+ bottom: "var(--slider-range-start)",
82
+ top: "var(--slider-range-end)"
83
+ };
84
+ }
85
+ return {
86
+ position: "absolute",
87
+ [ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
88
+ [ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
89
+ };
90
+ }
91
+ function getControlStyle() {
92
+ return {
93
+ touchAction: "none",
94
+ userSelect: "none",
95
+ position: "relative"
96
+ };
97
+ }
98
+ function getRootStyle(ctx) {
99
+ const range = getRangeOffsets(ctx);
100
+ return {
101
+ "--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : "translateX(-50%)",
102
+ "--slider-thumb-offset": getThumbOffset(ctx),
103
+ "--slider-range-start": range.start,
104
+ "--slider-range-end": range.end
105
+ };
106
+ }
107
+ function getMarkerStyle(ctx, percent) {
108
+ return {
109
+ position: "absolute",
110
+ pointerEvents: "none",
111
+ [ctx.isHorizontal ? "left" : "bottom"]: `${(ctx.isRtl ? 1 - percent : percent) * 100}%`
112
+ };
113
+ }
114
+ function getLabelStyle() {
115
+ return { userSelect: "none" };
116
+ }
117
+ function getTrackStyle() {
118
+ return { position: "relative" };
119
+ }
120
+ function getMarkerGroupStyle() {
121
+ return {
122
+ userSelect: "none",
123
+ pointerEvents: "none",
124
+ position: "relative"
125
+ };
126
+ }
127
+ var styles = {
128
+ getThumbOffset,
129
+ getControlStyle,
130
+ getThumbStyle,
131
+ getRangeStyle,
132
+ getRootStyle,
133
+ getMarkerStyle,
134
+ getLabelStyle,
135
+ getTrackStyle,
136
+ getMarkerGroupStyle
137
+ };
138
+ // Annotate the CommonJS export names for ESM import in node:
139
+ 0 && (module.exports = {
140
+ styles
141
+ });
@@ -0,0 +1,6 @@
1
+ import {
2
+ styles
3
+ } from "./chunk-YREEXXZP.mjs";
4
+ export {
5
+ styles
6
+ };
@@ -0,0 +1,166 @@
1
+ import { StateMachine } from '@zag-js/core';
2
+ import { RequiredBy, DirectionProperty, CommonProperties, Context } from '@zag-js/types';
3
+
4
+ type ElementIds = Partial<{
5
+ root: string;
6
+ thumb: string;
7
+ control: string;
8
+ track: string;
9
+ range: string;
10
+ label: string;
11
+ output: string;
12
+ }>;
13
+ type PublicContext = DirectionProperty & CommonProperties & {
14
+ /**
15
+ * The ids of the elements in the slider. Useful for composition.
16
+ */
17
+ ids?: ElementIds;
18
+ /**
19
+ * The value of the slider
20
+ */
21
+ value: number;
22
+ /**
23
+ * The name associated with the slider (when used in a form)
24
+ */
25
+ name?: string;
26
+ /**
27
+ * The associate form of the underlying input element.
28
+ */
29
+ form?: string;
30
+ /**
31
+ * Whether the slider is disabled
32
+ */
33
+ disabled?: boolean;
34
+ /**
35
+ * Whether the slider is read-only
36
+ */
37
+ readOnly?: boolean;
38
+ /**
39
+ * Whether the slider value is invalid
40
+ */
41
+ invalid?: boolean;
42
+ /**
43
+ * The minimum value of the slider
44
+ */
45
+ min: number;
46
+ /**
47
+ * The maximum value of the slider
48
+ */
49
+ max: number;
50
+ /**
51
+ * The step value of the slider
52
+ */
53
+ step: number;
54
+ /**
55
+ * The orientation of the slider
56
+ */
57
+ orientation?: "vertical" | "horizontal";
58
+ /**
59
+ * - "start": Useful when the value represents an absolute value
60
+ * - "center": Useful when the value represents an offset (relative)
61
+ */
62
+ origin?: "start" | "center";
63
+ /**
64
+ * The aria-label of the slider. Useful for providing an accessible name to the slider
65
+ */
66
+ "aria-label"?: string;
67
+ /**
68
+ * The `id` of the element that labels the slider. Useful for providing an accessible name to the slider
69
+ */
70
+ "aria-labelledby"?: string;
71
+ /**
72
+ * Whether to focus the slider thumb after interaction (scrub and keyboard)
73
+ */
74
+ focusThumbOnChange?: boolean;
75
+ /**
76
+ * Function that returns a human readable value for the slider
77
+ */
78
+ getAriaValueText?(value: number): string;
79
+ /**
80
+ * Function invoked when the value of the slider changes
81
+ */
82
+ onChange?(details: {
83
+ value: number;
84
+ }): void;
85
+ /**
86
+ * Function invoked when the slider value change is done
87
+ */
88
+ onChangeEnd?(details: {
89
+ value: number;
90
+ }): void;
91
+ /**
92
+ * Function invoked when the slider value change is started
93
+ */
94
+ onChangeStart?(details: {
95
+ value: number;
96
+ }): void;
97
+ /**
98
+ * The alignment of the slider thumb relative to the track
99
+ * - `center`: the thumb will extend beyond the bounds of the slider track.
100
+ * - `contain`: the thumb will be contained within the bounds of the track.
101
+ */
102
+ thumbAlignment?: "contain" | "center";
103
+ };
104
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
105
+ type ComputedContext = Readonly<{
106
+ /**
107
+ * @computed
108
+ * Whether the slider is interactive
109
+ */
110
+ readonly isInteractive: boolean;
111
+ /**
112
+ * @computed
113
+ * Whether the thumb size has been measured
114
+ */
115
+ readonly hasMeasuredThumbSize: boolean;
116
+ /**
117
+ * @computed
118
+ * Whether the slider is horizontal
119
+ */
120
+ readonly isHorizontal: boolean;
121
+ /**
122
+ * @computed
123
+ * Whether the slider is vertical
124
+ */
125
+ readonly isVertical: boolean;
126
+ /**
127
+ * @computed
128
+ * Whether the slider is in RTL mode
129
+ */
130
+ readonly isRtl: boolean;
131
+ /**
132
+ * @computed
133
+ * The value of the slider as a percentage
134
+ */
135
+ readonly valuePercent: number;
136
+ }>;
137
+ type PrivateContext = Context<{}>;
138
+ type MachineContext = PublicContext & ComputedContext & PrivateContext;
139
+ type MachineState = {
140
+ value: "unknown" | "idle" | "dragging" | "focus";
141
+ };
142
+ type State = StateMachine.State<MachineContext, MachineState>;
143
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
144
+ type SharedContext = {
145
+ min: number;
146
+ max: number;
147
+ step: number;
148
+ dir?: "ltr" | "rtl";
149
+ isRtl: boolean;
150
+ isVertical: boolean;
151
+ isHorizontal: boolean;
152
+ value: number;
153
+ thumbSize: {
154
+ width: number;
155
+ height: number;
156
+ } | null;
157
+ thumbAlignment?: "contain" | "center";
158
+ orientation?: "horizontal" | "vertical";
159
+ readonly hasMeasuredThumbSize: boolean;
160
+ };
161
+ type Point = {
162
+ x: number;
163
+ y: number;
164
+ };
165
+
166
+ export { MachineContext, MachineState, Point, Send, SharedContext, State, UserDefinedContext };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/slider.types.ts
17
+ var slider_types_exports = {};
18
+ module.exports = __toCommonJS(slider_types_exports);
File without changes
@@ -0,0 +1,10 @@
1
+ import { MachineContext } from './slider.types.js';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare function clampPercent(percent: number): number;
6
+ declare function constrainValue(ctx: MachineContext, value: number): number;
7
+ declare function decrement(ctx: MachineContext, step?: number): number;
8
+ declare function increment(ctx: MachineContext, step?: number): number;
9
+
10
+ export { clampPercent, constrainValue, decrement, increment };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/slider.utils.ts
21
+ var slider_utils_exports = {};
22
+ __export(slider_utils_exports, {
23
+ clampPercent: () => clampPercent,
24
+ constrainValue: () => constrainValue,
25
+ decrement: () => decrement,
26
+ increment: () => increment
27
+ });
28
+ module.exports = __toCommonJS(slider_utils_exports);
29
+ var import_numeric_range = require("@zag-js/numeric-range");
30
+ function clampPercent(percent) {
31
+ return (0, import_numeric_range.clampValue)(percent, 0, 1);
32
+ }
33
+ function constrainValue(ctx, value) {
34
+ const snapValue = (0, import_numeric_range.snapValueToStep)(value, ctx.min, ctx.max, ctx.step);
35
+ return (0, import_numeric_range.clampValue)(snapValue, ctx.min, ctx.max);
36
+ }
37
+ function decrement(ctx, step) {
38
+ const index = 0;
39
+ const values = (0, import_numeric_range.getPreviousStepValue)(index, {
40
+ ...ctx,
41
+ step: step != null ? step : ctx.step,
42
+ values: [ctx.value]
43
+ });
44
+ return values[index];
45
+ }
46
+ function increment(ctx, step) {
47
+ const index = 0;
48
+ const values = (0, import_numeric_range.getNextStepValue)(index, {
49
+ ...ctx,
50
+ step: step != null ? step : ctx.step,
51
+ values: [ctx.value]
52
+ });
53
+ return values[index];
54
+ }
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ clampPercent,
58
+ constrainValue,
59
+ decrement,
60
+ increment
61
+ });
@@ -0,0 +1,12 @@
1
+ import {
2
+ clampPercent,
3
+ constrainValue,
4
+ decrement,
5
+ increment
6
+ } from "./chunk-DRAPR6JV.mjs";
7
+ export {
8
+ clampPercent,
9
+ constrainValue,
10
+ decrement,
11
+ increment
12
+ };
package/package.json CHANGED
@@ -1,10 +1,7 @@
1
1
  {
2
2
  "name": "@zag-js/slider",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "Core logic for the slider widget implemented as a state machine",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
5
  "keywords": [
9
6
  "js",
10
7
  "machine",
@@ -29,21 +26,34 @@
29
26
  "url": "https://github.com/chakra-ui/zag/issues"
30
27
  },
31
28
  "dependencies": {
32
- "@zag-js/anatomy": "0.1.2",
33
- "@zag-js/core": "0.2.3",
34
- "@zag-js/element-size": "0.3.0",
35
- "@zag-js/types": "0.3.1"
29
+ "@zag-js/anatomy": "0.1.3",
30
+ "@zag-js/core": "0.2.5",
31
+ "@zag-js/element-size": "0.3.1",
32
+ "@zag-js/numeric-range": "0.1.0",
33
+ "@zag-js/types": "0.3.3"
36
34
  },
37
35
  "devDependencies": {
38
- "@zag-js/dom-utils": "0.2.1",
39
- "@zag-js/form-utils": "0.2.2",
40
- "@zag-js/utils": "0.3.1",
41
- "@zag-js/number-utils": "0.2.1"
36
+ "clean-package": "2.2.0",
37
+ "@zag-js/dom-utils": "0.2.3",
38
+ "@zag-js/form-utils": "0.2.3",
39
+ "@zag-js/utils": "0.3.2"
40
+ },
41
+ "clean-package": "../../../clean-package.config.json",
42
+ "main": "dist/index.js",
43
+ "module": "dist/index.mjs",
44
+ "types": "dist/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.mjs",
49
+ "require": "./dist/index.js"
50
+ },
51
+ "./package.json": "./package.json"
42
52
  },
43
53
  "scripts": {
44
- "build-fast": "tsup src/index.ts --format=esm,cjs",
54
+ "build-fast": "tsup src",
45
55
  "start": "pnpm build --watch",
46
- "build": "tsup src/index.ts --format=esm,cjs --dts",
56
+ "build": "tsup src --dts",
47
57
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
48
58
  "lint": "eslint src --ext .ts,.tsx",
49
59
  "test-ci": "pnpm test --ci --runInBand",