@zag-js/steps 0.0.0-dev-20240630000246

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Chakra UI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # @zag-js/steps
2
+
3
+ Core logic for the steps widget implemented as a state machine
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ yarn add @zag-js/steps
9
+ # or
10
+ npm i @zag-js/steps
11
+ ```
12
+
13
+ ## Contribution
14
+
15
+ Yes please! See the [contributing guidelines](https://github.com/chakra-ui/zag/blob/main/CONTRIBUTING.md) for details.
16
+
17
+ ## Licence
18
+
19
+ This project is licensed under the terms of the [MIT license](https://github.com/chakra-ui/zag/blob/main/LICENSE).
@@ -0,0 +1,135 @@
1
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
2
+ import { RequiredBy, PropTypes, DirectionProperty, CommonProperties, NormalizeProps } from '@zag-js/types';
3
+ import * as _zag_js_core from '@zag-js/core';
4
+ import { Machine, StateMachine } from '@zag-js/core';
5
+
6
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "list" | "item" | "trigger" | "indicator" | "separator" | "content" | "title" | "description" | "nextTrigger" | "prevTrigger" | "progress">;
7
+
8
+ interface StepChangeDetails {
9
+ step: number;
10
+ }
11
+ interface ElementIds {
12
+ root?: string;
13
+ list?: string;
14
+ triggerId?(index: number): string;
15
+ contentId?(index: number): string;
16
+ }
17
+ interface PublicContext extends DirectionProperty, CommonProperties {
18
+ /**
19
+ * The custom ids for the stepper elements
20
+ */
21
+ ids?: ElementIds;
22
+ /**
23
+ * The current value of the stepper
24
+ */
25
+ step: number;
26
+ /**
27
+ * Callback to be called when the value changes
28
+ */
29
+ onStepChange?(details: StepChangeDetails): void;
30
+ /**
31
+ * Callback to be called when a step is completed
32
+ */
33
+ onStepComplete?: VoidFunction;
34
+ /**
35
+ * If `true`, the stepper will allow you to skip steps
36
+ */
37
+ skippable?: boolean;
38
+ /**
39
+ * The orientation of the stepper
40
+ */
41
+ orientation?: "horizontal" | "vertical";
42
+ /**
43
+ * The total number of steps
44
+ */
45
+ count: number;
46
+ }
47
+ interface PrivateContext {
48
+ }
49
+ type ComputedContext = Readonly<{
50
+ percent: number;
51
+ hasNextStep: boolean;
52
+ hasPrevStep: boolean;
53
+ }>;
54
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
55
+ interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
56
+ }
57
+ interface MachineState {
58
+ value: "idle";
59
+ }
60
+ type State = StateMachine.State<MachineContext, MachineState>;
61
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
62
+ type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
63
+ interface ItemProps {
64
+ index: number;
65
+ }
66
+ interface ItemState {
67
+ index: number;
68
+ triggerId: string;
69
+ contentId: string;
70
+ current: boolean;
71
+ completed: boolean;
72
+ last: boolean;
73
+ first: boolean;
74
+ }
75
+ interface MachineApi<T extends PropTypes = PropTypes> {
76
+ /**
77
+ * The value of the stepper.
78
+ */
79
+ value: number;
80
+ /**
81
+ * The percentage of the stepper.
82
+ */
83
+ percent: number;
84
+ /**
85
+ * The total number of steps.
86
+ */
87
+ count: number;
88
+ /**
89
+ * Whether the stepper has a next step.
90
+ */
91
+ hasNextStep: boolean;
92
+ /**
93
+ * Whether the stepper has a previous step.
94
+ */
95
+ hasPrevStep: boolean;
96
+ /**
97
+ * Function to set the value of the stepper.
98
+ */
99
+ setValue(value: number): void;
100
+ /**
101
+ * Function to go to the next step.
102
+ */
103
+ goToNextStep(): void;
104
+ /**
105
+ * Function to go to the previous step.
106
+ */
107
+ goToPrevStep(): void;
108
+ /**
109
+ * Function to go to reset the stepper.
110
+ */
111
+ resetStep(): void;
112
+ /**
113
+ * Returns the state of the item at the given index.
114
+ */
115
+ getItemState(props: ItemProps): ItemState;
116
+ getRootProps(): T["element"];
117
+ getListProps(): T["element"];
118
+ getItemProps(props: ItemProps): T["element"];
119
+ getTriggerProps(props: ItemProps): T["element"];
120
+ getContentProps(props: ItemProps): T["element"];
121
+ getNextTriggerProps(): T["button"];
122
+ getPrevTriggerProps(): T["button"];
123
+ getProgressProps(): T["element"];
124
+ getIndicatorProps(props: ItemProps): T["element"];
125
+ getSeparatorProps(props: ItemProps): T["element"];
126
+ }
127
+
128
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
129
+
130
+ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
131
+
132
+ declare const props: ("step" | "dir" | "id" | "orientation" | "ids" | "onStepChange" | "onStepComplete" | "skippable" | "count" | "getRootNode")[];
133
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "step" | "dir" | "id" | "orientation" | "ids" | "onStepChange" | "onStepComplete" | "skippable" | "count" | "getRootNode">];
134
+
135
+ export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type ItemProps, type ItemState, type Service, type StepChangeDetails, anatomy, connect, machine, props, splitProps };
@@ -0,0 +1,135 @@
1
+ import * as _zag_js_anatomy from '@zag-js/anatomy';
2
+ import { RequiredBy, PropTypes, DirectionProperty, CommonProperties, NormalizeProps } from '@zag-js/types';
3
+ import * as _zag_js_core from '@zag-js/core';
4
+ import { Machine, StateMachine } from '@zag-js/core';
5
+
6
+ declare const anatomy: _zag_js_anatomy.AnatomyInstance<"root" | "list" | "item" | "trigger" | "indicator" | "separator" | "content" | "title" | "description" | "nextTrigger" | "prevTrigger" | "progress">;
7
+
8
+ interface StepChangeDetails {
9
+ step: number;
10
+ }
11
+ interface ElementIds {
12
+ root?: string;
13
+ list?: string;
14
+ triggerId?(index: number): string;
15
+ contentId?(index: number): string;
16
+ }
17
+ interface PublicContext extends DirectionProperty, CommonProperties {
18
+ /**
19
+ * The custom ids for the stepper elements
20
+ */
21
+ ids?: ElementIds;
22
+ /**
23
+ * The current value of the stepper
24
+ */
25
+ step: number;
26
+ /**
27
+ * Callback to be called when the value changes
28
+ */
29
+ onStepChange?(details: StepChangeDetails): void;
30
+ /**
31
+ * Callback to be called when a step is completed
32
+ */
33
+ onStepComplete?: VoidFunction;
34
+ /**
35
+ * If `true`, the stepper will allow you to skip steps
36
+ */
37
+ skippable?: boolean;
38
+ /**
39
+ * The orientation of the stepper
40
+ */
41
+ orientation?: "horizontal" | "vertical";
42
+ /**
43
+ * The total number of steps
44
+ */
45
+ count: number;
46
+ }
47
+ interface PrivateContext {
48
+ }
49
+ type ComputedContext = Readonly<{
50
+ percent: number;
51
+ hasNextStep: boolean;
52
+ hasPrevStep: boolean;
53
+ }>;
54
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
55
+ interface MachineContext extends PublicContext, PrivateContext, ComputedContext {
56
+ }
57
+ interface MachineState {
58
+ value: "idle";
59
+ }
60
+ type State = StateMachine.State<MachineContext, MachineState>;
61
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
62
+ type Service = Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
63
+ interface ItemProps {
64
+ index: number;
65
+ }
66
+ interface ItemState {
67
+ index: number;
68
+ triggerId: string;
69
+ contentId: string;
70
+ current: boolean;
71
+ completed: boolean;
72
+ last: boolean;
73
+ first: boolean;
74
+ }
75
+ interface MachineApi<T extends PropTypes = PropTypes> {
76
+ /**
77
+ * The value of the stepper.
78
+ */
79
+ value: number;
80
+ /**
81
+ * The percentage of the stepper.
82
+ */
83
+ percent: number;
84
+ /**
85
+ * The total number of steps.
86
+ */
87
+ count: number;
88
+ /**
89
+ * Whether the stepper has a next step.
90
+ */
91
+ hasNextStep: boolean;
92
+ /**
93
+ * Whether the stepper has a previous step.
94
+ */
95
+ hasPrevStep: boolean;
96
+ /**
97
+ * Function to set the value of the stepper.
98
+ */
99
+ setValue(value: number): void;
100
+ /**
101
+ * Function to go to the next step.
102
+ */
103
+ goToNextStep(): void;
104
+ /**
105
+ * Function to go to the previous step.
106
+ */
107
+ goToPrevStep(): void;
108
+ /**
109
+ * Function to go to reset the stepper.
110
+ */
111
+ resetStep(): void;
112
+ /**
113
+ * Returns the state of the item at the given index.
114
+ */
115
+ getItemState(props: ItemProps): ItemState;
116
+ getRootProps(): T["element"];
117
+ getListProps(): T["element"];
118
+ getItemProps(props: ItemProps): T["element"];
119
+ getTriggerProps(props: ItemProps): T["element"];
120
+ getContentProps(props: ItemProps): T["element"];
121
+ getNextTriggerProps(): T["button"];
122
+ getPrevTriggerProps(): T["button"];
123
+ getProgressProps(): T["element"];
124
+ getIndicatorProps(props: ItemProps): T["element"];
125
+ getSeparatorProps(props: ItemProps): T["element"];
126
+ }
127
+
128
+ declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
129
+
130
+ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
131
+
132
+ declare const props: ("step" | "dir" | "id" | "orientation" | "ids" | "onStepChange" | "onStepComplete" | "skippable" | "count" | "getRootNode")[];
133
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "step" | "dir" | "id" | "orientation" | "ids" | "onStepChange" | "onStepComplete" | "skippable" | "count" | "getRootNode">];
134
+
135
+ export { type MachineApi as Api, type UserDefinedContext as Context, type ElementIds, type ItemProps, type ItemState, type Service, type StepChangeDetails, anatomy, connect, machine, props, splitProps };
package/dist/index.js ADDED
@@ -0,0 +1,315 @@
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/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ anatomy: () => anatomy,
24
+ connect: () => connect,
25
+ machine: () => machine,
26
+ props: () => props,
27
+ splitProps: () => splitProps
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+
31
+ // src/steps.anatomy.ts
32
+ var import_anatomy = require("@zag-js/anatomy");
33
+ var anatomy = (0, import_anatomy.createAnatomy)("steps").parts(
34
+ "root",
35
+ "list",
36
+ "item",
37
+ "trigger",
38
+ "indicator",
39
+ "separator",
40
+ "content",
41
+ "title",
42
+ "description",
43
+ "nextTrigger",
44
+ "prevTrigger",
45
+ "progress"
46
+ );
47
+ var parts = anatomy.build();
48
+
49
+ // src/steps.dom.ts
50
+ var import_dom_query = require("@zag-js/dom-query");
51
+ var dom = (0, import_dom_query.createScope)({
52
+ getRootId: (ctx) => ctx.ids?.root ?? `steps:${ctx.id}`,
53
+ getListId: (ctx) => ctx.ids?.list ?? `steps:${ctx.id}:list`,
54
+ getTriggerId: (ctx, index) => ctx.ids?.triggerId?.(index) ?? `steps:${ctx.id}:trigger:${index}`,
55
+ getContentId: (ctx, index) => ctx.ids?.contentId?.(index) ?? `steps:${ctx.id}:content:${index}`
56
+ });
57
+
58
+ // src/steps.connect.ts
59
+ var import_dom_query2 = require("@zag-js/dom-query");
60
+ function connect(state, send, normalize) {
61
+ const value = state.context.step;
62
+ const count = state.context.count;
63
+ const percent = state.context.percent;
64
+ const hasNextStep = state.context.hasNextStep;
65
+ const hasPrevStep = state.context.hasPrevStep;
66
+ const getItemState = (props2) => ({
67
+ triggerId: dom.getTriggerId(state.context, props2.index),
68
+ contentId: dom.getContentId(state.context, props2.index),
69
+ current: props2.index === value,
70
+ completed: props2.index < value,
71
+ index: props2.index,
72
+ first: props2.index === 0,
73
+ last: props2.index === count - 1
74
+ });
75
+ const goToNextStep = () => {
76
+ send({ type: "STEP.NEXT", src: "next.trigger.click" });
77
+ };
78
+ const goToPrevStep = () => {
79
+ send({ type: "STEP.PREV", src: "prev.trigger.click" });
80
+ };
81
+ const resetStep = () => {
82
+ send({ type: "STEP.RESET", src: "reset.trigger.click" });
83
+ };
84
+ const setValue = (value2) => {
85
+ send({ type: "STEP.SET", value: value2, src: "api.setValue" });
86
+ };
87
+ return {
88
+ value,
89
+ count,
90
+ percent,
91
+ hasNextStep,
92
+ hasPrevStep,
93
+ goToNextStep,
94
+ goToPrevStep,
95
+ resetStep,
96
+ getItemState,
97
+ setValue,
98
+ getRootProps() {
99
+ return normalize.element({
100
+ ...parts.root.attrs,
101
+ id: dom.getRootId(state.context),
102
+ dir: state.context.dir,
103
+ "data-orientation": state.context.orientation,
104
+ style: {
105
+ "--percent": `${percent}%`
106
+ }
107
+ });
108
+ },
109
+ getListProps() {
110
+ return normalize.element({
111
+ ...parts.list.attrs,
112
+ dir: state.context.dir,
113
+ id: dom.getListId(state.context),
114
+ role: "tablist",
115
+ "aria-orientation": state.context.orientation,
116
+ "data-orientation": state.context.orientation
117
+ });
118
+ },
119
+ getItemProps(props2) {
120
+ const itemState = getItemState(props2);
121
+ return normalize.element({
122
+ ...parts.item.attrs,
123
+ dir: state.context.dir,
124
+ "aria-current": itemState.current ? "step" : void 0,
125
+ "data-orientation": state.context.orientation
126
+ });
127
+ },
128
+ getTriggerProps(props2) {
129
+ const itemState = getItemState(props2);
130
+ return normalize.button({
131
+ ...parts.trigger.attrs,
132
+ id: itemState.triggerId,
133
+ role: "tab",
134
+ dir: state.context.dir,
135
+ tabIndex: state.context.skippable || itemState.current ? 0 : -1,
136
+ "aria-selected": itemState.current,
137
+ "aria-controls": itemState.contentId,
138
+ "data-state": itemState.current ? "open" : "closed",
139
+ "data-orientation": state.context.orientation,
140
+ "data-complete": (0, import_dom_query2.dataAttr)(itemState.completed),
141
+ "data-current": (0, import_dom_query2.dataAttr)(itemState.current),
142
+ "data-incomplete": (0, import_dom_query2.dataAttr)(!itemState.current),
143
+ onClick(event) {
144
+ if (event.defaultPrevented) return;
145
+ if (!state.context.skippable) return;
146
+ send({ type: "STEP.SET", value: props2.index, src: "trigger.click" });
147
+ }
148
+ });
149
+ },
150
+ getContentProps(props2) {
151
+ const itemState = getItemState(props2);
152
+ return normalize.element({
153
+ ...parts.content.attrs,
154
+ dir: state.context.dir,
155
+ id: itemState.contentId,
156
+ role: "tabpanel",
157
+ tabIndex: 0,
158
+ hidden: !itemState.current,
159
+ "data-state": itemState.current ? "open" : "closed",
160
+ "data-orientation": state.context.orientation,
161
+ "aria-labelledby": itemState.triggerId
162
+ });
163
+ },
164
+ getIndicatorProps(props2) {
165
+ const itemState = getItemState(props2);
166
+ return normalize.element({
167
+ ...parts.indicator.attrs,
168
+ dir: state.context.dir,
169
+ "aria-hidden": true,
170
+ "data-complete": (0, import_dom_query2.dataAttr)(itemState.completed),
171
+ "data-current": (0, import_dom_query2.dataAttr)(itemState.current),
172
+ "data-incomplete": (0, import_dom_query2.dataAttr)(!itemState.current)
173
+ });
174
+ },
175
+ getSeparatorProps(props2) {
176
+ const itemState = getItemState(props2);
177
+ return normalize.element({
178
+ ...parts.separator.attrs,
179
+ dir: state.context.dir,
180
+ "data-orientation": state.context.orientation,
181
+ "data-complete": (0, import_dom_query2.dataAttr)(itemState.completed),
182
+ "data-current": (0, import_dom_query2.dataAttr)(itemState.current),
183
+ "data-incomplete": (0, import_dom_query2.dataAttr)(!itemState.current)
184
+ });
185
+ },
186
+ getNextTriggerProps() {
187
+ return normalize.button({
188
+ ...parts.nextTrigger.attrs,
189
+ dir: state.context.dir,
190
+ type: "button",
191
+ disabled: !hasNextStep,
192
+ onClick(event) {
193
+ if (event.defaultPrevented) return;
194
+ goToNextStep();
195
+ }
196
+ });
197
+ },
198
+ getPrevTriggerProps() {
199
+ return normalize.button({
200
+ dir: state.context.dir,
201
+ ...parts.prevTrigger.attrs,
202
+ type: "button",
203
+ disabled: !hasPrevStep,
204
+ onClick(event) {
205
+ if (event.defaultPrevented) return;
206
+ goToPrevStep();
207
+ }
208
+ });
209
+ },
210
+ getProgressProps() {
211
+ return normalize.element({
212
+ dir: state.context.dir,
213
+ ...parts.progress.attrs,
214
+ role: "progressbar",
215
+ "aria-valuenow": percent,
216
+ "aria-valuemin": 0,
217
+ "aria-valuemax": 100,
218
+ "aria-valuetext": `${percent}% complete`,
219
+ "data-complete": (0, import_dom_query2.dataAttr)(percent === 100)
220
+ });
221
+ }
222
+ };
223
+ }
224
+
225
+ // src/steps.machine.ts
226
+ var import_core = require("@zag-js/core");
227
+ var import_utils = require("@zag-js/utils");
228
+ function machine(userContext) {
229
+ const ctx = (0, import_utils.compact)(userContext);
230
+ return (0, import_core.createMachine)(
231
+ {
232
+ id: "steps",
233
+ initial: "idle",
234
+ context: {
235
+ step: 0,
236
+ count: 1,
237
+ ...ctx
238
+ },
239
+ computed: {
240
+ percent: (ctx2) => ctx2.step / ctx2.count * 100,
241
+ hasNextStep: (ctx2) => ctx2.step < ctx2.count,
242
+ hasPrevStep: (ctx2) => ctx2.step > 0
243
+ },
244
+ states: {
245
+ idle: {
246
+ on: {
247
+ "STEP.NEXT": {
248
+ actions: "goToNextStep"
249
+ },
250
+ "STEP.PREV": {
251
+ actions: "goToPrevStep"
252
+ },
253
+ "STEP.RESET": {
254
+ actions: "resetStep"
255
+ }
256
+ }
257
+ }
258
+ }
259
+ },
260
+ {
261
+ actions: {
262
+ goToNextStep(ctx2) {
263
+ const value = Math.min(ctx2.step + 1, ctx2.count);
264
+ set.value(ctx2, value);
265
+ },
266
+ goToPrevStep(ctx2) {
267
+ const value = Math.max(ctx2.step - 1, 0);
268
+ set.value(ctx2, value);
269
+ },
270
+ resetStep(ctx2) {
271
+ set.value(ctx2, 0);
272
+ },
273
+ setStep(ctx2, event) {
274
+ const value = event.value;
275
+ const inRange = value >= 0 && value < ctx2.count;
276
+ if (!inRange) throw new RangeError(`Index ${value} is out of bounds`);
277
+ set.value(ctx2, value);
278
+ }
279
+ }
280
+ }
281
+ );
282
+ }
283
+ var set = {
284
+ value(ctx, step) {
285
+ if ((0, import_utils.isEqual)(ctx.step, step)) return;
286
+ ctx.step = step;
287
+ ctx.onStepChange?.({ step });
288
+ }
289
+ };
290
+
291
+ // src/steps.props.ts
292
+ var import_types = require("@zag-js/types");
293
+ var import_utils2 = require("@zag-js/utils");
294
+ var props = (0, import_types.createProps)()([
295
+ "count",
296
+ "dir",
297
+ "getRootNode",
298
+ "id",
299
+ "ids",
300
+ "onStepChange",
301
+ "onStepComplete",
302
+ "orientation",
303
+ "skippable",
304
+ "step"
305
+ ]);
306
+ var splitProps = (0, import_utils2.createSplitProps)(props);
307
+ // Annotate the CommonJS export names for ESM import in node:
308
+ 0 && (module.exports = {
309
+ anatomy,
310
+ connect,
311
+ machine,
312
+ props,
313
+ splitProps
314
+ });
315
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/steps.anatomy.ts","../src/steps.dom.ts","../src/steps.connect.ts","../src/steps.machine.ts","../src/steps.props.ts"],"sourcesContent":["export { anatomy } from \"./steps.anatomy\"\nexport { connect } from \"./steps.connect\"\nexport { machine } from \"./steps.machine\"\nexport * from \"./steps.props\"\nexport type {\n MachineApi as Api,\n UserDefinedContext as Context,\n ElementIds,\n ItemProps,\n ItemState,\n Service,\n StepChangeDetails,\n} from \"./steps.types\"\n","import { createAnatomy } from \"@zag-js/anatomy\"\n\nexport const anatomy = createAnatomy(\"steps\").parts(\n \"root\",\n \"list\",\n \"item\",\n \"trigger\",\n \"indicator\",\n \"separator\",\n \"content\",\n \"title\",\n \"description\",\n \"nextTrigger\",\n \"prevTrigger\",\n \"progress\",\n)\n\nexport const parts = anatomy.build()\n","import { createScope } from \"@zag-js/dom-query\"\nimport type { MachineContext as Ctx } from \"./steps.types\"\n\nexport const dom = createScope({\n getRootId: (ctx: Ctx) => ctx.ids?.root ?? `steps:${ctx.id}`,\n getListId: (ctx: Ctx) => ctx.ids?.list ?? `steps:${ctx.id}:list`,\n getTriggerId: (ctx: Ctx, index: number) => ctx.ids?.triggerId?.(index) ?? `steps:${ctx.id}:trigger:${index}`,\n getContentId: (ctx: Ctx, index: number) => ctx.ids?.contentId?.(index) ?? `steps:${ctx.id}:content:${index}`,\n})\n","import type { NormalizeProps, PropTypes } from \"@zag-js/types\"\nimport type { State, Send, ItemProps, ItemState, MachineApi } from \"./steps.types\"\nimport { parts } from \"./steps.anatomy\"\nimport { dom } from \"./steps.dom\"\nimport { dataAttr } from \"@zag-js/dom-query\"\n\nexport function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {\n const value = state.context.step\n const count = state.context.count\n const percent = state.context.percent\n const hasNextStep = state.context.hasNextStep\n const hasPrevStep = state.context.hasPrevStep\n\n const getItemState = (props: ItemProps): ItemState => ({\n triggerId: dom.getTriggerId(state.context, props.index),\n contentId: dom.getContentId(state.context, props.index),\n current: props.index === value,\n completed: props.index < value,\n index: props.index,\n first: props.index === 0,\n last: props.index === count - 1,\n })\n\n const goToNextStep = () => {\n send({ type: \"STEP.NEXT\", src: \"next.trigger.click\" })\n }\n\n const goToPrevStep = () => {\n send({ type: \"STEP.PREV\", src: \"prev.trigger.click\" })\n }\n\n const resetStep = () => {\n send({ type: \"STEP.RESET\", src: \"reset.trigger.click\" })\n }\n\n const setValue = (value: number) => {\n send({ type: \"STEP.SET\", value, src: \"api.setValue\" })\n }\n\n return {\n value,\n count,\n percent,\n hasNextStep,\n hasPrevStep,\n goToNextStep,\n goToPrevStep,\n resetStep,\n getItemState,\n setValue,\n\n getRootProps() {\n return normalize.element({\n ...parts.root.attrs,\n id: dom.getRootId(state.context),\n dir: state.context.dir,\n \"data-orientation\": state.context.orientation,\n style: {\n \"--percent\": `${percent}%`,\n },\n })\n },\n\n getListProps() {\n return normalize.element({\n ...parts.list.attrs,\n dir: state.context.dir,\n id: dom.getListId(state.context),\n role: \"tablist\",\n \"aria-orientation\": state.context.orientation,\n \"data-orientation\": state.context.orientation,\n })\n },\n\n getItemProps(props) {\n const itemState = getItemState(props)\n return normalize.element({\n ...parts.item.attrs,\n dir: state.context.dir,\n \"aria-current\": itemState.current ? \"step\" : undefined,\n \"data-orientation\": state.context.orientation,\n })\n },\n\n getTriggerProps(props) {\n const itemState = getItemState(props)\n return normalize.button({\n ...parts.trigger.attrs,\n id: itemState.triggerId,\n role: \"tab\",\n dir: state.context.dir,\n tabIndex: state.context.skippable || itemState.current ? 0 : -1,\n \"aria-selected\": itemState.current,\n \"aria-controls\": itemState.contentId,\n \"data-state\": itemState.current ? \"open\" : \"closed\",\n \"data-orientation\": state.context.orientation,\n \"data-complete\": dataAttr(itemState.completed),\n \"data-current\": dataAttr(itemState.current),\n \"data-incomplete\": dataAttr(!itemState.current),\n onClick(event) {\n if (event.defaultPrevented) return\n if (!state.context.skippable) return\n send({ type: \"STEP.SET\", value: props.index, src: \"trigger.click\" })\n },\n })\n },\n\n getContentProps(props) {\n const itemState = getItemState(props)\n return normalize.element({\n ...parts.content.attrs,\n dir: state.context.dir,\n id: itemState.contentId,\n role: \"tabpanel\",\n tabIndex: 0,\n hidden: !itemState.current,\n \"data-state\": itemState.current ? \"open\" : \"closed\",\n \"data-orientation\": state.context.orientation,\n \"aria-labelledby\": itemState.triggerId,\n })\n },\n\n getIndicatorProps(props) {\n const itemState = getItemState(props)\n return normalize.element({\n ...parts.indicator.attrs,\n dir: state.context.dir,\n \"aria-hidden\": true,\n \"data-complete\": dataAttr(itemState.completed),\n \"data-current\": dataAttr(itemState.current),\n \"data-incomplete\": dataAttr(!itemState.current),\n })\n },\n\n getSeparatorProps(props) {\n const itemState = getItemState(props)\n return normalize.element({\n ...parts.separator.attrs,\n dir: state.context.dir,\n \"data-orientation\": state.context.orientation,\n \"data-complete\": dataAttr(itemState.completed),\n \"data-current\": dataAttr(itemState.current),\n \"data-incomplete\": dataAttr(!itemState.current),\n })\n },\n\n getNextTriggerProps() {\n return normalize.button({\n ...parts.nextTrigger.attrs,\n dir: state.context.dir,\n type: \"button\",\n disabled: !hasNextStep,\n onClick(event) {\n if (event.defaultPrevented) return\n goToNextStep()\n },\n })\n },\n\n getPrevTriggerProps() {\n return normalize.button({\n dir: state.context.dir,\n ...parts.prevTrigger.attrs,\n type: \"button\",\n disabled: !hasPrevStep,\n onClick(event) {\n if (event.defaultPrevented) return\n goToPrevStep()\n },\n })\n },\n\n getProgressProps() {\n return normalize.element({\n dir: state.context.dir,\n ...parts.progress.attrs,\n role: \"progressbar\",\n \"aria-valuenow\": percent,\n \"aria-valuemin\": 0,\n \"aria-valuemax\": 100,\n \"aria-valuetext\": `${percent}% complete`,\n \"data-complete\": dataAttr(percent === 100),\n })\n },\n }\n}\n","import { createMachine } from \"@zag-js/core\"\nimport { compact, isEqual } from \"@zag-js/utils\"\nimport type { MachineContext, MachineState, UserDefinedContext } from \"./steps.types\"\n\nexport function machine(userContext: UserDefinedContext) {\n const ctx = compact(userContext)\n return createMachine<MachineContext, MachineState>(\n {\n id: \"steps\",\n initial: \"idle\",\n\n context: {\n step: 0,\n count: 1,\n ...ctx,\n },\n\n computed: {\n percent: (ctx) => (ctx.step / ctx.count) * 100,\n hasNextStep: (ctx) => ctx.step < ctx.count,\n hasPrevStep: (ctx) => ctx.step > 0,\n },\n\n states: {\n idle: {\n on: {\n \"STEP.NEXT\": {\n actions: \"goToNextStep\",\n },\n \"STEP.PREV\": {\n actions: \"goToPrevStep\",\n },\n \"STEP.RESET\": {\n actions: \"resetStep\",\n },\n },\n },\n },\n },\n {\n actions: {\n goToNextStep(ctx) {\n const value = Math.min(ctx.step + 1, ctx.count)\n set.value(ctx, value)\n },\n goToPrevStep(ctx) {\n const value = Math.max(ctx.step - 1, 0)\n set.value(ctx, value)\n },\n resetStep(ctx) {\n set.value(ctx, 0)\n },\n setStep(ctx, event) {\n const value = event.value\n const inRange = value >= 0 && value < ctx.count\n if (!inRange) throw new RangeError(`Index ${value} is out of bounds`)\n set.value(ctx, value)\n },\n },\n },\n )\n}\n\nconst set = {\n value(ctx: MachineContext, step: number) {\n if (isEqual(ctx.step, step)) return\n ctx.step = step\n ctx.onStepChange?.({ step })\n },\n}\n","import { createProps } from \"@zag-js/types\"\nimport { createSplitProps } from \"@zag-js/utils\"\nimport type { UserDefinedContext } from \"./steps.types\"\n\nexport const props = createProps<UserDefinedContext>()([\n \"count\",\n \"dir\",\n \"getRootNode\",\n \"id\",\n \"ids\",\n \"onStepChange\",\n \"onStepComplete\",\n \"orientation\",\n \"skippable\",\n \"step\",\n])\n\nexport const splitProps = create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