@zag-js/radio-group 2.0.0-next.3 → 2.0.0-next.4

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.
@@ -1,260 +1,250 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/radio-group.connect.ts
31
- var radio_group_connect_exports = {};
32
- __export(radio_group_connect_exports, {
33
- connect: () => connect
1
+ Object.defineProperties(exports, {
2
+ __esModule: { value: true },
3
+ [Symbol.toStringTag]: { value: "Module" }
34
4
  });
35
- module.exports = __toCommonJS(radio_group_connect_exports);
36
- var import_dom_query = require("@zag-js/dom-query");
37
- var import_focus_visible = require("@zag-js/focus-visible");
38
- var import_utils = require("@zag-js/utils");
39
- var import_radio_group = require("./radio-group.anatomy.js");
40
- var dom = __toESM(require("./radio-group.dom.js"));
5
+ const require_radio_group_anatomy = require("./radio-group.anatomy.js");
6
+ const require_radio_group_dom = require("./radio-group.dom.js");
7
+ let _zag_js_dom_query = require("@zag-js/dom-query");
8
+ let _zag_js_focus_visible = require("@zag-js/focus-visible");
9
+ let _zag_js_utils = require("@zag-js/utils");
10
+ // src/radio-group.connect.ts
41
11
  function connect(service, normalize) {
42
- const { context, send, computed, prop, scope } = service;
43
- const groupDisabled = computed("isDisabled");
44
- const groupInvalid = !!prop("invalid");
45
- const readOnly = !!prop("readOnly");
46
- function getRootState() {
47
- return {
48
- disabled: groupDisabled,
49
- invalid: groupInvalid,
50
- required: !!prop("required"),
51
- readOnly,
52
- orientation: prop("orientation")
53
- };
54
- }
55
- function getItemState(props) {
56
- return {
57
- value: props.value,
58
- invalid: !!props.invalid || !!groupInvalid,
59
- disabled: !!props.disabled || groupDisabled,
60
- checked: context.get("value") === props.value,
61
- focused: context.get("focusedValue") === props.value,
62
- focusVisible: context.get("focusVisibleValue") === props.value,
63
- hovered: context.get("hoveredValue") === props.value,
64
- active: context.get("activeValue") === props.value
65
- };
66
- }
67
- function getItemDataAttrs(props) {
68
- const itemState = getItemState(props);
69
- return {
70
- "data-focus": (0, import_dom_query.dataAttr)(itemState.focused),
71
- "data-focus-visible": (0, import_dom_query.dataAttr)(itemState.focusVisible),
72
- "data-disabled": (0, import_dom_query.dataAttr)(itemState.disabled),
73
- "data-readonly": (0, import_dom_query.dataAttr)(readOnly),
74
- "data-state": itemState.checked ? "checked" : "unchecked",
75
- "data-hover": (0, import_dom_query.dataAttr)(itemState.hovered),
76
- "data-invalid": (0, import_dom_query.dataAttr)(itemState.invalid),
77
- "data-orientation": prop("orientation"),
78
- "data-ssr": (0, import_dom_query.dataAttr)(context.get("ssr"))
79
- };
80
- }
81
- const focus = () => {
82
- const nodeToFocus = dom.getFirstEnabledAndCheckedInputEl(scope) ?? dom.getFirstEnabledInputEl(scope);
83
- nodeToFocus?.focus();
84
- };
85
- return {
86
- focus,
87
- value: context.get("value"),
88
- setValue(value) {
89
- send({ type: "SET_VALUE", value, isTrusted: false });
90
- },
91
- clearValue() {
92
- send({ type: "SET_VALUE", value: null, isTrusted: false });
93
- },
94
- getRootState,
95
- getRootProps() {
96
- const rootState = getRootState();
97
- return normalize.element({
98
- ...import_radio_group.parts.root.attrs(scope.id),
99
- role: "radiogroup",
100
- "aria-labelledby": dom.getLabelId(scope),
101
- "aria-required": rootState.required || void 0,
102
- "aria-disabled": rootState.disabled || void 0,
103
- "aria-readonly": rootState.readOnly || void 0,
104
- "data-orientation": rootState.orientation,
105
- "data-disabled": (0, import_dom_query.dataAttr)(rootState.disabled),
106
- "data-invalid": (0, import_dom_query.dataAttr)(rootState.invalid),
107
- "data-required": (0, import_dom_query.dataAttr)(rootState.required),
108
- "aria-orientation": rootState.orientation,
109
- dir: prop("dir"),
110
- style: {
111
- position: "relative"
112
- }
113
- });
114
- },
115
- getLabelProps() {
116
- return normalize.element({
117
- ...import_radio_group.parts.label.attrs(scope.id),
118
- dir: prop("dir"),
119
- "data-orientation": prop("orientation"),
120
- "data-disabled": (0, import_dom_query.dataAttr)(groupDisabled),
121
- "data-invalid": (0, import_dom_query.dataAttr)(groupInvalid),
122
- "data-required": (0, import_dom_query.dataAttr)(prop("required")),
123
- id: dom.getLabelId(scope),
124
- onClick: focus
125
- });
126
- },
127
- getItemState,
128
- getItemProps(props) {
129
- const itemState = getItemState(props);
130
- return normalize.label({
131
- ...import_radio_group.parts.item.attrs(scope.id),
132
- dir: prop("dir"),
133
- id: dom.getItemId(scope, props.value),
134
- htmlFor: dom.getItemHiddenInputId(scope, props.value),
135
- ...getItemDataAttrs(props),
136
- onPointerMove() {
137
- if (itemState.disabled) return;
138
- if (itemState.hovered) return;
139
- send({ type: "SET_HOVERED", value: props.value, hovered: true });
140
- },
141
- onPointerLeave() {
142
- if (itemState.disabled) return;
143
- send({ type: "SET_HOVERED", value: null });
144
- },
145
- onPointerDown(event) {
146
- if (itemState.disabled) return;
147
- if (!(0, import_dom_query.isLeftClick)(event)) return;
148
- if (itemState.focused && event.pointerType === "mouse") {
149
- event.preventDefault();
150
- }
151
- send({ type: "SET_ACTIVE", value: props.value, active: true });
152
- },
153
- onPointerUp() {
154
- if (itemState.disabled) return;
155
- send({ type: "SET_ACTIVE", value: null });
156
- },
157
- onClick() {
158
- if (!itemState.disabled && (0, import_dom_query.isSafari)()) {
159
- dom.getItemHiddenInputEl(scope, props.value)?.focus();
160
- }
161
- }
162
- });
163
- },
164
- getItemTextProps(props) {
165
- return normalize.element({
166
- ...import_radio_group.parts.itemText.attrs(scope.id),
167
- dir: prop("dir"),
168
- id: dom.getItemLabelId(scope, props.value),
169
- ...getItemDataAttrs(props)
170
- });
171
- },
172
- getItemControlProps(props) {
173
- const itemState = getItemState(props);
174
- return normalize.element({
175
- ...import_radio_group.parts.itemControl.attrs(scope.id),
176
- dir: prop("dir"),
177
- "data-active": (0, import_dom_query.dataAttr)(itemState.active),
178
- "aria-hidden": true,
179
- ...getItemDataAttrs(props)
180
- });
181
- },
182
- getItemHiddenInputProps(props) {
183
- const itemState = getItemState(props);
184
- return normalize.input({
185
- id: dom.getItemHiddenInputId(scope, props.value),
186
- type: "radio",
187
- name: prop("name") || prop("id"),
188
- form: prop("form"),
189
- value: props.value,
190
- required: prop("required"),
191
- "aria-labelledby": dom.getItemLabelId(scope, props.value),
192
- "aria-invalid": itemState.invalid || void 0,
193
- onClick(event) {
194
- if (readOnly) {
195
- event.preventDefault();
196
- return;
197
- }
198
- if (event.currentTarget.checked) {
199
- send({ type: "SET_VALUE", value: props.value, isTrusted: true });
200
- }
201
- },
202
- onBlur() {
203
- send({ type: "SET_FOCUSED", value: null, focused: false, focusVisible: false });
204
- },
205
- onFocus() {
206
- const focusVisible = (0, import_focus_visible.isFocusVisible)();
207
- send({ type: "SET_FOCUSED", value: props.value, focused: true, focusVisible });
208
- },
209
- onKeyDown(event) {
210
- if (event.defaultPrevented) return;
211
- if (event.key === " ") {
212
- send({ type: "SET_ACTIVE", value: props.value, active: true });
213
- }
214
- },
215
- onKeyUp(event) {
216
- if (event.defaultPrevented) return;
217
- if (event.key === " ") {
218
- send({ type: "SET_ACTIVE", value: null });
219
- }
220
- },
221
- disabled: itemState.disabled || readOnly,
222
- defaultChecked: itemState.checked,
223
- style: import_dom_query.visuallyHiddenStyle
224
- });
225
- },
226
- getIndicatorProps() {
227
- const rect = context.get("indicatorRect");
228
- const animateIndicator = context.get("animateIndicator");
229
- return normalize.element({
230
- ...import_radio_group.parts.indicator.attrs(scope.id),
231
- dir: prop("dir"),
232
- hidden: context.get("value") == null || isRectEmpty(rect),
233
- "data-disabled": (0, import_dom_query.dataAttr)(groupDisabled),
234
- "data-orientation": prop("orientation"),
235
- onTransitionEnd(event) {
236
- if ((0, import_dom_query.getEventTarget)(event) !== event.currentTarget) return;
237
- send({ type: "INDICATOR_TRANSITION_END" });
238
- },
239
- style: {
240
- "--transition-property": "left, top, width, height",
241
- "--left": (0, import_utils.toPx)(rect?.x),
242
- "--top": (0, import_utils.toPx)(rect?.y),
243
- "--width": (0, import_utils.toPx)(rect?.width),
244
- "--height": (0, import_utils.toPx)(rect?.height),
245
- position: "absolute",
246
- willChange: animateIndicator ? "var(--transition-property)" : "auto",
247
- transitionProperty: animateIndicator ? "var(--transition-property)" : "none",
248
- transitionDuration: animateIndicator ? "var(--transition-duration, 150ms)" : "0ms",
249
- transitionTimingFunction: "var(--transition-timing-function)",
250
- [prop("orientation") === "horizontal" ? "left" : "top"]: prop("orientation") === "horizontal" ? "var(--left)" : "var(--top)"
251
- }
252
- });
253
- }
254
- };
12
+ const { context, send, computed, prop, scope } = service;
13
+ const groupDisabled = computed("isDisabled");
14
+ const groupInvalid = !!prop("invalid");
15
+ const readOnly = !!prop("readOnly");
16
+ function getRootState() {
17
+ return {
18
+ disabled: groupDisabled,
19
+ invalid: groupInvalid,
20
+ required: !!prop("required"),
21
+ readOnly,
22
+ orientation: prop("orientation")
23
+ };
24
+ }
25
+ function getItemState(props) {
26
+ return {
27
+ value: props.value,
28
+ invalid: !!props.invalid || !!groupInvalid,
29
+ disabled: !!props.disabled || groupDisabled,
30
+ checked: context.get("value") === props.value,
31
+ focused: context.get("focusedValue") === props.value,
32
+ focusVisible: context.get("focusVisibleValue") === props.value,
33
+ hovered: context.get("hoveredValue") === props.value,
34
+ active: context.get("activeValue") === props.value
35
+ };
36
+ }
37
+ const indicatorRect = context.get("indicatorRect");
38
+ const indicatorPending = context.get("ssr") || indicatorRect != null && isRectEmpty(indicatorRect);
39
+ function getItemDataAttrs(props) {
40
+ const itemState = getItemState(props);
41
+ return {
42
+ "data-focus": (0, _zag_js_dom_query.dataAttr)(itemState.focused),
43
+ "data-focus-visible": (0, _zag_js_dom_query.dataAttr)(itemState.focusVisible),
44
+ "data-disabled": (0, _zag_js_dom_query.dataAttr)(itemState.disabled),
45
+ "data-readonly": (0, _zag_js_dom_query.dataAttr)(readOnly),
46
+ "data-state": itemState.checked ? "checked" : "unchecked",
47
+ "data-hover": (0, _zag_js_dom_query.dataAttr)(itemState.hovered),
48
+ "data-invalid": (0, _zag_js_dom_query.dataAttr)(itemState.invalid),
49
+ "data-orientation": prop("orientation"),
50
+ "data-indicator-pending": (0, _zag_js_dom_query.dataAttr)(indicatorPending)
51
+ };
52
+ }
53
+ const focus = () => {
54
+ (require_radio_group_dom.getFirstEnabledAndCheckedInputEl(scope) ?? require_radio_group_dom.getFirstEnabledInputEl(scope))?.focus();
55
+ };
56
+ return {
57
+ focus,
58
+ value: context.get("value"),
59
+ setValue(value) {
60
+ send({
61
+ type: "SET_VALUE",
62
+ value,
63
+ isTrusted: false
64
+ });
65
+ },
66
+ clearValue() {
67
+ send({
68
+ type: "SET_VALUE",
69
+ value: null,
70
+ isTrusted: false
71
+ });
72
+ },
73
+ getRootState,
74
+ getRootProps() {
75
+ const rootState = getRootState();
76
+ return normalize.element({
77
+ ...require_radio_group_anatomy.parts.root.attrs(scope.id),
78
+ role: "radiogroup",
79
+ "aria-labelledby": require_radio_group_dom.getLabelId(scope),
80
+ "aria-required": rootState.required || void 0,
81
+ "aria-disabled": rootState.disabled || void 0,
82
+ "aria-readonly": rootState.readOnly || void 0,
83
+ "data-orientation": rootState.orientation,
84
+ "data-disabled": (0, _zag_js_dom_query.dataAttr)(rootState.disabled),
85
+ "data-invalid": (0, _zag_js_dom_query.dataAttr)(rootState.invalid),
86
+ "data-required": (0, _zag_js_dom_query.dataAttr)(rootState.required),
87
+ "aria-orientation": rootState.orientation,
88
+ dir: prop("dir"),
89
+ style: { position: "relative" }
90
+ });
91
+ },
92
+ getLabelProps() {
93
+ return normalize.element({
94
+ ...require_radio_group_anatomy.parts.label.attrs(scope.id),
95
+ dir: prop("dir"),
96
+ "data-orientation": prop("orientation"),
97
+ "data-disabled": (0, _zag_js_dom_query.dataAttr)(groupDisabled),
98
+ "data-invalid": (0, _zag_js_dom_query.dataAttr)(groupInvalid),
99
+ "data-required": (0, _zag_js_dom_query.dataAttr)(prop("required")),
100
+ id: require_radio_group_dom.getLabelId(scope),
101
+ onClick: focus
102
+ });
103
+ },
104
+ getItemState,
105
+ getItemProps(props) {
106
+ const itemState = getItemState(props);
107
+ return normalize.label({
108
+ ...require_radio_group_anatomy.parts.item.attrs(scope.id),
109
+ dir: prop("dir"),
110
+ id: require_radio_group_dom.getItemId(scope, props.value),
111
+ htmlFor: require_radio_group_dom.getItemHiddenInputId(scope, props.value),
112
+ ...getItemDataAttrs(props),
113
+ onPointerMove() {
114
+ if (itemState.disabled) return;
115
+ if (itemState.hovered) return;
116
+ send({
117
+ type: "SET_HOVERED",
118
+ value: props.value,
119
+ hovered: true
120
+ });
121
+ },
122
+ onPointerLeave() {
123
+ if (itemState.disabled) return;
124
+ send({
125
+ type: "SET_HOVERED",
126
+ value: null
127
+ });
128
+ },
129
+ onPointerDown(event) {
130
+ if (itemState.disabled) return;
131
+ if (!(0, _zag_js_dom_query.isLeftClick)(event)) return;
132
+ if (itemState.focused && event.pointerType === "mouse") event.preventDefault();
133
+ send({
134
+ type: "SET_ACTIVE",
135
+ value: props.value,
136
+ active: true
137
+ });
138
+ },
139
+ onPointerUp() {
140
+ if (itemState.disabled) return;
141
+ send({
142
+ type: "SET_ACTIVE",
143
+ value: null
144
+ });
145
+ },
146
+ onClick() {
147
+ if (!itemState.disabled && (0, _zag_js_dom_query.isSafari)()) require_radio_group_dom.getItemHiddenInputEl(scope, props.value)?.focus();
148
+ }
149
+ });
150
+ },
151
+ getItemTextProps(props) {
152
+ return normalize.element({
153
+ ...require_radio_group_anatomy.parts.itemText.attrs(scope.id),
154
+ dir: prop("dir"),
155
+ id: require_radio_group_dom.getItemLabelId(scope, props.value),
156
+ ...getItemDataAttrs(props)
157
+ });
158
+ },
159
+ getItemControlProps(props) {
160
+ const itemState = getItemState(props);
161
+ return normalize.element({
162
+ ...require_radio_group_anatomy.parts.itemControl.attrs(scope.id),
163
+ dir: prop("dir"),
164
+ "data-active": (0, _zag_js_dom_query.dataAttr)(itemState.active),
165
+ "aria-hidden": true,
166
+ ...getItemDataAttrs(props)
167
+ });
168
+ },
169
+ getItemHiddenInputProps(props) {
170
+ const itemState = getItemState(props);
171
+ return normalize.input({
172
+ id: require_radio_group_dom.getItemHiddenInputId(scope, props.value),
173
+ type: "radio",
174
+ name: prop("name") || prop("id"),
175
+ form: prop("form"),
176
+ value: props.value,
177
+ required: prop("required"),
178
+ "aria-labelledby": require_radio_group_dom.getItemLabelId(scope, props.value),
179
+ "aria-invalid": itemState.invalid || void 0,
180
+ onClick(event) {
181
+ if (readOnly) {
182
+ event.preventDefault();
183
+ return;
184
+ }
185
+ if (event.currentTarget.checked) send({
186
+ type: "SET_VALUE",
187
+ value: props.value,
188
+ isTrusted: true
189
+ });
190
+ },
191
+ onBlur() {
192
+ send({
193
+ type: "SET_FOCUSED",
194
+ value: null,
195
+ focused: false,
196
+ focusVisible: false
197
+ });
198
+ },
199
+ onFocus() {
200
+ const focusVisible = (0, _zag_js_focus_visible.isFocusVisible)();
201
+ send({
202
+ type: "SET_FOCUSED",
203
+ value: props.value,
204
+ focused: true,
205
+ focusVisible
206
+ });
207
+ },
208
+ onKeyDown(event) {
209
+ if (event.defaultPrevented) return;
210
+ if (event.key === " ") send({
211
+ type: "SET_ACTIVE",
212
+ value: props.value,
213
+ active: true
214
+ });
215
+ },
216
+ onKeyUp(event) {
217
+ if (event.defaultPrevented) return;
218
+ if (event.key === " ") send({
219
+ type: "SET_ACTIVE",
220
+ value: null
221
+ });
222
+ },
223
+ disabled: itemState.disabled,
224
+ defaultChecked: itemState.checked,
225
+ style: _zag_js_dom_query.visuallyHiddenStyle
226
+ });
227
+ },
228
+ getIndicatorProps() {
229
+ const rect = context.get("indicatorRect");
230
+ return normalize.element({
231
+ ...require_radio_group_anatomy.parts.indicator.attrs(scope.id),
232
+ dir: prop("dir"),
233
+ hidden: context.get("value") == null || isRectEmpty(rect),
234
+ "data-disabled": (0, _zag_js_dom_query.dataAttr)(groupDisabled),
235
+ "data-orientation": prop("orientation"),
236
+ "data-instant": (0, _zag_js_dom_query.dataAttr)(context.get("indicatorInstant")),
237
+ style: {
238
+ "--left": (0, _zag_js_utils.toPx)(rect?.x),
239
+ "--top": (0, _zag_js_utils.toPx)(rect?.y),
240
+ "--width": (0, _zag_js_utils.toPx)(rect?.width),
241
+ "--height": (0, _zag_js_utils.toPx)(rect?.height),
242
+ position: "absolute",
243
+ [prop("orientation") === "horizontal" ? "left" : "top"]: prop("orientation") === "horizontal" ? "var(--left)" : "var(--top)"
244
+ }
245
+ });
246
+ }
247
+ };
255
248
  }
256
- var isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
257
- // Annotate the CommonJS export names for ESM import in node:
258
- 0 && (module.exports = {
259
- connect
260
- });
249
+ const isRectEmpty = (rect) => rect == null || rect.width === 0 && rect.height === 0 && rect.x === 0 && rect.y === 0;
250
+ exports.connect = connect;