@zag-js/radio-group 0.0.0-dev-20221209151930

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.js ADDED
@@ -0,0 +1,510 @@
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
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
29
+ // src/radio-group.anatomy.ts
30
+ var import_anatomy = require("@zag-js/anatomy");
31
+ var anatomy = (0, import_anatomy.createAnatomy)("radio-group").parts(
32
+ "root",
33
+ "label",
34
+ "radio",
35
+ "radioLabel",
36
+ "radioControl",
37
+ "radioInput"
38
+ );
39
+ var parts = anatomy.build();
40
+
41
+ // ../../utilities/dom/dist/index.mjs
42
+ var dataAttr = (guard) => {
43
+ return guard ? "" : void 0;
44
+ };
45
+ var ariaAttr = (guard) => {
46
+ return guard ? "true" : void 0;
47
+ };
48
+ function isDocument(el) {
49
+ return el.nodeType === Node.DOCUMENT_NODE;
50
+ }
51
+ function isWindow(value) {
52
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
53
+ }
54
+ function getDocument(el) {
55
+ var _a;
56
+ if (isWindow(el))
57
+ return el.document;
58
+ if (isDocument(el))
59
+ return el;
60
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
61
+ }
62
+ function defineDomHelpers(helpers) {
63
+ const dom2 = {
64
+ getRootNode: (ctx) => {
65
+ var _a, _b;
66
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
67
+ },
68
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
69
+ getWin: (ctx) => {
70
+ var _a;
71
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
72
+ },
73
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
74
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
75
+ createEmitter: (ctx, target) => {
76
+ const win = dom2.getWin(ctx);
77
+ return function emit(evt, detail, options) {
78
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
79
+ const eventName = `zag:${evt}`;
80
+ const init = { bubbles, cancelable, composed, detail };
81
+ const event = new win.CustomEvent(eventName, init);
82
+ target.dispatchEvent(event);
83
+ };
84
+ },
85
+ createListener: (target) => {
86
+ return function listen(evt, handler) {
87
+ const eventName = `zag:${evt}`;
88
+ const listener = (e) => handler(e);
89
+ target.addEventListener(eventName, listener);
90
+ return () => target.removeEventListener(eventName, listener);
91
+ };
92
+ }
93
+ };
94
+ return {
95
+ ...dom2,
96
+ ...helpers
97
+ };
98
+ }
99
+ function queryAll(root, selector) {
100
+ var _a;
101
+ return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
102
+ }
103
+ var visuallyHiddenStyle = {
104
+ border: "0",
105
+ clip: "rect(0 0 0 0)",
106
+ height: "1px",
107
+ margin: "-1px",
108
+ overflow: "hidden",
109
+ padding: "0",
110
+ position: "absolute",
111
+ width: "1px",
112
+ whiteSpace: "nowrap",
113
+ wordWrap: "normal"
114
+ };
115
+
116
+ // src/radio-group.dom.ts
117
+ var dom = defineDomHelpers({
118
+ getRootId: (ctx) => {
119
+ var _a, _b;
120
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `radio-group:${ctx.id}`;
121
+ },
122
+ getLabelId: (ctx) => {
123
+ var _a, _b;
124
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `radio-group:${ctx.id}:label`;
125
+ },
126
+ getRadioId: (ctx, value) => {
127
+ var _a, _b, _c;
128
+ 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}`;
129
+ },
130
+ getRadioInputId: (ctx, value) => {
131
+ var _a, _b, _c;
132
+ 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}`;
133
+ },
134
+ getRadioControlId: (ctx, value) => {
135
+ var _a, _b, _c;
136
+ 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}`;
137
+ },
138
+ getRadioLabelId: (ctx, value) => {
139
+ var _a, _b, _c;
140
+ 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}`;
141
+ },
142
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
143
+ getRadioInputEl: (ctx, value) => dom.getById(ctx, dom.getRadioInputId(ctx, value)),
144
+ getFirstEnabledInputEl: (ctx) => {
145
+ var _a;
146
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled)");
147
+ },
148
+ getFirstEnabledAndCheckedInputEl: (ctx) => {
149
+ var _a;
150
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled):checked");
151
+ },
152
+ getInputEls: (ctx) => {
153
+ const ownerId = CSS.escape(dom.getRootId(ctx));
154
+ const selector = `input[type=radio][data-ownedby='${ownerId}']:not([disabled])`;
155
+ return queryAll(dom.getRootEl(ctx), selector);
156
+ }
157
+ });
158
+
159
+ // src/radio-group.connect.ts
160
+ function connect(state, send, normalize) {
161
+ const isGroupDisabled = state.context.disabled;
162
+ const isGroupReadOnly = state.context.readOnly;
163
+ function getRadioState(props) {
164
+ const radioState = {
165
+ isReadOnly: props.readOnly || isGroupReadOnly,
166
+ isInvalid: props.invalid,
167
+ isDisabled: props.disabled || isGroupDisabled,
168
+ isChecked: state.context.value === props.value,
169
+ isFocused: state.context.focusedId === props.value,
170
+ isHovered: state.context.hoveredId === props.value,
171
+ isActive: state.context.activeId === props.value
172
+ };
173
+ return {
174
+ ...radioState,
175
+ isInteractive: !(radioState.isReadOnly || radioState.isDisabled)
176
+ };
177
+ }
178
+ function getRadioDataSet(props) {
179
+ const radioState = getRadioState(props);
180
+ return {
181
+ "data-focus": dataAttr(radioState.isFocused),
182
+ "data-disabled": dataAttr(radioState.isDisabled),
183
+ "data-checked": dataAttr(radioState.isChecked),
184
+ "data-hover": dataAttr(radioState.isHovered),
185
+ "data-invalid": dataAttr(radioState.isInvalid),
186
+ "data-readonly": dataAttr(radioState.isReadOnly)
187
+ };
188
+ }
189
+ const focus = () => {
190
+ const firstEnabledAndCheckedInput = dom.getFirstEnabledAndCheckedInputEl(state.context);
191
+ if (firstEnabledAndCheckedInput) {
192
+ firstEnabledAndCheckedInput.focus();
193
+ return;
194
+ }
195
+ const firstEnabledInput = dom.getFirstEnabledInputEl(state.context);
196
+ firstEnabledInput == null ? void 0 : firstEnabledInput.focus();
197
+ };
198
+ return {
199
+ value: state.context.value,
200
+ setValue(value) {
201
+ send({ type: "SET_VALUE", value, manual: true });
202
+ },
203
+ focus,
204
+ blur() {
205
+ const focusedElement = dom.getActiveElement(state.context);
206
+ const inputEls = dom.getInputEls(state.context);
207
+ const radioInputIsFocused = inputEls.some((el) => el === focusedElement);
208
+ if (radioInputIsFocused)
209
+ focusedElement == null ? void 0 : focusedElement.blur();
210
+ },
211
+ rootProps: normalize.element({
212
+ ...parts.root.attrs,
213
+ role: "radiogroup",
214
+ id: dom.getRootId(state.context),
215
+ "data-orientation": state.context.orientation,
216
+ "aria-orientation": state.context.orientation,
217
+ dir: state.context.dir
218
+ }),
219
+ labelProps: normalize.element({
220
+ ...parts.label.attrs,
221
+ id: dom.getLabelId(state.context),
222
+ onClick: focus
223
+ }),
224
+ getRadioProps(props) {
225
+ const rootState = getRadioState(props);
226
+ return normalize.label({
227
+ ...parts.radio.attrs,
228
+ id: dom.getRadioId(state.context, props.value),
229
+ htmlFor: dom.getRadioInputId(state.context, props.value),
230
+ ...getRadioDataSet(props),
231
+ onPointerMove() {
232
+ if (!rootState.isInteractive)
233
+ return;
234
+ send({ type: "SET_HOVERED", value: props.value, hovered: true });
235
+ },
236
+ onPointerLeave() {
237
+ if (!rootState.isInteractive)
238
+ return;
239
+ send({ type: "SET_HOVERED", value: null });
240
+ },
241
+ onPointerDown(event) {
242
+ if (!rootState.isInteractive)
243
+ return;
244
+ if (rootState.isFocused)
245
+ event.preventDefault();
246
+ send({ type: "SET_ACTIVE", value: props.value, active: true });
247
+ },
248
+ onPointerUp() {
249
+ if (!rootState.isInteractive)
250
+ return;
251
+ send({ type: "SET_ACTIVE", value: null });
252
+ }
253
+ });
254
+ },
255
+ getRadioLabelProps(props) {
256
+ return normalize.element({
257
+ ...parts.radioLabel.attrs,
258
+ id: dom.getRadioLabelId(state.context, props.value),
259
+ ...getRadioDataSet(props)
260
+ });
261
+ },
262
+ getRadioControlProps(props) {
263
+ const controlState = getRadioState(props);
264
+ return normalize.element({
265
+ ...parts.radioControl.attrs,
266
+ id: dom.getRadioControlId(state.context, props.value),
267
+ "data-active": dataAttr(controlState.isActive),
268
+ "aria-hidden": true,
269
+ ...getRadioDataSet(props)
270
+ });
271
+ },
272
+ getRadioInputProps(props) {
273
+ const inputState = getRadioState(props);
274
+ const isRequired = props.required;
275
+ const trulyDisabled = inputState.isDisabled && !props.focusable;
276
+ return normalize.input({
277
+ ...parts.radioInput.attrs,
278
+ "data-ownedby": dom.getRootId(state.context),
279
+ id: dom.getRadioInputId(state.context, props.value),
280
+ type: "radio",
281
+ name: state.context.name || state.context.id,
282
+ form: state.context.form,
283
+ value: props.value,
284
+ onChange(event) {
285
+ if (inputState.isReadOnly || inputState.isDisabled) {
286
+ return;
287
+ }
288
+ if (event.target.checked) {
289
+ send({ type: "SET_VALUE", value: props.value });
290
+ }
291
+ },
292
+ onBlur() {
293
+ send({ type: "SET_FOCUSED", value: null });
294
+ },
295
+ onFocus() {
296
+ send({ type: "SET_FOCUSED", value: props.value, focused: true });
297
+ },
298
+ onKeyDown(event) {
299
+ if (event.key === " ") {
300
+ send({ type: "SET_ACTIVE", value: props.value, active: true });
301
+ }
302
+ },
303
+ onKeyUp(event) {
304
+ if (event.key === " ") {
305
+ send({ type: "SET_ACTIVE", value: null });
306
+ }
307
+ },
308
+ disabled: trulyDisabled,
309
+ required: isRequired,
310
+ defaultChecked: inputState.isChecked,
311
+ "data-disabled": dataAttr(inputState.isDisabled),
312
+ "aria-required": ariaAttr(isRequired),
313
+ "aria-invalid": ariaAttr(inputState.isInvalid),
314
+ readOnly: inputState.isReadOnly,
315
+ "data-readonly": dataAttr(inputState.isReadOnly),
316
+ "aria-disabled": ariaAttr(trulyDisabled),
317
+ "aria-checked": ariaAttr(inputState.isChecked),
318
+ style: visuallyHiddenStyle
319
+ });
320
+ }
321
+ };
322
+ }
323
+
324
+ // src/radio-group.machine.ts
325
+ var import_core = require("@zag-js/core");
326
+
327
+ // ../../utilities/form-utils/dist/index.mjs
328
+ function getWindow(el) {
329
+ var _a;
330
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
331
+ }
332
+ function observeAttributes(node, attributes, fn) {
333
+ if (!node)
334
+ return;
335
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
336
+ const win = node.ownerDocument.defaultView || window;
337
+ const obs = new win.MutationObserver((changes) => {
338
+ for (const change of changes) {
339
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
340
+ fn(change);
341
+ }
342
+ }
343
+ });
344
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
345
+ return () => obs.disconnect();
346
+ }
347
+ function getDescriptor(el, options) {
348
+ var _a;
349
+ const { type, property = "value" } = options;
350
+ const proto = getWindow(el)[type].prototype;
351
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
352
+ }
353
+ function dispatchInputCheckedEvent(el, checked) {
354
+ var _a;
355
+ if (!el)
356
+ return;
357
+ const win = getWindow(el);
358
+ if (!(el instanceof win.HTMLInputElement))
359
+ return;
360
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "checked" });
361
+ (_a = desc.set) == null ? void 0 : _a.call(el, checked);
362
+ const event = new win.Event("click", { bubbles: true });
363
+ el.dispatchEvent(event);
364
+ }
365
+ function getClosestForm(el) {
366
+ if (isFormElement(el))
367
+ return el.form;
368
+ else
369
+ return el.closest("form");
370
+ }
371
+ function isFormElement(el) {
372
+ return el.matches("textarea, input, select, button");
373
+ }
374
+ function trackFormReset(el, callback) {
375
+ if (!el)
376
+ return;
377
+ const form = getClosestForm(el);
378
+ form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
379
+ return () => {
380
+ form == null ? void 0 : form.removeEventListener("reset", callback);
381
+ };
382
+ }
383
+ function trackFieldsetDisabled(el, callback) {
384
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
385
+ if (!fieldset)
386
+ return;
387
+ callback(fieldset.disabled);
388
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
389
+ }
390
+ function trackFormControl(el, options) {
391
+ if (!el)
392
+ return;
393
+ const { onFieldsetDisabled, onFormReset } = options;
394
+ const cleanups = [
395
+ trackFormReset(el, onFormReset),
396
+ trackFieldsetDisabled(el, (disabled) => {
397
+ if (disabled)
398
+ onFieldsetDisabled();
399
+ })
400
+ ];
401
+ return () => {
402
+ cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
403
+ };
404
+ }
405
+
406
+ // ../../utilities/core/dist/index.mjs
407
+ var isArray = (v) => Array.isArray(v);
408
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
409
+ function compact(obj) {
410
+ if (obj === void 0)
411
+ return obj;
412
+ return Object.fromEntries(
413
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
414
+ );
415
+ }
416
+
417
+ // src/radio-group.machine.ts
418
+ function machine(userContext) {
419
+ const ctx = compact(userContext);
420
+ return (0, import_core.createMachine)(
421
+ {
422
+ id: "radio",
423
+ initial: "unknown",
424
+ context: {
425
+ value: null,
426
+ initialValue: null,
427
+ activeId: null,
428
+ focusedId: null,
429
+ hoveredId: null,
430
+ ...ctx
431
+ },
432
+ activities: ["trackFormControlState"],
433
+ on: {
434
+ SET_VALUE: {
435
+ actions: ["setValue"]
436
+ },
437
+ SET_HOVERED: {
438
+ actions: "setHovered"
439
+ },
440
+ SET_ACTIVE: {
441
+ actions: "setActive"
442
+ },
443
+ SET_FOCUSED: {
444
+ actions: "setFocused"
445
+ }
446
+ },
447
+ watch: {
448
+ value: ["dispatchChangeEvent", "invokeOnChange"]
449
+ },
450
+ states: {
451
+ unknown: {
452
+ on: {
453
+ SETUP: {
454
+ actions: "checkValue"
455
+ }
456
+ }
457
+ }
458
+ }
459
+ },
460
+ {
461
+ activities: {
462
+ trackFormControlState(ctx2, _evt, { send }) {
463
+ return trackFormControl(dom.getRootEl(ctx2), {
464
+ onFieldsetDisabled() {
465
+ ctx2.disabled = true;
466
+ },
467
+ onFormReset() {
468
+ send({ type: "SET_VALUE", value: ctx2.initialValue });
469
+ }
470
+ });
471
+ }
472
+ },
473
+ actions: {
474
+ checkValue(ctx2) {
475
+ ctx2.initialValue = ctx2.value;
476
+ },
477
+ setValue(ctx2, evt) {
478
+ ctx2.value = evt.value;
479
+ },
480
+ setHovered(ctx2, evt) {
481
+ ctx2.hoveredId = evt.value;
482
+ },
483
+ setActive(ctx2, evt) {
484
+ ctx2.activeId = evt.value;
485
+ },
486
+ setFocused(ctx2, evt) {
487
+ ctx2.focusedId = evt.value;
488
+ },
489
+ invokeOnChange(ctx2, evt) {
490
+ var _a;
491
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: evt.value });
492
+ },
493
+ dispatchChangeEvent(ctx2, evt) {
494
+ if (!evt.manual)
495
+ return;
496
+ const el = dom.getRadioInputEl(ctx2, evt.value);
497
+ if (!el)
498
+ return;
499
+ dispatchInputCheckedEvent(el, !!evt.value);
500
+ }
501
+ }
502
+ }
503
+ );
504
+ }
505
+ // Annotate the CommonJS export names for ESM import in node:
506
+ 0 && (module.exports = {
507
+ anatomy,
508
+ connect,
509
+ machine
510
+ });