@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/dist/index.mjs ADDED
@@ -0,0 +1,284 @@
1
+ // src/steps.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("steps").parts(
4
+ "root",
5
+ "list",
6
+ "item",
7
+ "trigger",
8
+ "indicator",
9
+ "separator",
10
+ "content",
11
+ "title",
12
+ "description",
13
+ "nextTrigger",
14
+ "prevTrigger",
15
+ "progress"
16
+ );
17
+ var parts = anatomy.build();
18
+
19
+ // src/steps.dom.ts
20
+ import { createScope } from "@zag-js/dom-query";
21
+ var dom = createScope({
22
+ getRootId: (ctx) => ctx.ids?.root ?? `steps:${ctx.id}`,
23
+ getListId: (ctx) => ctx.ids?.list ?? `steps:${ctx.id}:list`,
24
+ getTriggerId: (ctx, index) => ctx.ids?.triggerId?.(index) ?? `steps:${ctx.id}:trigger:${index}`,
25
+ getContentId: (ctx, index) => ctx.ids?.contentId?.(index) ?? `steps:${ctx.id}:content:${index}`
26
+ });
27
+
28
+ // src/steps.connect.ts
29
+ import { dataAttr } from "@zag-js/dom-query";
30
+ function connect(state, send, normalize) {
31
+ const value = state.context.step;
32
+ const count = state.context.count;
33
+ const percent = state.context.percent;
34
+ const hasNextStep = state.context.hasNextStep;
35
+ const hasPrevStep = state.context.hasPrevStep;
36
+ const getItemState = (props2) => ({
37
+ triggerId: dom.getTriggerId(state.context, props2.index),
38
+ contentId: dom.getContentId(state.context, props2.index),
39
+ current: props2.index === value,
40
+ completed: props2.index < value,
41
+ index: props2.index,
42
+ first: props2.index === 0,
43
+ last: props2.index === count - 1
44
+ });
45
+ const goToNextStep = () => {
46
+ send({ type: "STEP.NEXT", src: "next.trigger.click" });
47
+ };
48
+ const goToPrevStep = () => {
49
+ send({ type: "STEP.PREV", src: "prev.trigger.click" });
50
+ };
51
+ const resetStep = () => {
52
+ send({ type: "STEP.RESET", src: "reset.trigger.click" });
53
+ };
54
+ const setValue = (value2) => {
55
+ send({ type: "STEP.SET", value: value2, src: "api.setValue" });
56
+ };
57
+ return {
58
+ value,
59
+ count,
60
+ percent,
61
+ hasNextStep,
62
+ hasPrevStep,
63
+ goToNextStep,
64
+ goToPrevStep,
65
+ resetStep,
66
+ getItemState,
67
+ setValue,
68
+ getRootProps() {
69
+ return normalize.element({
70
+ ...parts.root.attrs,
71
+ id: dom.getRootId(state.context),
72
+ dir: state.context.dir,
73
+ "data-orientation": state.context.orientation,
74
+ style: {
75
+ "--percent": `${percent}%`
76
+ }
77
+ });
78
+ },
79
+ getListProps() {
80
+ return normalize.element({
81
+ ...parts.list.attrs,
82
+ dir: state.context.dir,
83
+ id: dom.getListId(state.context),
84
+ role: "tablist",
85
+ "aria-orientation": state.context.orientation,
86
+ "data-orientation": state.context.orientation
87
+ });
88
+ },
89
+ getItemProps(props2) {
90
+ const itemState = getItemState(props2);
91
+ return normalize.element({
92
+ ...parts.item.attrs,
93
+ dir: state.context.dir,
94
+ "aria-current": itemState.current ? "step" : void 0,
95
+ "data-orientation": state.context.orientation
96
+ });
97
+ },
98
+ getTriggerProps(props2) {
99
+ const itemState = getItemState(props2);
100
+ return normalize.button({
101
+ ...parts.trigger.attrs,
102
+ id: itemState.triggerId,
103
+ role: "tab",
104
+ dir: state.context.dir,
105
+ tabIndex: state.context.skippable || itemState.current ? 0 : -1,
106
+ "aria-selected": itemState.current,
107
+ "aria-controls": itemState.contentId,
108
+ "data-state": itemState.current ? "open" : "closed",
109
+ "data-orientation": state.context.orientation,
110
+ "data-complete": dataAttr(itemState.completed),
111
+ "data-current": dataAttr(itemState.current),
112
+ "data-incomplete": dataAttr(!itemState.current),
113
+ onClick(event) {
114
+ if (event.defaultPrevented) return;
115
+ if (!state.context.skippable) return;
116
+ send({ type: "STEP.SET", value: props2.index, src: "trigger.click" });
117
+ }
118
+ });
119
+ },
120
+ getContentProps(props2) {
121
+ const itemState = getItemState(props2);
122
+ return normalize.element({
123
+ ...parts.content.attrs,
124
+ dir: state.context.dir,
125
+ id: itemState.contentId,
126
+ role: "tabpanel",
127
+ tabIndex: 0,
128
+ hidden: !itemState.current,
129
+ "data-state": itemState.current ? "open" : "closed",
130
+ "data-orientation": state.context.orientation,
131
+ "aria-labelledby": itemState.triggerId
132
+ });
133
+ },
134
+ getIndicatorProps(props2) {
135
+ const itemState = getItemState(props2);
136
+ return normalize.element({
137
+ ...parts.indicator.attrs,
138
+ dir: state.context.dir,
139
+ "aria-hidden": true,
140
+ "data-complete": dataAttr(itemState.completed),
141
+ "data-current": dataAttr(itemState.current),
142
+ "data-incomplete": dataAttr(!itemState.current)
143
+ });
144
+ },
145
+ getSeparatorProps(props2) {
146
+ const itemState = getItemState(props2);
147
+ return normalize.element({
148
+ ...parts.separator.attrs,
149
+ dir: state.context.dir,
150
+ "data-orientation": state.context.orientation,
151
+ "data-complete": dataAttr(itemState.completed),
152
+ "data-current": dataAttr(itemState.current),
153
+ "data-incomplete": dataAttr(!itemState.current)
154
+ });
155
+ },
156
+ getNextTriggerProps() {
157
+ return normalize.button({
158
+ ...parts.nextTrigger.attrs,
159
+ dir: state.context.dir,
160
+ type: "button",
161
+ disabled: !hasNextStep,
162
+ onClick(event) {
163
+ if (event.defaultPrevented) return;
164
+ goToNextStep();
165
+ }
166
+ });
167
+ },
168
+ getPrevTriggerProps() {
169
+ return normalize.button({
170
+ dir: state.context.dir,
171
+ ...parts.prevTrigger.attrs,
172
+ type: "button",
173
+ disabled: !hasPrevStep,
174
+ onClick(event) {
175
+ if (event.defaultPrevented) return;
176
+ goToPrevStep();
177
+ }
178
+ });
179
+ },
180
+ getProgressProps() {
181
+ return normalize.element({
182
+ dir: state.context.dir,
183
+ ...parts.progress.attrs,
184
+ role: "progressbar",
185
+ "aria-valuenow": percent,
186
+ "aria-valuemin": 0,
187
+ "aria-valuemax": 100,
188
+ "aria-valuetext": `${percent}% complete`,
189
+ "data-complete": dataAttr(percent === 100)
190
+ });
191
+ }
192
+ };
193
+ }
194
+
195
+ // src/steps.machine.ts
196
+ import { createMachine } from "@zag-js/core";
197
+ import { compact, isEqual } from "@zag-js/utils";
198
+ function machine(userContext) {
199
+ const ctx = compact(userContext);
200
+ return createMachine(
201
+ {
202
+ id: "steps",
203
+ initial: "idle",
204
+ context: {
205
+ step: 0,
206
+ count: 1,
207
+ ...ctx
208
+ },
209
+ computed: {
210
+ percent: (ctx2) => ctx2.step / ctx2.count * 100,
211
+ hasNextStep: (ctx2) => ctx2.step < ctx2.count,
212
+ hasPrevStep: (ctx2) => ctx2.step > 0
213
+ },
214
+ states: {
215
+ idle: {
216
+ on: {
217
+ "STEP.NEXT": {
218
+ actions: "goToNextStep"
219
+ },
220
+ "STEP.PREV": {
221
+ actions: "goToPrevStep"
222
+ },
223
+ "STEP.RESET": {
224
+ actions: "resetStep"
225
+ }
226
+ }
227
+ }
228
+ }
229
+ },
230
+ {
231
+ actions: {
232
+ goToNextStep(ctx2) {
233
+ const value = Math.min(ctx2.step + 1, ctx2.count);
234
+ set.value(ctx2, value);
235
+ },
236
+ goToPrevStep(ctx2) {
237
+ const value = Math.max(ctx2.step - 1, 0);
238
+ set.value(ctx2, value);
239
+ },
240
+ resetStep(ctx2) {
241
+ set.value(ctx2, 0);
242
+ },
243
+ setStep(ctx2, event) {
244
+ const value = event.value;
245
+ const inRange = value >= 0 && value < ctx2.count;
246
+ if (!inRange) throw new RangeError(`Index ${value} is out of bounds`);
247
+ set.value(ctx2, value);
248
+ }
249
+ }
250
+ }
251
+ );
252
+ }
253
+ var set = {
254
+ value(ctx, step) {
255
+ if (isEqual(ctx.step, step)) return;
256
+ ctx.step = step;
257
+ ctx.onStepChange?.({ step });
258
+ }
259
+ };
260
+
261
+ // src/steps.props.ts
262
+ import { createProps } from "@zag-js/types";
263
+ import { createSplitProps } from "@zag-js/utils";
264
+ var props = createProps()([
265
+ "count",
266
+ "dir",
267
+ "getRootNode",
268
+ "id",
269
+ "ids",
270
+ "onStepChange",
271
+ "onStepComplete",
272
+ "orientation",
273
+ "skippable",
274
+ "step"
275
+ ]);
276
+ var splitProps = createSplitProps(props);
277
+ export {
278
+ anatomy,
279
+ connect,
280
+ machine,
281
+ props,
282
+ splitProps
283
+ };
284
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/steps.anatomy.ts","../src/steps.dom.ts","../src/steps.connect.ts","../src/steps.machine.ts","../src/steps.props.ts"],"sourcesContent":["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 = createSplitProps<Partial<UserDefinedContext>>(props)\n"],"mappings":";AAAA,SAAS,qBAAqB;AAEvB,IAAM,UAAU,cAAc,OAAO,EAAE;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA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A,SAAS,mBAAmB;AAC5B,SAAS,wBAAwB;AAG1B,IAAM,QAAQ,YAAgC,EAAE;AAAA,EACrD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAEM,IAAM,aAAa,iBAA8C,KAAK;","names":["props","value","ctx"]}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@zag-js/steps",
3
+ "version": "0.0.0-dev-20240630000246",
4
+ "description": "Core logic for the steps widget implemented as a state machine",
5
+ "keywords": [
6
+ "js",
7
+ "machine",
8
+ "xstate",
9
+ "statechart",
10
+ "component",
11
+ "chakra-ui",
12
+ "steps"
13
+ ],
14
+ "author": "Segun Adebayo <sage@adebayosegun.com>",
15
+ "homepage": "https://github.com/chakra-ui/zag#readme",
16
+ "license": "MIT",
17
+ "main": "dist/index.js",
18
+ "repository": "https://github.com/chakra-ui/zag/tree/main/packages/steps",
19
+ "sideEffects": false,
20
+ "files": [
21
+ "dist",
22
+ "src"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/chakra-ui/zag/issues"
29
+ },
30
+ "dependencies": {
31
+ "@zag-js/anatomy": "0.0.0-dev-20240630000246",
32
+ "@zag-js/core": "0.0.0-dev-20240630000246",
33
+ "@zag-js/dom-query": "0.0.0-dev-20240630000246",
34
+ "@zag-js/utils": "0.0.0-dev-20240630000246",
35
+ "@zag-js/types": "0.0.0-dev-20240630000246"
36
+ },
37
+ "devDependencies": {
38
+ "clean-package": "2.2.0"
39
+ },
40
+ "clean-package": "../../../clean-package.config.json",
41
+ "module": "dist/index.mjs",
42
+ "types": "dist/index.d.ts",
43
+ "exports": {
44
+ ".": {
45
+ "types": "./dist/index.d.ts",
46
+ "import": "./dist/index.mjs",
47
+ "require": "./dist/index.js"
48
+ },
49
+ "./package.json": "./package.json"
50
+ },
51
+ "scripts": {
52
+ "build": "tsup",
53
+ "lint": "eslint src --ext .ts,.tsx",
54
+ "typecheck": "tsc --noEmit"
55
+ }
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export { anatomy } from "./steps.anatomy"
2
+ export { connect } from "./steps.connect"
3
+ export { machine } from "./steps.machine"
4
+ export * from "./steps.props"
5
+ export type {
6
+ MachineApi as Api,
7
+ UserDefinedContext as Context,
8
+ ElementIds,
9
+ ItemProps,
10
+ ItemState,
11
+ Service,
12
+ StepChangeDetails,
13
+ } from "./steps.types"
@@ -0,0 +1,18 @@
1
+ import { createAnatomy } from "@zag-js/anatomy"
2
+
3
+ export const anatomy = createAnatomy("steps").parts(
4
+ "root",
5
+ "list",
6
+ "item",
7
+ "trigger",
8
+ "indicator",
9
+ "separator",
10
+ "content",
11
+ "title",
12
+ "description",
13
+ "nextTrigger",
14
+ "prevTrigger",
15
+ "progress",
16
+ )
17
+
18
+ export const parts = anatomy.build()
@@ -0,0 +1,186 @@
1
+ import type { NormalizeProps, PropTypes } from "@zag-js/types"
2
+ import type { State, Send, ItemProps, ItemState, MachineApi } from "./steps.types"
3
+ import { parts } from "./steps.anatomy"
4
+ import { dom } from "./steps.dom"
5
+ import { dataAttr } from "@zag-js/dom-query"
6
+
7
+ export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
8
+ const value = state.context.step
9
+ const count = state.context.count
10
+ const percent = state.context.percent
11
+ const hasNextStep = state.context.hasNextStep
12
+ const hasPrevStep = state.context.hasPrevStep
13
+
14
+ const getItemState = (props: ItemProps): ItemState => ({
15
+ triggerId: dom.getTriggerId(state.context, props.index),
16
+ contentId: dom.getContentId(state.context, props.index),
17
+ current: props.index === value,
18
+ completed: props.index < value,
19
+ index: props.index,
20
+ first: props.index === 0,
21
+ last: props.index === count - 1,
22
+ })
23
+
24
+ const goToNextStep = () => {
25
+ send({ type: "STEP.NEXT", src: "next.trigger.click" })
26
+ }
27
+
28
+ const goToPrevStep = () => {
29
+ send({ type: "STEP.PREV", src: "prev.trigger.click" })
30
+ }
31
+
32
+ const resetStep = () => {
33
+ send({ type: "STEP.RESET", src: "reset.trigger.click" })
34
+ }
35
+
36
+ const setValue = (value: number) => {
37
+ send({ type: "STEP.SET", value, src: "api.setValue" })
38
+ }
39
+
40
+ return {
41
+ value,
42
+ count,
43
+ percent,
44
+ hasNextStep,
45
+ hasPrevStep,
46
+ goToNextStep,
47
+ goToPrevStep,
48
+ resetStep,
49
+ getItemState,
50
+ setValue,
51
+
52
+ getRootProps() {
53
+ return normalize.element({
54
+ ...parts.root.attrs,
55
+ id: dom.getRootId(state.context),
56
+ dir: state.context.dir,
57
+ "data-orientation": state.context.orientation,
58
+ style: {
59
+ "--percent": `${percent}%`,
60
+ },
61
+ })
62
+ },
63
+
64
+ getListProps() {
65
+ return normalize.element({
66
+ ...parts.list.attrs,
67
+ dir: state.context.dir,
68
+ id: dom.getListId(state.context),
69
+ role: "tablist",
70
+ "aria-orientation": state.context.orientation,
71
+ "data-orientation": state.context.orientation,
72
+ })
73
+ },
74
+
75
+ getItemProps(props) {
76
+ const itemState = getItemState(props)
77
+ return normalize.element({
78
+ ...parts.item.attrs,
79
+ dir: state.context.dir,
80
+ "aria-current": itemState.current ? "step" : undefined,
81
+ "data-orientation": state.context.orientation,
82
+ })
83
+ },
84
+
85
+ getTriggerProps(props) {
86
+ const itemState = getItemState(props)
87
+ return normalize.button({
88
+ ...parts.trigger.attrs,
89
+ id: itemState.triggerId,
90
+ role: "tab",
91
+ dir: state.context.dir,
92
+ tabIndex: state.context.skippable || itemState.current ? 0 : -1,
93
+ "aria-selected": itemState.current,
94
+ "aria-controls": itemState.contentId,
95
+ "data-state": itemState.current ? "open" : "closed",
96
+ "data-orientation": state.context.orientation,
97
+ "data-complete": dataAttr(itemState.completed),
98
+ "data-current": dataAttr(itemState.current),
99
+ "data-incomplete": dataAttr(!itemState.current),
100
+ onClick(event) {
101
+ if (event.defaultPrevented) return
102
+ if (!state.context.skippable) return
103
+ send({ type: "STEP.SET", value: props.index, src: "trigger.click" })
104
+ },
105
+ })
106
+ },
107
+
108
+ getContentProps(props) {
109
+ const itemState = getItemState(props)
110
+ return normalize.element({
111
+ ...parts.content.attrs,
112
+ dir: state.context.dir,
113
+ id: itemState.contentId,
114
+ role: "tabpanel",
115
+ tabIndex: 0,
116
+ hidden: !itemState.current,
117
+ "data-state": itemState.current ? "open" : "closed",
118
+ "data-orientation": state.context.orientation,
119
+ "aria-labelledby": itemState.triggerId,
120
+ })
121
+ },
122
+
123
+ getIndicatorProps(props) {
124
+ const itemState = getItemState(props)
125
+ return normalize.element({
126
+ ...parts.indicator.attrs,
127
+ dir: state.context.dir,
128
+ "aria-hidden": true,
129
+ "data-complete": dataAttr(itemState.completed),
130
+ "data-current": dataAttr(itemState.current),
131
+ "data-incomplete": dataAttr(!itemState.current),
132
+ })
133
+ },
134
+
135
+ getSeparatorProps(props) {
136
+ const itemState = getItemState(props)
137
+ return normalize.element({
138
+ ...parts.separator.attrs,
139
+ dir: state.context.dir,
140
+ "data-orientation": state.context.orientation,
141
+ "data-complete": dataAttr(itemState.completed),
142
+ "data-current": dataAttr(itemState.current),
143
+ "data-incomplete": dataAttr(!itemState.current),
144
+ })
145
+ },
146
+
147
+ getNextTriggerProps() {
148
+ return normalize.button({
149
+ ...parts.nextTrigger.attrs,
150
+ dir: state.context.dir,
151
+ type: "button",
152
+ disabled: !hasNextStep,
153
+ onClick(event) {
154
+ if (event.defaultPrevented) return
155
+ goToNextStep()
156
+ },
157
+ })
158
+ },
159
+
160
+ getPrevTriggerProps() {
161
+ return normalize.button({
162
+ dir: state.context.dir,
163
+ ...parts.prevTrigger.attrs,
164
+ type: "button",
165
+ disabled: !hasPrevStep,
166
+ onClick(event) {
167
+ if (event.defaultPrevented) return
168
+ goToPrevStep()
169
+ },
170
+ })
171
+ },
172
+
173
+ getProgressProps() {
174
+ return normalize.element({
175
+ dir: state.context.dir,
176
+ ...parts.progress.attrs,
177
+ role: "progressbar",
178
+ "aria-valuenow": percent,
179
+ "aria-valuemin": 0,
180
+ "aria-valuemax": 100,
181
+ "aria-valuetext": `${percent}% complete`,
182
+ "data-complete": dataAttr(percent === 100),
183
+ })
184
+ },
185
+ }
186
+ }
@@ -0,0 +1,9 @@
1
+ import { createScope } from "@zag-js/dom-query"
2
+ import type { MachineContext as Ctx } from "./steps.types"
3
+
4
+ export const dom = createScope({
5
+ getRootId: (ctx: Ctx) => ctx.ids?.root ?? `steps:${ctx.id}`,
6
+ getListId: (ctx: Ctx) => ctx.ids?.list ?? `steps:${ctx.id}:list`,
7
+ getTriggerId: (ctx: Ctx, index: number) => ctx.ids?.triggerId?.(index) ?? `steps:${ctx.id}:trigger:${index}`,
8
+ getContentId: (ctx: Ctx, index: number) => ctx.ids?.contentId?.(index) ?? `steps:${ctx.id}:content:${index}`,
9
+ })
@@ -0,0 +1,70 @@
1
+ import { createMachine } from "@zag-js/core"
2
+ import { compact, isEqual } from "@zag-js/utils"
3
+ import type { MachineContext, MachineState, UserDefinedContext } from "./steps.types"
4
+
5
+ export function machine(userContext: UserDefinedContext) {
6
+ const ctx = compact(userContext)
7
+ return createMachine<MachineContext, MachineState>(
8
+ {
9
+ id: "steps",
10
+ initial: "idle",
11
+
12
+ context: {
13
+ step: 0,
14
+ count: 1,
15
+ ...ctx,
16
+ },
17
+
18
+ computed: {
19
+ percent: (ctx) => (ctx.step / ctx.count) * 100,
20
+ hasNextStep: (ctx) => ctx.step < ctx.count,
21
+ hasPrevStep: (ctx) => ctx.step > 0,
22
+ },
23
+
24
+ states: {
25
+ idle: {
26
+ on: {
27
+ "STEP.NEXT": {
28
+ actions: "goToNextStep",
29
+ },
30
+ "STEP.PREV": {
31
+ actions: "goToPrevStep",
32
+ },
33
+ "STEP.RESET": {
34
+ actions: "resetStep",
35
+ },
36
+ },
37
+ },
38
+ },
39
+ },
40
+ {
41
+ actions: {
42
+ goToNextStep(ctx) {
43
+ const value = Math.min(ctx.step + 1, ctx.count)
44
+ set.value(ctx, value)
45
+ },
46
+ goToPrevStep(ctx) {
47
+ const value = Math.max(ctx.step - 1, 0)
48
+ set.value(ctx, value)
49
+ },
50
+ resetStep(ctx) {
51
+ set.value(ctx, 0)
52
+ },
53
+ setStep(ctx, event) {
54
+ const value = event.value
55
+ const inRange = value >= 0 && value < ctx.count
56
+ if (!inRange) throw new RangeError(`Index ${value} is out of bounds`)
57
+ set.value(ctx, value)
58
+ },
59
+ },
60
+ },
61
+ )
62
+ }
63
+
64
+ const set = {
65
+ value(ctx: MachineContext, step: number) {
66
+ if (isEqual(ctx.step, step)) return
67
+ ctx.step = step
68
+ ctx.onStepChange?.({ step })
69
+ },
70
+ }
@@ -0,0 +1,18 @@
1
+ import { createProps } from "@zag-js/types"
2
+ import { createSplitProps } from "@zag-js/utils"
3
+ import type { UserDefinedContext } from "./steps.types"
4
+
5
+ export const props = createProps<UserDefinedContext>()([
6
+ "count",
7
+ "dir",
8
+ "getRootNode",
9
+ "id",
10
+ "ids",
11
+ "onStepChange",
12
+ "onStepComplete",
13
+ "orientation",
14
+ "skippable",
15
+ "step",
16
+ ])
17
+
18
+ export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)