@zag-js/radio-group 0.1.6 → 0.1.7

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,317 @@
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/radio-group.machine.ts
21
+ var radio_group_machine_exports = {};
22
+ __export(radio_group_machine_exports, {
23
+ machine: () => machine
24
+ });
25
+ module.exports = __toCommonJS(radio_group_machine_exports);
26
+ var import_core = require("@zag-js/core");
27
+
28
+ // ../../utilities/core/src/guard.ts
29
+ var isArray = (v) => Array.isArray(v);
30
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
31
+
32
+ // ../../utilities/core/src/object.ts
33
+ function compact(obj) {
34
+ if (obj === void 0)
35
+ return obj;
36
+ return Object.fromEntries(
37
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
38
+ );
39
+ }
40
+
41
+ // ../../utilities/dom/src/query.ts
42
+ function isDocument(el) {
43
+ return el.nodeType === Node.DOCUMENT_NODE;
44
+ }
45
+ function isWindow(value) {
46
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
47
+ }
48
+ function getDocument(el) {
49
+ var _a;
50
+ if (isWindow(el))
51
+ return el.document;
52
+ if (isDocument(el))
53
+ return el;
54
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
55
+ }
56
+ function getWindow(el) {
57
+ var _a;
58
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
59
+ }
60
+ function defineDomHelpers(helpers) {
61
+ const dom2 = {
62
+ getRootNode: (ctx) => {
63
+ var _a, _b;
64
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
65
+ },
66
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
67
+ getWin: (ctx) => {
68
+ var _a;
69
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
70
+ },
71
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
72
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
73
+ createEmitter: (ctx, target) => {
74
+ const win = dom2.getWin(ctx);
75
+ return function emit(evt, detail, options) {
76
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
77
+ const eventName = `zag:${evt}`;
78
+ const init = { bubbles, cancelable, composed, detail };
79
+ const event = new win.CustomEvent(eventName, init);
80
+ target.dispatchEvent(event);
81
+ };
82
+ },
83
+ createListener: (target) => {
84
+ return function listen(evt, handler) {
85
+ const eventName = `zag:${evt}`;
86
+ const listener = (e) => handler(e);
87
+ target.addEventListener(eventName, listener);
88
+ return () => target.removeEventListener(eventName, listener);
89
+ };
90
+ }
91
+ };
92
+ return {
93
+ ...dom2,
94
+ ...helpers
95
+ };
96
+ }
97
+
98
+ // ../../utilities/dom/src/mutation-observer.ts
99
+ function observeAttributes(node, attributes, fn) {
100
+ if (!node)
101
+ return;
102
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
103
+ const win = node.ownerDocument.defaultView || window;
104
+ const obs = new win.MutationObserver((changes) => {
105
+ for (const change of changes) {
106
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
107
+ fn(change);
108
+ }
109
+ }
110
+ });
111
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
112
+ return () => obs.disconnect();
113
+ }
114
+
115
+ // ../../utilities/dom/src/nodelist.ts
116
+ function queryAll(root, selector) {
117
+ var _a;
118
+ return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
119
+ }
120
+
121
+ // ../../utilities/form-utils/src/input-event.ts
122
+ function getDescriptor(el, options) {
123
+ var _a;
124
+ const { type, property = "value" } = options;
125
+ const proto = getWindow(el)[type].prototype;
126
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
127
+ }
128
+ function dispatchInputCheckedEvent(el, checked) {
129
+ var _a;
130
+ if (!el)
131
+ return;
132
+ const win = getWindow(el);
133
+ if (!(el instanceof win.HTMLInputElement))
134
+ return;
135
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "checked" });
136
+ (_a = desc.set) == null ? void 0 : _a.call(el, checked);
137
+ const event = new win.Event("click", { bubbles: true });
138
+ el.dispatchEvent(event);
139
+ }
140
+
141
+ // ../../utilities/form-utils/src/form.ts
142
+ function getClosestForm(el) {
143
+ if (isFormElement(el))
144
+ return el.form;
145
+ else
146
+ return el.closest("form");
147
+ }
148
+ function isFormElement(el) {
149
+ return el.matches("textarea, input, select, button");
150
+ }
151
+ function trackFormReset(el, callback) {
152
+ if (!el)
153
+ return;
154
+ const form = getClosestForm(el);
155
+ form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
156
+ return () => {
157
+ form == null ? void 0 : form.removeEventListener("reset", callback);
158
+ };
159
+ }
160
+ function trackFieldsetDisabled(el, callback) {
161
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
162
+ if (!fieldset)
163
+ return;
164
+ callback(fieldset.disabled);
165
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
166
+ }
167
+ function trackFormControl(el, options) {
168
+ if (!el)
169
+ return;
170
+ const { onFieldsetDisabled, onFormReset } = options;
171
+ const cleanups = [
172
+ trackFormReset(el, onFormReset),
173
+ trackFieldsetDisabled(el, (disabled) => {
174
+ if (disabled)
175
+ onFieldsetDisabled();
176
+ })
177
+ ];
178
+ return () => {
179
+ cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
180
+ };
181
+ }
182
+
183
+ // src/radio-group.dom.ts
184
+ var dom = defineDomHelpers({
185
+ getRootId: (ctx) => {
186
+ var _a, _b;
187
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `radio-group:${ctx.id}`;
188
+ },
189
+ getLabelId: (ctx) => {
190
+ var _a, _b;
191
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `radio-group:${ctx.id}:label`;
192
+ },
193
+ getRadioId: (ctx, value) => {
194
+ var _a, _b, _c;
195
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.radio) == null ? void 0 : _b.call(_a, value)) != null ? _c : `radio-group:${ctx.id}:radio:${value}`;
196
+ },
197
+ getRadioInputId: (ctx, value) => {
198
+ var _a, _b, _c;
199
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.radioInput) == null ? void 0 : _b.call(_a, value)) != null ? _c : `radio-group:${ctx.id}:radio:input:${value}`;
200
+ },
201
+ getRadioControlId: (ctx, value) => {
202
+ var _a, _b, _c;
203
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.radioControl) == null ? void 0 : _b.call(_a, value)) != null ? _c : `radio-group:${ctx.id}:radio:control:${value}`;
204
+ },
205
+ getRadioLabelId: (ctx, value) => {
206
+ var _a, _b, _c;
207
+ return (_c = (_b = (_a = ctx.ids) == null ? void 0 : _a.radioLabel) == null ? void 0 : _b.call(_a, value)) != null ? _c : `radio-group:${ctx.id}:radio:label:${value}`;
208
+ },
209
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
210
+ getRadioInputEl: (ctx, value) => dom.getById(ctx, dom.getRadioInputId(ctx, value)),
211
+ getFirstEnabledInputEl: (ctx) => {
212
+ var _a;
213
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled)");
214
+ },
215
+ getFirstEnabledAndCheckedInputEl: (ctx) => {
216
+ var _a;
217
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled):checked");
218
+ },
219
+ getInputEls: (ctx) => {
220
+ const ownerId = CSS.escape(dom.getRootId(ctx));
221
+ const selector = `input[type=radio][data-ownedby='${ownerId}']:not([disabled])`;
222
+ return queryAll(dom.getRootEl(ctx), selector);
223
+ }
224
+ });
225
+
226
+ // src/radio-group.machine.ts
227
+ function machine(userContext) {
228
+ const ctx = compact(userContext);
229
+ return (0, import_core.createMachine)(
230
+ {
231
+ id: "radio",
232
+ initial: "unknown",
233
+ context: {
234
+ value: null,
235
+ initialValue: null,
236
+ activeId: null,
237
+ focusedId: null,
238
+ hoveredId: null,
239
+ ...ctx
240
+ },
241
+ activities: ["trackFormControlState"],
242
+ on: {
243
+ SET_VALUE: {
244
+ actions: ["setValue"]
245
+ },
246
+ SET_HOVERED: {
247
+ actions: "setHovered"
248
+ },
249
+ SET_ACTIVE: {
250
+ actions: "setActive"
251
+ },
252
+ SET_FOCUSED: {
253
+ actions: "setFocused"
254
+ }
255
+ },
256
+ watch: {
257
+ value: ["dispatchChangeEvent", "invokeOnChange"]
258
+ },
259
+ states: {
260
+ unknown: {
261
+ on: {
262
+ SETUP: {
263
+ actions: "checkValue"
264
+ }
265
+ }
266
+ }
267
+ }
268
+ },
269
+ {
270
+ activities: {
271
+ trackFormControlState(ctx2, _evt, { send }) {
272
+ return trackFormControl(dom.getRootEl(ctx2), {
273
+ onFieldsetDisabled() {
274
+ ctx2.disabled = true;
275
+ },
276
+ onFormReset() {
277
+ send({ type: "SET_VALUE", value: ctx2.initialValue });
278
+ }
279
+ });
280
+ }
281
+ },
282
+ actions: {
283
+ checkValue(ctx2) {
284
+ ctx2.initialValue = ctx2.value;
285
+ },
286
+ setValue(ctx2, evt) {
287
+ ctx2.value = evt.value;
288
+ },
289
+ setHovered(ctx2, evt) {
290
+ ctx2.hoveredId = evt.value;
291
+ },
292
+ setActive(ctx2, evt) {
293
+ ctx2.activeId = evt.value;
294
+ },
295
+ setFocused(ctx2, evt) {
296
+ ctx2.focusedId = evt.value;
297
+ },
298
+ invokeOnChange(ctx2, evt) {
299
+ var _a;
300
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: evt.value });
301
+ },
302
+ dispatchChangeEvent(ctx2, evt) {
303
+ if (!evt.manual)
304
+ return;
305
+ const el = dom.getRadioInputEl(ctx2, evt.value);
306
+ if (!el)
307
+ return;
308
+ dispatchInputCheckedEvent(el, !!evt.value);
309
+ }
310
+ }
311
+ }
312
+ );
313
+ }
314
+ // Annotate the CommonJS export names for ESM import in node:
315
+ 0 && (module.exports = {
316
+ machine
317
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ machine
3
+ } from "./chunk-EQKWEHJW.mjs";
4
+ import "./chunk-JSIZLP57.mjs";
5
+ export {
6
+ machine
7
+ };
@@ -0,0 +1,86 @@
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
+ label: string;
7
+ radio(value: string): string;
8
+ radioLabel(value: string): string;
9
+ radioControl(value: string): string;
10
+ radioInput(value: string): string;
11
+ }>;
12
+ type PublicContext = DirectionProperty & CommonProperties & {
13
+ /**
14
+ * The ids of the elements in the radio. Useful for composition.
15
+ */
16
+ ids?: ElementIds;
17
+ /**
18
+ * The value of the checked radio
19
+ */
20
+ value: string | null;
21
+ /**
22
+ * The name of the input fields in the radio
23
+ * (Useful for form submission).
24
+ */
25
+ name?: string;
26
+ /**
27
+ * The associate form of the underlying input.
28
+ */
29
+ form?: string;
30
+ /**
31
+ * If `true`, the radio group will be disabled
32
+ */
33
+ disabled?: boolean;
34
+ /**
35
+ * If `true`, the radio group will be readonly
36
+ */
37
+ readOnly?: boolean;
38
+ /**
39
+ * Function called once a radio is checked
40
+ * @param value the value of the checked radio
41
+ */
42
+ onChange?(details: {
43
+ value: string;
44
+ }): void;
45
+ /**
46
+ * Orientation of the radio group
47
+ */
48
+ orientation?: "horizontal" | "vertical";
49
+ };
50
+ type PrivateContext = Context<{}>;
51
+ type UserDefinedContext = RequiredBy<PublicContext, "id">;
52
+ type ComputedContext = Readonly<{}>;
53
+ type MachineContext = PublicContext & PrivateContext & ComputedContext;
54
+ type MachineState = {
55
+ value: "unknown";
56
+ };
57
+ type State = StateMachine.State<MachineContext, MachineState>;
58
+ type Send = StateMachine.Send<StateMachine.AnyEventObject>;
59
+ type RadioProps = {
60
+ value: string;
61
+ /**
62
+ * If `true`, the radio will be disabled
63
+ */
64
+ disabled?: boolean;
65
+ /**
66
+ * If `true`, the radio will be readonly
67
+ */
68
+ readOnly?: boolean;
69
+ /**
70
+ * If `true`, the radio is marked as invalid.
71
+ */
72
+ invalid?: boolean;
73
+ };
74
+ type InputProps = RadioProps & {
75
+ /**
76
+ * If `true` and `disabled` is passed, the radio will
77
+ * remain tabbable but not interactive
78
+ */
79
+ focusable?: boolean;
80
+ /**
81
+ * If `true`, the radio input is marked as required,
82
+ */
83
+ required?: boolean;
84
+ };
85
+
86
+ export { InputProps, MachineContext, MachineState, RadioProps, Send, 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/radio-group.types.ts
17
+ var radio_group_types_exports = {};
18
+ module.exports = __toCommonJS(radio_group_types_exports);
File without changes
package/package.json CHANGED
@@ -1,10 +1,7 @@
1
1
  {
2
2
  "name": "@zag-js/radio-group",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Core logic for the radio group 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",
@@ -30,19 +27,32 @@
30
27
  "url": "https://github.com/chakra-ui/zag/issues"
31
28
  },
32
29
  "dependencies": {
33
- "@zag-js/anatomy": "0.1.2",
34
- "@zag-js/core": "0.2.3",
35
- "@zag-js/types": "0.3.1"
30
+ "@zag-js/anatomy": "0.1.3",
31
+ "@zag-js/core": "0.2.4",
32
+ "@zag-js/types": "0.3.2"
36
33
  },
37
34
  "devDependencies": {
38
- "@zag-js/dom-utils": "0.2.1",
39
- "@zag-js/form-utils": "0.2.2",
40
- "@zag-js/utils": "0.3.1"
35
+ "clean-package": "2.2.0",
36
+ "@zag-js/dom-utils": "0.2.2",
37
+ "@zag-js/form-utils": "0.2.3",
38
+ "@zag-js/utils": "0.3.2"
39
+ },
40
+ "clean-package": "../../../clean-package.config.json",
41
+ "main": "dist/index.js",
42
+ "module": "dist/index.mjs",
43
+ "types": "dist/index.d.ts",
44
+ "exports": {
45
+ ".": {
46
+ "types": "./dist/index.d.ts",
47
+ "import": "./dist/index.mjs",
48
+ "require": "./dist/index.js"
49
+ },
50
+ "./package.json": "./package.json"
41
51
  },
42
52
  "scripts": {
43
- "build-fast": "tsup src/index.ts --format=esm,cjs",
53
+ "build-fast": "tsup src",
44
54
  "start": "pnpm build --watch",
45
- "build": "tsup src/index.ts --format=esm,cjs --dts",
55
+ "build": "tsup src --dts",
46
56
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
47
57
  "lint": "eslint src --ext .ts,.tsx",
48
58
  "test-ci": "pnpm test --ci --runInBand",