@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,192 @@
1
+ import {
2
+ dom,
3
+ getWindow
4
+ } from "./chunk-JSIZLP57.mjs";
5
+
6
+ // src/radio-group.machine.ts
7
+ import { createMachine } from "@zag-js/core";
8
+
9
+ // ../../utilities/core/src/guard.ts
10
+ var isArray = (v) => Array.isArray(v);
11
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
12
+
13
+ // ../../utilities/core/src/object.ts
14
+ function compact(obj) {
15
+ if (obj === void 0)
16
+ return obj;
17
+ return Object.fromEntries(
18
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
19
+ );
20
+ }
21
+
22
+ // ../../utilities/dom/src/mutation-observer.ts
23
+ function observeAttributes(node, attributes, fn) {
24
+ if (!node)
25
+ return;
26
+ const attrs = Array.isArray(attributes) ? attributes : [attributes];
27
+ const win = node.ownerDocument.defaultView || window;
28
+ const obs = new win.MutationObserver((changes) => {
29
+ for (const change of changes) {
30
+ if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
31
+ fn(change);
32
+ }
33
+ }
34
+ });
35
+ obs.observe(node, { attributes: true, attributeFilter: attrs });
36
+ return () => obs.disconnect();
37
+ }
38
+
39
+ // ../../utilities/form-utils/src/input-event.ts
40
+ function getDescriptor(el, options) {
41
+ var _a;
42
+ const { type, property = "value" } = options;
43
+ const proto = getWindow(el)[type].prototype;
44
+ return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
45
+ }
46
+ function dispatchInputCheckedEvent(el, checked) {
47
+ var _a;
48
+ if (!el)
49
+ return;
50
+ const win = getWindow(el);
51
+ if (!(el instanceof win.HTMLInputElement))
52
+ return;
53
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "checked" });
54
+ (_a = desc.set) == null ? void 0 : _a.call(el, checked);
55
+ const event = new win.Event("click", { bubbles: true });
56
+ el.dispatchEvent(event);
57
+ }
58
+
59
+ // ../../utilities/form-utils/src/form.ts
60
+ function getClosestForm(el) {
61
+ if (isFormElement(el))
62
+ return el.form;
63
+ else
64
+ return el.closest("form");
65
+ }
66
+ function isFormElement(el) {
67
+ return el.matches("textarea, input, select, button");
68
+ }
69
+ function trackFormReset(el, callback) {
70
+ if (!el)
71
+ return;
72
+ const form = getClosestForm(el);
73
+ form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
74
+ return () => {
75
+ form == null ? void 0 : form.removeEventListener("reset", callback);
76
+ };
77
+ }
78
+ function trackFieldsetDisabled(el, callback) {
79
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
80
+ if (!fieldset)
81
+ return;
82
+ callback(fieldset.disabled);
83
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
84
+ }
85
+ function trackFormControl(el, options) {
86
+ if (!el)
87
+ return;
88
+ const { onFieldsetDisabled, onFormReset } = options;
89
+ const cleanups = [
90
+ trackFormReset(el, onFormReset),
91
+ trackFieldsetDisabled(el, (disabled) => {
92
+ if (disabled)
93
+ onFieldsetDisabled();
94
+ })
95
+ ];
96
+ return () => {
97
+ cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
98
+ };
99
+ }
100
+
101
+ // src/radio-group.machine.ts
102
+ function machine(userContext) {
103
+ const ctx = compact(userContext);
104
+ return createMachine(
105
+ {
106
+ id: "radio",
107
+ initial: "unknown",
108
+ context: {
109
+ value: null,
110
+ initialValue: null,
111
+ activeId: null,
112
+ focusedId: null,
113
+ hoveredId: null,
114
+ ...ctx
115
+ },
116
+ activities: ["trackFormControlState"],
117
+ on: {
118
+ SET_VALUE: {
119
+ actions: ["setValue"]
120
+ },
121
+ SET_HOVERED: {
122
+ actions: "setHovered"
123
+ },
124
+ SET_ACTIVE: {
125
+ actions: "setActive"
126
+ },
127
+ SET_FOCUSED: {
128
+ actions: "setFocused"
129
+ }
130
+ },
131
+ watch: {
132
+ value: ["dispatchChangeEvent", "invokeOnChange"]
133
+ },
134
+ states: {
135
+ unknown: {
136
+ on: {
137
+ SETUP: {
138
+ actions: "checkValue"
139
+ }
140
+ }
141
+ }
142
+ }
143
+ },
144
+ {
145
+ activities: {
146
+ trackFormControlState(ctx2, _evt, { send }) {
147
+ return trackFormControl(dom.getRootEl(ctx2), {
148
+ onFieldsetDisabled() {
149
+ ctx2.disabled = true;
150
+ },
151
+ onFormReset() {
152
+ send({ type: "SET_VALUE", value: ctx2.initialValue });
153
+ }
154
+ });
155
+ }
156
+ },
157
+ actions: {
158
+ checkValue(ctx2) {
159
+ ctx2.initialValue = ctx2.value;
160
+ },
161
+ setValue(ctx2, evt) {
162
+ ctx2.value = evt.value;
163
+ },
164
+ setHovered(ctx2, evt) {
165
+ ctx2.hoveredId = evt.value;
166
+ },
167
+ setActive(ctx2, evt) {
168
+ ctx2.activeId = evt.value;
169
+ },
170
+ setFocused(ctx2, evt) {
171
+ ctx2.focusedId = evt.value;
172
+ },
173
+ invokeOnChange(ctx2, evt) {
174
+ var _a;
175
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: evt.value });
176
+ },
177
+ dispatchChangeEvent(ctx2, evt) {
178
+ if (!evt.manual)
179
+ return;
180
+ const el = dom.getRadioInputEl(ctx2, evt.value);
181
+ if (!el)
182
+ return;
183
+ dispatchInputCheckedEvent(el, !!evt.value);
184
+ }
185
+ }
186
+ }
187
+ );
188
+ }
189
+
190
+ export {
191
+ machine
192
+ };
@@ -0,0 +1,110 @@
1
+ // ../../utilities/dom/src/query.ts
2
+ function isDocument(el) {
3
+ return el.nodeType === Node.DOCUMENT_NODE;
4
+ }
5
+ function isWindow(value) {
6
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
7
+ }
8
+ function getDocument(el) {
9
+ var _a;
10
+ if (isWindow(el))
11
+ return el.document;
12
+ if (isDocument(el))
13
+ return el;
14
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
15
+ }
16
+ function getWindow(el) {
17
+ var _a;
18
+ return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
19
+ }
20
+ function defineDomHelpers(helpers) {
21
+ const dom2 = {
22
+ getRootNode: (ctx) => {
23
+ var _a, _b;
24
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
25
+ },
26
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
27
+ getWin: (ctx) => {
28
+ var _a;
29
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
30
+ },
31
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
32
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
33
+ createEmitter: (ctx, target) => {
34
+ const win = dom2.getWin(ctx);
35
+ return function emit(evt, detail, options) {
36
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
37
+ const eventName = `zag:${evt}`;
38
+ const init = { bubbles, cancelable, composed, detail };
39
+ const event = new win.CustomEvent(eventName, init);
40
+ target.dispatchEvent(event);
41
+ };
42
+ },
43
+ createListener: (target) => {
44
+ return function listen(evt, handler) {
45
+ const eventName = `zag:${evt}`;
46
+ const listener = (e) => handler(e);
47
+ target.addEventListener(eventName, listener);
48
+ return () => target.removeEventListener(eventName, listener);
49
+ };
50
+ }
51
+ };
52
+ return {
53
+ ...dom2,
54
+ ...helpers
55
+ };
56
+ }
57
+
58
+ // ../../utilities/dom/src/nodelist.ts
59
+ function queryAll(root, selector) {
60
+ var _a;
61
+ return Array.from((_a = root == null ? void 0 : root.querySelectorAll(selector)) != null ? _a : []);
62
+ }
63
+
64
+ // src/radio-group.dom.ts
65
+ var dom = defineDomHelpers({
66
+ getRootId: (ctx) => {
67
+ var _a, _b;
68
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `radio-group:${ctx.id}`;
69
+ },
70
+ getLabelId: (ctx) => {
71
+ var _a, _b;
72
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `radio-group:${ctx.id}:label`;
73
+ },
74
+ getRadioId: (ctx, value) => {
75
+ var _a, _b, _c;
76
+ 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}`;
77
+ },
78
+ getRadioInputId: (ctx, value) => {
79
+ var _a, _b, _c;
80
+ 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}`;
81
+ },
82
+ getRadioControlId: (ctx, value) => {
83
+ var _a, _b, _c;
84
+ 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}`;
85
+ },
86
+ getRadioLabelId: (ctx, value) => {
87
+ var _a, _b, _c;
88
+ 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}`;
89
+ },
90
+ getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
91
+ getRadioInputEl: (ctx, value) => dom.getById(ctx, dom.getRadioInputId(ctx, value)),
92
+ getFirstEnabledInputEl: (ctx) => {
93
+ var _a;
94
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled)");
95
+ },
96
+ getFirstEnabledAndCheckedInputEl: (ctx) => {
97
+ var _a;
98
+ return (_a = dom.getRootEl(ctx)) == null ? void 0 : _a.querySelector("input:not(:disabled):checked");
99
+ },
100
+ getInputEls: (ctx) => {
101
+ const ownerId = CSS.escape(dom.getRootId(ctx));
102
+ const selector = `input[type=radio][data-ownedby='${ownerId}']:not([disabled])`;
103
+ return queryAll(dom.getRootEl(ctx), selector);
104
+ }
105
+ });
106
+
107
+ export {
108
+ getWindow,
109
+ dom
110
+ };
@@ -0,0 +1,197 @@
1
+ import {
2
+ parts
3
+ } from "./chunk-MGLN5L3R.mjs";
4
+ import {
5
+ dom
6
+ } from "./chunk-JSIZLP57.mjs";
7
+
8
+ // ../../utilities/dom/src/attrs.ts
9
+ var dataAttr = (guard) => {
10
+ return guard ? "" : void 0;
11
+ };
12
+ var ariaAttr = (guard) => {
13
+ return guard ? "true" : void 0;
14
+ };
15
+
16
+ // ../../utilities/dom/src/visually-hidden.ts
17
+ var visuallyHiddenStyle = {
18
+ border: "0",
19
+ clip: "rect(0 0 0 0)",
20
+ height: "1px",
21
+ margin: "-1px",
22
+ overflow: "hidden",
23
+ padding: "0",
24
+ position: "absolute",
25
+ width: "1px",
26
+ whiteSpace: "nowrap",
27
+ wordWrap: "normal"
28
+ };
29
+
30
+ // src/radio-group.connect.ts
31
+ function connect(state, send, normalize) {
32
+ const isGroupDisabled = state.context.disabled;
33
+ const isGroupReadOnly = state.context.readOnly;
34
+ function getRadioState(props) {
35
+ const radioState = {
36
+ isReadOnly: props.readOnly || isGroupReadOnly,
37
+ isInvalid: props.invalid,
38
+ isDisabled: props.disabled || isGroupDisabled,
39
+ isChecked: state.context.value === props.value,
40
+ isFocused: state.context.focusedId === props.value,
41
+ isHovered: state.context.hoveredId === props.value,
42
+ isActive: state.context.activeId === props.value
43
+ };
44
+ return {
45
+ ...radioState,
46
+ isInteractive: !(radioState.isReadOnly || radioState.isDisabled)
47
+ };
48
+ }
49
+ function getRadioDataSet(props) {
50
+ const radioState = getRadioState(props);
51
+ return {
52
+ "data-focus": dataAttr(radioState.isFocused),
53
+ "data-disabled": dataAttr(radioState.isDisabled),
54
+ "data-checked": dataAttr(radioState.isChecked),
55
+ "data-hover": dataAttr(radioState.isHovered),
56
+ "data-invalid": dataAttr(radioState.isInvalid),
57
+ "data-readonly": dataAttr(radioState.isReadOnly)
58
+ };
59
+ }
60
+ const focus = () => {
61
+ const firstEnabledAndCheckedInput = dom.getFirstEnabledAndCheckedInputEl(state.context);
62
+ if (firstEnabledAndCheckedInput) {
63
+ firstEnabledAndCheckedInput.focus();
64
+ return;
65
+ }
66
+ const firstEnabledInput = dom.getFirstEnabledInputEl(state.context);
67
+ firstEnabledInput == null ? void 0 : firstEnabledInput.focus();
68
+ };
69
+ return {
70
+ value: state.context.value,
71
+ setValue(value) {
72
+ send({ type: "SET_VALUE", value, manual: true });
73
+ },
74
+ focus,
75
+ blur() {
76
+ const focusedElement = dom.getActiveElement(state.context);
77
+ const inputEls = dom.getInputEls(state.context);
78
+ const radioInputIsFocused = inputEls.some((el) => el === focusedElement);
79
+ if (radioInputIsFocused)
80
+ focusedElement == null ? void 0 : focusedElement.blur();
81
+ },
82
+ rootProps: normalize.element({
83
+ ...parts.root.attrs,
84
+ role: "radiogroup",
85
+ id: dom.getRootId(state.context),
86
+ "data-orientation": state.context.orientation,
87
+ "aria-orientation": state.context.orientation,
88
+ dir: state.context.dir
89
+ }),
90
+ labelProps: normalize.element({
91
+ ...parts.label.attrs,
92
+ id: dom.getLabelId(state.context),
93
+ onClick: focus
94
+ }),
95
+ getRadioProps(props) {
96
+ const rootState = getRadioState(props);
97
+ return normalize.label({
98
+ ...parts.radio.attrs,
99
+ id: dom.getRadioId(state.context, props.value),
100
+ htmlFor: dom.getRadioInputId(state.context, props.value),
101
+ ...getRadioDataSet(props),
102
+ onPointerMove() {
103
+ if (!rootState.isInteractive)
104
+ return;
105
+ send({ type: "SET_HOVERED", value: props.value, hovered: true });
106
+ },
107
+ onPointerLeave() {
108
+ if (!rootState.isInteractive)
109
+ return;
110
+ send({ type: "SET_HOVERED", value: null });
111
+ },
112
+ onPointerDown(event) {
113
+ if (!rootState.isInteractive)
114
+ return;
115
+ if (rootState.isFocused)
116
+ event.preventDefault();
117
+ send({ type: "SET_ACTIVE", value: props.value, active: true });
118
+ },
119
+ onPointerUp() {
120
+ if (!rootState.isInteractive)
121
+ return;
122
+ send({ type: "SET_ACTIVE", value: null });
123
+ }
124
+ });
125
+ },
126
+ getRadioLabelProps(props) {
127
+ return normalize.element({
128
+ ...parts.radioLabel.attrs,
129
+ id: dom.getRadioLabelId(state.context, props.value),
130
+ ...getRadioDataSet(props)
131
+ });
132
+ },
133
+ getRadioControlProps(props) {
134
+ const controlState = getRadioState(props);
135
+ return normalize.element({
136
+ ...parts.radioControl.attrs,
137
+ id: dom.getRadioControlId(state.context, props.value),
138
+ "data-active": dataAttr(controlState.isActive),
139
+ "aria-hidden": true,
140
+ ...getRadioDataSet(props)
141
+ });
142
+ },
143
+ getRadioInputProps(props) {
144
+ const inputState = getRadioState(props);
145
+ const isRequired = props.required;
146
+ const trulyDisabled = inputState.isDisabled && !props.focusable;
147
+ return normalize.input({
148
+ ...parts.radioInput.attrs,
149
+ "data-ownedby": dom.getRootId(state.context),
150
+ id: dom.getRadioInputId(state.context, props.value),
151
+ type: "radio",
152
+ name: state.context.name || state.context.id,
153
+ form: state.context.form,
154
+ value: props.value,
155
+ onChange(event) {
156
+ if (inputState.isReadOnly || inputState.isDisabled) {
157
+ return;
158
+ }
159
+ if (event.target.checked) {
160
+ send({ type: "SET_VALUE", value: props.value });
161
+ }
162
+ },
163
+ onBlur() {
164
+ send({ type: "SET_FOCUSED", value: null });
165
+ },
166
+ onFocus() {
167
+ send({ type: "SET_FOCUSED", value: props.value, focused: true });
168
+ },
169
+ onKeyDown(event) {
170
+ if (event.key === " ") {
171
+ send({ type: "SET_ACTIVE", value: props.value, active: true });
172
+ }
173
+ },
174
+ onKeyUp(event) {
175
+ if (event.key === " ") {
176
+ send({ type: "SET_ACTIVE", value: null });
177
+ }
178
+ },
179
+ disabled: trulyDisabled,
180
+ required: isRequired,
181
+ defaultChecked: inputState.isChecked,
182
+ "data-disabled": dataAttr(inputState.isDisabled),
183
+ "aria-required": ariaAttr(isRequired),
184
+ "aria-invalid": ariaAttr(inputState.isInvalid),
185
+ readOnly: inputState.isReadOnly,
186
+ "data-readonly": dataAttr(inputState.isReadOnly),
187
+ "aria-disabled": ariaAttr(trulyDisabled),
188
+ "aria-checked": ariaAttr(inputState.isChecked),
189
+ style: visuallyHiddenStyle
190
+ });
191
+ }
192
+ };
193
+ }
194
+
195
+ export {
196
+ connect
197
+ };
@@ -0,0 +1,16 @@
1
+ // src/radio-group.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("radio-group").parts(
4
+ "root",
5
+ "label",
6
+ "radio",
7
+ "radioLabel",
8
+ "radioControl",
9
+ "radioInput"
10
+ );
11
+ var parts = anatomy.build();
12
+
13
+ export {
14
+ anatomy,
15
+ parts
16
+ };